如何在PYOMO中排序参数?

发布于 2025-02-11 10:49:05 字数 142 浏览 1 评论 0原文

我正在使用PYOMO软件包(用于Python中的数学建模)。我的参数定义为:model.d = param(model.t,model.i)。这是一个多维参数。 t是针对时间段的范围,我适用于实例范围,d是需求的价值。我如何通过其值对此参数进行排序,并获取相应的t和i?

I am working with the Pyomo package(for mathematical modeling in python).I have a parameter defined as: model.D=Param(model.t,model.i) .this is a multi-dimensional parameter. t is for the range of time periods, i is for the range of instances and D is the value of the demand. how can I sort this parameter by its value and get the corresponding t and i ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心激荡 2025-02-18 10:49:05

在建立模型之前,请使用基本的Python 进行所有数据清洁/排序。答案的一部分取决于数据的原始格式和您想要的答案格式,但我认为总体要素很明确。...

代码

import pyomo.environ as pyo

d = {   0: [1.2, 3.1, 0.5],
        1: [9.5, 2.0, 3.1] }

# a good format to pass in to a parameter, tuple-indexed dictionary
d_data = {(t, i): d[t][i] for t in d for i in range(len(d[t]))}


# now let's sort the values in d for the missing info on order
d_tuples = {t: sorted([(val, idx) for idx, val in enumerate(d[t])]) for t in d}

d_sorted = {t: [temp[1] for temp in d_tuples[t]] for t in d}


print(d_data)
print(d_tuples)
print(d_sorted)

# now set up the model...
m = pyo.ConcreteModel()

m.T = pyo.Set(initialize=d.keys())
m.I = pyo.Set(initialize=list(range(len(d[0]))))  # assumes all d[t] are equal length

m.D = pyo.Param(m.T, m.I, initialize=d_data)

m.pprint()

产量:

{(0, 0): 1.2, (0, 1): 3.1, (0, 2): 0.5, (1, 0): 9.5, (1, 1): 2.0, (1, 2): 3.1}
{0: [(0.5, 2), (1.2, 0), (3.1, 1)], 1: [(2.0, 1), (3.1, 2), (9.5, 0)]}
{0: [2, 0, 1], 1: [1, 2, 0]}
3 Set Declarations
    D_index : Size=1, Index=None, Ordered=True
        Key  : Dimen : Domain : Size : Members
        None :     2 :    T*I :    6 : {(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)}
    I : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {0, 1, 2}
    T : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    2 : {0, 1}

1 Param Declarations
    D : Size=6, Index=D_index, Domain=Any, Default=None, Mutable=False
        Key    : Value
        (0, 0) :   1.2
        (0, 1) :   3.1
        (0, 2) :   0.5
        (1, 0) :   9.5
        (1, 1) :   2.0
        (1, 2) :   3.1

4 Declarations: T I D_index D

Do all that data cleaning/sorting stuff with basic python before you build the model. Part of this answer depends on the original format of the data and the format you'd like your answer in, but I think the overall gist is clear....

Code

import pyomo.environ as pyo

d = {   0: [1.2, 3.1, 0.5],
        1: [9.5, 2.0, 3.1] }

# a good format to pass in to a parameter, tuple-indexed dictionary
d_data = {(t, i): d[t][i] for t in d for i in range(len(d[t]))}


# now let's sort the values in d for the missing info on order
d_tuples = {t: sorted([(val, idx) for idx, val in enumerate(d[t])]) for t in d}

d_sorted = {t: [temp[1] for temp in d_tuples[t]] for t in d}


print(d_data)
print(d_tuples)
print(d_sorted)

# now set up the model...
m = pyo.ConcreteModel()

m.T = pyo.Set(initialize=d.keys())
m.I = pyo.Set(initialize=list(range(len(d[0]))))  # assumes all d[t] are equal length

m.D = pyo.Param(m.T, m.I, initialize=d_data)

m.pprint()

Yields:

{(0, 0): 1.2, (0, 1): 3.1, (0, 2): 0.5, (1, 0): 9.5, (1, 1): 2.0, (1, 2): 3.1}
{0: [(0.5, 2), (1.2, 0), (3.1, 1)], 1: [(2.0, 1), (3.1, 2), (9.5, 0)]}
{0: [2, 0, 1], 1: [1, 2, 0]}
3 Set Declarations
    D_index : Size=1, Index=None, Ordered=True
        Key  : Dimen : Domain : Size : Members
        None :     2 :    T*I :    6 : {(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)}
    I : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {0, 1, 2}
    T : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    2 : {0, 1}

1 Param Declarations
    D : Size=6, Index=D_index, Domain=Any, Default=None, Mutable=False
        Key    : Value
        (0, 0) :   1.2
        (0, 1) :   3.1
        (0, 2) :   0.5
        (1, 0) :   9.5
        (1, 1) :   2.0
        (1, 2) :   3.1

4 Declarations: T I D_index D
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文