I am a new Pyomo/Python user. In the process of learning pyomo, I need to solve some optimization problems.
I need to make a balance between the total generation and demand (800 MW). If I want to find the minimum value of 'Sum(ap^2+bp+c))'.And this is the mathematic model:mathematics model of this problem
How can I construct an abstract model that I can choose the value of a, b, c from the same row in the table below? If I set the 'sets' individually, then the value of abc will come from the different row which cannot satisfy the formula. And how do I set a random value between the Pmin and Pmax? Use two constraints to limit the value? It really makes me confused.
from pyomo.environ import *
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import random
model = AbstractModel()
model.J = Set()
model.A = Param(model.J)
model.B = Param(model.J)
model.C = Param(model.J)
model.P_min = Param(model.J)
model.P_max= Param(model.J)
model.P = Var(model.J, domain=NonNegativeReals)
def obj_expression(model):
return sum(model.A* model.P**2+model.B*model.P+model.C for j in model.J)
model.OBJ = Objective(rule=obj_expression, sense=minimize)
# Upper bounds rule
def upper_bounds_rule(model,j):
return model.P[j] <= model.P_max[j]
model.upper = Constraint(model.J,rule=upper_bounds_rule)
# Lower bounds rule
def lower_bounds_rule(model, j):
return model.P[j] >= model.P_min[j]
model.lower = Constraint(model.J, rule=lower_bounds_rule)
def rule_eq1(model,j):
return sum(model.P[j] >= 800;
model.eq1 = Constraint(model.J, rule=rule_eq1)
opt = SolverFactory('Ipopt')
instance = model.create_instance("E:/pycharm_project/PD/pd.dat")
results = opt.solve(instance) # solves and updates instance
#data file
# Creating the set J:
set J := g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 ;
# Creating Parameters A, B, C, P_min, P_max:
param : A B C P_min P_max:=
g1 0.0148 12.1 82 80 200
g2 0.0289 12.6 49 120 320
g3 0.0135 13.2 100 50 150
g4 0.0127 13.9 105 250 520
g5 0.0261 13.5 72 80 280
g6 0.0212 15.4 29 50 150
g7 0.0382 14.0 32 30 120
g8 0.0393 13.5 40 30 110
g9 0.0396 15.0 25 20 80
g10 0.0510 14.3 15 20 60
;
Can you help me with this? Thanks!
Vivi
I think you should structure your .dat files in accordance with what is described in the Pyomo documentation here.
I believe something like this would work for you for parameters A, B, C:
# Creating the set J:
set J := g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 ;
# Creating Parameters A, B, C:
param : A B C :=
g1 0.0148 12.1 82
g2 0.0289 12.6 49
g3 0.0135 13.2 100
g4 0.0127 13.9 105
g5 0.0261 13.5 72
g6 0.0212 15.4 29
g7 0.0382 14.0 32
g8 0.0393 13.5 40
g9 0.0396 15.0 25
g10 0.0510 14.3 15
;
Now I am not sure about Pmin
and Pmax
, since in your model they seem to be 2-dimensional, while in your data, they seem to only be 1-dimensional. But generally, you can follow the instructions in the link above to create your .dat files.
As for your second question, I am not sure I understand you correctly, but you refer to the P_min <= P <= P_max
constraint in the mathematical model description, correct?
Then, for the P_min <= P
part you need to slightly change your current constraint:
def lower_bounds_rule(model, i, j):
return model.P[i,j] >= model.P_min[i,j]
model.lower = Constraint(model.i,model.j,rule=lower_bounds_rule)