提问人:Fatima Mansour 提问时间:8/29/2022 最后编辑:Fatima Mansour 更新时间:8/31/2022 访问量:150
在 python 中使用 pyomo 绘制帕累托前沿时出错
Error in plotting pareto front using pyomo in python
问:
我正在尝试为我的多目标优化问题的简化版本绘制帕累托前沿(试图弄清楚一切是如何工作的)。我对此完全陌生,无法弄清楚我得到的错误意味着什么:
TypeError: '>' not supported between instances of 'generator' and 'int'
我正在使用现有代码进行绘图,所以我很确定错误出在我如何表述问题上,但我不确定它是什么。这是我的代码:
v=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
w=[22.0, 31.0, 0.0, 0.0, 11416.0, 0.0, 0.0, 0.0, 0.0, 15376.6, 977.97, 4324.97, 3264.79, 32.4, 43.02, 0.029, 0.2,0.00185, 0.00185, 0.0001, 0.03, 0.017, 0.0,0,0,0,0,0,0]
e=[562.51, 562.51, 0.0, 0.0, 223.16, 0.0, 0.0, 0.0, 0.0, 1401.63, 411.42, 1401.63, 0.0,312.53, 17195.71, 0.623, 15.14,0.01, 4.5, 23.42, 0.66,0,0,0,0,0,0,0,0]
g=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 840.000,469.000,46.000,18.000,4.000,12.000,0,0,0,0]
class WEN(Problem):
def __init__(self):
super().__init__(n_var=v, n_obj=2, n_ieq_constr=2, xl=np.array(0 for i in range(29)), xu=None)
def _evaluate(self, x, out, *args, **kwargs):
f1=[numpy.dot(v,e)]
f2=[numpy.dot(v,w)]
g1=[(numpy.dot(v*w)/100)-585]
g2=[(numpy.dot(v*g)*0.00001)-1610]
out["F"] = [f1, f2]
out["G"] = [g1, g2]
from pymoo.visualization.scatter import Scatter
from pymoo.algorithms.moo.nsga2 import RankAndCrowdingSurvival
from pymoo.core.mixed import MixedVariableGA
from pymoo.optimize import minimize
problem = WEN()
algorithm = MixedVariableGA(pop_size=20, survival=RankAndCrowdingSurvival())
res = minimize(problem,
algorithm,
('n_gen', 50),
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()
任何帮助、提示、建议(关于 Python 编码和/或 Python 优化的任何方面)将不胜感激。
答:
0赞
Fatima Mansour
8/30/2022
#1
如果有人遇到同样的问题,错误是因为我在类的 init 函数中放入了 n_var 参数的列表,而不是列表的长度(或整数值)。因此,该行的正确代码为:
class WEN(Problem):
def __init__(self):
super().__init__(n_var=len(v), n_obj=2, n_ieq_constr=2, xl=0.0, xu=None)
然而,代码的其余部分不起作用,因为我试图强加和现有的代码来绘制具有不同维度的问题,所以我接下来将尝试弄清楚这部分。
评论