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, min_val=conf.get('min'), max_val=conf.get('max')): super().__init__() if min_val > max_val: raise ValueError('minimum is larger than maximum') self.min = min_val self.max = max_val self._add_reprs(['min', 'max']) self.platypus_type = platypus.Real(self.min, self.max)
def shift(self, x): values = pt.Real(-1, 1) sign = values.rand() x += sign if x > 1: x -= 1 elif x < 0: x *= -1 return x
def _make_variables(self, variables): """Setup the variable types. """ ix = 0 for var in variables: if var.double_size > 0: lower = var.get_double_lower_bounds() upper = var.get_double_upper_bounds() for i in range(var.double_size): self.problem.types[ix] = platypus.Real(lower[i], upper[i]) ix += 1 if var.integer_size > 0: lower = var.get_integer_lower_bounds() upper = var.get_integer_upper_bounds() for i in range(var.integer_size): # Integers are cast to real self.problem.types[ix] = platypus.Real(lower[i], upper[i]) ix += 1
def __init__(self, cov_matrix, mean_daily_returns, days): super(ProblemPortfolio, self).__init__(nvars=15, nobjs=2, nconstrs=4) self.cov_matrix = cov_matrix self.mean_daily_returns = mean_daily_returns self.days = days self.types[:] = pt.Real(0.0, 1.0) self.constraints.__setitem__(0, "==1") self.constraints.__setitem__(1, "<=0.06") self.constraints.__setitem__(2, ">=0.02") self.constraints.__setitem__(3, "<=0.18")
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 __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 ]
(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]) index = numpy.argmax(temp) objective_function_value = algorithm.result[index].objectives[0]
def __init__(self, nobjs=2): super(MyProblem, self).__init__(nvars=2, nobjs=2) # self.types [:] = Real* self.types[:] = ps.Real(-5, 5)