def get_optimal_solutions(self, max_number): k = self.num_variables - self.num_objectives + 1 n = self.num_variables solutions = [] rand_generator = random.Random() rand_generator.seed(2) # generate full factorial sample of position parameters param_combis = [[]] for i in range(n - k): new_combis = [] for combi in param_combis: combi_copy = copy.deepcopy(combi) combi.append(1.0) combi_copy.append(0.0) new_combis.append(combi_copy) param_combis.extend(new_combis) for combi in param_combis: combi.extend([0.5] * k) solutions.append(Individual(combi)) # fill up with random optimal solutions while len(solutions) < max_number: phenome = [0.0] * n for i in range(n - k): phenome[i] += rand_generator.random() for i in range(n - k, n): phenome[i] += 0.5 solutions.append(Individual(phenome)) # assert length is not exceeded return solutions[0:max_number]
def optimal_solution(self, k, l, pos_params): """Create one Pareto-optimal solution with given position parameters.""" # the result vector result = [] result.extend(pos_params) # set the distance parameters for i in range(k, k + l): result.append(0.35) # scale to the correct domains for i in range(k + l): result[i] *= 2.0 * (i + 1) ind = Individual() ind.phenome = result return ind
def get_locally_optimal_solutions(self, max_number=None): """Return locally optimal solutions (includes global ones). Parameters ---------- max_number : int, optional Potentially restrict the number of optima. Returns ------- optima : list of Individual """ local_optima = [] for peak in self.peaks: peak_obj_value = self.objective_function(peak) all_worse_or_equal = True for i in range(len(peak)): neighbor = peak[:] neighbor[i] = not neighbor[i] if self.objective_function(neighbor) > peak_obj_value: all_worse_or_equal = False break if all_worse_or_equal: local_optima.append(Individual(phenome=list(peak))) if max_number is not None: local_optima = local_optima[:max_number] return local_optima
def get_optimal_solutions(self, max_number=100): """Return Pareto-optimal solutions. .. note:: The returned solutions do not yet contain the objective values. Parameters ---------- max_number : int, optional As the number of Pareto-optimal solutions is infinite, the returned set has to be restricted to a finite sample. Returns ------- solutions : list of Individual The Pareto-optimal solutions """ assert max_number > 1 solutions = [] for i in range(max_number): phenome = [0.0] * self.num_variables phenome[0] = float(i) / float(max_number - 1) solutions.append(Individual(phenome)) return solutions
def get_optimal_solutions(self, max_number=None): """Return globally optimal solutions. Parameters ---------- max_number : int, optional Potentially restrict the number of optima. Returns ------- optima : list of Individual """ # test peaks optima = [] max_height = -1.0 opt_phenomes = [] for peak in self.peaks: if peak.height > max_height: opt_phenomes = [peak] max_height = peak.height elif peak.height == max_height: opt_phenomes.append(peak) for phenome in opt_phenomes: optima.append(Individual(phenome=list(phenome))) if max_number is not None: optima = optima[:max_number] return optima
def test_problems(name, params): n_obj, n_var, k = params problem = get_problem(name, n_var, n_obj, k) X, F, CV = load(name.upper(), suffix=["WFG", "%sobj" % n_obj]) if F is None: print("Warning: No correctness check for %s" % name) return _F, _G, _CV = problem.evaluate(X, return_values_of=["F", "G", "CV"]) if problem.n_obj == 1: F = F[:, None] np.testing.assert_allclose(_F, F) # this is not tested automatically try: optprobs_F = [] for x in X: from optproblems.base import Individual ind = Individual(phenome=x) from_optproblems(problem).evaluate(ind) optprobs_F.append(ind.objective_values) optprobs_F = np.array(optprobs_F) np.testing.assert_allclose(_F, optprobs_F) except: print("NOT MATCHING")
def get_optimal_solutions(self, max_number=None): """Return globally optimal solutions. Parameters ---------- max_number : int, optional Potentially restrict the number of optima. Returns ------- optima : list of Individual """ # test peaks optima = [] max_obj_value = float("-inf") opt_phenomes = [] for peak in self.peaks: peak_obj_value = self.objective_function(peak) if peak_obj_value > max_obj_value: opt_phenomes = [peak] max_obj_value = peak_obj_value elif peak_obj_value == max_obj_value: opt_phenomes.append(peak) for phenome in opt_phenomes: optima.append(Individual(phenome=list(phenome))) if max_number is not None: optima = optima[:max_number] return optima
def optimal_solution(self, k, l, pos_params): """Create one Pareto-optimal solution with given position parameters.""" result = [] result.extend(pos_params) # calculate the distance parameters for i in range(k, k + l): w = [1.0] * len(result) u = r_sum(result, w) tmp1 = abs(math.floor(0.5 - u) + 0.98 / 49.98) tmp2 = 0.02 + 49.98 * (0.98 / 49.98 - (1.0 - 2.0 * u) * tmp1) result.append(pow(0.35, pow(tmp2, -1.0))) # scale to the correct domains for i in range(k + l): result[i] *= 2.0 * (i + 1) ind = Individual() ind.phenome = result return ind
def optimal_solution(self, k, l, pos_params): """Create one Pareto-optimal solution with given position parameters.""" result = [] result.extend(pos_params) result.extend([0.0] * l) # calculate the distance parameters result[k + l - 1] = 0.35 for i in range(k + l - 2, k - 1, -1): result_sub = [] for j in range(i + 1, k + l): result_sub.append(result[j]) w = [1.0] * len(result_sub) tmp1 = r_sum(result_sub, w) result[i] = pow(0.35, pow(0.02 + 1.96 * tmp1, -1.0)) # scale to the correct domains for i in range(k + l): result[i] *= 2.0 * (i + 1) ind = Individual() ind.phenome = result return ind
def get_optimal_solutions(self, max_number=None): """Return the optimal solution. The returned solution does not yet contain the objective values. Returns ------- solutions : list of Individual """ assert max_number is None or max_number > 0 opt = Individual([1] * self.num_variables) return [opt]
def get_locally_optimal_solutions(self, max_number=None): """Return locally optimal solutions (includes global ones). Parameters ---------- max_number : int, optional Potentially restrict the number of optima. Returns ------- optima : list of Individual """ get_active_peak = self.get_active_peak # test peaks local_optima = [] peaks = np.vstack(self.peaks) for i, peak in enumerate(peaks): if get_active_peak(peak) is self.peaks[i]: local_optima.append(Individual(phenome=list(peak))) if max_number is not None: local_optima = local_optima[:max_number] return local_optima
def get_optimal_solutions(self, max_number=None): """Return Pareto-optimal solutions. The returned solutions do not yet contain the objective values. Parameters ---------- max_number : int, optional Optionally restrict the number of solutions. Returns ------- solutions : list of Individual The Pareto-optimal solutions """ assert max_number is None or max_number > 0 individuals = [] for i in range(self.num_variables + 1): opt = Individual([1] * i + [0] * (self.num_variables - i)) individuals.append(opt) if max_number is not None: individuals = individuals[:max_number] return individuals
def get_optimal_solutions(self, max_number=None): return [Individual(self.offsets[0][:self.num_variables])]
def solver(input, problem, num_objectives, *kwargs): mutliprocess = False maximize = False k = None for key, value in kwargs.items(): if key == "multiprocess": multiprocess = value continue if key == "maximize": maximize = value continue if key == "k": k = value continue num_variables = len(input) # For logging relevant outputs in file import logging as l l.basicConfig(filename='solver.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s', level=l.INFO) l.warning('Starting SOLVER log for %s', problem) # Solver should allow only 2 or more than 2 objectives if num_objectives < 2: l.error("SOLVER: Less than two objectives is not allowed. Terminating") exit() problem = problem.upper() Problem = None # Nested ifs for problem if "ZDT" in problem: if problem == "ZDT1": Problem = ZDT1(num_variables=num_variables) elif problem == "ZDT2": Problem = ZDT2(num_variables=num_variables) elif problem == "ZDT3": Problem = ZDT3(num_variables=num_variables) elif problem == "ZDT4": Problem = ZDT4(num_variables=num_variables) elif problem == "ZDT6": Problem = ZDT6(num_variables=num_variables) elif "DTLZ" in problem: if problem == "DTLZ1": Problem = DTLZ1(num_objectives=num_objectives, num_variables=num_variables) elif problem == "DTLZ2": Problem = DTLZ2(num_objectives=num_objectives, num_variables=num_variables) elif problem == "DTLZ3": Problem = DTLZ3(num_objectives=num_objectives, num_variables=num_variables) elif problem == "DTLZ4": Problem = DTLZ4(num_objectives=num_objectives, num_variables=num_variables) elif problem == "DTLZ5": Problem = DTLZ5(num_objectives=num_objectives, num_variables=num_variables) elif problem == "DTLZ6": Problem = DTLZ6(num_objectives=num_objectives, num_variables=num_variables) elif problem == "DTLZ7": Problem = DTLZ7(num_objectives=num_objectives, num_variables=num_variables) elif "WFG" in problem: # Define the value of k # It must hold that k<num_variables # Huband et al. recommend k = 4 for two objectives and k = 2 * (m - 1) for m objectives. if k == None: if num_objectives == 2: k = 4 elif num_objectives > 2: k = 2 * (num_objectives - 1) if k >= num_variables: l.error("WFG: k > Number of Variables is not allowed. Terminating") exit() if problem == "WFG1": Problem = WFG1(num_objectives=num_objectives, num_variables=num_variables, k=k) elif problem == "WFG2": Problem = WFG2(num_objectives=num_objectives, num_variables=num_variables, k=k) elif problem == "WFG3": Problem = WFG3(num_objectives=num_objectives, num_variables=num_variables, k=k) elif problem == "WFG4": Problem = WFG4(num_objectives=num_objectives, num_variables=num_variables, k=k) elif problem == "WFG5": Problem = WFG5(num_objectives=num_objectives, num_variables=num_variables, k=k) elif problem == "WFG6": Problem = WFG6(num_objectives=num_objectives, num_variables=num_variables, k=k) elif problem == "WFG7": Problem = WFG7(num_objectives=num_objectives, num_variables=num_variables, k=k) elif problem == "WFG8": Problem = WFG8(num_objectives=num_objectives, num_variables=num_variables, k=k) elif problem == "WFG9": Problem = WFG9(num_objectives=num_objectives, num_variables=num_variables, k=k) X = Individual(input) Problem.evaluate(X) l.info(X.objective_values) return (X)