anyoptimization / pymoo

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

pyinstaller ModuleNotFoundError: No module named 'pymoo.cython.non_dominated_sorting' #85

Closed zhengzheng170512 closed 4 years ago

zhengzheng170512 commented 4 years ago

I use pyinstaller to generate an executable code for the example in the Getting Started section successfully. However, when I run the code, I get an error: `import autograd.numpy as anp import numpy as np from pymoo.util.misc import stack from pymoo.model.problem import Problem from pymoo.algorithms.nsga2 import NSGA2 from pymoo.algorithms.nsga3 import NSGA3 from pymoo.algorithms.moead import MOEAD from pymoo.factory import get_sampling, get_crossover, get_mutation from pymoo.factory import get_termination from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter import matplotlib.pyplot as plt from pymoo.performance_indicator.hv import Hypervolume from pymoo.factory import get_reference_directions

class MyProblem(Problem):

def __init__(self):
    super().__init__(n_var=2,
                     n_obj=2,
                     n_constr=2,
                     xl=anp.array([-2,-2]),
                     xu=anp.array([2,2]))

def _evaluate(self, x, out, *args, **kwargs):
    f1 = x[:,0]**2 + x[:,1]**2
    f2 = (x[:,0]-1)**2 + x[:,1]**2

    g1 = 2*(x[:, 0]-0.1) * (x[:, 0]-0.9) / 0.18
    g2 = - 20*(x[:, 0]-0.4) * (x[:, 0]-0.6) / 4.8

    out["F"] = anp.column_stack([f1, f2])
    out["G"] = anp.column_stack([g1, g2])

# --------------------------------------------------
# Pareto-front - not necessary but used for plotting
# --------------------------------------------------
def _calc_pareto_front(self, flatten=True, **kwargs):
    f1_a = np.linspace(0.1**2, 0.4**2, 100)
    f2_a = (np.sqrt(f1_a) - 1)**2

    f1_b = np.linspace(0.6**2, 0.9**2, 100)
    f2_b = (np.sqrt(f1_b) - 1)**2

    a, b = np.column_stack([f1_a, f2_a]), np.column_stack([f1_b, f2_b])
    return stack(a, b, flatten=flatten)

# --------------------------------------------------
# Pareto-set - not necessary but used for plotting
# --------------------------------------------------
def _calc_pareto_set(self, flatten=True, **kwargs):
    x1_a = np.linspace(0.1, 0.4, 50)
    x1_b = np.linspace(0.6, 0.9, 50)
    x2 = np.zeros(50)

    a, b = np.column_stack([x1_a, x2]), np.column_stack([x1_b, x2])
    return stack(a,b, flatten=flatten)

problem = MyProblem() _algorithm = 'nsga3' if _algorithm == 'nsga2': algorithm = NSGA2( pop_size=40, n_offsprings=10, sampling=get_sampling("real_random"), crossover=get_crossover("real_sbx", prob=0.9, eta=15), mutation=get_mutation("real_pm", eta=20), eliminate_duplicates=True ) if _algorithm == 'nsga3': algorithm = NSGA3( get_reference_directions("das-dennis", 2, n_partitions=12) ) if _algorithm == 'moead': algorithm = MOEAD( get_reference_directions("das-dennis", 2, n_partitions=12) )

termination = get_termination("n_gen", 40)

res = minimize(problem, algorithm, termination, seed=1, pf=problem.pareto_front(use_cache=False), save_history=True, verbose=True)

ps = problem.pareto_set(use_cache=False, flatten=False) pf = problem.pareto_front(use_cache=False, flatten=False)

plot = Scatter(title = "Design Space", axis_labels="x") plot.add(res.X, s=30, facecolors='none', edgecolors='r') plot.add(ps, plot_type="line", color="black", alpha=0.7) plot.do() plot.apply(lambda ax: ax.set_xlim(-0.5, 1.5)) plot.apply(lambda ax: ax.set_ylim(-2, 2)) plot.show()

plot = Scatter(title = "Objective Space") plot.add(res.F) plot.add(pf, plot_type="line", color="black", alpha=0.7) plot.show()

metric = Hypervolume(ref_point=np.array([1.0, 1.0]))

pop_each_gen = [a.pop for a in res.history]

obj_and_feasible_each_gen = [pop[pop.get("feasible")[:,0]].get("F") for pop in pop_each_gen]

hv = [metric.calc(f) for f in obj_and_feasible_each_gen]

plt.plot(np.arange(len(hv)), hv, '-o') plt.title("Convergence") plt.xlabel("Generation") plt.ylabel("Hypervolume") plt.show()`

[zhengz@localhost pyinstaller]$ ./dist/Getting_Started

/home/zhengz/softwares/python38/lib/python3.8/site-packages/pyinstaller-4.0.dev0+3510886087-py3.8.egg/PyInstaller/loader/pyimod03_importers.py:489: MatplotlibDeprecationWarning:

The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.

exec(bytecode, module.dict)

==========================================================================================

n_gen | n_eval | cv (min) | cv (avg) | igd | gd | hv

========================================================================================== 1 | 13 | 3.713093941 | 2.49237E+01 | - | - | - Traceback (most recent call last): File "Getting_Started.py", line 82, in res = minimize(problem, File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/optimize.py", line 76, in minimize File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/model/algorithm.py", line 208, in solve File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/algorithms/nsga3.py", line 103, in _solve File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/model/algorithm.py", line 289, in _solve File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/model/algorithm.py", line 260, in next File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/algorithms/genetic_algorithm.py", line 105, in _next File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/model/survival.py", line 41, in do File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/algorithms/nsga3.py", line 133, in _do File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/util/nds/non_dominated_sorting.py", line 22, in do File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/util/function_loader.py", line 76, in load_function File "site-packages/pymoo-0.4.1-py3.8-linux-x86_64.egg/pymoo/util/function_loader.py", line 69, in load File "importlib/init.py", line 127, in import_module File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 973, in _find_and_load_unlocked ModuleNotFoundError: No module named 'pymoo.cython.non_dominated_sorting' [6928] Failed to execute script Getting_Started

I find some information about Cython on: https://pymoo.org/installation.html

Some computationally more expensive function have been implemented using Cython for speedup. ... As said above, the pymoo installation will not fail if the modules are not compiled successfully, but no speedup will be available. To check if the compilation has worked during the installation, you can use the following command:

python -c "from pymoo.util.function_loader import is_compiled;print('Compiled Extensions: ', is_compiled())"

So I check the installation: [zhengz@localhost pyinstaller]$ python -c "from pymoo.util.function_loader import is_compiled;print('Compiled Extensions: ', is_compiled())" Compiled Extensions: True

I try the method from here: https://stackoverflow.com/questions/24525861/building-cython-compiled-python-code-with-pyinstaller, but I still cannot solve the problem.

On windows system, I install pymoo without using Cython, but I can use pyinstaller to generate and run the executable code successfully. So maybe I can solve the problem this way on linux system with the sacrifice of speedup.

blankjul commented 4 years ago

Sorry I was not able to answer early enough. I have actually tried Pyinstaller and ran into the same issue.

What was the solution to your problem?

zhengzheng170512 commented 4 years ago

pyinstaller -F Getting_Started.py --exclude-module pymoo.cython.non_dominated_sorting