In [40]:
from pyomo.environ import *
mc = ConcreteModel()
In [41]:
mc.sRow = Set(initialize=range(2))
mc.sCol = Set(initialize=range(3))
mc.pprint()
2 Set Declarations
sCol : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=(0, 2)
[0, 1, 2]
sRow : Dim=0, Dimen=1, Size=2, Domain=None, Ordered=False, Bounds=(0, 1)
[0, 1]
2 Declarations: sRow sCol
In [42]:
def pA_rule(model, r, c):
return (-2)**r + (-3)**c
mc.pA = Param(mc.sRow, mc.sCol, initialize=pA_rule)
mc.pb = Param(mc.sRow, initialize=[2 * val for val in mc.sRow])
mc.pc = Param(mc.sCol, initialize=[(-2)**val for val in mc.sCol])
mc.pprint()
3 Set Declarations
pA_index : Dim=0, Dimen=2, Size=6, Domain=None, Ordered=False, Bounds=None
Virtual
sCol : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=(0, 2)
[0, 1, 2]
sRow : Dim=0, Dimen=1, Size=2, Domain=None, Ordered=False, Bounds=(0, 1)
[0, 1]
3 Param Declarations
pA : Size=6, Index=pA_index, Domain=Any, Default=None, Mutable=False
Key : Value
(0, 0) : 2
(0, 1) : -2
(0, 2) : 10
(1, 0) : -1
(1, 1) : -5
(1, 2) : 7
pb : Size=2, Index=sRow, Domain=Any, Default=None, Mutable=False
Key : Value
0 : None
1 : None
pc : Size=3, Index=sCol, Domain=Any, Default=None, Mutable=False
Key : Value
0 : None
1 : None
2 : None
6 Declarations: sRow sCol pA_index pA pb pc
In [43]:
mc.vx = Var(mc.sCol, within=NonNegativeReals)
mc.pprint()
3 Set Declarations
pA_index : Dim=0, Dimen=2, Size=6, Domain=None, Ordered=False, Bounds=None
Virtual
sCol : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=(0, 2)
[0, 1, 2]
sRow : Dim=0, Dimen=1, Size=2, Domain=None, Ordered=False, Bounds=(0, 1)
[0, 1]
3 Param Declarations
pA : Size=6, Index=pA_index, Domain=Any, Default=None, Mutable=False
Key : Value
(0, 0) : 2
(0, 1) : -2
(0, 2) : 10
(1, 0) : -1
(1, 1) : -5
(1, 2) : 7
pb : Size=2, Index=sRow, Domain=Any, Default=None, Mutable=False
Key : Value
0 : None
1 : None
pc : Size=3, Index=sCol, Domain=Any, Default=None, Mutable=False
Key : Value
0 : None
1 : None
2 : None
1 Var Declarations
vx : Size=3, Index=sCol, Domain=NonNegativeReals
Key : Lower : Value : Upper : Initial : Fixed : Stale
0 : 0 : None : None : None : False : True
1 : 0 : None : None : None : False : True
2 : 0 : None : None : None : False : True
7 Declarations: sRow sCol pA_index pA pb pc vx
In [45]:
mc.cCL1 = ConstraintList()
mc.cCL1.add(sum(mc.pA[0,c] * mc.vx[c] for c in mc.sCol) >= mc.pb[0])
WARNING:pyomo.core:Implicitly replacing the Component attribute cCL1 (type=<class 'pyomo.core.base.constraint.ConstraintList'>) on block unknown with a new Component (type=<class 'pyomo.core.base.constraint.ConstraintList'>).
This is usually indicative of a modelling error.
To avoid this warning, use block.del_component() and block.add_component().
WARNING: Implicitly replacing the Component attribute cCL1 (type=<class 'pyomo.core.base.constraint.ConstraintList'>) on block unknown with a new Component (type=<class 'pyomo.core.base.constraint.ConstraintList'>).
This is usually indicative of a modelling error.
To avoid this warning, use block.del_component() and block.add_component().
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-45-60c8a413fb9e> in <module>()
----> 1 mc.cCL1 = ConstraintList()
2 mc.cCL1.add(sum(mc.pA[0,c] * mc.vx[c] for c in mc.sCol) >= mc.pb[0])
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/block.py in __setattr__(self, name, val)
435 type(val) ) )
436 self.del_component(name)
--> 437 self.add_component(name, val)
438 else:
439 # Because we want to raise a special error if the
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/block.py in add_component(self, name, val)
608 # components.
609 if hasattr(val,'_index'):
--> 610 self._add_temporary_set(val)
611
612 # Add the component to the underlying Component store
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/block.py in _add_temporary_set(self, val)
528 if isinstance(val._index, _SetDataBase) and \
529 val._index.parent_component().cname() == "_unknown_":
--> 530 self._construct_temporary_set(val._index,val.cname()+"_index")
531 if isinstance(getattr(val,'initialize',None), _SetDataBase) and \
532 val.initialize.parent_component().cname() == "_unknown_":
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/block.py in _construct_temporary_set(self, obj, name)
551 return tobj
552 elif isinstance(obj,Set):
--> 553 self.add_component(name,obj)
554 return obj
555 raise Exception("BOGUS")
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/block.py in add_component(self, name, val)
564 "Cannot add component '%s' (type %s) to block '%s': a "
565 "component by that name (type %s) is already defined."
--> 566 % (name, type(val), self.cname(), type(getattr(self, name))))
567
568 _component = self.parent_component()
RuntimeError: Cannot add component 'cCL1_index' (type <class 'pyomo.core.base.sets.SimpleSet'>) to block 'unknown': a component by that name (type <class 'pyomo.core.base.sets.SimpleSet'>) is already defined.
In [46]:
def cCL2_rule(model, r):
if r == 2:
return ConstraintList.End
else:
return (mc.pb[r], sum(mc.pA[r,c] * mc.vx[c] for c in mc.sCol), None)
mc.cCL2 = ConstraintList(rule=cCL2_rule)
ERROR:pyomo.core:Constructing component 'cCL2' from data=None failed:
ValueError: Cannot convert object of type 'list' (value = [0, 2]) to a numeric value.
ERROR: Constructing component 'cCL2' from data=None failed:
ValueError: Cannot convert object of type 'list' (value = [0, 2]) to a numeric value.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-46-614c387627aa> in <module>()
4 else:
5 return (mc.pb[r], sum(mc.pA[r,c] * mc.vx[c] for c in mc.sCol), None)
----> 6 mc.cCL2 = ConstraintList(rule=cCL2_rule)
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/block.py in __setattr__(self, name, val)
418 if name not in self.__dict__:
419 if isinstance(val, Component):
--> 420 self.add_component(name, val)
421 else:
422 super(_BlockData, self).__setattr__(name, val)
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/block.py in add_component(self, name, val)
687 _blockName, str(data) )
688 try:
--> 689 val.construct(data)
690 except:
691 err = sys.exc_info()[1]
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/constraint.py in construct(self, data)
755 if (expr.__class__ is tuple and expr == ConstraintList.End):
756 return
--> 757 self.add(expr)
758 else:
759 for expr in _generator:
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/constraint.py in add(self, expr)
771 self._nconstraints += 1
772 self._index.add(self._nconstraints)
--> 773 return Constraint.add(self, self._nconstraints, expr)
774
775
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/constraint.py in add(self, index, expr)
447 arg0 = expr[0]
448 if arg0 is not None:
--> 449 arg0 = as_numeric(arg0)
450 if not arg0.is_fixed():
451 raise ValueError(
/usr/local/opt/pyenv/versions/miniconda3-3.7.0/lib/python3.4/site-packages/pyomo/core/base/numvalue.py in as_numeric(obj)
262 raise ValueError(
263 "Cannot convert object of type '%s' (value = %s) to a numeric value."
--> 264 % (type(obj).__name__, obj, ))
265
266
ValueError: Cannot convert object of type 'list' (value = [0, 2]) to a numeric value.
In [ ]:
Content source: feigaochn/StudyNotes
Similar notebooks: