Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Apologies in advance if this is simple - which I suspect it is - as I've searched and failed to find an example of this!

I am constructing an energy system dispatch model in Pyomo, and have a version of this running. I am defining a new variable, "SystemShortRunMarginalCost", which should be defined as the maximum of "ActiveShortRunMarginalCostByGenerator", as follows:

def SystemShortRunMarginalCost_rule(model,h):
    max(model.ActiveShortRunMarginalCostByGenerator[g,h] for g in model.GeneratorName) == SystemShortRunMarginalCost[h]
model.SystemShortRunMarginalCostHourly = Constraint(model.Hour, rule=SystemShortRunMarginalCost_rule)

Is there some basic syntax I'm missing here? I get the following error message:

ERROR: Rule failed when generating expression for constraint
    SystemShortRunMarginalCostHourly with index 1: NameError: name
    'SystemShortRunMarginalCost' is not defined
ERROR: Constructing component 'SystemShortRunMarginalCostHourly' from
    data=None failed:
        NameError: name 'SystemShortRunMarginalCost' is not defined
[    0.10] Pyomo Finished
ERROR: Unexpected exception while running model:
        name 'SystemShortRunMarginalCost' is not defined

Thanks.

UPDATED

So I have now added amended the objective function as suggested, and amended the constraint code as follows:

def SystemShortRunMarginalCost_rule(model,g,h):
    return SystemShortRunMarginalCost[h] >= model.ActiveShortRunMarginalCostByGenerator[g,h]
model.SystemShortRunMarginalCostHourly = Constraint(model.GeneratorName, model.Hour, rule=SystemShortRunMarginalCost_rule)

I now get a different error, as follows:

ERROR: Rule failed when generating expression for constraint
    SystemShortRunMarginalCostHourly with index ('Wind1', 1): NameError: name
    'SystemShortRunMarginalCost' is not defined
ERROR: Constructing component 'SystemShortRunMarginalCostHourly' from
    data=None failed:
        NameError: name 'SystemShortRunMarginalCost' is not defined
[    0.11] Pyomo Finished
ERROR: Unexpected exception while running model:
        name 'SystemShortRunMarginalCost' is not defined
question from:https://stackoverflow.com/questions/66052604/pyomo-defining-a-variable-using-a-maximum-of-other-variables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

max() is not a linear function, so you won't be able to use that inside of a constraint. So pyomo is barfing when you try to make the constraint. If you just want to capture the highest marginal cost here, you could easily reformulate to making your SystemShortRunMarginalCost` variable greater than each of the things you want it to capture the max of and then minimizing it in your objective function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...