def cma_tune(config): pid_controller = PendulumPID(1, 0, 0, Ki=0, config_path=config.get("pid-constants", None)) def test_on_env(params): return do_experiment_error(pid_controller, params[0], params[1], params[2], max_iterations=MAX_ITERATIONS) es = cma.CMAEvolutionStrategy([-20.266, 7.00, 0.202], 1.0) try: while not es.stop(): solutions = es.ask() es.tell(solutions, [test_on_env(x) for x in solutions]) es.logger.add(modulo=2) # write data to disc to be plotted es.disp() except KeyboardInterrupt as e: print(e) # es.result_pretty() logger.debug("mean : {}".format(es.mean)) logger.debug("var : {}".format(es.sigma)) logger.debug("best : {}".format(es.best.x)) cma.plot() return es.best.x
def goalBabble(self, x): print("Doing goal babbling now\n") p = self.net.params p[:] = x[3] i = 0 self.sMemory = np.array([1] * (INPUTSIZE + PREDICTSIZE)) self.mMemory = np.array([0] * OUTPUTSIZE) self.rest() sensedAngles = self.get_sensor_values() pv = sensedAngles[x[2]] #Choose a goal sensor value in the range of the predicted angle #self.randomGoal = self.rand.uniform(self.Body[x[1]][0],self.Body[x[1]][1]) self.randomGoal = self.rand.uniform(-0.5, 0.5) print(self.randomGoal) #CMA-ES can be used to discover joint angles that get #closer to the goal state!!!!!!!! self.x1 = x[1] self.x2 = x[2] res = cma.fmin(self.calcGoalScore, 0.1 * (np.random.random_sample(2, ) - 0.5), 0.5, maxfevals=500, verb_disp=1, bounds=[self.Body[x[1]][0], self.Body[x[1]][1]]) cma.plot() cma.show()
def optimize(fn): es = cma.CMAEvolutionStrategy(nz * [0], 0.5) es.optimize(fn, args=[5]) best = np.array(es.best.get()[0]) print ("BEST ", best) latent_vector = torch.FloatTensor(best).view(1, nz) output_image(latent_vector, "optimized") cma.plot()
def cmaOptFrcVals(env, trainDict, polDict, expDict): import cma #set to newPolDict if using retrained baseline polDictToUse = polDict #polDictToUse = newPolDict #for trained baseline #use func==fitnessRwrd for policy, ==fitnessBL for baseline func = fitnessRwrd #func = fitnessBL #initial state and state dot for CMA optimization #idxsToUse is a list of indexes in precalced state/statedot to use - set to none to use all idxsToUse = None #idxsToUse = [0] initQList, initQdotList = tFuncs.loadInitStates(env, expDict, idxsToUse=idxsToUse) #set CMA options opts = cma.CMAOptions() opts['bounds'] = [0, .5] # Limit our values to be between 0 and .5 opts['maxfevals'] = 450 #max # of evals of fitness func opts[ 'tolfun'] = 1.0e-02 #termination criterion: tolerance in function value, quite useful #for each proposal set initial state/statedot #env.wrapped_env.env.unwrapped.setDebugMode(False) #initialize CMA optimizer initVals = 2 * [0.25] initSTD = 0.25 es = cma.CMAEvolutionStrategy(initVals, initSTD, opts) itr = 1 #whether to use force value or force multiplier value - ignored for rwrd function calculation, only used to match training for baseline calc useFrc = expDict[ 'bl_useForce'] #should be true, not training policy using only multiplier #while not converged while not es.stop(): solns = es.ask() #for each solution proposal, set init state/state-dot and sol proposal, record return resList, resDictList, _ = execFunc(func, env, trainDict, polDictToUse, initQList, initQdotList, solns, useFrc) #inform CMA of results es.tell(solns, resList) es.logger.add() es.disp() print('') print('iter : {} done'.format(itr)) print('') itr = itr + 1 es.result_pretty() cma.plot() # shortcut for es.logger.plot() #result value : es.mean #actual force values, instead of multiplier frcX = env.wrapped_env.env.unwrapped.getForceFromMult(es.mean[0]) frcY = env.wrapped_env.env.unwrapped.getForceFromMult(es.mean[1]) print('final optimal force result : {},{}'.format(frcX, frcY))
def tuned_XGB(): fun = Objective_Function(objective_XGB) res = cma.fmin(fun, [0.3, 0], 1) cma.plot() cma.savefig('figures_cma.png') params = res[0] eta = params[0]**2 gamma = params[1]**2 model = XGBClassifier(eta=eta, gamma=gamma) return res[0], model
def main(): es = cma.CMAEvolutionStrategy(2 * [0], 0.5, { 'popsize': 8, 'maxfevals': 320, 'verb_disp': 1, 'seed': 3 }) # es = cma.purecma.CMAES(2 * [0], 0.5, popsize=8, maxfevals=16) t0 = time.time() while not es.stop(): solutions = es.ask() es.tell(solutions, torc.map(rosenbrock, solutions)) es.logger.add(es) # write data to disc to be plotted es.disp() t1 = time.time() print(es.result[0]) print(es.result[1]) print(t1 - t0) cma.plot()
def goalBabble(self, x): print("Doing goal babbling now\n") p = self.net.params p[:] = x[3] i = 0 self.sMemory = np.array([1]*(INPUTSIZE + PREDICTSIZE)) self.mMemory = np.array([0]*OUTPUTSIZE) self.rest() sensedAngles = self.get_sensor_values() pv = sensedAngles[x[2]] #Choose a goal sensor value in the range of the predicted angle #self.randomGoal = self.rand.uniform(self.Body[x[1]][0],self.Body[x[1]][1]) self.randomGoal = self.rand.uniform(-0.5,0.5) print(self.randomGoal) #CMA-ES can be used to discover joint angles that get #closer to the goal state!!!!!!!! self.x1 = x[1] self.x2 = x[2] res = cma.fmin(self.calcGoalScore, 0.1*(np.random.random_sample(2,)-0.5), 0.5,maxfevals=500, verb_disp=1, bounds=[self.Body[x[1]][0], self.Body[x[1]][1]] ) cma.plot() cma.show()
import sys import cma # python -m optimization.plot_cma './data/cma/trial_180625_1_' if __name__ == '__main__': cma.plot(sys.argv[1]) input()
fp.close() res= es.result() print 'best solutions = ', res[0] print 'best solutions fitness = %f' % (res[1]) print res fp= file('outcmaes_obj.dat','w') for x1 in frange(-4.0,4.0,100): for x2 in frange(-4.0,4.0,100): x= np.array([x1,x2]) fp.write('%s %f\n' % (' '.join(map(str,x)),fobj(x,-10))) fp.write('\n') fp.close() fp= file('outcmaes_res.dat','w') #for x in res[0]: x= res[0] fp.write('%s %f\n' % (' '.join(map(str,x)),fobj(x,-10))) fp.close() cma.plot(); print 'press a key to exit > ', raw_input() #cma.savefig('outcmaesgraph')
import cma es = cma.CMAEvolutionStrategy([0, 0.1, 0.2], 0.1) while not es.stop(): solutions = es.ask() #print(solutions) es.tell(solutions, [cma.ff.rosen(x) for x in solutions]) es.logger.add() # write data to disc to be plotted es.disp() es.result_pretty() cma.plot()
def main(self): # 500g foot at 18cm : addedInertia = 0.0162 self.updateModelConstants(12, self.i0, self.ke, self.r, self.klin, self.linearTransition, self.staticFriction, self.coulombFriction, addedInertia) self.simulationTest(0.7) return # print self # T = numpy.arange(0, 1, 0.0001) # timedCommands = zip(T, itertools.repeat(8)) # self.updateModelConstants(12, self.i0, self.ke, self.r, self.klin, self.linearTransition, self.staticFriction, self.coulombFriction) # self.simulation(timedCommands, 0, 0, printIt=True) # # return print "Initial model values :" print "voltage = ", self.voltage #12 print "io = ", self.i0 #0.00353 print "ke = ", self.ke #1.6 print "r = ", self.r #5.86 print "klin = ", self.klin #-1.60368670825 print "linearTransition = ", self.linearTransition #0.306796157577 print "staticFriction = ", self.staticFriction #0.150170648464 print "coulombFriction = ", self.coulombFriction #0.0750853242321 print "addedInertia = ", self.addedInertia#0.004 listOfMeasures = self.readMeasures("measures/completeTest", timeLimit=0.150, typeOffset=0) listOfMeasuresheavyLoad = self.readMeasures("measures/completeTestHeavyLoad", timeLimit=0.150, typeOffset=10) #Adding the heavy load measures listOfMeasures.extend(listOfMeasuresheavyLoad) #Max speed is 4PI/s = 120 rpm self.fixGaps(listOfMeasures, 4*math.pi) self.noDisplay = False #self.noDisplay = True #value = self.evaluateModelForMeasures(listOfMeasures, 12, self.i0, self.ke, self.r, self.klin, self.linearTransition, self.staticFriction, self.coulombFriction) #print value #tolfun 100 # value = self.evaluateModelForMeasures(listOfMeasures, 12, -0.0017217180565952648, 3.9265053688586535, 4.4190475060541337, -1.642030980764918, -0.19456964024780998, 0.19221036872483038, 0.60201284035701119) #tolfun 100 with limits on values # value = self.evaluateModelForMeasures(listOfMeasures, 12, 0.00905955, 3.29605672, 1.63030819, -1.6484249, 0.04996982, 0.19198735, 0.21656958) #tolfun 100 with limits on values # value = self.evaluateModelForMeasures(listOfMeasures, 12, 0.00794687, 1.49117495, 0.85706012, 1.64383661, -0.04078583, 0.07417589, 0.30257492) # value = self.evaluateModelForMeasures(listOfMeasures, 12, 0.0034689028202296171, 1.555666164824836, 1.1287356922053267, 1.6444843194436245, 0.41284770773063401, 0.1294117709539814, 0.29580470901307804) #tolfun 80 : # [0.0021731366250028568, 1.355067751766774, 0.94623234209939366, 1.6460100334124927, 0.027949448057014062, 0.12011789072794421, 0.27722890096104058] #Score of 5.27 # value = self.evaluateModelForMeasures(listOfMeasures, 12, 4.66664605e-04, 1.21498291e+00, 5.13895843e-01, 1.80236842e+00, 3.23283556e-01, 1.31244462e-01, 1.36619002e-01) #Score of 23 # value = self.evaluateModelForMeasures(listOfMeasures, 12, 0.32711334, 5.0, 11.26126126, 12.46876941, 37.99361901, 0.50304455, 0.50304455) #Score of 5 (only on the first 150 ms though) # value = self.evaluateModelForMeasures(listOfMeasures, 12, 0.00589097, 1.4666091, 4.10039631, 1.55765625, 0.25689851, 0.12147452, 0.08027712) #Score of 0.91 (only on the first 150 ms though) # value = self.evaluateModelForMeasures(listOfMeasures, 12, 0.00459395, 1.39735606, 3.26399923, 1.7137901, 0.59109595, 0.14349084, 0.11449425) #Score of 0.127 (only on the first 150 ms though) # value = self.evaluateModelForMeasures(listOfMeasures, 12, 0.00250709722, 1.57426512, 4.57635209, 1.62426655, 0.149425682, 0.0985980367, 0.0915512434) #Score of 10 (full length) # 3.83787890e-03 1.47894946e+00 4.25865142e+00 1.63136824e+00 9.17768154e-02 1.26099628e-01 9.98887909e-02 #Adding the "addedInertia" attribute from here # value = self.evaluateModelForMeasures(listOfMeasures, 12, 0.00250709722, 1.57426512, 4.57635209, 1.62426655, 0.149425682, 0.0985980367, 0.0915512434, 0.004) #Score of 0.94 with addedInertia, on the first 150ms # 1.07728666e-03 1.36831818e+00 3.91813279e+00 1.65976880e+00 1.75653773e-01 1.26764092e-01 9.03878047e-02 3.11286186e-03 #Score of 0.39 with addedInertia, on the first 150ms # 4.91495976e-04 1.62594599e+00 4.72658717e+00 1.63540545e+00 1.95274189e-01 1.21803765e-01 8.79444393e-02 4.07977291e-03 #Score of 0.39 with addedInertia, on the first 150ms # 0.00537005 1.39865656 4.06586179 1.63538543 0.19542816 0.12123053 0.1030164 0.00408195 #Score of 0.386 with addedInertia, on the first 150ms # 6.74933458e-03 1.50816125e+00 4.38408117e+00 1.63554751e+00 1.38872124e-01 1.45544129e-01 1.01677621e-01 4.09684545e-03 #Score of 0.382214837376 with addedInertia, on the first 150ms # [ 1.55818308e-02 1.51294795e+00 4.39798614e+00 1.63553529e+00 8.09641650e-02 2.03719639e-01 1.20403543e-01 4.07587229e-03] # print "value = ", value # return self.noDisplay = True params = [self.i0, self.ke, self.r, self.klin, self.linearTransition, self.staticFriction, self.coulombFriction, self.addedInertia] options = cma.CMAOptions() #Rescaling the sigmas for each variable options['scaling_of_variables'] = [params[0], params[1], params[2]/5.0, params[3]/5.0, params[4], params[5]/10.0, params[6]*2, params[7]/2.5] options['tolfun'] = 0.5 options['ftarget'] = 0.2 res = cma.fmin(self.evaluateModelForMeasures1D, params, 0.5, options, args=[listOfMeasures], restarts=6, bipop=True) print "Best solution = ", res[0] print "Best score = ", res[1] print "Function evals = ", res[2] print "Function evals? = ", res[3] print "Nb iterations = ", res[4] print "mean of final sample distribution", res[5] cma.plot() cma.show() while(True) : temp = 22
def main(): cma.plot()
def cmaPlot(self, filename): cma.plot() cma.savefig(filename) cma.closefig()
import cma import matplotlib.pyplot as plt cma.plot() cma.show()
def cmaPlot(self, filename): cma.plot(); cma.savefig(filename) cma.closefig()
# es = cma.CMAEvolutionStrategy(12 * [0], 0.5) # while not es.stop(): # solutions = es.ask() # es.tell(solutions, [cma.ff.rosen(x) for x in solutions]) # es.logger.add() # write data to disc to be plotted # es.disp() # es.result_pretty() # cma.plot() # shortcut for es.logger.plot() # plt.show() es = cma.CMAEvolutionStrategy(3*[0], 0.5) def F(x): return x[0] ** 2 + x[1] ** 2 while not es.stop(): solutions = es.ask() for x in solutions: print(x) es.tell(solutions, [F(x) for x in solutions]) es.logger.add() # write data to disc to be plotted es.disp() break es.result_pretty() cma.plot() # shortcut for es.logger.plot() input()