Bonsai Tic Tac Toe

%3CLINGO-SUB%20id%3D%22lingo-sub-2063598%22%20slang%3D%22en-US%22%3EBonsai%20Tic%20Tac%20Toe%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2063598%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20doing%20a%20Tic%20Tac%20Toe%20game%2C%20in%20which%20one%20of%20the%20two%20players%20use%20bonsai%20to%20get%20the%20movements.%20I%20implemented%20a%20simulator%20in%20which%20in%20each%20step%20the%20action%20received%20from%20bonsai%20and%20the%20action%20for%20player%202%2C%20this%20player%20choose%20his%20movement%20randomly%2C%20are%20performed.%20I%20define%20in%20Inkling%20file%20a%20positive%20reward%20for%20correct%20movements%20and%20when%20player%201%20wins%2C%20and%20negative%20reward%20for%20wrong%20movements%2C%20player%202%20wins%20or%26nbsp%3B%20there%20aren't%20available%20movements.%20I%20define%20a%20terminal%20function%20when%20player1%20wins%2C%20player2%20wins%2C%20bonsai%20chooses%20wrong%20movement%20or%20there%20aren't%20available%20movement.%20The%20main%20problem%20is%20that%20bonsai%20doesn't%20learn%20the%20game%20rules%2C%20when%20the%20train%20ends%20it%20continues%20doing%20wrong%20movements%20(choose%20a%20occupied%20position).%20Can%20you%20help%20me%20find%20the%20problem%3F%20Or%20does%20bonsai%20not%20work%20well%20in%20this%20game%3F.%20I%20think%20it%20is%20a%20difficult%20problem%20because%20bonsai%20isn't%20rewarded%20only%20for%20its%20movement%2C%20but%20for%20its%20movement%20and%20the%20movement%20of%20the%20other%20player.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2065021%22%20slang%3D%22en-US%22%3ERe%3A%20Bonsai%20Tic%20Tac%20Toe%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2065021%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F930211%22%20target%3D%22_blank%22%3E%40victor91%3C%2FA%3E%26nbsp%3BThanks%20for%20trying%20the%20platform.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20are%20correct%20that%20this%20is%20a%20difficult%20problem%20because%20the%20trajectories%20change%20based%20on%20another%20agent%20whose%20moves%20are%20not%20entirely%20predictable.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWould%20you%20be%20willing%20to%20share%20your%20inkling%20contents%20here%3F%20Perhaps%20there%20are%20some%20suggestions%20we%20could%20offer.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20few%20additional%20questions%3A%3C%2FP%3E%0A%3CP%3E1.%20You%20said%20that%20you%20provide%20a%20negative%20reward%20for%20%22wrong%20movements%22.%20What%20constitutes%20a%20wrong%20movement%3F%20Is%20it%20one%20that%20is%20illegal%20(i.e.%20choosing%20a%20square%20that%20already%20has%20an%20X%20or%20an%20O%20present)%3F%20Or%20are%20you%20applying%20some%20heuristic%20to%20indicate%20whether%20the%20move%20was%20strategically%20sound%3F%3C%2FP%3E%0A%3CP%3E2.%20How%20long%20have%20you%20allowed%20this%20to%20train%3F%20How%20many%20episodes%3F%20For%20a%20problem%20like%20this%2C%20it%20could%20take%20millions%20or%20tens%20of%20millions%20of%20episodes.%20To%20do%20so%20in%20a%20reasonable%20time%2C%20you'd%20probably%20want%20to%20run%20many%20sim%20instances%20in%20parallel.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2069869%22%20slang%3D%22en-US%22%3ERe%3A%20Bonsai%20Tic%20Tac%20Toe%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2069869%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F930211%22%20target%3D%22_blank%22%3E%40victor91%3C%2FA%3E%26nbsp%3B108K%20episodes%20is%20probably%20not%20nearly%20enough%20to%20train%20a%20policy%20like%20this.%20If%20this%20policy%20is%20trainable%2C%20it's%20likely%20to%20take%2010x%20to%20100x%20as%20many%20episodes.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYour%20reward%20and%20terminal%20functions%20look%20reasonable%20to%20me%2C%20although%20you%20might%20want%20to%20boost%20the%20reward%20value%20for%20a%20win%20to%20something%20like%20%2B100.%20Otherwise%20it%20may%20learn%20to%20draw%20out%20the%20game%20as%20long%20as%20possible%20so%20it%20can%20receive%20multiple%20%2B4%20rewards%20rather%20than%20quickly%20win%20and%20receive%20%2B20.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20also%20typically%20recommend%20using%20negative%20reward%20values%20for%20all%20terminal%20states%20that%20represent%20failures%20like%20%22the%20other%20side%20won%22%20and%20%22it's%20a%20tie%20game%22.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20manually%20launch%20multiple%20instances%20of%20your%20sim.%20The%20Bonsai%20service%20will%20then%20run%20multiple%20episodes%20in%20parallel.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20get%20really%20adventuresome%2C%20you%20could%20package%20your%20simulator%20into%20a%20docker%20container%2C%20upload%20it%20to%20Azure%2C%20and%20have%20the%20Bonsai%20service%20automatically%20manage%20the%20launching%20and%20scaling%20of%20your%20sim.%20If%20you%20try%20the%20%22cartpole%22%20or%20%22moab%22%20sample%20projects%2C%20you'll%20see%20what%20that%20looks%20like.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi,

 

