Pyomo: Multi-objective optimization

1.8k views Asked by At

I'm using this code to solve a multi-objective optimization model(power dispatch) and trying to adapt an example in my code.

The example:https://stackoverflow.com/questions/50742999/multi-objective-optimization-example-pyomo.

And I am trying to skip the 'inefficient Pareto-front' part and plot 'efficient Pareto-front' directly.

The first tab can run properly and generate Cost_min, Cost_max, Emission_min, Emission_max.

from pyomo.environ import *
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import random

# create a model
model = AbstractModel()

# declare decision variables

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(mutable=True)

# declare constraints

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

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

# declare objective_cost

def obj_cost(model):
     return sum(model.A[j]* model.P[j] ** 2 + model.B[j] * model.P[j] + model.C[j] for j in model.J) 
model.cost= Objective(rule=obj_cost, sense=minimize)
# declare objective_emission
def obj_emission(model):
     return sum(model.E[j]* model.P[j] ** 2 + model.D[j] * model.P[j] + model.F[j] for j in model.J) 
model.emission= Objective(rule=obj_emission, sense=minimize)

# deactivate model.emission  calculate emission_max,cost_min

model.emission.deactivate()
instance = model.create_instance("E:\pycharm_project\PD\END-10units.dat")
opt = SolverFactory('Ipopt')
results = opt.solve(instance)
for i in instance.J:
    print(i,value(instance.P[i]))
print( 'cost = ' + str(value(instance.cost)) )
print( 'emission = ' + str(value(instance.emission)) )
emission_max = value(instance.emission)
cost_min = value(instance.cost)


# ## max emission  deactivate model.cost    calculate emission_min,cost_max

model.emission.activate()
model.cost.deactivate()
instance = model.create_instance("E:\pycharm_project\PD\END-10units.dat")
results = opt.solve(instance)


for i in instance.J:
    print(i,value(instance.P[i]))
print( 'cost = ' + str(value(instance.cost)) )
print( 'emission = ' + str(value(instance.emission)) )
emission_min = value(instance.emission)
cost_max = value(instance.cost)

After running the code in this tab, no errors were generated. But when outputting a Pareto-front, there is only one dot shown in this.

# ## apply normal $\epsilon$-Constraint

model.emission.deactivate()
model.cost.activate()
model.emission_value = Param(initialize=0, mutable=True)

def c_epsilon(model):
    return model.emission <= model.emission_value
model.C_epsilon = Constraint(rule=c_epsilon)
results = opt.solve(instance)

print('Each iteration will keep emission lower than some values between emission_min and emission_max, so ['       + str(emission_min) + ', ' + str(emission_max) + ']')

n = 5
step = int((emission_max - emission_min) / n)
steps = list(range(int(emission_min), int(emission_max), step)) + [emission_max]


# ## apply augmented $\epsilon$-Constraint

# max   emission + delta*epsilon <br>
#  s.t. emission - s = emission_value

model.del_component(model.cost)
model.del_component(model.emission)
model.del_component(model.C_epsilon)

model.delta = Param(initialize=0.00001)

model.s = Var(within=NonNegativeReals)

def obj_cost_1(model):
    return sum(model.cost+model.delta * model.s)
model.obj_cost_1 = Objective(rule=obj_cost_1, sense=maximize)

def C_e(model):
    return model.emission-model.s==model.emission_value
model.C_e= Constraint(rule=C_e)

cost_l = []
emission_l = []
for i in steps:
    model.emission_value = i
    results = opt.solve(instance)  
    cost_l.append(value(instance.cost))
    emission_l.append(value(instance.emission))
plt.plot(cost_l,emission_l,'o-.');
plt.title('efficient Pareto-front');
plt.grid(True);
plt.show()

The result is shown below. I don't know why this can't output a correct Pareto chart.I don't know which step of the code is wrong.

efficient Pareto-front Can anyone helps me with this code? Thanks.Vivi

1

There are 1 answers

5
AirSquid On BEST ANSWER

A couple things.... :)

What's wrong:

Inside of your loop, the only thing that affects the model is your assignment of a new value to model.e What is that? I think it is a typo and you are mistakenly just declaring a new and unused model component instance variable called e. This is why you get no different values out. I think you want to change to model.emission.

Additionally, I wouldn't try for 1000 solves in the first go, just try 5.

What should be cleaned up:

you are instantiating a new solver in your loop. Not needed. You don't need 1000 different solvers, just re-solve. You already have a solver declared earlier.

adding some comments to your code for clarity will not catch your fingers on fire, and will help in T/S, along with a bit of re-organization.

Additionally, model.A model.B model.C ... isn't very informative. I'd suggest clearer variable names if that can be done.