Pyomo 找到列表值的最小总和
Pyomo find minimal sum of list values
我想要一个索引二进制变量,所以 pyomo 优化它以最小化列表的总和,同时至少选择 2 个元素。当我删除(imo 冗余)model.q 时,我收到:
ValueError: No variables appear in the Pyomo model constraints or objective. This is not supported by the NL file interface
pyomo 给我的解决方案 model.q 包含 q=0,这违反了约束 c1。
5 Declarations: i x q y objective
q 0.0
y[0] 1
y[1] 1
y[2] 1
from pyomo.environ import *
# create a model instance
model = ConcreteModel()
#Parameters
model.i = RangeSet(0, 2)
model.x = Param(model.i, initialize=[5,1,2])
#Variables
model.q = Var(domain=Binary, initialize=1)
model.y = Var(model.i, domain=Binary)
#Constraints
model.c1 = model.Constraint(expr=model.q == 1)
model.c2 = model.Constraint(expr=sum(model.y[i] for i in model.i) >= 2)
#Objective function
model.objective = Objective(expr = sum(model.x[i]*model.y[i]*model.q for i in model.i), sense=minimize)
# compute a solution
results = SolverFactory('mindtpy').solve(model, mip_solver='glpk', nlp_solver='ipopt', tee=True)
model.pprint()
欢迎来到本站。
您有几个错误导致了问题。
当你构造你的参数时,你需要传入一个字典,这样pyomo就可以将集合中的项目与值相关联。您不能传入列表并假设事情按顺序发生......该集合可以有任何顺序等。
您在制作约束 C2 时出现了一个可怕的拼写错误。请参阅我在代码注释中的注释
您的变量 q
完全没有必要。并且,通过将 q
乘以 y
,您通过乘以变量来解决问题 non-linear。
有点固定:
from pyomo.environ import *
# create a model instance
model = ConcreteModel()
#Parameters
model.i = RangeSet(0, 2)
values = {0:5, 1:1, 2:2}
model.x = Param(model.i, initialize=values)
#Variables
#model.q = Var(domain=Binary, initialize=1)
model.y = Var(model.i, domain=Binary)
#Constraints
#model.c1 = model.Constraint(expr=model.q == 1)
# NOTE: you mistakenly had "model.Constraint" which is a sneaky & bad typo!!
model.c2 = Constraint(expr=sum(model.y[i] for i in model.i) >= 2)
#Objective function
model.objective = Objective(expr = sum(model.x[i]*model.y[i] for i in model.i), sense=minimize)
# compute a solution
results = SolverFactory('glpk').solve(model) #, mip_solver='glpk', nlp_solver='ipopt', tee=True)
print(results)
model.display()
model.pprint()
产生(有点长,但我认为它会帮助您查看所有 3 个项目...
Problem:
- Name: unknown
Lower bound: 3.0
Upper bound: 3.0
Number of objectives: 1
Number of constraints: 2
Number of variables: 4
Number of nonzeros: 4
Sense: minimize
Solver:
- Status: ok
Termination condition: optimal
Statistics:
Branch and bound:
Number of bounded subproblems: 1
Number of created subproblems: 1
Error rc: 0
Time: 0.006919145584106445
Solution:
- number of solutions: 0
number of solutions displayed: 0
Model unknown
Variables:
y : Size=3, Index=i
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : 0.0 : 1 : False : False : Binary
1 : 0 : 1.0 : 1 : False : False : Binary
2 : 0 : 1.0 : 1 : False : False : Binary
Objectives:
objective : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 3.0
Constraints:
c2 : Size=1
Key : Lower : Body : Upper
None : 2.0 : 2.0 : None
1 RangeSet Declarations
i : Dimen=1, Size=3, Bounds=(0, 2)
Key : Finite : Members
None : True : [0:2]
1 Param Declarations
x : Size=3, Index=i, Domain=Any, Default=None, Mutable=False
Key : Value
0 : 5
1 : 1
2 : 2
1 Var Declarations
y : Size=3, Index=i
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : 0.0 : 1 : False : False : Binary
1 : 0 : 1.0 : 1 : False : False : Binary
2 : 0 : 1.0 : 1 : False : False : Binary
1 Objective Declarations
objective : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : 5*y[0] + y[1] + 2*y[2]
1 Constraint Declarations
c2 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : 2.0 : y[0] + y[1] + y[2] : +Inf : True
5 Declarations: i x y c2 objective
[Finished in 564ms]
我想要一个索引二进制变量,所以 pyomo 优化它以最小化列表的总和,同时至少选择 2 个元素。当我删除(imo 冗余)model.q 时,我收到:
ValueError: No variables appear in the Pyomo model constraints or objective. This is not supported by the NL file interface
pyomo 给我的解决方案 model.q 包含 q=0,这违反了约束 c1。
5 Declarations: i x q y objective q 0.0 y[0] 1 y[1] 1 y[2] 1
from pyomo.environ import *
# create a model instance
model = ConcreteModel()
#Parameters
model.i = RangeSet(0, 2)
model.x = Param(model.i, initialize=[5,1,2])
#Variables
model.q = Var(domain=Binary, initialize=1)
model.y = Var(model.i, domain=Binary)
#Constraints
model.c1 = model.Constraint(expr=model.q == 1)
model.c2 = model.Constraint(expr=sum(model.y[i] for i in model.i) >= 2)
#Objective function
model.objective = Objective(expr = sum(model.x[i]*model.y[i]*model.q for i in model.i), sense=minimize)
# compute a solution
results = SolverFactory('mindtpy').solve(model, mip_solver='glpk', nlp_solver='ipopt', tee=True)
model.pprint()
欢迎来到本站。
您有几个错误导致了问题。
当你构造你的参数时,你需要传入一个字典,这样pyomo就可以将集合中的项目与值相关联。您不能传入列表并假设事情按顺序发生......该集合可以有任何顺序等。
您在制作约束 C2 时出现了一个可怕的拼写错误。请参阅我在代码注释中的注释
您的变量
q
完全没有必要。并且,通过将q
乘以y
,您通过乘以变量来解决问题 non-linear。
有点固定:
from pyomo.environ import *
# create a model instance
model = ConcreteModel()
#Parameters
model.i = RangeSet(0, 2)
values = {0:5, 1:1, 2:2}
model.x = Param(model.i, initialize=values)
#Variables
#model.q = Var(domain=Binary, initialize=1)
model.y = Var(model.i, domain=Binary)
#Constraints
#model.c1 = model.Constraint(expr=model.q == 1)
# NOTE: you mistakenly had "model.Constraint" which is a sneaky & bad typo!!
model.c2 = Constraint(expr=sum(model.y[i] for i in model.i) >= 2)
#Objective function
model.objective = Objective(expr = sum(model.x[i]*model.y[i] for i in model.i), sense=minimize)
# compute a solution
results = SolverFactory('glpk').solve(model) #, mip_solver='glpk', nlp_solver='ipopt', tee=True)
print(results)
model.display()
model.pprint()
产生(有点长,但我认为它会帮助您查看所有 3 个项目...
Problem:
- Name: unknown
Lower bound: 3.0
Upper bound: 3.0
Number of objectives: 1
Number of constraints: 2
Number of variables: 4
Number of nonzeros: 4
Sense: minimize
Solver:
- Status: ok
Termination condition: optimal
Statistics:
Branch and bound:
Number of bounded subproblems: 1
Number of created subproblems: 1
Error rc: 0
Time: 0.006919145584106445
Solution:
- number of solutions: 0
number of solutions displayed: 0
Model unknown
Variables:
y : Size=3, Index=i
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : 0.0 : 1 : False : False : Binary
1 : 0 : 1.0 : 1 : False : False : Binary
2 : 0 : 1.0 : 1 : False : False : Binary
Objectives:
objective : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 3.0
Constraints:
c2 : Size=1
Key : Lower : Body : Upper
None : 2.0 : 2.0 : None
1 RangeSet Declarations
i : Dimen=1, Size=3, Bounds=(0, 2)
Key : Finite : Members
None : True : [0:2]
1 Param Declarations
x : Size=3, Index=i, Domain=Any, Default=None, Mutable=False
Key : Value
0 : 5
1 : 1
2 : 2
1 Var Declarations
y : Size=3, Index=i
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : 0.0 : 1 : False : False : Binary
1 : 0 : 1.0 : 1 : False : False : Binary
2 : 0 : 1.0 : 1 : False : False : Binary
1 Objective Declarations
objective : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : 5*y[0] + y[1] + 2*y[2]
1 Constraint Declarations
c2 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : 2.0 : y[0] + y[1] + y[2] : +Inf : True
5 Declarations: i x y c2 objective
[Finished in 564ms]