I'm doing a Tic Tac Toe game, in which one of the two players use bonsai to get the movements. I implemented a simulator in which in each step the action received from bonsai and the action for player 2, this player choose his movement randomly, are performed. I define in Inkling file a positive reward for correct movements and when player 1 wins, and negative reward for wrong movements, player 2 wins or  there aren't available movements. I define a terminal function when player1 wins, player2 wins, bonsai chooses wrong movement or there aren't available movement. The main problem is that bonsai doesn't learn the game rules, when the train ends it continues doing wrong movements (choose a occupied position). Can you help me find the problem? Or does bonsai not work well in this game?. I think it is a difficult problem because bonsai isn't rewarded only for its movement, but for its movement and the movement of the other player.

7 Replies

@victor91 Thanks for trying the platform.

 

You are correct that this is a difficult problem because the trajectories change based on another agent whose moves are not entirely predictable. 

 

Would you be willing to share your inkling contents here? Perhaps there are some suggestions we could offer.

 

A few additional questions:

1. You said that you provide a negative reward for "wrong movements". What constitutes a wrong movement? Is it one that is illegal (i.e. choosing a square that already has an X or an O present)? Or are you applying some heuristic to indicate whether the move was strategically sound?

2. How long have you allowed this to train? How many episodes? For a problem like this, it could take millions or tens of millions of episodes. To do so in a reasonable time, you'd probably want to run many sim instances in parallel.

Hi @erictr,

 

Yes, I attach the Inkling file.

 

1. A wrong movement is just an ilegal movement, choosing a square that already has an X or an O present.

2. This trains for 8 hours, 108.000 episodes. Train for these episodes because the NoProgressIterationLimit control stop the training with 250.000 iterations without processing. How can I run many sim instances in parallel?.

@victor91 108K episodes is probably not nearly enough to train a policy like this. If this policy is trainable, it's likely to take 10x to 100x as many episodes. 

 

Your reward and terminal functions look reasonable to me, although you might want to boost the reward value for a win to something like +100. Otherwise it may learn to draw out the game as long as possible so it can receive multiple +4 rewards rather than quickly win and receive +20.

 

I also typically recommend using negative reward values for all terminal states that represent failures like "the other side won" and "it's a tie game".

 

You can manually launch multiple instances of your sim. The Bonsai service will then run multiple episodes in parallel.

 

If you want to get really adventuresome, you could package your simulator into a docker container, upload it to Azure, and have the Bonsai service automatically manage the launching and scaling of your sim. If you try the "cartpole" or "moab" sample projects, you'll see what that looks like.

Hi @erictr,

 

When you say "you can manually launch multiple instances of your sim. The Bonsai service will then run multiple episodes in parallel". How can I manually launch multiple instances in my local sim?. Because when I launch a new instance a new simulator is created, and from bonsai you can train a brain choosing only one simulator.

 

Regards.

@victor91 

If you want to connect multiple local simulators, you'll have to use the bonsai-cli.

Docs are located here -> https://docs.microsoft.com/en-us/bonsai/cli/


This is the command you probably want to use in this situation -> https://docs.microsoft.com/en-us/bonsai/cli/simulator/unmanaged/connect

 

 

Hi @Navvaran_Mann,

 

I have more questions about this game.

 

  1. Is the Inkling file well implemented in terms of good practices. I don't know if it is correct to calculate the repeated_movenum_available_moves and winner in the simulator and pass these values in the state to the brain or it is more correct to pass to the brain the board and previous state of the board and calculate in Inkling file if is a correct move, if there are available moves or some player has won.
  2. ¿Is this game a correct example to correctly understand the bonsai project, or are better other examples?. ¿What examples would be better?. I can understand that the bonsai was optimized for other types of projects. I think this game isn't very difficult and as you told me the brain will have to train for many episodes, if I want to train a more complex simulator, for example a simulation with airsim on a dron, would this training be much longer, right?.
  3. Would it be possible to implement this problem using goals instead of terminal and reward functions?. I didn't find a way to do this, because goals only allow ranges, not a concrete value.

Best Regards,

Víctor Vicente.

 

 

 

 

 

 

@victor91 Those are all insightful questions.

 

The set of state inputs that you make available to the brain is usually dictated by the problem definition. The bonsai platform is designed to solve real-world autonomous systems applications, and the constraints of these real-world problems normally dictate which state inputs are available. In many cases, this translates to a set of sensors that are available in the target deployment environment. Since your example is a "toy" example, you'll need to decide how you want to define the problem.

 

The sample you've chosen isn't a great example for a few reasons. First, it's a multi-agent problem, and our platform is designed for single-agent. That means the state will change in ways that are somewhat unpredictable between iterations, making it difficult for an RL algorithm to learn optimal actions. Second, it is a problem that is better solved using other traditional approaches rather than reinforcement learning. It's a bit like reaching for a screwdriver to pound in a nail. Third, the reward signals are sparse and don't allow for "reward shaping". Fourth, if you are able to get this policy to converge (and I think you will be able to with sufficient training), the policy will effectively just "memorize" the optimal action for each board state.

 

You might want to look at our "Moab" sample project. We designed that as an example of a problem that is well-suited for the bonsai platform — and one that can be extended in interesting ways. It's also an example that can be applied to a large class of real-world control problems.

 

Yes, you should be able to use goals, but it probably won't work any better than a hand-coded reward function. For many problems, goals can perform better because the platform is able to generate reward shaping that helps the policy converge faster. You are correct in noting that goal require ranges, but the range can contain be zero-length (`Goal.Range(1, 1)`). You can use an "avoid" objective to avoid illegal moves and loss conditions.