簡體   English   中英

在 PYOMO 中為 2 個變量定義一組特定的值

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

我正在嘗試使用多個變量分配材料屬性。 例如; 密度和電導率是材料_1、材料_2 和材料_3 的兩個決策變量。

我必須輸入以下信息:

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

Pyomo 中定義變量的標准格式如下:

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

上面的代碼意味着 variable_1 是一個下限 = 800,上限 = 2000 的變量。

但是我們如何用一組特定的值而不是一個界限來定義一個變量呢?

這個想法是將數據值輸入優化器,這樣當它選擇密度值時,它也應該從相同的材料中選擇電導率值

我們如何將這樣的條件強加到 pyomo 框架中? 有人可以幫我嗎?

因此,如果您只是選擇多個選項之一,則可以將其設置為 Integer 線性程序。 基本要點是我們讓下例中的二進制變量x表示選擇材料i的行為,其中i是材料集的成員。

在您上面的問題中,您似乎在為將 model 中的參數(價格、密度、電導率等)分離的概念而苦苦掙扎,這些參數的值與您想要對 model 做出的決定的變量進行固定。

比下面稍微高級的 model 可能是混合 model,您可以在某些約束等范圍內采用各種材料的比例,這需要將x的域更改為非負實數。 這只是模擬選擇的二元作用。 當然,在像這樣微不足道的 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()

產量:

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