def run_platypus(self, algorithm): min_b = self.config.find_bump_parameters["magnet_min_field"] max_b = self.config.find_bump_parameters["magnet_max_field"] fixed = self.optimisation["fix_bumps"] seed = copy.deepcopy(self.config.find_bump_parameters["seed_fields"]) sigma = self.config.find_bump_parameters["seed_errors"][0] self.field_names = [name for name in self.field_names_gen()] max_iterations = self.config.find_bump_parameters["max_iterations"] n_target_fields = len( self.config.find_bump_parameters["target_fields"]) for name in fixed: index = self.field_names.index(name) self.fixed[name] = seed[index] print("Fixing bump", name, "to", self.fixed[name]) del self.field_names[index] del seed[index] n_parameters = len(self.field_names) n_variables = 6 + n_target_fields problem = platypus.Problem(n_parameters, n_variables) problem.types[:] = platypus.Real(min_b, max_b) problem.function = self.moo_function algorithm = platypus.CMAES(problem) algorithm.initial_search_point = seed algorithm.sigma = sigma # note this is a single value try: algorithm.run(max_iterations) except Exception: sys.excepthook(*sys.exc_info()) print("Platypus failed")
def __init__(self, *args, **kwargs): super(PlatypusWrapper, self).__init__(*args, **kwargs) # To determine the number of variables, etc m = self.model # Cache the variables, objectives and constraints variables, variable_map = cache_variable_parameters(m) objectives = cache_objectives(m) constraints = cache_constraints(m) if len(variables) < 1: raise ValueError('At least one variable must be defined.') if len(objectives) < 1: raise ValueError('At least one objective must be defined.') self.problem = platypus.Problem(variable_map[-1], len(objectives), len(constraints)) self.problem.function = self.evaluate self.problem.wrapper = self # Setup the problem; subclasses can change this behaviour self._make_variables(variables) self._make_constraints(constraints)
def to_platypus(self) -> platypus.Problem: """Converts this problem to a platypus problem. No evaluator will be included. :return: A corresponding platypus problem """ problem = platypus.Problem(self.num_inputs, self.num_outputs, self.num_constraints) for i, parameter in enumerate(self.inputs): problem.types[i] = parameter.platypus_type for i, direction in enumerate(self.minimize_outputs): problem.directions[ i] = platypus.Problem.MINIMIZE if direction else platypus.Problem.MAXIMIZE for i, bound in enumerate(self.constraint_bounds): problem.constraints[i] = bound return problem
def setup_optimisation_cmaes(self): n_parameters = len( [is_fixed for is_fixed in self.is_fixed if not is_fixed]) n_variables = 1 problem = platypus.Problem(n_parameters, n_variables) min_ = [x for i, x in enumerate(self.min) if not self.is_fixed[i]] max_ = [x for i, x in enumerate(self.max) if not self.is_fixed[i]] seed_ = [x for i, x in enumerate(self.seed) if not self.is_fixed[i]] problem.types = [ platypus.Real(min_[i], max_[i]) for i in range(n_parameters) ] problem.directions[:] = Problem.MINIMIZE problem.function = self.run_one algorithm = platypus.CMAES(problem) algorithm.initial_search_point = seed_ algorithm.sigma = self.sigma # note this is a single value return algorithm
def _optimise_one_chunk(self, chunk, nodes, iterations=100): event_opt = self._event_opt event_opt.set_farming_nodes(nodes) n_variables = sum(len(x) for x in chunk) n_constraints = len(chunk) problem = platypus.Problem(n_variables, 1, n_constraints) problem.types[:] = platypus.Integer(0, 1) problem.constraints[:] = '==1' problem.function = self._problem_function problem.directions[:] = platypus.Problem.MINIMIZE algorithm = platypus.NSGAII(problem) algorithm.run(iterations) possible_results = [s for s in algorithm.result if s.feasible] possible_results.sort(key=lambda x: self._calculate_required(x.variables).magnitude(), reverse=True) possible_results.sort(key=lambda x: x.objectives[0]) possible_results.sort(key=lambda x: x.constraint_violation) if not possible_results: print('No feasible solutions.') return required = self._calculate_required(possible_results[0].variables) event_opt.set_target(required) runs = event_opt.optimise_runs() current_items = event_opt.total_items(runs) + event_opt._current event_opt.set_current(current_items-required) return { 'projects': [self._chunk_projects[i]['name'] for i, s in enumerate(possible_results[0].variables) if s[0]], 'required_materials': required, 'runs': runs, 'total_runs': sum(runs.values()), 'ap': 40*sum(runs.values()), }
def __init__(self, experiment): self.experiment = experiment self.domain = self.experiment.domain # Set up platypus problem self.problem = pp.Problem( nvars=self.domain.num_variables(), nobjs=len(self.domain.output_variables), nconstrs=len(self.domain.constraints), ) # Set maximization or minimization for each objective j=0 for i, v in enumerate(self.domain.variables): if v.is_objective: direction = self.problem.MAXIMIZE if v.maximize else self.problem.MINIMIZE self.problem.directions[j] = direction j+=1 elif v.variable_type == "continuous": self.problem.types[i] = pp.Real(v.lower_bound, v.upper_bound) elif v.variable_type == "discrete": # Select a subset of one of the available options raise NotImplementedError( "The NSGAII optimizer does not work with discrete variables" ) # self.problem.types[i] = pp.Subset(elements=v.levels, size=1) elif v.variable_type == "descriptors": raise NotImplementedError( "The NSGAII optimizer does not work with descriptors variables" ) else: raise TypeError(f"{v.variable_type} is not a valid variable type.") # Set up constraints self.problem.constraints[:] = [ c.constraint_type + "0" for c in self.domain.constraints ]
(Qm_i / M)) / ((1 + d)**i) elif iteration == 2: v_i = ((s - r) * Qr_i - m * Qm_i - c * Qc_i - fi * (Qc_i / C)) / ((1 + d)**i) elif iteration == 3: v_i = ((s - r) * Qr_i - m * Qm_i - c * Qc_i - fi * (Qr_i / R)) / ((1 + d)**i) #v_i = (s-r)*Qr_i - m*Qm_i - c*Qc_i - fi*(Qm_i/M) / ( (1+d)**i) c1 = Qr_i - gbar * y * Qc_i return [v_i], [c1] # 2. Running Gen-Algo problem = platypus.Problem( 3, 1, 1) # n_variables, n_objectiveFuncs, n_constraints problem.types[:] = [ platypus.Real(0, M), platypus.Real(0, C), platypus.Real(0, R) ] problem.constraints[:] = "==0" problem.function = belegundu algorithm = platypus.NSGAII(problem) algorithm.run(1000 * 100) # 3. Outputing best value temp = [] for solution in algorithm.result: temp.append(solution.objectives[0])