簡體   English   中英

如何在pyomo目標函數中迭代外部輸入列表?

[英]How to iterate over external input list in pyomo objective function?

我正在嘗試使用Gurobi求解器運行一個簡單的 LP pyomo Concrete 模型:

import pyomo.environ as pyo
from pyomo.opt import SolverFactory

model = pyo.ConcreteModel()

nb_years = 3
nb_mins = 2
step = 8760*1.5
delta = 10000

#Range of hour
model.h = pyo.RangeSet(0,8760*nb_years-1)

#Individual minimums
model.min = pyo.RangeSet(0, nb_mins-1)

model.mins = pyo.Var(model.min, within=model.h, initialize=[i for i in model.min])

def maximal_step_between_mins_constraint_rule(model, min):

    next_min = min + 1 if min < nb_mins-1 else 0
    if next_min == 0: # We need to take circularity into account
        return 8760*nb_years - model.mins[min] + model.mins[next_min] <= step + delta
    return model.mins[next_min] - model.mins[min] <= step + delta

def minimal_step_between_mins_constraint_rule(model, min):

    next_min = min + 1 if min < nb_mins-1 else 0
    if next_min == 0: # We need to take circularity into account
        return 8760*nb_years - model.mins[min] + model.mins[next_min] >= step - delta
    return model.mins[next_min] - model.mins[min] >= step - delta

model.input_list = pyo.Param(model.h, initialize=my_input_list, within=pyo.Reals, mutable=False)

def objective_rule(model):

    return sum([model.input_list[model.mins[min]] for min in model.min])

model.maximal_step_between_mins_constraint= pyo.Constraint(model.min, rule=maximal_step_between_mins_constraint_rule)

model.minimal_step_between_mins_constraint= pyo.Constraint(model.min, rule=minimal_step_between_mins_constraint_rule)

model.objective = pyo.Objective(rule=objective_rule, sense=pyo.minimize)

opt = SolverFactory('gurobi')
results = opt.solve(model, options={'Presolve':2})

基本上,我試圖在跨越 3 年的數據的輸入列表(看起來像這樣)中找到兩個小時,限制了它們之間的距離,並且模型最小化了兩個值的總和。

我將列表實現為固定值的參數,但是即使mutable設置為False運行我的模型也會產生此錯誤:

ERROR: Rule failed when generating expression for Objective objective with
    index None: RuntimeError: Error retrieving the value of an indexed item
    input_list: index 0 is not a constant value.  This is likely not what you
    meant to do, as if you later change the fixed value of the object this
    lookup will not change.  If you understand the implications of using non-
    constant values, you can get the current value of the object using the
    value() function.
ERROR: Constructing component 'objective' from data=None failed: RuntimeError:
    Error retrieving the value of an indexed item input_list: index 0 is not a
    constant value.  This is likely not what you meant to do, as if you later
    change the fixed value of the object this lookup will not change.  If you
    understand the implications of using non-constant values, you can get the
    current value of the object using the value() function.

知道為什么我會收到此錯誤以及如何解決它嗎? 顯然,將目標函數更改為sum([pyo.value(model.input_list[model.mins[min]]) for min in model.min])並不能解決我的問題。 我也嘗試不使用 pyomo 參數(使用sum([input_list[model.mins[min]] for min in model.min])類的東西,但是 pyomo 無法對其進行迭代並引發以下錯誤:

ERROR: Constructing component 'objective' from data=None failed: TypeError:
    list indices must be integers or slices, not _GeneralVarData

您的模型中有幾個嚴重的語法和結構問題。 並非所有元素都包含在您提供的代碼中,但您(至少)需要修復這些:

在此代碼段中,您將每個變量的初始化為一個列表,這是無效的。 從沒有變量初始化開始:

model.mins = pyo.Var(model.min, within=model.h, initialize=[i for i in model.min])

在這個總結中,您似乎正在使用一個變量作為某些數據的索引。 這是一個無效的構造。 構建模型時變量的值是未知的。 您需要重新制定:

return sum([model.input_list[model.mins[min]] for min in model.min])

我的建議:從一部分數據和pprint()模型開始,並在嘗試解決之前仔細閱讀它的質量。

model.pprint()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM