def optimFunc(individual): # for i in range(1000): f3 = benchmarks.rastrigin(individual) f1, f2 = benchmarks.zdt1(individual) out = (f1, f2) + f3 # print os.getpid() return out
def ackley_arg0(sol): X,Y = sol[0], sol[1] Z = np.zeros(X.shape) for i in xrange(X.shape[0]): Z[i] = benchmarks.rastrigin((X[i],Y[i]))[0] return Z
def ackley_arg0(sol): X, Y = sol[0], sol[1] Z = np.zeros(X.shape) for i in range(X.shape[0]): Z[i] = benchmarks.rastrigin((X[i], Y[i]))[0] return Z
def main(): numpy.random.seed(64) # The CMA-ES algorithm strategy = cma.Strategy(centroid=[5.0] * N, sigma=3.0, lambda_=20 * N) toolbox.register("generate", strategy.generate, creator.Individual) toolbox.register("update", strategy.update) halloffame = tools.HallOfFame(1) halloffame_array = [] C_array = [] centroid_array = [] for gen in range(NGEN): # 新たな世代の個体群を生成 population = toolbox.generate() # 個体群の評価 fitnesses = toolbox.map(toolbox.evaluate, population) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit # 個体群の評価から次世代の計算のためのパラメタ更新 toolbox.update(population) # hall-of-fameの更新 halloffame.update(population) halloffame_array.append(halloffame[0]) C_array.append(strategy.C) centroid_array.append(strategy.centroid) # 計算結果を描画 import matplotlib.pyplot as plt import matplotlib.cm as cm from matplotlib.patches import Ellipse plt.ion() fig = plt.figure() ax = fig.add_subplot(111) X = numpy.arange(-5.12, 5.12, 0.1) Y = numpy.arange(-5.12, 5.12, 0.1) X, Y = numpy.meshgrid(X, Y) Z = [[benchmarks.rastrigin((x, y))[0] for x, y in zip(xx, yy)] for xx, yy in zip(X, Y)] ax.imshow(Z, cmap=cm.jet, extent=[-5.12, 5.12, -5.12, 5.12]) for x, sigma, xmean in zip(halloffame_array, C_array, centroid_array): # 多変量分布の分散を楕円で描画 Darray, Bmat = numpy.linalg.eigh(sigma) ax.add_artist( Ellipse((xmean[0], xmean[1]), numpy.sqrt(Darray[0]), numpy.sqrt(Darray[1]), numpy.arctan2(Bmat[1, 0], Bmat[0, 0]) * 180 / numpy.pi, color="g", alpha=0.7)) ax.plot([x[0]], [x[1]], c='r', marker='o') ax.axis([-5.12, 5.12, -5.12, 5.12]) plt.draw() plt.show(block=True)
def rastrigin_arg_vari(sol): #D1,D2,D3,D4,D5,D6,D7,D8,D9,D10 = sol[0], sol[1], sol[2], sol[3], sol[4], sol[5], sol[6], sol[7], sol[8], sol[9] Z = np.zeros(sol.shape[0]) #print sol.shape[0] for i in xrange(sol.shape[0]): #print sol[i] Z[i] = benchmarks.rastrigin(sol[i])[0] #print Z[i] return Z
def __init__(self, problem_idx, value_threshold): SyntheticEnv.__init__(self, problem_idx) self.name = 'synthetic_rastrigin' if problem_idx == 0: self.dim_x = 3 self.feasible_action_value_threshold = -10 elif problem_idx == 1: self.dim_x = 10 self.feasible_action_value_threshold = value_threshold elif problem_idx == 2: self.dim_x = 20 self.feasible_action_value_threshold = -100 self.feasible_reward = 100 self.reward_function = lambda sol: -benchmarks.rastrigin(sol)[0]
def get_objective_function(sol): if obj_fcn == 'shekel': return shekel(sol, A, C)[0] elif obj_fcn == 'schwefel': return -benchmarks.schwefel(sol)[0] elif obj_fcn == 'griewank': return -benchmarks.griewank(sol)[0] elif obj_fcn == 'rastrigin': return -benchmarks.rastrigin(sol)[0] elif obj_fcn == 'ackley': return -benchmarks.ackley(sol)[0] elif obj_fcn == 'rosenbrock': return -benchmarks.rosenbrock(sol)[0] elif obj_fcn == 'schaffer': return -benchmarks.schaffer(sol)[0] else: print "wrong function name" sys.exit(-1)
def rastrigin_arg0(sol): return benchmarks.rastrigin(sol)[0]
def MOFitnessFunc(part): code = 0 if is_valid(part) else 1 fitValues = (benchmarks.sphere(part)[0], benchmarks.rastrigin(part)[0]) #fitValues = benchmarks.fonseca(part) return fitValues, array([code]), array([0]), array([1.0]) ## cost always 1
def rastrigin_arg0(sol): return benchmarks.rastrigin(convertArrToFloat(sol))
def final(x): a = benchmarks.rastrigin(x) print "rastrigin: ",a return (a > 1)
def calFit(self, ind): return benchmarks.rastrigin(ind)[0]
def evalBenchmark(individual): return benchmarks.rastrigin(individual)
def evaluate(self, x): return rastrigin(x)[0]
def rastrigin_on_cube(u: [float]) -> float: # https://deap.readthedocs.io/en/master/api/benchmarks.html#deap.benchmarks.rastrigin u_squished = [10.24 * (ui**1.1 - 0.5) for ui in u] return 0.01 * benchmarks.rastrigin(u_squished)[0]
def rastrigin(x): return benchmarks.rastrigin(x)[0]
def objective(X): X = np.atleast_2d(X) return np.array([benchmarks.rastrigin(x)[0] for x in X])