我尝试在 PyCharm 中运行 pymoo for NSGA2 的示例代码。
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.factory import get_problem
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter
problem = get_problem("zdt1")
algorithm = NSGA2(pop_size=100)
res = minimize(problem,
algorithm,
('n_gen', 200),
seed=1,
verbose=False)
plot = Scatter()
plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7)
plot.add(res.F, facecolor="none", edgecolor="red")
plot.show()
它总是给我这个错误:
/Users/myname/opt/miniconda3/envs/namename/bin/python /Users/myname/PycharmProjects/name/namename/pymoo.py
Traceback (most recent call last):
File "/Users/myname/PycharmProjects/name/namename/pymoo.py", line 1, in <module>
from pymoo.algorithms.moo.nsga2 import NSGA2
File "/Users/mynae/PycharmProjects/name/namename/pymoo.py", line 1, in <module>
from pymoo.algorithms.moo.nsga2 import NSGA2
ModuleNotFoundError: No module named 'pymoo.algorithms'; 'pymoo' is not a package
我已经在首选项下下载了 pymoo,当我再次尝试在终端中下载 pymoo 时,它说所有软件包都已安装。
有人可以帮忙吗?
重命名您的文件,例如重命名为
pymoo_test.py
(或完全其他名称),它应该可以工作。
您的堆栈跟踪告诉我您的文件名为
pymoo.py
:
File "/Users/mynae/PycharmProjects/name/namename/pymoo.py", line 1, in <module>
文件名是导致您出现问题的原因。发生的情况是,由于名称的原因,正在尝试从您的
pymoo.py
文件导入导入,并且您尝试从同一文件中的 pymoo.<...>
导入。
我尝试在 Jupyter 笔记本中运行相同的代码:
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.factory import get_problem
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter
problem = get_problem("zdt1")
algorithm = NSGA2(pop_size=100)
res = minimize(problem,
algorithm,
('n_gen', 200),
seed=1,
verbose=False)
plot = Scatter()
plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7)
plot.add(res.F, facecolor="none", edgecolor="red")
plot.show()
但出现以下错误:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10604/691024984.py in <module>
----> 1 from pymoo.algorithms.moo.nsga2 import NSGA2
2 from pymoo.factory import get_problem
3 from pymoo.optimize import minimize
4 from pymoo.visualization.scatter import Scatter
5
ModuleNotFoundError: No module named 'pymoo.algorithms.moo'
我已经下载了 pymoo,当我再次尝试在终端中下载 pymoo 时,它说所有软件包都已安装。
有人可以帮忙吗?