Pyomo:无法对索引集组件应用集运算符
我是一个新的Pyomo用户,我尝试将ampl模型转换为Pyomo,我发现这项工作太难了 当我创建变量x时,set lev开关被另一个集合a索引,我得到以下错误警告: 无法将集合运算符应用于索引集合组件(lev) 提前谢谢 Ampl模型和pyomo代码如下所示 Ampl 俾莫 错误警告Pyomo:无法对索引集组件应用集运算符,pyomo,Pyomo,我是一个新的Pyomo用户,我尝试将ampl模型转换为Pyomo,我发现这项工作太难了 当我创建变量x时,set lev开关被另一个集合a索引,我得到以下错误警告: 无法将集合运算符应用于索引集合组件(lev) 提前谢谢 Ampl模型和pyomo代码如下所示 Ampl 俾莫 错误警告 --------------------------------------------------------------------------- TypeError
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-74-f8fe2ec9e77d> in <module>()
10
11 model.y=pyo.Var(model.A,model.T,domain=pyo.Binary)
---> 12 model.x=pyo.Var(model.A,model.lev,model.T,domain=pyo.Binary)
C:\Users\HaichengLing\anaconda3\envs\nilmtk-env\lib\site-packages\pyomo\core\base\var.py in __init__(self, *args, **kwd)
522 #
523 kwd.setdefault('ctype', Var)
--> 524 IndexedComponent.__init__(self, *args, **kwd)
525 #
526 # Determine if the domain argument is a functor or other object
C:\Users\HaichengLing\anaconda3\envs\nilmtk-env\lib\site-packages\pyomo\core\base\indexed_component.py in __init__(self, *args, **kwds)
215 # "transferred" to the model).
216 #
--> 217 tmp = [process_setarg(x) for x in args]
218 self._implicit_subsets = tmp
219 self._index = tmp[0].cross(*tmp[1:])
C:\Users\HaichengLing\anaconda3\envs\nilmtk-env\lib\site-packages\pyomo\core\base\indexed_component.py in <listcomp>(.0)
215 # "transferred" to the model).
216 #
--> 217 tmp = [process_setarg(x) for x in args]
218 self._implicit_subsets = tmp
219 self._index = tmp[0].cross(*tmp[1:])
C:\Users\HaichengLing\anaconda3\envs\nilmtk-env\lib\site-packages\pyomo\core\base\set.py in process_setarg(arg)
118 raise TypeError("Cannot apply a Set operator to an "
119 "indexed %s component (%s)"
--> 120 % (arg.ctype.__name__, arg.name,))
121 elif isinstance(arg, Component):
122 raise TypeError("Cannot apply a Set operator to a non-Set "
TypeError: Cannot apply a Set operator to an indexed Set component (lev)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
10
11 model.y=pyo.Var(model.A、model.T、domain=pyo.Binary)
--->12 model.x=pyo.Var(model.A、model.lev、model.T、domain=pyo.Binary)
C:\Users\HaichengLing\anaconda3\envs\nilmtk env\lib\site packages\pyomo\core\base\var.py in\uuuuuuu init\uuuuu(self,*args,**kwd)
522 #
523 kwd.setdefault('ctype',变量)
-->524 IndexedComponent.\uuuu init\uuuu(self,*args,**kwd)
525 #
526#确定域参数是函子还是其他对象
C:\Users\HaichengLing\anaconda3\envs\nilmtk env\lib\site packages\pyomo\core\base\index\u component.py in\uuuuuuu init\uuuu(self,*args,**kwds)
215#“转移”到模型)。
216 #
-->217 tmp=[处理参数中x的设置参数(x)]
218自隐子集=tmp
219 self._index=tmp[0]。交叉(*tmp[1:])
C:\Users\HaichengLing\anaconda3\envs\nilmtk env\lib\site packages\pyomo\core\base\index\u component.py in(.0)
215#“转移”到模型)。
216 #
-->217 tmp=[处理参数中x的设置参数(x)]
218自隐子集=tmp
219 self._index=tmp[0]。交叉(*tmp[1:])
C:\Users\HaichengLing\anaconda3\envs\nilmtk env\lib\site packages\pyomo\core\base\set.py in process\u setarg(arg)
118 raise TypeError(“无法将Set运算符应用于”
119“索引%s组件(%s)”
-->120%(arg.ctype.\uuuuuuu名称,arg.name,)
121 elif isinstance(参数,部件):
122 raise TypeError(“无法将集合运算符应用于非集合”
TypeError:无法将集合运算符应用于索引集合组件(lev)
最简单的方法是使用中间集:
model.A=pyo.Set(ordered=True)
model.L=pyo.Set()
model.lev=pyo.Set(model.A,in=model.L)
def AL_规则(m):
报税表[(a,l)为m.a为l为m.lev[a]]
model.AL=Set(在=model.A*model.L内,初始化=AL\u规则)
model.x=pyo.Var(model.AL,model.T,domain=pyo.Binary)
model=pyo.AbstractModel()
model.T = pyo.RangeSet(1,48)
model.A=pyo.Set(ordered=True)
model.L=pyo.Set()
model.lev=pyo.Set(model.A,within=model.L)
model.y=pyo.Var(model.A,model.T,domain=pyo.Binary)
model.x=pyo.Var(model.A,model.lev,model.T,domain=pyo.Binary)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-74-f8fe2ec9e77d> in <module>()
10
11 model.y=pyo.Var(model.A,model.T,domain=pyo.Binary)
---> 12 model.x=pyo.Var(model.A,model.lev,model.T,domain=pyo.Binary)
C:\Users\HaichengLing\anaconda3\envs\nilmtk-env\lib\site-packages\pyomo\core\base\var.py in __init__(self, *args, **kwd)
522 #
523 kwd.setdefault('ctype', Var)
--> 524 IndexedComponent.__init__(self, *args, **kwd)
525 #
526 # Determine if the domain argument is a functor or other object
C:\Users\HaichengLing\anaconda3\envs\nilmtk-env\lib\site-packages\pyomo\core\base\indexed_component.py in __init__(self, *args, **kwds)
215 # "transferred" to the model).
216 #
--> 217 tmp = [process_setarg(x) for x in args]
218 self._implicit_subsets = tmp
219 self._index = tmp[0].cross(*tmp[1:])
C:\Users\HaichengLing\anaconda3\envs\nilmtk-env\lib\site-packages\pyomo\core\base\indexed_component.py in <listcomp>(.0)
215 # "transferred" to the model).
216 #
--> 217 tmp = [process_setarg(x) for x in args]
218 self._implicit_subsets = tmp
219 self._index = tmp[0].cross(*tmp[1:])
C:\Users\HaichengLing\anaconda3\envs\nilmtk-env\lib\site-packages\pyomo\core\base\set.py in process_setarg(arg)
118 raise TypeError("Cannot apply a Set operator to an "
119 "indexed %s component (%s)"
--> 120 % (arg.ctype.__name__, arg.name,))
121 elif isinstance(arg, Component):
122 raise TypeError("Cannot apply a Set operator to a non-Set "
TypeError: Cannot apply a Set operator to an indexed Set component (lev)