Skip to content

Instantly share code, notes, and snippets.

@sharavsambuu
Last active June 25, 2023 08:30
Show Gist options
  • Save sharavsambuu/7b9041d6a7d4abd7b284abcce6195008 to your computer and use it in GitHub Desktop.
Save sharavsambuu/7b9041d6a7d4abd7b284abcce6195008 to your computer and use it in GitHub Desktop.
#%%
import random
import numpy as np
from pymoo.core.problem import ElementwiseProblem
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.algorithms.moo.nsga3 import NSGA3
from pymoo.optimize import minimize
from pymoo.util.ref_dirs import get_reference_directions
from pymoo.operators.sampling.rnd import FloatRandomSampling
from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.mutation.pm import PM
from pymoo.termination import get_termination
#%%
#%%
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()
#%%
ref_dirs = get_reference_directions("das-dennis", 2, n_partitions=12)
algorithm = NSGA3(
pop_size = 100,
ref_dirs = ref_dirs,
sampling = FloatRandomSampling(),
crossover = SBX(prob=0.9, eta=15),
mutation = PM(eta=20),
termination = get_termination("n_gen", 40),
eliminate_duplicates=True
)
#%%
res = minimize(
problem,
algorithm,
('n_gen', 10),
seed=1,
verbose=True
)
#print('hash', res)
#%%
#%%
#%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment