Skip to content

Commit

Permalink
Implementation of NSGA3
Browse files Browse the repository at this point in the history
  • Loading branch information
C committed Apr 17, 2024
1 parent f89b35f commit 193c961
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 8 deletions.
101 changes: 101 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ h5py>=2.9.0
numba>=0.46
PySide6 >= 6.5.0
Cython
pyproj
pyproj~=3.6.1
pyarrow
fastparquet
darkdetect
darkdetect~=0.7.1
pyqtdarktheme
# qdarktheme
nptyping
windpowerlib
pvlib
hyperopt
requests
nptyping~=2.5.0
windpowerlib~=0.2.2
pvlib~=0.10.4
hyperopt~=0.2.7
requests~=2.31.0
pytest >= 7.2.0
rdflib
rdflib~=7.0.0
pymoo~=0.6.1.1
setuptools~=68.2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import numpy as np
import matplotlib.pyplot as plt
from pymoo.core.problem import ElementwiseProblem
from pymoo.util.ref_dirs import get_reference_directions
from pymoo.optimize import minimize
from pymoo.algorithms.moo.nsga3 import NSGA3
from pymoo.visualization.scatter import Scatter

from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.mutation.pm import PM
from pymoo.operators.repair.rounding import RoundingRepair
from pymoo.operators.sampling.rnd import IntegerRandomSampling


class GridNsga(ElementwiseProblem):

def __init__(self):
super().__init__(n_var=20,
n_obj=2,
n_ieq_constr=1,
xl=np.zeros(20),
xu=np.ones(20),
vtype=int,
)

def _evaluate(self, x, out, *args, **kwargs):
objective_function = args[0]
objectives = objective_function(x)
out["F"] = objectives


def NSGA_3(obj_func,
n_partitions: int=10,
n_var: int=1,
n_obj: int=1,
max_evals: int=30,
pop_size: int=1,
prob: float=1.0,
eta: float=3.0):

problem = GridNsga()
ref_dirs = get_reference_directions("das-dennis", n_obj, n_partitions=n_partitions)
algorithm = NSGA3(pop_size=pop_size,
sampling=IntegerRandomSampling(),
crossover=SBX(prob=prob, eta=eta, vtype=float, repair=RoundingRepair()),
mutation=PM(prob=prob, eta=eta, vtype=float, repair=RoundingRepair()),
eliminate_duplicates=True,
ref_dirs=ref_dirs)

res = minimize(problem,
algorithm,
('n_gen', max_evals),
seed=1,
verbose=True,
save_history=True)

X = res.X
F = res.F

print(f'Best X: ', X)
print(f'Best F: ', F)

# Extract the objective function values from each generation
obj_values = [gen.pop.get("F") for gen in res.history]

# Calculate the minimum objective function value in each generation
min_obj_values = [np.min(val) for val in obj_values]

return X, obj_values
Loading

0 comments on commit 193c961

Please sign in to comment.