optimizer.py
Go to the documentation of this file.
1 # CMA-ES implementation here as a subclass of optimizer
2 
3 import cma
4 import sys
5 from timeout import *
6 
7 from random import randint
8 import rospy
9 
10 
11 
12 class optimizer:
13 
14  _params_desc = {}
15  _eval_callback = lambda _:None
16  _timeout = 60
17 
18  # SIGMA = 0.005
19  # SIGMA = 0.25
20  SIGMA = 0.1
21 
22  _timeout_count = 0.
23 
24  INF = 100000000000
25 
26 
27  def set_params_desc(self, desc):
28  # update the configuration of the parameters e.g. min, max, default values.
29  self._params_desc = desc
30  pass
31 
32  def set_eval_callback(self, func):
33  # set the objective value that the optimizer uses for evaluation
34  self._eval_callback = func
35  pass
36 
37  def set_params_config(self, config):
38  for key, value in config.items():
39  print key, value
40  if key not in self._params_desc:
41  self._params_desc[key] = {}
42  self._params_desc[key]['default'] = value
43  pass
44 
45  def __init__(self, desc, eval_callback, timeout = 60):
46  # initializer
47  # set the params configs and the objective function
48  self.set_params_desc(desc)
49  self.set_eval_callback(eval_callback)
50  self.set_timeout(timeout)
51  pass
52 
53  def set_timeout(self, timeout):
54  self._timeout = timeout
55  pass
56 
57  def optimize(self):
58  default_values = [ self.normalize(param_desc) for _, param_desc in self._params_desc.items() ]
59  print default_values
60 
61  # do the normalization and interfacing
62  # self.eval(default_values)
63  # return self.eval(default_values)
64 
65  # res = cma.fmin(self.eval, default_values, self.SIGMA)
66 
67  es = cma.CMAEvolutionStrategy(default_values, self.SIGMA)
68 
69  while not es.stop():
70  solutions = es.ask()
71 
72 
73  costs = []
74 
75  for sol in solutions:
76 
77  rospy.loginfo("==============================" + \
78  "==============================" )
79  rospy.loginfo("Iteration #%d: ", es.itereigenupdated)
80  rospy.loginfo("______________________________" + \
81  "______________________________" )
82  if es.best.evalsall:
83  rospy.loginfo("\t# of samples: %d", es.best.evalsall )
84  if es.best.x != None:
85  rospy.loginfo("\tbest solution found = \n\t\t%s: ",
86  ",\n\t\t".join([ str(key) + " : " + str(value)
87  for key, value in self.extract_config(es.best.x).items() ]))
88  rospy.loginfo("\tbest objective value = \t%f: ", es.best.f )
89 
90 
91  rospy.loginfo("______________________________" + \
92  "______________________________" )
93 
94  rospy.loginfo("running experiment for: \n\t %s", repr(list(sol)) )
95  cost = self.eval(sol)
96  rospy.loginfo("objective value = %f: \n", cost)
97  costs.append(cost)
98 
99  rospy.loginfo("==============================" + \
100  "==============================" )
101 
102  es.tell(solutions, costs)
103  es.disp()
104  es.result_pretty()
105 
106  res = es.result()
107 
108  return res
109 
110 
111  def extract_config(self, values):
112  params = self._params_desc.keys()
113 
114  # print "keys are: {0}".format(params)
115 
116  config = {}
117  for i in range(len(params)):
118  param = params[i]
119  desc = self._params_desc[param]
120  config[param] = self.denormalize(values[i], desc["min"], desc["max"])
121 
122  return config
123 
124  def normalize(self, param_desc):
125  # default_values = [ param_desc['default'] for _, param_desc in params_desc.items() ]
126  value_range = param_desc['max'] - param_desc['min']
127  value = (param_desc['default'] - param_desc['min']) / value_range
128  return value
129 
130  def denormalize(self, value, vmin, vmax):
131  value_range = vmax - vmin
132  nvalue = value * value_range + vmin
133  return nvalue
134 
135 
136 
137  def eval(self, values):
138 
139  print "raw values are: {0}".format(values)
140 
141  if any(val>1 or val<0 for val in values):
142  obj_val = 0.
143  for val in values:
144  if val>1:
145  obj_val += val-1
146  elif val<0:
147  obj_val += -val
148 
149  print("OUT OF BOUND")
150  return self.INF * obj_val
151 
152 
153  try:
154 
155  ## 131 1441 0.00896432940323 0 0.234643642667
156  # values = [ 0.00789639790315,
157  # 0.625585161861,
158  # 0.0292802172125,
159  # 0.452373387972,
160  # 0.034331367985,
161  # 0.269772560896,
162  # 0.0255957600625,
163  # 0.353741608684,
164  # 0.340681632899,
165  # 0.000624421036917,
166  # 0.00710255638588,
167  # 0.263852030857 ]
168 
169  # values = [0.0] * 12
170 
171  config = self.extract_config(values)
172  print "extracted configs are: {0}".format(config)
173 
174  print "timeout is: {0}".format(self._timeout)
175  @timeout(self._timeout)
176  def timeout_wrapper():
177  return self._eval_callback(config)
178  res = timeout_wrapper()
179 
180  print "result is: {0}".format(res)
181  return res
182 
183  except TimeoutError:
184  self._timeout_count += 1
185 
186  e = sys.exc_info()[0]
187 
188  print("experiment timeout. Error: %s" % e)
189  print("returning infinity")
190 
191  if self._timeout_count >= 2:
192  _timeout_count = 0
193  ## TODO: restart the simulation
194  print "Seems something is wrong!! Do you want to continue?"
195  # raise e
196  return self.INF + randint(-50000, 50000)
197 
198  except: # TypeError: # catch *all* exceptions
199  e = sys.exc_info()[0]
200  import traceback
201  traceback.print_exc()
202 
203  print("Something went wrong. Error: %s" % e)
204  print("returning infinity")
205  # raise e
206  return self.INF + randint(-50000, 50000)
def set_timeout(self, timeout)
Definition: optimizer.py:53
def __init__(self, desc, eval_callback, timeout=60)
Definition: optimizer.py:45
def set_params_config(self, config)
Definition: optimizer.py:37
def denormalize(self, value, vmin, vmax)
Definition: optimizer.py:130
def eval(self, values)
Definition: optimizer.py:137
def normalize(self, param_desc)
Definition: optimizer.py:124
def set_params_desc(self, desc)
Definition: optimizer.py:27
def timeout(seconds=10, error_message=os.strerror(errno.ETIME))
Definition: timeout.py:9
def set_eval_callback(self, func)
Definition: optimizer.py:32
def extract_config(self, values)
Definition: optimizer.py:111


dyn_tune
Author(s):
autogenerated on Mon Jun 10 2019 13:03:17