Answers.Body: <p>From your linked description of the game, we can see it has a key property when used normally in AI teaching:</p>
  <blockquote>
  <ul>
  <li><strong>Partially observable:</strong> The Wumpus world is partially observable because the agent can only perceive the close environment such as an adjacent room.</li>
  </ul>
  </blockquote>
  <p>This makes sense, the problem of avoiding hazards would be trivial if the full map was revealed to the agent. The problem is also not about learning a specific map.</p>
  <p>You <em>could</em> use the agent's perception to construct a simple state table based on current observations:</p>
  <ul>
  <li>Current location</li>
  <li>Current facing</li>
  <li>Boolean flags for stench, breeze, glitter, bump, scream</li>
  </ul>
  <p>This would be relatively easy to build into a Q table, and might have some success. However, it would likely perform a lot worse than the propositional logic and planning suggested suggested at <a href="https://www.javatpoint.com/the-wumpus-world-in-artificial-intelligence" rel="nofollow noreferrer">https://www.javatpoint.com/the-wumpus-world-in-artificial-intelligence</a> for two reasons:</p>
  <ul>
  <li><p>Agent will not take account of knowledge specific to the current map that it has gathered. This is critical, because the stench and breeze sensors only tell you that at least <em>one</em> of the adjacent rooms has a hazard. In theory you know that this will not be any of the rooms that the agent has already visited, but a simple state representation based on current observations will not capture this.</p>
  </li>
  <li><p>Agent will not plan using the deterministic rules of the game that you know and could code for.</p>
  </li>
  </ul>
  <p>There are a few different approaches you could take to improve on these issues. Sticking with Q learning and trying to solve this by improving the state representation, you could look at the suggested knowledge-building structure from the java T point site, and replicate something like it as input features:</p>
  <ul>
  <li>Current facing</li>
  <li>Whether or not the last move forward resulted in a bump</li>
  <li>Whether or not there has been a scream</li>
  <li>For each allowed room, a binary flag set to true if the room has been visited by the agent</li>
  <li>For each allowed room, a set of binary variables covering the three observations that are about the room: stench, breeze, glitter</li>
  <li>Either the current location separately, or another binary variable added to each grid square set to true for the one room that the agent is in</li>
  </ul>
  <p>When multiplied by 16 to cover each room, you will end up with ~84 binary flags. Although many combinations will not be possible, this is still going to be far too large a state space to use with a Q table. You would probably use a simple neural network and DQN agent to solve this problem using Q learning.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-14T16:33:49.437Z
Answers.Text: 1) "For each allowed room" - do you mean for each room of the gridworld - 16 in 4x4 grid ? 2) So in the 4x4 grid world there will be: 16 (4x4 - x * y) * 4 (agent's direction) * 2 (if last move forward was bump) * 2 (if scream) * 2 (if visited by agent) * (2 * 2 * 2) (stench, breeze, glitter) * 16 (agent's location) = 65536 ? 3) For each allowed room, a binary flag set to true if the room has been visited by the agent - why do we use this information ?
Answers.UserDisplayName: Geperd
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-14T17:34:36.440Z
Answers.Text: @Geperd Your state space caclulation is way off, because it is not tracking separate flgas for each room. The state space is around $2^{84}$. We need to use this information because figuring out logical positions of the pits and wumpus is part of the problem, and cannot be done using current observation (this is why I critique the simple observation "Agent will not take account of knowledge specific to the current map that it has gathered."). You need a structure within the agent that summarises information discovered so far - my suggestion at the end of the answer is one way to do this.
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-14T18:32:53.007Z
Answers.Text: Sorry to ask you again and again but i can't see it yet. So the state space is: $2^4$ (direction) * $2^2$ (bump) * $2^2$ (scream) * $2^{16} * 2^2$ (each room if visited) * $2^{16} * 2^2 * 2^{16} * 2^2 * 2^{16} * 2^2$ (for each room stench, breeze, glitter) *  $2^{16} * 2^2$ (if agent is in room) = $2^{98}$ ??? If my calculations are wrong can you please write it in comment in similar fashion as above?
Answers.UserDisplayName: Geperd
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-14T18:57:55.700Z
Answers.Text: @Geperd You have some extra $2^2$ in there for most of the factors. For instance, a binary flag on each room for "stench" is $2^{16}$, not $2^{16} \times 2^2$. There is some flexibility about how you represent current location and direction, so you may not get exactly $2^{84}$. That is not too important, but you should expect to have something closer to it than $2^{98}$
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-14T15:54:52.017Z
Answers.LastActivityDate: 2021-03-14T16:01:53.777Z
Answers.LastEditDate: 2021-03-14T16:01:53.777Z
Answers.UserDisplayName: Neil Slater
Body: <p>I'm trying to use Q-learning in order to solve Wumpus world environment.</p>
  <p>Wumpus world is a toy problem on 4x4 gridworld. The agent starts in entry position of the cave, looks for gold (agent can sense that he is on the gold field), then he has to pick it up and leave cave in the entry position. Some fields are safe, other contain pit or wumpus (monster). If agent move to the pit or wumpus field he dies. The fields next to wumpus or pit (not diagonally!) have properties that agent can sense - stench (wumpus), breeze (pit). Agent achieve positive reward if he leaves cave with gold and negative if he dies. Action space: turn right/left, move forward, shoot arrow (if shot in good direction it can kill wumpus, only 1 available), pick up gold, leave cave. There: <a href="https://www.javatpoint.com/the-wumpus-world-in-artificial-intelligence" rel="nofollow noreferrer">https://www.javatpoint.com/the-wumpus-world-in-artificial-intelligence</a> you can find more detailed description of environment.</p>
  <p>It is easy to solve this problem if the gridworld is constant. I have a huge problem to even start thinking about it if gridworld is random in every learning episode (random fields and random size) and also random in testing. How should I define it (especially state space)? How should I create q table? I'm using Python.</p>
  <p>Thank you in advance for any help.</p>
  
