Created
June 25, 2023 08:16
-
-
Save sharavsambuu/aff453b9f737fe78b9aaf16e2e355638 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%% | |
import random | |
import numpy as np | |
from pymoo.core.problem import ElementwiseProblem | |
from pymoo.algorithms.moo.nsga2 import NSGA2 | |
from pymoo.optimize import minimize | |
from pymoo.operators.sampling.rnd import FloatRandomSampling | |
from pymoo.operators.crossover.sbx import SBX | |
from pymoo.operators.mutation.pm import PM | |
#%% | |
#%% | |
MAFAST_LOWER = 10.0 | |
MAFAST_UPPER = 200.0 | |
MASLOW_LOWER = 50.0 | |
MASLOW_UPPER = 450.0 | |
PCT_EPS_LOWER = 0.05 | |
PCT_EPS_UPPER = 10.0 | |
class HelloWorld(ElementwiseProblem): | |
def __init__(self, **kwargs): | |
super().__init__( | |
n_var = 3, | |
n_obj = 2, | |
xl = np.array([MAFAST_LOWER, MASLOW_LOWER, PCT_EPS_LOWER]), | |
xu = np.array([MAFAST_UPPER, MASLOW_UPPER, PCT_EPS_UPPER]), | |
**kwargs) | |
def _evaluate(self, x, out, *args, **kwargs): | |
print(f"evaluation : {x[0]} {x[1]} {x[2]}") | |
fitnesses = [ | |
random.choice(list(np.random.uniform(size=100, low=0.01, high=10.01))), | |
random.choice(list(np.random.uniform(size=300, low=0.05, high=20.01))), | |
] | |
print(f"fitnesses {fitnesses}") | |
out["F"] = fitnesses | |
problem = HelloWorld() | |
#%% | |
algorithm = NSGA2( | |
pop_size = 100, | |
n_offsprings = 20, | |
sampling = FloatRandomSampling(), | |
crossover = SBX(prob=0.9, eta=15), | |
mutation = PM(eta=20), | |
eliminate_duplicates=True | |
) | |
#%% | |
res = minimize( | |
problem, | |
algorithm, | |
('n_gen', 10), | |
seed=1, | |
verbose=True | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment