anyoptimization / pymoo

NSGA2, NSGA3, R-NSGA3, MOEAD, Genetic Algorithms (GA), Differential Evolution (DE), CMAES, PSO
https://pymoo.org
Apache License 2.0
2.29k stars 392 forks source link

Edge case error when n_obj=1 in AGE-MOEA2 #619

Closed muazhari closed 3 months ago

muazhari commented 4 months ago

Reproducible codes:

import ray
from pymoo.util.ref_dirs import get_reference_directions, LayerwiseRieszEnergyReferenceDirectionFactory
from pymoo.algorithms.moo.ctaea import CTAEA
from pymoo.algorithms.moo.age2 import AGEMOEA2
from pymoo.core.variable import Binary, Choice, Integer, Real
from pymoo.core.problem import LoopedElementwiseEvaluation, RayParallelization
from pymoo.algorithms.moo.nsga2 import RankAndCrowding, NSGA2, RankAndCrowdingSurvival
from pymoo.core.mixed import MixedVariableGA, MixedVariableMating, MixedVariableDuplicateElimination, \
    MixedVariableSampling
from pymoo.optimize import minimize

from pymoo.core.problem import ElementwiseProblem

class MultiObjectiveMixedVariableProblem(ElementwiseProblem):

    def __init__(self, **kwargs):
        vars = {
            "z": Real(bounds=(-5 * 10 ** 3, 5 * 10 ** 3)),
        }
        super().__init__(vars=vars, n_obj=1, **kwargs)

    def _evaluate(self, X, out, *args, **kwargs):
        z = X["z"]

        f1 = z ** 2
        out["F"] = [f1]

runner = LoopedElementwiseEvaluation()
problem = MultiObjectiveMixedVariableProblem(elementwise_runner=runner)
algorithm = AGEMOEA2(
    sampling=MixedVariableSampling(),
    mating=MixedVariableMating(eliminate_duplicates=MixedVariableDuplicateElimination()),
    eliminate_duplicates=MixedVariableDuplicateElimination(),
)

res = minimize(
    problem,
    algorithm,
    verbose=True,
    seed=1
)

Error logs:

/home/muazhari/miniconda3/bin/conda run -p /home/muazhari/miniconda3 --no-capture-output python /mnt/c/Data/Apps/playground/seven_test.py 
/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/algorithms/moo/age2.py:85: RuntimeWarning: divide by zero encountered in log
  f = np.log(f)
Traceback (most recent call last):
  File "/mnt/c/Data/Apps/playground/seven_test.py", line 40, in <module>
    res = minimize(
          ^^^^^^^^^
  File "/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/optimize.py", line 67, in minimize
    res = algorithm.run()
          ^^^^^^^^^^^^^^^
  File "/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/core/algorithm.py", line 138, in run
    self.next()
  File "/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/core/algorithm.py", line 159, in next
    self.advance(infills=infills)
  File "/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/core/algorithm.py", line 214, in advance
    self._initialize_advance(infills=infills, **kwargs)
  File "/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/algorithms/base/genetic.py", line 80, in _initialize_advance
    self.pop = self.survival.do(self.problem, infills, n_survive=len(infills), algorithm=self, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/core/survival.py", line 55, in do
    survivors = self._do(problem, pop, *args, n_survive=n_survive, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/algorithms/moo/age.py", line 128, in _do
    crowd_dist[front_no == 0], p, normalization = self.survival_score(front1, ideal_point)
                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/muazhari/miniconda3/lib/python3.11/site-packages/pymoo/algorithms/moo/age.py", line 170, in survival_score
    distances = self.pairwise_distances(front, p) / nn[:, None]
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ZeroDivisionError: division by zero
ERROR conda.cli.main_run:execute(124): `conda run python /mnt/c/Data/Apps/playground/seven_test.py` failed. (See above for error)

Process finished with exit code 1
blankjul commented 4 months ago

any reason you want to apply this algorithm on a single-objective problem?

I have not implemented AGE-MOEA2 myself becaues it was a contribution by a researcher. It seems to fail because always only one individual is in a front (degenerated case when MOO applied to SOO)

muazhari commented 4 months ago

any reason you want to apply this algorithm on a single-objective problem?

I have not implemented AGE-MOEA2 myself becaues it was a contribution by a researcher. It seems to fail because always only one individual is in a front (degenerated case when MOO applied to SOO)

Okay, if it can't.

muazhari commented 4 months ago

There are other errors even though contextual to many objective problems in this https://github.com/anyoptimization/pymoo/issues/620.

blankjul commented 3 months ago

I have tagged the user who contributed the implementation in the other issue. I will close this for now.