Answers.Body: <p>This is an episodic problem, and there should be no issue in theory with most learning algorithms coping without a discount factor (or setting gamma = 1).</p>
  
  <blockquote>
    <p>Are there any explanations to why the agent only works for small discount factors in this example?</p>
  </blockquote>
  
  <p>The most likely explanations are:</p>
  
  <ul>
  <li><p>You have a mistake in your implementation or use of DQN.</p></li>
  <li><p>You have an incorrect setting of a neural network hyperparameter. My initial thought would be you have mistakenly put a non-linearity like softmax or sigmoid on the NN outputs (it needs to be a linear output). Or it could just be that 10 hidden neurons is not enough for this representation</p></li>
  <li><p>Convergence requires far longer to train than you thought, once you introduce non-linear relationships between state and expected reward.</p></li>
  </ul>
  
  <p>It is not surprising that the simpler puzzle trains more easily, as the agent can reduce it down to a linear counting puzzle without really needing to "look" at the representation. I would expect the Q values to be very poor approximations to the correct values after so few iterations, but the problem is simple enough that the incorrect values still produce an optimal behaviour.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-07T01:22:33.833Z
Answers.Text: Thanks so much for your reply! I have tried more hidden nodes, output layer is linear and have waited more than 10k episodes for convergence. I think it might be a mistake in my implementation. Something that I have suspected is due to my implementation of illegal moves. During the choose action step: If the entire column is already filled (S_ij = 1, forall i), I enforced that the column is not allowed to be chosen. And I would choose the next highest action value as the best action. Will this be the main cause of the problem?
Answers.UserDisplayName: terenceflow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-07T08:20:23.150Z
Answers.Text: @terenceflow: I don't think that would be a direct cause of a problem - although you should look how you handle the data around that change. I would actually recommend changing the problem so that you terminate the episode on an illegal move, meaning the agent scores whatever it got so far (this is what would happen in Tetris). Note that filling the last column is not necessary for optimal behaviour, the agent scores 0 whatever it does, so it doesn't matter and you should not expect an agent to fill all columns.
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-06T08:36:59.880Z
Answers.LastActivityDate: 2018-02-06T08:36:59.880Z
Answers.UserDisplayName: Neil Slater
Body: <p>I have tried several environment libraries like OpenAI gym/gridworld but now I am trying to create a toy environment for experimentation. The environment I've created is as follows: </p>
  
  <ol>
  <li><p>State: grid with n rows by m columns, represented by a boolean matrix. Each grid cell can be empty or filled and the grid starts empty.</p></li>
  <li><p>Action: one of the m columns to be filled, which must have at least the top row empty. </p></li>
  <li><p>Next state: Once a column is chosen, the lowest unfilled cell in that column is filled. This works from bottom up like a very simple version of Tetris. </p></li>
  <li><p>Reward: after every action, a reward equal to the number of empty columns is awarded. </p></li>
  </ol>
  
  <p>Therefore in a sample world of 5 rows by 3 column, starting off with an empty grid, the maximum attainable reward would be by filling column wise first. This policy will give a maximum total reward of 2*5 + 1*5 = 15. (2 free columns by 5 row action, once first column is filled then 1 free column by 5 row action.) </p>
  
  <p>This very simple environment is trained using DQN with a single ff layer. The agent only took a few episodes to converge and is able to produce the maximum attainable reward. </p>
  
  <p>In a next toy environment, I've made it a little more complex. I modified the very first action to be random choice of any column. I have retrained the RL model with the new environment modification. However, after convergence, the agent does not attain max score of 15 for all possible starting columns. I.e. If column 1 was randomly chosen first, max score might be 15, however column 2 or 3 was randomly chosen first, max score might only reach 11 or 9. In theory, the optimum policy would be for the agent to fill column that was randomly chosen first - i.e. repeat the first randomly chosen action.</p>
  
  <p>I have tried several ways to tweak my input parameters (e.g. episilon_decay_rate, learning_rate, batch_size, number of hidden nodes) to see if the agent could act optimally for all possible starting columns. I also tried DDQN and Sarsa. The only way I could make the agent perform optimally is by reducing gamma (discount factor) to 0.5 or below. Are there any explanations to why the agent only works for small discount factors in this example? Also, are there alternative ways to obtain the optimum policy?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-03T09:53:47.357Z
Comments.Text: The description of your environment seems incomplete. I cannot make sense of it. For example you say "Once a column is chosen, **that** row of the column is filled from bottom up like in a tetris fashion." [emphasis mine]. But at no point has a row been described or selected by the agent or the environment . . . some visualised steps of the game might help, but also review whether you have given a complete and valid description. You definitely have some kind of implementation issue, since for an episodic problem it should be fine to set $\gamma = 1$ and still expect to find optimal solution
Comments.UserDisplayName: Neil Slater
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-03T15:07:04.283Z
Comments.Text: Sorry for being unclear, indeed my description of action is incomplete. I will try to explain it again.
  
  Action: one of the m columns chosen to be filled. Or more precisely.
  If column j is chosen, S_ij will be set to 1, where i is the min row i such that S_ij != 0. (Here S is the boolean matrix representation of the states)
