{"id":2501,"date":"2019-01-05T14:19:49","date_gmt":"2019-01-05T14:19:49","guid":{"rendered":"http:\/\/intelligentonlinetools.com\/blog\/?page_id=2501"},"modified":"2019-01-07T15:25:15","modified_gmt":"2019-01-07T15:25:15","slug":"reinforcement-learning-dqn-run-planning","status":"publish","type":"page","link":"http:\/\/intelligentonlinetools.com\/blog\/reinforcement-learning-dqn-run-planning\/","title":{"rendered":"Reinforcement Learning DQN Run Planning"},"content":{"rendered":"<p>This is the python source code of run_planning_RL_DQN.py for post <a href=\"http:\/\/intelligentonlinetools.com\/blog\/2019\/01\/02\/reinforcement-learning-python-dqn-application-resource-allocation\/\" target=\"_blank\">Reinforcement Learning Python DQN Application for Resource Allocation<\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&quot;&quot;&quot;\r\nSimplest model-based RL, DQN\r\nRewards 3, 10 are specified in env script in the following\r\nThe first column is the goal to achieve - for example number of time units to complete.\r\nGoal_completion_criteria_and_rewards = [\r\n                        [2,3],\r\n                        [3,10]\r\n                        ]              \r\n\r\nThis script is the main part which controls the update method of this example.\r\nThe RL is in RL_brainDQN.py.\r\nagent = &quot;RANDOM_AGENT&quot;   or &quot;&quot;\r\nactions 0 , 1\r\ngoals  ML project,   RL project  \r\n\r\nstate positions\r\nnumber of hours(steps) for each goal   [4,8]  example\r\ninitial [0,0]    for each episode\r\n&quot;&quot;&quot;\r\n\r\nfrom planning_envDQN import Maze\r\nfrom RL_brainDQN import DeepQNetwork   \r\n\r\n\r\noutput_data=[]\r\nindexes=[]\r\ndef update():\r\n   \r\n    counter=0\r\n    sum=0\r\n    step=0\r\n  \r\n    for episode in range(2000):    \r\n        \r\n        env.reset()\r\n        print (&quot;episode=&quot; + str(episode))\r\n      \r\n        s_position=[0,0]    \r\n       \r\n        while True:\r\n            \r\n            a = RL.choose_action((s_position))\r\n            print (&quot;Action :&quot;)\r\n            print (a)\r\n      \r\n\r\n                    \r\n            s_next_position, r, done = env.step(a)\r\n     \r\n            RL.store_transition(s_position, a, r, s_next_position)\r\n         \r\n          \r\n            step=step+1\r\n            if (step &gt; 200) and (step % 5 == 0):\r\n                 RL.learn()\r\n         \r\n            s_position = s_next_position.copy()\r\n\r\n            if done:\r\n              \r\n                sum=sum+r\r\n                if episode % 50 == 0:\r\n                    output_data.append (sum \/ 50)\r\n                    sum=0\r\n                    indexes.append ( episode)\r\n                    counter=counter+1\r\n                \r\n                             \r\n                break\r\n\r\n   \r\n    print('episodes over')\r\n   \r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    env = Maze()\r\n  \r\n    RL = DeepQNetwork(env.n_actions, env.n_features,\r\n                      learning_rate=0.0045,\r\n                      reward_decay=0.9,\r\n                      e_greedy=0.9,\r\n                      replace_target_iter=400,\r\n                      memory_size=2000,\r\n                    \r\n                      )\r\n\r\n    update()\r\n  \r\n    import matplotlib.pyplot as plt\r\n\r\n    plt.figure(1)\r\n    plt.plot(indexes, output_data, label='RL')\r\n    plt.ylabel('Reward')\r\n    plt.xlabel('training steps')\r\n    plt.show()\r\n    \r\n    RL.plot_cost()\r\n\r\n&quot;&quot;&quot;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is the python source code of run_planning_RL_DQN.py for post Reinforcement Learning Python DQN Application for Resource Allocation<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false},"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P7h1IJ-El","jetpack-related-posts":[{"id":2495,"url":"http:\/\/intelligentonlinetools.com\/blog\/reinforcement-learning-dqn-planning-environment\/","url_meta":{"origin":2501,"position":0},"title":"Reinforcement Learning DQN Planning Environment","date":"January 5, 2019","format":false,"excerpt":"This is the python source code of planning_envDQN.py for post Reinforcement Learning Python DQN Application for Resource Allocation","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2364,"url":"http:\/\/intelligentonlinetools.com\/blog\/rl-dyna-q-run-planning-rl\/","url_meta":{"origin":2501,"position":1},"title":"Reinforcement Learning Dyna-Q Run Planning","date":"November 3, 2018","format":false,"excerpt":"This is the python source code of run_planning_RL.py for post Reinforcement Learning Example for Planning Tasks Using Q Learning and Dyna-Q","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2366,"url":"http:\/\/intelligentonlinetools.com\/blog\/rl-dyna-q\/","url_meta":{"origin":2501,"position":2},"title":"Reinforcement Learning Dyna-Q","date":"November 3, 2018","format":false,"excerpt":"This is the python source code of RL_brain.py for post Reinforcement Learning Example for Planning Tasks Using Q Learning and Dyna-Q","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2499,"url":"http:\/\/intelligentonlinetools.com\/blog\/reinforcement-learning-dqn\/","url_meta":{"origin":2501,"position":3},"title":"Reinforcement Learning DQN","date":"January 5, 2019","format":false,"excerpt":"This is the python source code of RL_brainDQN.py for post Reinforcement Learning Python DQN Application for Resource Allocation","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2368,"url":"http:\/\/intelligentonlinetools.com\/blog\/rl-dyna-q-planning-env\/","url_meta":{"origin":2501,"position":4},"title":"Reinforcement Learning Dyna-Q Planning Environment","date":"November 3, 2018","format":false,"excerpt":"This is the python source code of planning_env.py for post Reinforcement Learning Example for Planning Tasks Using Q Learning and Dyna-Q","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2096,"url":"http:\/\/intelligentonlinetools.com\/blog\/source-code-for-machine-learning-correlation-data-analysis-between-food-and-mood\/","url_meta":{"origin":2501,"position":5},"title":"Source Code for Machine Learning Correlation Data Analysis Between Food and Mood","date":"June 14, 2018","format":false,"excerpt":"Here is the code for Machine Learning Correlation Data Analysis Between Food and Mood Python pandas dataframe is used in this script for calculation correlation between two time series.","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/pages\/2501"}],"collection":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/comments?post=2501"}],"version-history":[{"count":4,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/pages\/2501\/revisions"}],"predecessor-version":[{"id":2518,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/pages\/2501\/revisions\/2518"}],"wp:attachment":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/media?parent=2501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}