• attachment:qlearn.py of openai_ros/TurtleBot2 with openai_ros

Attachment 'qlearn.py'

Download

   1 '''
   2 Q-learning approach for different RL problems
   3 as part of the basic series on reinforcement learning @
   4 https://github.com/vmayoral/basic_reinforcement_learning
   5  
   6 Inspired by https://gym.openai.com/evaluations/eval_kWknKOkPQ7izrixdhriurA
   7  
   8         @author: Victor Mayoral Vilches <victor@erlerobotics.com>
   9 '''
  10 import random
  11 
  12 class QLearn:
  13     def __init__(self, actions, epsilon, alpha, gamma):
  14         self.q = {}
  15         self.epsilon = epsilon  # exploration constant
  16         self.alpha = alpha      # discount constant
  17         self.gamma = gamma      # discount factor
  18         self.actions = actions
  19 
  20     def getQ(self, state, action):
  21         return self.q.get((state, action), 0.0)
  22 
  23     def learnQ(self, state, action, reward, value):
  24         '''
  25         Q-learning:
  26             Q(s, a) += alpha * (reward(s,a) + max(Q(s') - Q(s,a))            
  27         '''
  28         oldv = self.q.get((state, action), None)
  29         if oldv is None:
  30             self.q[(state, action)] = reward
  31         else:
  32             self.q[(state, action)] = oldv + self.alpha * (value - oldv)
  33 
  34     def chooseAction(self, state, return_q=False):
  35         q = [self.getQ(state, a) for a in self.actions]
  36         maxQ = max(q)
  37 
  38         if random.random() < self.epsilon:
  39             minQ = min(q); mag = max(abs(minQ), abs(maxQ))
  40             # add random values to all the actions, recalculate maxQ
  41             q = [q[i] + random.random() * mag - .5 * mag for i in range(len(self.actions))] 
  42             maxQ = max(q)
  43 
  44         count = q.count(maxQ)
  45         # In case there're several state-action max values 
  46         # we select a random one among them
  47         if count > 1:
  48             best = [i for i in range(len(self.actions)) if q[i] == maxQ]
  49             i = random.choice(best)
  50         else:
  51             i = q.index(maxQ)
  52 
  53         action = self.actions[i]        
  54         if return_q: # if they want it, give it!
  55             return action, q
  56         return action
  57 
  58     def learn(self, state1, action1, reward, state2):
  59         maxqnew = max([self.getQ(state2, a) for a in self.actions])
  60         self.learnQ(state1, action1, reward, reward + self.gamma*maxqnew)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2018-08-01 18:38:25, 99.9 KB) [[attachment:gym_computers1.png]]
  • [get | view] (2018-08-01 18:38:01, 45.6 KB) [[attachment:gym_computers2.png]]
  • [get | view] (2018-08-01 18:38:11, 77.6 KB) [[attachment:gym_computers3.png]]
  • [get | view] (2018-08-01 18:13:37, 5413.9 KB) [[attachment:maze_training.gif]]
  • [get | view] (2019-04-11 15:12:41, 4.8 KB) [[attachment:my_start_qlearning_wall_v2.py]]
  • [get | view] (2018-08-01 18:02:54, 1.6 KB) [[attachment:my_turtlebot2_maze_params.yaml]]
  • [get | view] (2018-08-02 10:26:36, 12.5 KB) [[attachment:my_turtlebot2_wall.py]]
  • [get | view] (2018-08-01 18:24:41, 1.6 KB) [[attachment:my_turtlebot2_wall_params.yaml]]
  • [get | view] (2019-10-10 16:43:26, 2.1 KB) [[attachment:qlearn.py]]
  • [get | view] (2018-08-01 18:16:23, 28.8 KB) [[attachment:reward1.png]]
  • [get | view] (2018-08-01 18:55:23, 50.0 KB) [[attachment:ria_logo.png]]
  • [get | view] (2018-08-02 10:18:02, 4.5 KB) [[attachment:start_qlearning.py]]
  • [get | view] (2018-08-01 18:10:03, 0.4 KB) [[attachment:start_training.launch]]
  • [get | view] (2018-08-02 10:24:21, 4.5 KB) [[attachment:start_training.py]]
  • [get | view] (2018-08-01 18:26:18, 4.5 KB) [[attachment:start_training_wall.py]]
  • [get | view] (2018-08-01 17:32:53, 339.6 KB) [[attachment:turtlebot2_maze.png]]
  • [get | view] (2019-04-11 15:12:53, 0.4 KB) [[attachment:turtlebot2_openai_qlearn_params_wall_v2.yaml]]
  • [get | view] (2018-08-02 10:28:28, 20436.3 KB) [[attachment:turtlebot2_tut_maze.gif]]
  • [get | view] (2018-08-01 18:20:02, 124.3 KB) [[attachment:turtlebot2_wall.png]]
  • [get | view] (2018-08-02 10:27:55, 400.6 KB) [[attachment:turtlebot_maze_v2.png]]
  • [get | view] (2018-08-01 18:34:36, 3992.2 KB) [[attachment:wall_training.gif]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.