I need help using only one observation to represent a board game, and handling invalid moves
Hi everyone!
EDIT: I'm updating my questions as I keep testing different approaches, sorry!
I'm starting to use ML-Agents in our game, a simultaneous turn based strategy board game. Here's a screenshot to have a quick look at the game.

I started building the state as a list of properties in a one-hot fashion, but I think the state was way too large (8676, that's big for a continuous state, right?), so now I switched to use a camera for observation, using a simplified UI representation of the board like this:

Every agent receives a 126x126 black and white texture that represents a board, using 6x6 pixels for every cell, with all the needed data encoded in those pixels (like cell status, player, as well as available moves). Some pixels work as boolean, and some as normalized values.
Question 1: What do you think of this approach? Is this a valid way to build a state for ML?
.
The other thing I don't know is how to handle invalid moves and the rewards.
A Move has 5 properties: Type (enum 4 values), X, Y, (int), NewDirection (enum 6 values), MoveLength (int), so I set the action state to continuous and size 5.
Question 2 How should I reward the agent regarding invalid moves? Should I just ignore every move that a step receives an invalid move, or should I give the agent a negative reward?
.
I'm currently testing this setup:
- 128x128 b&w observation
- Continuous action state of size 5
- Ignoring invalid moves, thus giving 0 as reward
- Giving a +0.01 reward for every valid move
- And, if the move is valid, I give different rewards depending on the outcome of the match
Question 3 I'm using different combinations of hyperparameters to see how it changes, but I'm a little lost. What values do you think I should use for hyperparameters with this approach?
Thank you in advance!
Hello everyone! I'm sorry to insist on this, but I've been playing with everything for 2 months and I still do not know if I'm wasting my time with useless settings.
- Is it possible for an agent to learn from a single observation?
- Is it easier to learn from observations in black and white than in color? (If not, I could build a smaller observation, coding data in different channels)
I am new to all of this, so I do not know what I should look for in the TensorBoard charts as an indicator of learning. (So far I take into account the % of valid moves in the last X steps to see if at least it is doing something logical) These are the charts of some of my runs overlapped, and I guess they are all stuck, right?

Thanks!
I'm in no way an expert on the matter and there are people way more experienced, so everything I state here is not to trust blindly.
Question 1
What do you think of this approach? Is this a valid way to build a state for ML?
First thing all, if you've got "turn based" and "board" in your project description, there's a great chance you'll need discrete instead of continuous for both your states and actions. Indeed, your actions are all integer, and continuous action state returns float.
It's hard to know how efficient a setup can be when it's that big. I'd suggest reducing the complexity first, this can be done in various ways :
- Try with a smaller map at first
- Reduce number of informations available for each cell
- If your agents are units in your game, you might want to give them only information relevant to them (like the surrounding cells only, for instance).
- If your agents are more some kind of "players", you could try using 6 different observations, all pixels index would be the same, only color would change. I believe I've seen some pushes recently about multiple observations but you might want to check if it actually works first.
- Find a way to include curriculum in your training, as it really helps training to happen faster.
Question 2
How should I reward the agent regarding invalid moves?
It's good to have rewards that are normalized. For instance, when looking at your plots, it's hard to "feel" how bad or how good they're doing. Generally, you want your reward to range between -1 and 1. In order to do this, you have to at least vaguely know how many steps there are in your simulation. Do you call done = true? Or is it always reset at max_steps?
If your agents must train on picking a valid move at any turn, you could give +1 reward for a valid move, and -1 reward for an invalid one, and call done = true. This can be a good thing to have agents doing well in choosing a valid move at any point, before trying to have them choose the best one in the part of a strategy that can last several steps.
But as it looks like you're trying to reward them later for actions they did in the past, you might want to take a look at recurrent neural networks (I think the parameter is called state history in the editor). I know they did work on that on branch developement-0.3, but I am not sure how far it is in the developement, so you might want to check that too before diving in.
Question 3
What values do you think I should use for hyperparameters with this approach?
The best answer to that question might be this doc, which explain what each parameter does. Also, when tweaking around these values, try to stay in the indicated ranges, and to move only one hyperparameter at a time. You must also remember that scales are not natural, and if changing 0.001 to 0.002 seems insignifiant, it's a x2 factor.
I hope it will help you even a bit, good luck!
I am also experiencing similar problem. My app is a board game, I use discrete action and works well. But there are invalid move issues, e.g. AI select a move that already occupied by other player In order to solve this, I change the action into continuous. Then my problem starts, it seems continuous action can never be trained up no matter how I adjust the parameters
I am still having a difficult time to make continuous action work
Hi @ZephyrRaine ! Thank you for taking the time to answer :)
First thing all, if you've got "turn based" and "board" in your project description, there's a great chance you'll need discrete instead of continuous for both your states and actions. Indeed, your actions are all integer, and continuous action state returns float.
Currently I'm converting the floats returned in the action to ints, and then I create a Move object. I don't think this is the right way to do it, but I wouldn't know how to work with a discrete action state. There can be hundreds of possible moves in every turn, how can I choose which move to use with a discrete action state? (Correct me if I'm wrong, but in a discrete space you only recieve one int, right? and you just set how many values this int can have)
I was also thinking about sending a sort of list of moves as the state and expect an int as index, could this work?
Try with a smaller map at first Reduce number of informations available for each cell
Yes, I'm gonna start testing with the smallest board to see if I can achieve to train an AI, and I will also reduce the information in each cell. This should change the observation from 128x128 to something around 18x18.
If your agents are more some kind of "players", you could try using 6 different observations [...]
Each player has its own observation, and each cell for that player lights up in the data. 6 observations? Interesting, for the same agent? What data would change for each observation?
Find a way to include curriculum in your training, as it really helps training to happen faster.
I'm currently using curriculum learning but never got a good score to change lesson :(
Do you call done = true? Or is it always reset at max_steps?
I call done = true only when a player wins or loses, which obviusly rarely happens, but it does happen.
If your agents must train on picking a valid move at any turn, you could give +1 reward for a valid move, and -1 reward for an invalid one, and call done = true.
Ok, can the behaviour change with curriculum? Let's say I reward +1 and -1 and call done = true for each attempt, and after a certain efficiency the lesson changes, can I change my reward and done policy?
[...] you might want to take a look at recurrent neural networks
Yes, every time I turned recurrent = true the memory was exhausted, but I'll try again with a smaller board.
Thank you again for your suggestions, you gave me a new things to keep testing, I was starting to move on haha.
Hi @q7777777hk! How did you handle a discrete action state to choose a move?
My game is a very simple.
It has 10 x 10 map, each player can select a column in turn. Its move will be the bottomest of the slected column
Winning the game (4 move in a line) will has 1 reward
Losing the game will have -1 reward
My state is 100 and action is 10
When in discrete action 0 = select column 0, action 1 =select column 1 ........
Training result is normal as below

But once I change into continuous input, it never learns
Discrete action can only return 1 result, this is always a problem to solve invalid move. Besides, it also makes AI feedback without randomness. AI always takes same action under same situation, player will soon learn the pattern to defeat such AI And when a pattern is discovered, player can always win with same pattern
Oh ok, you have a small scope of action, an int for the column. In my case a move is a little complex, I need coordinates of the cell, and more data about what to do in that cell. That's why I started with a continuous action space of 5, and tried to build a move object from that, but I don't think it will work.
I wonder if passing a list of available moves in the state along with the board data, and using a discrete action to choose a move from that list could work.
I wonder if passing a list of available moves in the state along with the board data, and using a discrete action to choose a move from that list could work.
This is an interesting idea.
Hi all,
Thanks for all the discussion. Solving these kinds of problems is interesting, and we plan to continue to build ML-Agents in a way to support more complex kinds of games. We are closing this issue due to inactivity. Feel free to reopen it if you’d like to continue to discussion though.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.