简体   繁体   English

在 PYOMO 中为 2 个变量定义一组特定的值

[英]Defining specific set of values for 2 variables in PYOMO

I am trying to assign material property using multiple variables.我正在尝试使用多个变量分配材料属性。 For example;例如; density and conductivity are two decision variables for material_1, material_2 and material_3.密度和电导率是材料_1、材料_2 和材料_3 的两个决策变量。

I have to input the following information:我必须输入以下信息:

density of material_1 = 1000
density of material_2 = 2000
density of material_3 = 1500

conductivity of material_1 = 250
conductivity of material_2 = 400
conductivity of material_3 = 100

The standard format for defining variables in Pyomo is given below: Pyomo 中定义变量的标准格式如下:

model.variable_1 = Var(bounds=(800,2000))

The above code means variable_1 is a variable with lower bound = 800, and upper bound = 2000.上面的代码意味着 variable_1 是一个下限 = 800,上限 = 2000 的变量。

But how can we define a variable with a specific set of values instead of a bound?但是我们如何用一组特定的值而不是一个界限来定义一个变量呢?

The idea is to input data values into the optimizer such that when it chooses a density value, it should also choose the conductivity value from the same material .这个想法是将数据值输入优化器,这样当它选择密度值时,它也应该从相同的材料中选择电导率值

How can we impose such a condition into pyomo framework?我们如何将这样的条件强加到 pyomo 框架中? Can someone please help me with this?有人可以帮我吗?

So if you are just selecting one of many options, you can set this up as an Integer Linear Program.因此,如果您只是选择多个选项之一,则可以将其设置为 Integer 线性程序。 The basic gist is that we let a binary variable x in the example below represent the act of selecting material i , where i is a member of the set of materials.基本要点是我们让下例中的二进制变量x表示选择材料i的行为,其中i是材料集的成员。

In your question above, you seem to be struggling with the concept of separating the parameters in the model (price, density, conductivity, etc.) which are fixed in value from the variables which are the decisions you want to model.在您上面的问题中,您似乎在为将 model 中的参数(价格、密度、电导率等)分离的概念而苦苦挣扎,这些参数的值与您想要对 model 做出的决定的变量进行固定。

A slightly more advanced model than below might be a mixing model, where you can take proportions of various materials within some constraints, etc. which would require changing the domain of x to be non-negative real numbers.比下面稍微高级的 model 可能是混合 model,您可以在某些约束等范围内采用各种材料的比例,这需要将x的域更改为非负实数。 This one just models the binary action of selection.这只是模拟选择的二元作用。 Of course in a model as trivial as this, you could just solve it with list/dictionary comprehensions or a filter, so using algebraic modeling is really overkill, but it is an example to differentiate the concepts you asked about.当然,在像这样微不足道的 model 中,您可以使用列表/字典推导或过滤器来解决它,因此使用代数建模确实有点矫枉过正,但这是区分您所询问的概念的一个示例。

# material selection model

import pyomo.environ as pyo

# data
materials = ['steel', 'alum', 'carbon', 'cheese']

density =   {   'steel' : 1.2,
                'alum'  : 0.8,
                'carbon': 1.8,
                'cheese': 0.7}

conductivity = {'steel' : 6.4,
                'alum'  : 3.1,
                'carbon': 4.4,
                'cheese': 0.3}

price =     {   'steel' : 2.3,
                'alum'  : 3.5,
                'carbon': 5.8,
                'cheese': 6.0}

m = pyo.ConcreteModel('material selector')

# SETS (used to index the decision variable and the parameters)
m.matl = pyo.Set(initialize=materials)

# VARIABLES
m.x = pyo.Var(m.matl, domain=pyo.Binary)   # a binary decision variable representing the selection of matl

# PARAMETERS
m.density = pyo.Param(m.matl, initialize=density)
m.conductivity = pyo.Param(m.matl, initialize=conductivity)
m.price = pyo.Param(m.matl, initialize=price)


# OBJ (minimize price)
m.obj = pyo.Objective(expr=sum(m.x[i] * m.price[i] for i in m.matl))

# Constraints
m.c1 = pyo.Constraint(expr=(sum(m.x[i] * m.density[i] for i in m.matl) >= 1.0))     # min density
m.c2 = pyo.Constraint(expr=(sum(m.x[i] * m.conductivity[i] for i in m.matl) <= 5.0)) # max cond.

# solve it
solver = pyo.SolverFactory('glpk')
result = solver.solve(m)
m.display()

Yields:产量:

Model material selector

  Variables:
    x : Size=4, Index=matl
        Key    : Lower : Value : Upper : Fixed : Stale : Domain
          alum :     0 :   0.0 :     1 : False : False : Binary
        carbon :     0 :   1.0 :     1 : False : False : Binary
        cheese :     0 :   0.0 :     1 : False : False : Binary
         steel :     0 :   0.0 :     1 : False : False : Binary

  Objectives:
    obj : Size=1, Index=None, Active=True
        Key  : Active : Value
        None :   True :   5.8

  Constraints:
    c1 : Size=1
        Key  : Lower : Body : Upper
        None :   1.0 :  1.8 :  None
    c2 : Size=1
        Key  : Lower : Body : Upper
        None :  None :  4.4 :   5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM