Q-value_iteration.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: huiming zhou
  5. """
  6. import env
  7. import plotting
  8. import motion_model
  9. import numpy as np
  10. import sys
  11. class Q_value_iteration:
  12. def __init__(self, x_start, x_goal):
  13. self.xI, self.xG = x_start, x_goal
  14. self.e = 0.001 # threshold for convergence
  15. self.gamma = 0.9 # discount factor
  16. self.env = env.Env(self.xI, self.xG) # class Env
  17. self.motion = motion_model.Motion_model(self.xI, self.xG) # class Motion_model
  18. self.plotting = plotting.Plotting(self.xI, self.xG) # class Plotting
  19. self.u_set = self.env.motions # feasible input set
  20. self.stateSpace = self.env.stateSpace # state space
  21. self.obs = self.env.obs_map() # position of obstacles
  22. self.lose = self.env.lose_map() # position of lose states
  23. self.name1 = "Q-value_iteration, gamma=" + str(self.gamma)
  24. self.name2 = "converge process, e=" + str(self.e)
  25. [self.value, self.policy, self.diff] = self.iteration(self.xI, self.xG)
  26. self.path = self.extract_path(self.xI, self.xG, self.policy)
  27. self.plotting.animation(self.path, self.name1)
  28. self.plotting.plot_diff(self.diff, self.name2)
  29. def iteration(self, xI, xG):
  30. """
  31. Q_value_iteration
  32. :return: converged Q table and policy
  33. """
  34. Q_table = {}
  35. policy = {}
  36. diff = []
  37. delta = sys.maxsize
  38. count = 0
  39. for x in self.stateSpace:
  40. Q_table[x] = [0, 0, 0, 0] # initialize Q_table
  41. while delta > self.e: # convergence condition
  42. count += 1
  43. x_value = 0
  44. for x in self.stateSpace:
  45. if x not in x_Goal:
  46. for k in range(len(self.u_set)):
  47. [x_next, p_next] = self.motion.move_next(x, self.u_set[k])
  48. Q_value = self.cal_Q_value(x_next, p_next, Q_table)
  49. v_diff = abs(Q_table[x][k] - Q_value)
  50. Q_table[x][k] = Q_value
  51. if v_diff > 0:
  52. x_value = max(x_value, v_diff)
  53. diff.append(x_value)
  54. delta = x_value
  55. for x in self.stateSpace:
  56. if x not in xG:
  57. policy[x] = np.argmax(Q_table[x])
  58. self.message(count)
  59. return Q_table, policy, diff
  60. def cal_Q_value(self, x, p, table):
  61. """
  62. cal Q_value.
  63. :param x: next state vector
  64. :param p: probability of each state
  65. :param table: value table
  66. :return: Q-value
  67. """
  68. value = 0
  69. reward = self.env.get_reward(x) # get reward of next state
  70. for i in range(len(x)):
  71. value += p[i] * (reward[i] + self.gamma * max(table[x[i]]))
  72. return value
  73. def extract_path(self, xI, xG, policy):
  74. """
  75. extract path from converged policy.
  76. :param xI: starting state
  77. :param xG: goal states
  78. :param policy: converged policy
  79. :return: path
  80. """
  81. x, path = xI, [xI]
  82. while x not in xG:
  83. u = self.u_set[policy[x]]
  84. x_next = (x[0] + u[0], x[1] + u[1])
  85. if x_next in self.obs:
  86. print("Collision! Please run again!")
  87. break
  88. else:
  89. path.append(x_next)
  90. x = x_next
  91. return path
  92. def message(self, count):
  93. """
  94. print important message.
  95. :param count: iteration numbers
  96. :return: print
  97. """
  98. print("starting state: ", self.xI)
  99. print("goal states: ", self.xG)
  100. print("condition for convergence: ", self.e)
  101. print("discount factor: ", self.gamma)
  102. print("iteration times: ", count)
  103. if __name__ == '__main__':
  104. x_Start = (5, 5)
  105. x_Goal = [(49, 5), (49, 25)]
  106. QVI = Q_value_iteration(x_Start, x_Goal)