Tags: <reinforcement-learning><q-learning><state-spaces>
Title: Q-learning in gridworld with random board
UserDisplayName: Geperd
ThreadText: # Q-learning in gridworld with random board
  
  (Asked by: username_0 on 2021-03-14)
  
  I'm trying to use Q-learning in order to solve Wumpus world environment.
  
  Wumpus world is a toy problem on 4x4 gridworld. The agent starts in entry position of the cave, looks for gold (agent can sense that he is on the gold field), then he has to pick it up and leave cave in the entry position. Some fields are safe, other contain pit or wumpus (monster). If agent move to the pit or wumpus field he dies. The fields next to wumpus or pit (not diagonally!) have properties that agent can sense - stench (wumpus), breeze (pit). Agent achieve positive reward if he leaves cave with gold and negative if he dies. Action space: turn right/left, move forward, shoot arrow (if shot in good direction it can kill wumpus, only 1 available), pick up gold, leave cave. There: <https://www.javatpoint.com/the-wumpus-world-in-artificial-intelligence> you can find more detailed description of environment.
  
  It is easy to solve this problem if the gridworld is constant. I have a huge problem to even start thinking about it if gridworld is random in every learning episode (random fields and random size) and also random in testing. How should I define it (especially state space)? How should I create q table? I'm using Python.
  
  Thank you in advance for any help.
  
  **Tags:** reinforcement-learning, q-learning, state-spaces
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-03-14. Score: 1 | Accepted answer
  
  From your linked description of the game, we can see it has a key property when used normally in AI teaching:
  
  > * **Partially observable:** The Wumpus world is partially observable because the agent can only perceive the close environment such as an adjacent room.
  
  This makes sense, the problem of avoiding hazards would be trivial if the full map was revealed to the agent. The problem is also not about learning a specific map.
  
  You *could* use the agent's perception to construct a simple state table based on current observations:
  
  * Current location
  * Current facing
  * Boolean flags for stench, breeze, glitter, bump, scream
  
  This would be relatively easy to build into a Q table, and might have some success. However, it would likely perform a lot worse than the propositional logic and planning suggested suggested at <https://www.javatpoint.com/the-wumpus-world-in-artificial-intelligence> for two reasons:
  
  * Agent will not take account of knowledge specific to the current map that it has gathered. This is critical, because the stench and breeze sensors only tell you that at least *one* of the adjacent rooms has a hazard. In theory you know that this will not be any of the rooms that the agent has already visited, but a simple state representation based on current observations will not capture this.
  * Agent will not plan using the deterministic rules of the game that you know and could code for.
  
  There are a few different approaches you could take to improve on these issues. Sticking with Q learning and trying to solve this by improving the state representation, you could look at the suggested knowledge-building structure from the java T point site, and replicate something like it as input features:
  
  * Current facing
  * Whether or not the last move forward resulted in a bump
  * Whether or not there has been a scream
  * For each allowed room, a binary flag set to true if the room has been visited by the agent
  * For each allowed room, a set of binary variables covering the three observations that are about the room: stench, breeze, glitter
  * Either the current location separately, or another binary variable added to each grid square set to true for the one room that the agent is in
  
  When multiplied by 16 to cover each room, you will end up with ~84 binary flags. Although many combinations will not be possible, this is still going to be far too large a state space to use with a Q table. You would probably use a simple neural network and DQN agent to solve this problem using Q learning.
  
  > **Comments:**
  > **username_0**: 1) "For each allowed room" - do you mean for each room of the gridworld - 16 in 4x4 grid ? 2) So in the 4x4 grid world there will be: 16 (4x4 - x * y) * 4 (agent's direction) * 2 (if last move forward was bump) * 2 (if scream) * 2 (if visited by agent) * (2 * 2 * 2) (stench, breeze, glitter) * 16 (agent's location) = 65536 ? 3) For each allowed room, a binary flag set to true if the room has been visited by the agent - why do we use this information ?  
  > **username_1**: @Geperd Your state space caclulation is way off, because it is not tracking separate flgas for each room. The state space is around $2^{84}$. We need to use this information because figuring out logical positions of the pits and wumpus is part of the problem, and cannot be done using current observation (this is why I critique the simple observation "Agent will not take account of knowledge specific to the current map that it has gathered."). You need a structure within the agent that summarises information discovered so far - my suggestion at the end of the answer is one way to do this.  
  > **username_0**: Sorry to ask you again and again but i can't see it yet. So the state space is: $2^4$ (direction) * $2^2$ (bump) * $2^2$ (scream) * $2^{16} * 2^2$ (each room if visited) * $2^{16} * 2^2 * 2^{16} * 2^2 * 2^{16} * 2^2$ (for each room stench, breeze, glitter) *  $2^{16} * 2^2$ (if agent is in room) = $2^{98}$ ??? If my calculations are wrong can you please write it in comment in similar fashion as above?  
  > **username_1**: @Geperd You have some extra $2^2$ in there for most of the factors. For instance, a binary flag on each room for "stench" is $2^{16}$, not $2^{16} \times 2^2$. There is some flexibility about how you represent current location and direction, so you may not get exactly $2^{84}$. That is not too important, but you should expect to have something closer to it than $2^{98}$  
  
  ---
  
  