value_iteration.py 4.0 KB

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