Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1.  
  2. class classic_nelder_mead_solver:
  3. def __init__(self,
  4. initial_state,
  5. degrees_of_freedom,
  6. reflector = 1.0,
  7. expander = 2.0,
  8. contractor= 0.5,
  9. shrinker = 0.5,
  10. convergence_threshold = 1e-8,
  11. verbose_level = 1):
  12.  
  13. #Classic Nelder Mead Parameters
  14. #https://en.wikipedia.org/wiki/Nelder-Mead_method
  15.  
  16. self.reflector = reflector
  17. self.expander = expander
  18. self.contractor = contractor
  19. self.shrinker = shrinker
  20. self.conv_threshold = convergence_threshold
  21.  
  22. self.dofs = degrees_of_freedom
  23. self.state = np.zeros((self.dofs+1,self.dofs+1))
  24. self.state[0:self.dofs,:] = initial_state
  25. self.status = "Initializing"
  26.  
  27. self.sanity_check()
  28. self.verbose_level = verbose_level
  29. self.function_eval_count = 0
  30. self.initialize_triangle()
  31. self.sort_state()
  32.  
  33. def sanity_check(self):
  34. state_shape = self.state.shape
  35. if(state_shape[1] != state_shape[0]):
  36. print("Your initial guess is malformed.")
  37.  
  38. def initialize_triangle(self):
  39. for indx in range(self.dofs):
  40. position = self.state[0:self.dofs,indx]
  41. self.state[self.dofs,indx] = self.evaluate_objective_function(position)
  42.  
  43. def sort_state(self):
  44. self.state = self.state[:,self.state[self.dofs,:].argsort()]
  45.  
  46. def get_standing_best(self):
  47. self.standing_best = self.state[self.dofs,0]
  48.  
  49. def evaluate_objective_function(self, position):
  50. #Objective Function
  51. #out = -1.0 * objective_function(position)
  52. self.function_eval_count += 1
  53. out = obj(position)
  54. return out
  55.  
  56. def optimize(self):
  57. self.sort_state()
  58. self.get_standing_best()
  59. self.stop_flag = False
  60. current_best = 1.0 * self.standing_best
  61. self.iteration_count = 0
  62. while not self.stop_flag:
  63. self.iterate()
  64. std_dev = np.std(self.state[self.dofs,:])
  65. if(std_dev < self.conv_threshold):
  66. self.stop_flag = True
  67. if self.verbose_level > 0:
  68. print("Total Iterations: " + str(self.iteration_count))
  69. print("Total Function Evaluations: " + str(self.function_eval_count))
  70.  
  71. def update_state(self,position,feval):
  72. self.state[self.dofs,-1] = feval
  73. self.state[0:self.dofs,-1] = position
  74.  
  75. def update_by_shrinkage(self):
  76. best_state = self.state[0:self.dofs,0]
  77. for col in range(1,self.dofs+1):
  78. current_state = self.state[0:self.dofs, col]
  79. contracted_state = best_state + self.contractor*(current_state - best_state)
  80. self.state[0:self.dofs,col] = contracted_state
  81. self.state[self.dofs, col] = self.evaluate_objective_function(contracted_state)
  82.  
  83. def iterate(self):
  84. #ORDER
  85. self.sort_state()
  86. self.iteration_count+=1
  87.  
  88. remaining_points = self.state[0:self.dofs,0:self.dofs]
  89. #Compute the centroid of the facet opposite the worst point
  90. face_centroid = np.einsum('ij->i',remaining_points) * (1.0 / self.dofs)
  91.  
  92. #Compute and evaluate the reflection point
  93. reflection = face_centroid + self.reflector*(face_centroid - self.state[0:self.dofs,-1])
  94. f_reflection = self.evaluate_objective_function(reflection)
  95.  
  96. f_best = self.state[self.dofs,0]
  97. f_worst = self.state[self.dofs,-1]
  98. f_2nd_worst = self.state[self.dofs,-2]
  99.  
  100. if(self.verbose_level > 1):
  101. self.verbose_header()
  102.  
  103. if((f_best <= f_reflection) and (f_reflection < f_2nd_worst)):
  104. self.update_state(reflection, f_reflection)
  105. self.status = "Reflection"
  106. elif(f_reflection < f_best):
  107. expansion = face_centroid + self.expander*(reflection - face_centroid)
  108. f_expansion = self.evaluate_objective_function(expansion)
  109. if(f_expansion < f_reflection):
  110. self.update_state(expansion, f_expansion)
  111. self.status = "Expansion"
  112. else:
  113. self.update_state(reflection, f_reflection)
  114. self.status = "Reflection"
  115. else:
  116. contraction = face_centroid + self.contractor * (self.state[0:self.dofs,-1] - face_centroid)
  117. f_contraction = self.evaluate_objective_function(contraction)
  118. if(f_contraction < f_worst):
  119. self.update_state(contraction, f_contraction)
  120. self.status = "Contraction"
  121. else:
  122. self.update_by_shrinkage()
  123. self.status = "Shrinkage"
  124. if self.verbose_level > 1:
  125. self.verbose_footer()
  126.  
  127. def verbose_header(self):
  128. print("--------------------------------------------------------------------------------")
  129. print("Iteration " + str(self.iteration_count))
  130. print("Current Best: " + str(self.state[self.dofs,0]))
  131.  
  132. def verbose_footer(self):
  133. print("Iteration Outcome: " + str(self.status))
  134. print("--------------------------------------------------------------------------------")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement