jMetal / jMetalPy

A framework for single/multi-objective optimization with metaheuristics
https://jmetal.github.io/jMetalPy/index.html
MIT License
497 stars 150 forks source link

crossover.py C2 becomes a complex number #169

Open tesla-cat opened 4 months ago

tesla-cat commented 4 months ago
  File "C:\Users\ricky\Desktop\ricky_s_lib\tests\jmetal_wrapper\optimize.py", line 69, in optimize
    algo.run()
  File "D:\software\miniconda\lib\site-packages\jmetal\core\algorithm.py", line 86, in run
    self.step()
  File "D:\software\miniconda\lib\site-packages\jmetal\core\algorithm.py", line 146, in step
    offspring_population = self.reproduction(mating_population)
  File "D:\software\miniconda\lib\site-packages\jmetal\algorithm\singleobjective\genetic_algorithm.py", line 93, in reproduction
    offspring = self.crossover_operator.execute(parents)
  File "D:\software\miniconda\lib\site-packages\jmetal\operator\crossover.py", line 195, in execute
    if c2 < lower_bound:
TypeError: '<' not supported between instances of 'complex' and 'float'
def optimize(
    Params: Params,
    func,
    pop_size,
    dist_index,
):
    dic = {k: v for k, v in Params.__dict__.items() if not k.startswith("__")}
    lower = [v[1] for v in dic.values()]
    upper = [v[2] for v in dic.values()]
    n_var = len(dic)

    pa = Params()
    for k, v in dic.items():
        setattr(pa, k, v[0])
    n_obj = len(func(pa))

    class Problem(FloatProblem):
        def __init__(s):
            super().__init__()
            s.lower_bound = lower
            s.upper_bound = upper

        def name(s):
            return "problem"

        def number_of_variables(s):
            return n_var

        def number_of_constraints(s):
            return 0

        def number_of_objectives(s):
            return n_obj

        def evaluate(s, sol: FloatSolution):
            pa = Params()
            for i, (k, v) in enumerate(dic.items()):
                setattr(pa, k, round(sol.variables[i], v[-1]))
            sol.objectives = func(pa)
            return sol

    prob = Problem()
    algo = NSGAII(
        problem=prob,
        population_size=pop_size,
        offspring_population_size=pop_size,
        mutation=PolynomialMutation(
            probability=1.0 / n_var,
            distribution_index=dist_index,
        ),
        crossover=SBXCrossover(
            probability=1.0,
            distribution_index=dist_index,
        ),
    )
    algo.run()
    front = get_non_dominated_solutions(algo.get_result())
    plot = Plot()
    plot.plot(front, filename="pareto_front", format="png")