Pyomo中为约束提供上限和下限的问题



我是Pyomo的新手。在下面的代码部分中,我试图最小化一个函数,以便满足另外两个约束我有一个问题,特别是在一个约束中,我试图通过指定上限和下限来提供范围字典的格式为,dict[key]=[d,r,p];'这里不使用p'决策变量"x"是二进制的。目标是找出可供选择的项目(仅一个(,以便"d"是"r"值范围内的最小值。"r"值的这个范围由"constraint rxlambda_2constraint_rule_upper"给定。因此,目标是找出三个字典项目中的项目(来自dict(,以便"d"的值在一系列"r"值中最小。T据我所知,预期输出应该是第一项(正如我在这里所说(,如在[2,0.8,0.3]中,r=0.8,小于0.9,大于0.5,在这个范围内,d=2是唯一的最小值。然而,我不理解这个错误的原因,并附上了错误消息和代码。我正在通过neos服务器使用"cplex"。

#代码
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
from pyomo.core import *
from pyomo.environ import *
dict ={((1,2),(2,4)): [2, 0.8 ,0.3],((1,2),(2,3)): [3, 0.2 ,0.3], ((1,2),(2,5)): [3, 0.1,0.3] }
model = ConcreteModel()
model.I = Param(initialize =1)
model.J= Param(initialize = len(dict))
model.m = RangeSet(1,model.I)
model.n = RangeSet(1, model.J)
d_list =[]
r_list = []
for key in dict:
dict_value = dict.get(key)
d_list.append(dict_value[0])
r_list.append(dict_value[1])
d_dict ={}
r_dict={}

for i in range(len(dict)):
d_dict[i+1]=d_list[i]
r_dict[i+1]=r_list[i]

model.x = Var(model.n, within = Binary)

model.d = Param(model.n, initialize =d_dict )
model.r = Param(model.n, within=NonNegativeReals,initialize = r_dict)

def ObjRule(model):
return summation(model.d, model.x)

model.OBJ =  Objective( rule =ObjRule  ,sense = minimize)
def rxlambda_constraint_rule_upper(model,i):
return  (0.5, model.r[i]*model.x[i], 0.9 ) 
model.rxlambdaConstraintUpper = Constraint(model.n, rule=rxlambda_constraint_rule_upper)

def sumx_constraint_rule(model,i):
value = sum(model.x[i] for i in model.n)
return value ==1
model.sumxConstraint = Constraint(model.n, rule = sumx_constraint_rule)

model.pprint()
instance = model.create()
opt = SolverFactory("cplex")
solver_manager = SolverManagerFactory('neos')
results = solver_manager.solve(instance, opt=opt, tee=True)
model.pprint()
instance.display()

for v in instance.component_objects(Var, active=True):
varobject = getattr(instance, str(v))
for index in varobject:
print (index, varobject[index].value)

########错误日志为:######

2 RangeSet Declarations
m : Dim=0, Dimen=1, Size=1, Domain=Integers, Ordered=True, Bounds=(1, 1)
Virtual
n : Dim=0, Dimen=1, Size=3, Domain=Integers, Ordered=True, Bounds=(1, 3)
Virtual
4 Param Declarations
I : Size=1, Index=None, Domain=Any, Default=None, Mutable=False
Key  : Value
None :     1
J : Size=1, Index=None, Domain=Any, Default=None, Mutable=False
Key  : Value
None :     3
d : Size=3, Index=n, Domain=Any, Default=None, Mutable=False
Key : Value
1 :     2
2 :     3
3 :     3
r : Size=3, Index=n, Domain=NonNegativeReals, Default=None, Mutable=False
Key : Value
1 :   0.8
2 :   0.1
3 :   0.2
1 Var Declarations
x : Size=3, Index=n
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 :     0 :  None :     1 : False :  True : Binary
2 :     0 :  None :     1 : False :  True : Binary
3 :     0 :  None :     1 : False :  True : Binary
1 Objective Declarations
OBJ : Size=1, Index=None, Active=True
Key  : Active : Sense    : Expression
None :   True : minimize : 2*x[1] + 3*x[2] + 3*x[3]
2 Constraint Declarations
rxlambdaConstraintUpper : Size=3, Index=n, Active=True
Key : Lower : Body       : Upper : Active
1 :   0.5 : 0.8 * x[1] :   0.9 :   True
2 :   0.5 : 0.1 * x[2] :   0.9 :   True
3 :   0.5 : 0.2 * x[3] :   0.9 :   True
sumxConstraint : Size=3, Index=n, Active=True
Key : Lower : Body               : Upper : Active
1 :   1.0 : x[1] + x[2] + x[3] :   1.0 :   True
2 :   1.0 : x[1] + x[2] + x[3] :   1.0 :   True
3 :   1.0 : x[1] + x[2] + x[3] :   1.0 :   True
10 Declarations: I J m n x d r OBJ rxlambdaConstraintUpper sumxConstraint
WARNING: DEPRECATION WARNING: the Model.create() method is deprecated.  Call
Model.create_instance() to create a concrete instance from an abstract
model.  You do not need to call Model.create() for a concrete model.
WARNING: DEPRECATED: Cannot call Model.create_instance() on a constructed
model; returning a clone of the current model instance.
Job "xxxxx" submitted to NEOS, password='xxxxx'
Check the following URL for progress report :
"link deleted"
Job "xxxxx" dispatched
password: "XXXXX"
---------- Begin Solver Output -----------
Condor submit: 'neos.submit'
Condor submit: 'watchdog.submit'
Job submitted to NEOS HTCondor pool.
WARNING: Loading a SolverResults object with a warning status into
model=unknown;
message from solver=CPLEX 12.7.0.0x3a integer infeasible.; 0 MIP
simplex iterations; 0 branch-and-bound nodes; No basis.
2 RangeSet Declarations
m : Dim=0, Dimen=1, Size=1, Domain=Integers, Ordered=True, Bounds=(1, 1)
Virtual
n : Dim=0, Dimen=1, Size=3, Domain=Integers, Ordered=True, Bounds=(1, 3)
Virtual
4 Param Declarations
I : Size=1, Index=None, Domain=Any, Default=None, Mutable=False
Key  : Value
None :     1
J : Size=1, Index=None, Domain=Any, Default=None, Mutable=False
Key  : Value
None :     3
d : Size=3, Index=n, Domain=Any, Default=None, Mutable=False
Key : Value
1 :     2
2 :     3
3 :     3
r : Size=3, Index=n, Domain=NonNegativeReals, Default=None, Mutable=False
Key : Value
1 :   0.8
2 :   0.1
3 :   0.2
1 Var Declarations
x : Size=3, Index=n
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 :     0 :  None :     1 : False :  True : Binary
2 :     0 :  None :     1 : False :  True : Binary
3 :     0 :  None :     1 : False :  True : Binary
1 Objective Declarations
OBJ : Size=1, Index=None, Active=True
Key  : Active : Sense    : Expression
None :   True : minimize : 2*x[1] + 3*x[2] + 3*x[3]
2 Constraint Declarations
rxlambdaConstraintUpper : Size=3, Index=n, Active=True
Key : Lower : Body       : Upper : Active
1 :   0.5 : 0.8 * x[1] :   0.9 :   True
2 :   0.5 : 0.1 * x[2] :   0.9 :   True
3 :   0.5 : 0.2 * x[3] :   0.9 :   True
sumxConstraint : Size=3, Index=n, Active=True
Key : Lower : Body               : Upper : Active
1 :   1.0 : x[1] + x[2] + x[3] :   1.0 :   True
2 :   1.0 : x[1] + x[2] + x[3] :   1.0 :   True
3 :   1.0 : x[1] + x[2] + x[3] :   1.0 :   True
10 Declarations: I J m n x d r OBJ rxlambdaConstraintUpper sumxConstraint
Model unknown
Variables:
x : Size=3, Index=n
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 :     0 :  None :     1 : False :  True : Binary
2 :     0 :  None :     1 : False :  True : Binary
3 :     0 :  None :     1 : False :  True : Binary
Objectives:
OBJ : Size=1, Index=None, Active=True
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: 2*x[1] + 3*x[2] + 3*x[3])
ERROR: evaluating object as numeric value: OBJ
(object: <class 'pyomo.core.base.objective.SimpleObjective'>)
No value for uninitialized NumericValue object x[1]
Key : Active : Value
None :   None :  None
Constraints:
rxlambdaConstraintUpper : Size=3
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: 0.8 * x[1])
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[2]
(expression: 0.1 * x[2])
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[3]
(expression: 0.2 * x[3])
Key : Lower : Body : Upper
1 :  None : None :  None
2 :  None : None :  None
3 :  None : None :  None
sumxConstraint : Size=3
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: x[1] + x[2] + x[3])
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: x[1] + x[2] + x[3])
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: x[1] + x[2] + x[3])
Key : Lower : Body : Upper
1 :  None : None :  None
2 :  None : None :  None
3 :  None : None :  None
(1, None)
(2, None)
(3, None)
​

您的rxlambdaConstraintUpper for 2,3是不可行的。

2 :   0.5 : 0.1 * x[2] :   0.9 :   True
3 :   0.5 : 0.2 * x[3] :   0.9 :   True 

如果x[2]和x[3]取0或1,则它们将小于0.5(它们的下界(。因此,如果您更改线路

return  (0.5, model.r[i]*model.x[i], 0.9 )   

作为

return  (0, model.r[i]*model.x[i], 0.9 ) 

你可以得出工作模型和结果。

最新更新