简体   繁体   English

Pyomo:一组一组

[英]Pyomo: a Set of a Set

I'm new to Pyomo and Python.我是 Pyomo 和 Python 的新手。 I was wondering if someone could help me with the case of a Set Q that is based on a Set P.我想知道是否有人可以帮助我解决基于 Set P 的 Set Q 的情况。

model.P = pyo.Set(initialize=list(df.iloc[:,0]))
model.Q = pyo.Set(model.P)

Model.P stands for the products I read in out of a file. Model.P 代表我从文件中读入的产品。 Model.Q is the number of possible quantities for every product. Model.Q 是每种产品的可能数量。 So, for example: product "AAA" has 3 different quantities, than I would like to set model.Q["AAA"] equal to the range(1,3).因此,例如:产品“AAA”有 3 个不同的数量,而不是我想设置 model.Q["AAA"] 等于范围(1,3)。 Product "BBB" has 7 different quantities, than I would like to set model.Q["BBB"] equal to the range(1,7).产品“BBB”有 7 个不同的数量,而不是我想设置 model.Q["BBB"] 等于范围(1,7)。

The number of quantities are read into a list [3, 7, ...]数量的数量被读入列表 [3, 7, ...]

When I want to initialize a parameter like the following, it gives an error like below the example.当我想初始化如下所示的参数时,它会给出如下示例所示的错误。

# Setup time
model.st = pyo.Param(model.P,model.Q,initialize=setupTimes,domain=pyo.NonNegativeReals)

TypeError: Cannot apply a Set operator to an indexed Set component (Q)

How can I implement this?我该如何实施? Thanks for the help.谢谢您的帮助。

I really do not know what you intend to do with that set, but I feel there may be other ways to efficiently model your problem with just a set and a parameter of quantities for each set.我真的不知道你打算用那一套做什么,但我觉得可能有其他方法可以有效地解决你的问题,只需一套和每套的数量参数。

However, to do what you want, one way I suggest is to create a second set "model.PP" which has the range(max(list)) as elements.但是,要执行您想要的操作,我建议的一种方法是创建第二组“model.PP”,其中包含 range(max(list)) 作为元素。 Then you create a list of tuples mapping each of model.P to the elements of model.PP appropriately which you then use to initilise model.Q ie.然后,您创建一个元组列表,将每个 model.P 映射到 model.PP 的元素,然后使用它来启动 model.Q。

model.Q = pyo.Set(within=model.P*model.PP, initialise=[("AAA",1), ("AAA",2). . .])

You can create that new list with a for loop.您可以使用 for 循环创建该新列表。

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

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