def objective_function(self, representation): num_individuals = len(representation) evaluations = np.zeros((num_individuals, self.num_objs)) for i in range(num_individuals): evaluations[i] = list( benchmarks.dtlz2(representation[i], self.num_objs)) return evaluations
def get_function_definition(function_name: str, num_input_dims: int, num_objectives: int): domain = [] fitnessfunc = None d1x_opt = [] if function_name == "ZDT1": fitnessfunc = lambda ind: benchmarks.zdt1(ind) num_objectives = 2 for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.0, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "ZDT2": fitnessfunc = lambda ind: benchmarks.zdt2(ind) num_objectives = 2 for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.0, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "ZDT3": fitnessfunc = lambda ind: benchmarks.zdt3(ind) num_objectives = 2 for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.0, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "ZDT4": fitnessfunc = lambda ind: benchmarks.zdt4(ind) num_objectives = 2 domain.append({ 'name': "x_{}]".format(1), 'type': 'continuous', 'domain': (0.0, 1.0) }) for i in range(1, num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (-5.0, 5.0) }) d1x_opt = np.repeat(0.0, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "ZDT6": fitnessfunc = lambda ind: benchmarks.zdt6(ind) num_objectives = 2 for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.0, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "KURSAWE": fitnessfunc = lambda ind: benchmarks.kursawe(ind) elif function_name == "POLONI": fitnessfunc = lambda ind: benchmarks.kursawe(ind) elif function_name == "DTLZ1": fitnessfunc = lambda ind: benchmarks.dtlz1(ind, num_objectives) for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.5, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "DTLZ2": fitnessfunc = lambda ind: benchmarks.dtlz2(ind, num_objectives) for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.5, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "DTLZ3": fitnessfunc = lambda ind: benchmarks.dtlz3(ind, num_objectives) for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.5, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "DTLZ4": fitnessfunc = lambda ind: benchmarks.dtlz4(ind, num_objectives, 100) for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.5, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "DTLZ5": fitnessfunc = lambda ind: benchmarks.dtlz5(ind, num_objectives) for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.5, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "DTLZ6": fitnessfunc = lambda ind: benchmarks.dtlz6(ind, num_objectives) for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.5, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) elif function_name == "DTLZ7": fitnessfunc = lambda ind: benchmarks.dtlz7(ind, num_objectives) for i in range(num_input_dims): domain.append({ 'name': "x_{}]".format(i + 1), 'type': 'continuous', 'domain': (0.0, 1.0) }) d1x_opt = np.repeat(0.0, num_input_dims - 1).reshape([1, -1]) d1x_opt = np.repeat(d1x_opt, 1000, 0) d1x_opt = np.hstack((np.linspace(0.0, 1.0, 1000).reshape([-1, 1]), d1x_opt)) return domain, fitnessfunc, d1x_opt, num_input_dims, num_objectives
ax.set_ylabel('Memory') return None def variate(ind1, ind2, mate, mutate): c1, c2 = mate(ind1, ind2) c1 = mutate(c1) c2 = mutate(c2) return c1, c2 toolbox.register("attr_float", uniform, BOUND_LOW, BOUND_UP, NDIM) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.attr_float) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", lambda ind: benchmarks.dtlz2(ind, 2)) toolbox.register("mate", tools.cxSimulatedBinary, eta=20.0) toolbox.register("mutate", tools.mutPolynomialBounded, low=0, up=10000, eta=20.0, indpb=1.0 / NDIM) toolbox.register("select", tools.selNSGA2) def main(seed=None): random.seed(seed) NGEN = 2 MU = 4
color=plot_colors[i % len(plot_colors)]) ax.set_title('$t=$' + str(frame_index)) ax.set_xlabel('RT');ax.set_ylabel('Memory') return None def variate(ind1, ind2, mate, mutate): c1, c2 = mate(ind1, ind2) c1 = mutate(c1) c2 = mutate(c2) return c1, c2 toolbox.register("attr_float", uniform, BOUND_LOW, BOUND_UP, NDIM) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.attr_float) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", lambda ind: benchmarks.dtlz2(ind, 2)) toolbox.register("mate", tools.cxSimulatedBinary, eta=20.0) toolbox.register("mutate", tools.mutPolynomialBounded, low=0, up=10000, eta=20.0, indpb=1.0/NDIM) toolbox.register("select", tools.selNSGA2) def main(seed=None): random.seed(seed) NGEN = 2 MU = 4 CXPB = 0.6 pop = toolbox.population(n=MU) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0)
def evalBenchmark(individual): return benchmarks.dtlz2(individual, objectives)