ViviAaron
ViviAaron

Reputation: 108

Pyomo: Build an abstract model

I am trying to do optimization with Pyomo (solver Ipopt).

I have 2 sets (j for the numbers of generator and t for times),6 indexed parameters (A,B,C,Pmin,Pmax :indexed on model.J, Demand indexed on model.T)

What I want to do is generate 24 different costs base on 24 different demands.

After I ran the code, this error appeared:

TypeError: P_LoadgenBalance() takes 1 positional argument but 2 were given.

I don’t know why this error occurs,Hope you can help me with it. Thanks for your help!

Vivi

from pyomo.environ import *
import matplotlib.pyplot as plt
import numpy as np
# create a model
model = AbstractModel()

#     There are ten generators with different values of ABC.
#     Minimizing the costs and find the optimal dispatch for 24 different demands changed with time  
#     obj:Cost= sum(ap^2 +bp+c)
#     constraints: sum_i P(i,t)  >= load(t) , (Pmin =< P =< Pmax).

# declare decision variables

model.M = Param(mutable=True)
model.T = RangeSet(model.M)
model.N = Param(mutable=True)
model.J = RangeSet(model.N)
model.A = Param(model.J)
model.B = Param(model.J)
model.C = Param(model.J)
model.D = Param(model.J)
model.E = Param(model.J)
model.F = Param(model.J)
model.P_min = Param(model.J, within=PositiveReals)
model.P_max = Param(model.J, within=PositiveReals)
model.demand = Param(model.T)
model.emission_value = Param(initialize=1000000, mutable=True)

# declare constraints_Pbounds (Pmin =< P =< Pmax)

def Pbounds(model, j,t):
    return (model.P_min[j], model.P_max[j])
model.P = Var(model.J, model.T, bounds=Pbounds, domain=NonNegativeReals)

# declare constraints_P_LoadgenBalance  ( sum P >= demand)

def P_LoadgenBalance(model,t):
    return sum(model.P[j,t] for j in model.J  ) >= model.demand[t]
model.P_LoadgenBalance = Constraint(model.T, rule=P_LoadgenBalance)


# declare objective_cost

def obj_cost(model):
     return sum(model.A[j]* model.P[j,t] ** 2 + model.B[j] * model.P[j,t] + model.C[j] for j in model.J for t in model.T) 
model.cost= Objective(rule=obj_cost, sense=minimize)

# declare objective_emission

def obj_emission(model):
      return sum(model.E[j]* model.P[j,t] ** 2 + model.D[j] * model.P[j,t] + model.F[j] for j in model.J for t in model.T) 
model.emission= Objective(rule=obj_emission, sense=minimize)
model.emission.deactivate()
opt = SolverFactory('Ipopt')
instance = model.create_instance("E:\pycharm_project\END-10units.dat")
results = opt.solve(instance)

print(value(instance.cost)

Data file

param M:=24;
param N:=10;
# Creating Parameters A, B, C,D,E,F, P_min, P_max:
param : A B C D E F P_min P_max:=
1   0.0148  12.1    82  2.15    3.59    -11.4   80  200
2   0.0289  12.6    49  3.63    2.02    -3.65   120 320
3   0.0135  13.2    100 3.3     4.7     -4.04   50  150
4   0.0127  13.9    105 3.73    1.61    -13.44  250 520
5   0.0261  13.5    72  2.27    2.29    -4.41   80  280
6   0.0212  15.4    29  2.37    2.77    -8.61   50  150
7   0.0382  14      32  2.03    4.86    -8.91   30  120
8   0.0393  13.5    40  2.4     3.32    -31.74  30  110
9   0.0396  15      25  2.5     4.03    -19.14  20  80
10  0.051   14.3    15  3.43    3.27    -21.02  20  60

param demand:=
1 600
2 650
3 680
4 651
5 630
6 650
7 810
8 820
9 883
10 893
11 888
12 901
13 892
14 875
15 843
16 877
17 880
18 904
19 865
20 855
21 766
22 733
23 688
24 654;

Upvotes: 0

Views: 576

Answers (1)

You're not defining t in the function P_LoadgenBalance, so t is the other expected argument. Code should be like:

def P_LoadgenBalance(model ,t):

For storage the individual cost, you must create a variable for it as follows with the new objective function:

# Cost Variable to store individual cost per j through t
model.X = Var(model.J, model.T, domain=NonNegativeReals)


# Cost Function

def cost(model, j, t):
    return model.X[j, t]  == model.A[j] * model.P[j, t] ** 2 + model.B[j] * model.P[j, t] + model.C[j]


model.cost = Constraint(model.T, Model.J, rule=cost)

# Objective
def obj_cost(model):
    return sum(model.X[j, t] for j in model.J for t in model.T)


model.total_cost = Objective(rule=obj_cost, sense=minimize)

Upvotes: 1

Related Questions