Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def call_cma(self):
w1= self.controller.get_weight(layer_nb, numpoids1)[0]
w2= self.controller.get_weight(layer_nb, numpoids2)[0]
x = np.array([w1,w2])
self.green_x.append(w1)
self.green_y.append(w2)
fx = cma.fmin(self.run_episode, x, sigma, options = self.options)
self.final_draw()
if verbose:
print('Optimization: set x0 as zeros')
if self.cen is not None:
x0 = self.cen
else:
x0 = np.zeros(self.prob.dim)
self.create_directory()
if verbose:
print('CMA-ES: cen = ', self.cen)
print('CMA-ES: rng = ', self.rng)
print('Optimization begins at ', str(datetime.now()))
#print('normalized_center = ', self.normalize(x0))
# for k, v in self.options.iteritems():
# print(k, '\t', v)
res = cma.fmin(None,
self.normalize(x0),
sigma,
parallel_objective=self.eval_f,
options=self.options)
if verbose:
print('Optimization ends at ', str(datetime.now()))
print('Total times = %.2fs' % (time.time() - begin))
ret = scipy.optimize.OptimizeResult()
ret['y'] = res[0]
ret['x'] = self.unnormalize(res[0])
ret['fun'] = res[1]
# assert(np.allclose(res[1], self.prob.f(ret['x'])))
ret['nfev'] = self.eval_counter
# ret['jac'] = self.eval_g(ret['x'])
ret['message'] = 'Optimization terminated successfully.'
candsol = []
candsol.append(model.getCoordinateSet().get('LHip_rz').getDefaultValue())
candsol.append(model.getCoordinateSet().get('RHip_rz').getDefaultValue())
candsol.append(model.getCoordinateSet().get('LKnee_rz').getDefaultValue())
candsol.append(model.getCoordinateSet().get('RKnee_rz').getDefaultValue())
candsol.append(0.1)
candsol.append(model.getCoordinateSet().get('LHip_rz').getDefaultSpeedValue())
candsol.append(model.getCoordinateSet().get('RHip_rz').getDefaultSpeedValue())
candsol.append(model.getCoordinateSet().get('LKnee_rz').getDefaultSpeedValue())
candsol.append(model.getCoordinateSet().get('RKnee_rz').getDefaultSpeedValue())
# Optimize.
t_start = time.time()
# For a description of arguments to fmin(), run cma.CMAOptions() or see
# http://cma.gforge.inria.fr/apidocs-pycma/cma.evolution_strategy.html#fmin
result = cma.fmin(walker_simulation_objective_function, candsol, 0.5,
options = {'popsize':20, 'tolfun':1e-3, 'tolx':1e-3,
'maxfevals':100})
t_elapsed = time.time() - t_start
print('Elapsed time: %f seconds' % (t_elapsed))
# Find the best solution.
max_distance = max(all_distances)
print('Best distance: %f meters' % (max_distance))
idx = all_distances.index(max_distance)
bestsol = all_candsols[idx]
print(bestsol)
# Assign best solution to model and save.
model.getCoordinateSet().get('LHip_rz').setDefaultValue(bestsol[0])
model.getCoordinateSet().get('RHip_rz').setDefaultValue(bestsol[1])
model.getCoordinateSet().get('LKnee_rz').setDefaultValue(bestsol[2])
score_train, score_test = readout.readout_sk(states_train, states_test,
np.asarray(_label_train), np.asarray(_label_test),
solver="lbfgs", multi_class="multinomial")
# ----------show results-----------
print('parameters %s' % parameter)
print('Train score: ', score_train)
print('Test score: ', score_test)
return 1 - score_test
##########################################
# -------CMA-ES parameters search---------------
if __name__ == '__main__':
core = 10
pool = Pool(core)
parameters = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
res = cma.fmin(parameters_search, parameters, 0.5, options={'ftarget': -1e+3,'bounds': [0, 1],
'maxiter':30})
def _env_optimize_posterior_mean_and_std(self, model, X_lower, X_upper,
is_env, startpoint):
def f(x):
mu, var = model.predict(x[np.newaxis, :])
return (mu + np.sqrt(var))[0, 0]
res = cma.fmin(f, startpoint, 0.6,
options={"bounds": [X_lower, X_upper]})
env_values = X_upper[is_env == 1]
xopt = res[0]
# Map incumbent to smax
xopt[is_env == 1] = env_values
fval = np.array([res[1]])
return xopt, fval
score_train, score_test = readout.readout_sk(states_train, states_test,
np.asarray(_label_train), np.asarray(_label_test),
solver="lbfgs", multi_class="multinomial")
# ----------show results-----------
print('parameters %s' % parameter)
print('Train score: ', score_train)
print('Test score: ', score_test)
return 1 - score_test
##########################################
# -------CMA-ES parameters search---------------
if __name__ == '__main__':
core = 10
pool = Pool(core)
parameters = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
res = cma.fmin(parameters_search, parameters, 0.5, options={'ftarget': -1e+3,'bounds': [0, 1],
'maxiter':30})
x_[self.is_env == 0] = res[0]
x_opt[i] = x_
fval[i] = res[1]
else:
res = optimize.minimize(self.f,
startpoint[self.is_env == 0],
bounds=list(zip(self.sub_X_lower, self.sub_X_upper)),
method="L-BFGS-B",
options={"disp": True})
x_ = np.zeros([self.is_env.shape[0]])
x_[self.is_env == 1] = self.env_values
x_[self.is_env == 0] = res["x"]
x_opt[i] = x_
fval[i] = res["fun"]
elif self.method == 'cmaes':
res = cma.fmin(self.f, startpoint[self.is_env == 0], 0.6,
options={"bounds": [self.sub_X_lower, self.sub_X_upper]})
x_ = np.zeros([self.is_env.shape[0]])
x_[self.is_env == 1] = self.env_values
x_[self.is_env == 0] = res[0]
x_opt[i] = x_
fval[i] = res[1]
# Return the point with the lowest function value
best = np.argmin(fval)
return x_opt[best, np.newaxis, :], np.array([[fval[best]]])
def acq_max_(self,ac, gp, y_max, bounds, random_state):
x_seeds = random_state.uniform(bounds[:, 0], bounds[:, 1],
size=(bounds.shape[0]))
options = {'ftarget': 1e-3, 'bounds': bounds.T.tolist(), 'maxiter': 1000,
'verb_log': 0,'verb_time':False,'verbose':-9}
res = cma.fmin(lambda x: 1 - ac(x.reshape(1, -1), gp=gp, y_max=y_max), x_seeds, 0.25, options=options,
restarts=0, incpopsize=0, restart_from_best=False, bipop=False)
x_max = res[0]
return np.clip(x_max, bounds[:, 0], bounds[:, 1])
np.asarray(_label_train), np.asarray(_label_validation),
np.asarray(_label_test), solver="lbfgs", multi_class="multinomial")
# ----------show results-----------
print('parameters %s' % parameter)
print('Train score: ', score_train)
print('Validation score: ', score_validation)
print('Test score: ', score_test)
return 1-score_validation, 1 - score_test, 1- score_train, parameter
##########################################
# -------CMA-ES parameters search---------------
if __name__ == '__main__':
core = 10
pool = Pool(core)
parameters = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
res = cma.fmin(parameters_search, parameters, 0.5, options={'ftarget': -1e+3,'bounds': [0, 1],
'maxiter':30})