动态变量和约束在Pyomo中

huangapple go评论81阅读模式
英文:

Dynamic variables and constraints in Pyomo

问题

You can modify your Pyomo code to dynamically create the variables and constraints based on the input 'N'. Here's the adjusted code:

  1. !pip install -q pyomo
  2. !apt-get install -y -qq glpk-utils
  3. import pyomo.environ as pyo
  4. from pyomo.environ import *
  5. from pyomo.opt import SolverFactory
  6. alpha = 0.8
  7. N = 9 # This is the input that can be changed and will impact the number of variables and constraints
  8. model = pyo.ConcreteModel()
  9. model.a = pyo.Var(domain=NonNegativeReals)
  10. # Create a dictionary to hold the variables 'b' dynamically
  11. model.b = pyo.Var(range(1, N+1), domain=NonNegativeReals)
  12. # Define the objective function
  13. TTL = model.a + (1/(1-alpha)) * sum(model.b[i] for i in range(1, N+1))
  14. # Create constraints dynamically
  15. model.c = ConstraintList()
  16. for i in range(1, N+1):
  17. model.c.add(expr = model.b[i] + model.a >= 0)
  18. model.obj = pyo.Objective(expr= TTL, sense=minimize)

With this code, you can change the value of 'N,' and the code will dynamically create the corresponding variables and constraints.

英文:

I need to solve an LP problem in Pyomo. The number of variables and constraints changes depending on the purpose it will be used and can be freely changed by the user (input 'N' in the code below). I did a code for a fixed number of variables and constraints (N=9). It is working well but I don't know how to make it dynamically create the variables and constraints as the input N changes. The code below is a simplified version of my code for better understanding.

  1. !pip install -q pyomo
  2. !apt-get install -y -qq glpk-utils
  3. import pyomo.environ as pyo
  4. from pyomo.environ import *
  5. from pyomo.opt import SolverFactory
  6. alpha = 0.8
  7. N = 9 # this is the input that can be changed and will impact on the number of variables and constraints
  8. model = pyo.ConcreteModel()
  9. model.a = pyo.Var(domain=NonNegativeReals)
  10. # the number of the variables below must follow the input 'N' that in this case is 9
  11. model.b1 = pyo.Var(domain=NonNegativeReals)
  12. model.b2 = pyo.Var(domain=NonNegativeReals)
  13. model.b3 = pyo.Var(domain=NonNegativeReals)
  14. model.b4 = pyo.Var(domain=NonNegativeReals)
  15. model.b5 = pyo.Var(domain=NonNegativeReals)
  16. model.b6 = pyo.Var(domain=NonNegativeReals)
  17. model.b7 = pyo.Var(domain=NonNegativeReals)
  18. model.b8 = pyo.Var(domain=NonNegativeReals)
  19. model.b9 = pyo.Var(domain=NonNegativeReals)
  20. # below is a calculation during the building of obj function that also needs to change based on input 'N'. Necessary to sum all variables 'b' created
  21. TTL = model.a + (1/(1-alpha)) * (model.b1 + model.b2 + model.b3 + model.b4 + model.b5 + model.b6 + model.b7 + model.b8 + model.b9)
  22. # below are the constraints that should change based on the input 'N' as well
  23. model.c1 = Constraint(expr = model.b1 + model.a >= 0)
  24. model.c2 = Constraint(expr = model.b2 + model.a >= 0)
  25. model.c3 = Constraint(expr = model.b3 + model.a >= 0)
  26. model.c4 = Constraint(expr = model.b4 + model.a >= 0)
  27. model.c5 = Constraint(expr = model.b5 + model.a >= 0)
  28. model.c6 = Constraint(expr = model.b6 + model.a >= 0)
  29. model.c7 = Constraint(expr = model.b7 + model.a >= 0)
  30. model.c8 = Constraint(expr = model.b8 + model.a >= 0)
  31. model.c9 = Constraint(expr = model.b9 + model.a >= 0)
  32. model.obj = pyo.Objective(expr= TTL, sense=minimize)

For the creation of the variables, I am using the code below, which I understand may work. But once I cannot run the entire code with this variable declaration, I even don't know if it really works.

Using this

  1. model.b = pyo.Var(range(N), domain=NonNegativeReals)

Instead of this

  1. model.b1 = pyo.Var(domain=NonNegativeReals)
  2. model.b2 = pyo.Var(domain=NonNegativeReals)
  3. model.b3 = pyo.Var(domain=NonNegativeReals)
  4. model.b4 = pyo.Var(domain=NonNegativeReals)
  5. model.b5 = pyo.Var(domain=NonNegativeReals)
  6. model.b6 = pyo.Var(domain=NonNegativeReals)
  7. model.b7 = pyo.Var(domain=NonNegativeReals)
  8. model.b8 = pyo.Var(domain=NonNegativeReals)
  9. model.b9 = pyo.Var(domain=NonNegativeReals)

How can I adjust the code above to meet the application requirements?

Many thanks in advance!

答案1

得分: 0

以下是翻译的代码部分:

  1. import pyomo.environ as pyo
  2. var_count = int(input("有多少个变量:"))
  3. m = pyo.ConcreteModel()
  4. m.N = pyo.Set(initialize=range(var_count))
  5. m.x = pyo.Var(m.N)
  6. m.pprint()

希望这对您有所帮助。如果有任何其他翻译需求,请随时告诉我。

英文:

Instead of making the variables 1-by-1, which is difficult to build and almost impossible to manage, just make an index set from whatever "dynamic" stuff happens in your model and use that. It is much more robust and succinct. A simple example... Main point, dynamically make the sets and then just use them to index your variables.

Code:

  1. import pyomo.environ as pyo
  2. var_count = int(input("how many vars: "))
  3. m = pyo.ConcreteModel()
  4. m.N = pyo.Set(initialize=range(var_count))
  5. m.x = pyo.Var(m.N)
  6. m.pprint()

Sample run...

  1. how many vars: 4
  2. 1 Set Declarations
  3. N : Size=1, Index=None, Ordered=Insertion
  4. Key : Dimen : Domain : Size : Members
  5. None : 1 : Any : 4 : {0, 1, 2, 3}
  6. 1 Var Declarations
  7. x : Size=4, Index=N
  8. Key : Lower : Value : Upper : Fixed : Stale : Domain
  9. 0 : None : None : None : False : True : Reals
  10. 1 : None : None : None : False : True : Reals
  11. 2 : None : None : None : False : True : Reals
  12. 3 : None : None : None : False : True : Reals

huangapple
  • 本文由 发表于 2023年5月14日 00:20:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定