Answers.Body: <p>A policy that handles an arbitrary number of actions can be specified as a function (think: neural network) that takes one specific action as input and outputs some kind of score.</p>
  <p>That score may be the estimated action-value, but the point is that you can simply pick the action with maximum score now. Or feed the variable number of scores into a softmax distribution, then sample from it to pick a plausible random action.</p>
  <p>I'm not sure if the Gym/Gymnasium API is any help in modelling this kind of action-space. For board games specifically you may find some recent papers[1] that explain their approach, which usually involves tree-search.</p>
  <p>Also think about how your agent will see the world. If it gets to select one of 500 actions, it will have a really hard time (need a lot of data) to learn that action number 311 means something completely different in one state compared to another. So depending on the game, it may be useful to add local features. (Like state of the evaluated action's neighbours, if applicable.) If the game state is just one huge opaque bitmask, the agent will have to discover via trial and error that there are local neighbourhoods, symmetry, etc.</p>
  <p>Again specifically for board-games, if you can exactly predict the next state based on an action, it may be sufficient learn the value of a state.</p>
  <p>[1]: Schrittwieser et al., 2020: <a href="https://arxiv.org/abs/1911.08265" rel="nofollow noreferrer">Mastering Atari, Go, Chess and Shogi by Planning with a
  Learned Model</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-23T10:09:53.347Z
Answers.LastActivityDate: 2023-06-23T10:51:23.567Z
Answers.LastEditDate: 2023-06-23T10:51:23.567Z
Answers.UserDisplayName: maxy
Body: <p>I'm new to the world of AI and have been primarily reading through the documentation for OpenAI's Gym/Gymnasium in hopes of training an AI to play a board game. One piece of information I haven't been able to find is the best way to define an action space when the number of possible actions is countably infinite.</p>
  <p>For instance, I've seen several examples of simple games where the player must choose a card to play from their hand. In all of these, the hand has a maximum size, and the action space is discrete with its cardinality being the largest possible hand size. But what if there is no theoretical limit to the size of the player's hand? Let's say a player starts with a hand of seven cards, but the number of cards in their hand could grow (or shrink) to any natural number throughout the course of the game. How would one go about defining the action space in this scenario?</p>
  <p>I've thought of four potential ways to handle this:</p>
  <ol>
  <li>Define one action for every possible card and mask the action space so that only cards in the player's hand are legal actions. This would mean 52 actions for a standard deck of playing cards, which doesn't seem so bad. But for certain games, there could be many more kinds of unique cards (potentially thousands in the case of trading card games), and I could see the action space becoming unwieldy in those situations.</li>
  <li>Define a continuous action space on the interval [0,1] and partition it equally into however many cards are in the player's hand. For example, if there are 10 cards in the hand, an action in [0,0.1) would play the first card, one in [0.1,0.2) would play the second, and so on. I'm unsure if this would be a wise setup since &quot;nearby&quot; actions like 0.09 and 0.11 wouldn't necessarily correspond to similar results because cards 1 and 2 aren't ordered in a meaningful way.</li>
  <li>Define two actions: one for playing a card and the other for passing. Loop through each card in the hand and apply the action to each one until the &quot;play&quot; action is used. In this case, I'm not sure if the AI would be able to &quot;look ahead&quot; to know that a better card is coming up so therefore it should pass on the current card.</li>
  <li>Pick an arbitrarily large hand limit that a player would never reach in practice and define a discrete number of actions up to that limit.</li>
  </ol>
  <p>Would any of these approaches work? Are any favored over the others? Are there other ways of approaching this that I haven't thought of?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-06-26T22:39:51.453Z
Comments.Text: is this a question in general or is there a specific task you are trying to get an agent to perform? Without further information it may not be possible to give a satisfactory answer because this is a very open ended question. For example, what are the rules of this game? Do we start with a limited number of cards in our hand but this value could grow? Are we "dealt" a countably infinite hand?
Comments.UserDisplayName: respectful
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-06-27T16:48:14.110Z
Comments.Text: Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
Comments.UserDisplayName: Community
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-06-28T00:42:34.350Z
Comments.Text: @respectful I meant this as a general question, but I've added a couple of details to make it a little more specific--namely, starting with a fixed hand size that could grow or shrink throughout the game.
Comments.UserDisplayName: haec0007
Tags: <reinforcement-learning><game-ai><open-ai><gym><action-spaces>
Title: How do you define an action space for a card game with an unlimited and variable hand size?
UserDisplayName: haec0007
ThreadText: # How do you define an action space for a card game with an unlimited and variable hand size?
  
  (Asked by: username_0 on 2023-06-20)
  
  I'm new to the world of AI and have been primarily reading through the documentation for OpenAI's Gym/Gymnasium in hopes of training an AI to play a board game. One piece of information I haven't been able to find is the best way to define an action space when the number of possible actions is countably infinite.
  
  For instance, I've seen several examples of simple games where the player must choose a card to play from their hand. In all of these, the hand has a maximum size, and the action space is discrete with its cardinality being the largest possible hand size. But what if there is no theoretical limit to the size of the player's hand? Let's say a player starts with a hand of seven cards, but the number of cards in their hand could grow (or shrink) to any natural number throughout the course of the game. How would one go about defining the action space in this scenario?
  
  I've thought of four potential ways to handle this:
  
  1. Define one action for every possible card and mask the action space so that only cards in the player's hand are legal actions. This would mean 52 actions for a standard deck of playing cards, which doesn't seem so bad. But for certain games, there could be many more kinds of unique cards (potentially thousands in the case of trading card games), and I could see the action space becoming unwieldy in those situations.
  2. Define a continuous action space on the interval [0,1] and partition it equally into however many cards are in the player's hand. For example, if there are 10 cards in the hand, an action in [0,0.1) would play the first card, one in [0.1,0.2) would play the second, and so on. I'm unsure if this would be a wise setup since "nearby" actions like 0.09 and 0.11 wouldn't necessarily correspond to similar results because cards 1 and 2 aren't ordered in a meaningful way.
  3. Define two actions: one for playing a card and the other for passing. Loop through each card in the hand and apply the action to each one until the "play" action is used. In this case, I'm not sure if the AI would be able to "look ahead" to know that a better card is coming up so therefore it should pass on the current card.
  4. Pick an arbitrarily large hand limit that a player would never reach in practice and define a discrete number of actions up to that limit.
  
  Would any of these approaches work? Are any favored over the others? Are there other ways of approaching this that I haven't thought of?
  
  **Tags:** reinforcement-learning, game-ai, open-ai, gym, action-spaces
  
  > **Comments:**
  > **username_1**: is this a question in general or is there a specific task you are trying to get an agent to perform? Without further information it may not be possible to give a satisfactory answer because this is a very open ended question. For example, what are the rules of this game? Do we start with a limited number of cards in our hand but this value could grow? Are we "dealt" a countably infinite hand?  
  > **username_2**: Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.  
  > **username_0**: @username_1 I meant this as a general question, but I've added a couple of details to make it a little more specific--namely, starting with a fixed hand size that could grow or shrink throughout the game.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2023-06-23. Score: 1
  
  A policy that handles an arbitrary number of actions can be specified as a function (think: neural network) that takes one specific action as input and outputs some kind of score.
  
  That score may be the estimated action-value, but the point is that you can simply pick the action with maximum score now. Or feed the variable number of scores into a softmax distribution, then sample from it to pick a plausible random action.
  
  I'm not sure if the Gym/Gymnasium API is any help in modelling this kind of action-space. For board games specifically you may find some recent papers[1] that explain their approach, which usually involves tree-search.
  
  Also think about how your agent will see the world. If it gets to select one of 500 actions, it will have a really hard time (need a lot of data) to learn that action number 311 means something completely different in one state compared to another. So depending on the game, it may be useful to add local features. (Like state of the evaluated action's neighbours, if applicable.) If the game state is just one huge opaque bitmask, the agent will have to discover via trial and error that there are local neighbourhoods, symmetry, etc.
  
  Again specifically for board-games, if you can exactly predict the next state based on an action, it may be sufficient learn the value of a state.
  
  [1]: Schrittwieser et al., 2020: [Mastering Atari, Go, Chess and Shogi by Planning with a
  Learned Model](https://arxiv.org/abs/1911.08265)
  
  
  ---
  
  