Comments.UserDisplayName: terenceflow
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-03T17:28:57.230Z
Comments.Text: Regarding: "This very simple environment is trained using DQN with a single ff layer." If I understand this correctly, you have a linear regression here, there is no hidden layer. Could you confirm the NN architecture? I am imagining it is 15/3 linear network, with 15 inputs -> 3 outputs predicting reward for each of the 3 actions?
Comments.UserDisplayName: Neil Slater
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-04T02:31:09.323Z
Comments.Text: Yes state matrix is flatten, the network has 15 input and 3 output. Hidden layer has 10 nodes, learning rate is 1e-3. In the first environment, after 20+ episodes the agent is able to obtain the maximum score for any chosen m and n. But not after the modification.
Comments.UserDisplayName: terenceflow
Tags: <reinforcement-learning>
Title: Agent in toy environment only learns to act optimally with small discount factors
UserDisplayName: terenceflow
ThreadText: # Agent in toy environment only learns to act optimally with small discount factors
  
  (Asked by: username_0 on 2018-02-03)
  
  I have tried several environment libraries like OpenAI gym/gridworld but now I am trying to create a toy environment for experimentation. The environment I've created is as follows:
  
  1. State: grid with n rows by m columns, represented by a boolean matrix. Each grid cell can be empty or filled and the grid starts empty.
  2. Action: one of the m columns to be filled, which must have at least the top row empty.
  3. Next state: Once a column is chosen, the lowest unfilled cell in that column is filled. This works from bottom up like a very simple version of Tetris.
  4. Reward: after every action, a reward equal to the number of empty columns is awarded.
  
  Therefore in a sample world of 5 rows by 3 column, starting off with an empty grid, the maximum attainable reward would be by filling column wise first. This policy will give a maximum total reward of 2\*5 + 1\*5 = 15. (2 free columns by 5 row action, once first column is filled then 1 free column by 5 row action.)
  
  This very simple environment is trained using DQN with a single ff layer. The agent only took a few episodes to converge and is able to produce the maximum attainable reward.
  
  In a next toy environment, I've made it a little more complex. I modified the very first action to be random choice of any column. I have retrained the RL model with the new environment modification. However, after convergence, the agent does not attain max score of 15 for all possible starting columns. I.e. If column 1 was randomly chosen first, max score might be 15, however column 2 or 3 was randomly chosen first, max score might only reach 11 or 9. In theory, the optimum policy would be for the agent to fill column that was randomly chosen first - i.e. repeat the first randomly chosen action.
  
  I have tried several ways to tweak my input parameters (e.g. episilon\_decay\_rate, learning\_rate, batch\_size, number of hidden nodes) to see if the agent could act optimally for all possible starting columns. I also tried DDQN and Sarsa. The only way I could make the agent perform optimally is by reducing gamma (discount factor) to 0.5 or below. Are there any explanations to why the agent only works for small discount factors in this example? Also, are there alternative ways to obtain the optimum policy?
  
  **Tags:** reinforcement-learning
  > **Comments:**
  > **username_1**: The description of your environment seems incomplete. I cannot make sense of it. For example you say "Once a column is chosen, **that** row of the column is filled from bottom up like in a tetris fashion." [emphasis mine]. But at no point has a row been described or selected by the agent or the environment . . . some visualised steps of the game might help, but also review whether you have given a complete and valid description. You definitely have some kind of implementation issue, since for an episodic problem it should be fine to set $\gamma = 1$ and still expect to find optimal solution  
  > **username_0**: Sorry for being unclear, indeed my description of action is incomplete. I will try to explain it again.  Action: one of the m columns chosen to be filled. Or more precisely. If column j is chosen, S_ij will be set to 1, where i is the min row i such that S_ij != 0. (Here S is the boolean matrix representation of the states)  
  > **username_1**: Regarding: "This very simple environment is trained using DQN with a single ff layer." If I understand this correctly, you have a linear regression here, there is no hidden layer. Could you confirm the NN architecture? I am imagining it is 15/3 linear network, with 15 inputs -> 3 outputs predicting reward for each of the 3 actions?  
  > **username_0**: Yes state matrix is flatten, the network has 15 input and 3 output. Hidden layer has 10 nodes, learning rate is 1e-3. In the first environment, after 20+ episodes the agent is able to obtain the maximum score for any chosen m and n. But not after the modification.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-02-06. Score: 1
  
  This is an episodic problem, and there should be no issue in theory with most learning algorithms coping without a discount factor (or setting gamma = 1).
  
  > Are there any explanations to why the agent only works for small discount factors in this example?
  
  The most likely explanations are:
  
  * You have a mistake in your implementation or use of DQN.
  * You have an incorrect setting of a neural network hyperparameter. My initial thought would be you have mistakenly put a non-linearity like softmax or sigmoid on the NN outputs (it needs to be a linear output). Or it could just be that 10 hidden neurons is not enough for this representation
  * Convergence requires far longer to train than you thought, once you introduce non-linear relationships between state and expected reward.
  
  It is not surprising that the simpler puzzle trains more easily, as the agent can reduce it down to a linear counting puzzle without really needing to "look" at the representation. I would expect the Q values to be very poor approximations to the correct values after so few iterations, but the problem is simple enough that the incorrect values still produce an optimal behaviour.
  
  > **Comments:**
  > **username_0**: Thanks so much for your reply! I have tried more hidden nodes, output layer is linear and have waited more than 10k episodes for convergence. I think it might be a mistake in my implementation. Something that I have suspected is due to my implementation of illegal moves. During the choose action step: If the entire column is already filled (S_ij = 1, forall i), I enforced that the column is not allowed to be chosen. And I would choose the next highest action value as the best action. Will this be the main cause of the problem?  
  > **username_1**: @terenceflow: I don't think that would be a direct cause of a problem - although you should look how you handle the data around that change. I would actually recommend changing the problem so that you terminate the episode on an illegal move, meaning the agent scores whatever it got so far (this is what would happen in Tetris). Note that filling the last column is not necessary for optimal behaviour, the agent scores 0 whatever it does, so it doesn't matter and you should not expect an agent to fill all columns.  
  
  ---
  
  