How can I set a value of an indexed variable? - Pyomo

Viewed 1228

I'm working on a project related with AC OPF (Optimal Power Flow) and I was trying to solve a problem in python, using pyomo. There are 3 buses and the bus voltage and bus angle are limited. However, the 1st bus must have a voltage=1 and an angle=0.

So, I tried this:

model.busvoltage = Var(model.bus, initialize=1, bounds=(0.95, 1.05), doc='Bus Voltage')
model.busvoltage[1].fixed=True
model.busangle = Var(model.bus, initialize=0, bounds=(-3.14, 3.14), doc='Bus angle')
model.busangle[1].fixed=True

The problem is that I just want to set the busvoltage and busangle for the first bus, without initializing the other ones with those values. I don't know if this is important to write, but I'm using ipopt as solver.

(This is my first time programming in Python) Any help would be appreciated!

2 Answers

You're after the .value attribute of the variable. Furthermore, setting the value of a variable and fixing it at the same time can be simplified to calling .fix():

model.busvoltage = Var(model.bus, bounds=(0.95, 1.05), doc='Bus Voltage')
model.busvoltage[1].fixed = True
model.busvoltage[1].value = 1

model.busangle = Var(model.bus, bounds=(-3.14, 3.14), doc='Bus angle')
model.busangle[1].fix(0)

Have you considered initializing the value in a constraint rather than in the Var definition?

model.busvoltage = Var(model.bus, bounds=(0.95, 1.05), doc='Bus Voltage')
model.busvoltage[1].fixed=True

def bus_voltage_1_value_constraint(model, b):
    if b == 1:
        return model.busvoltage[b] == 1
    return Constraint.Skip

model.bus_voltage_1_value = Constraint(model.bus, rule=bus_voltage_1_value_constraint)
Related