Closed zhengzheng170512 closed 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?
pyinstaller -F Getting_Started.py --exclude-module pymoo.cython.non_dominated_sorting
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):
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()`
========================================================================================== 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
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.