Hello Luis,
I hope you are doing well.
You have two options:
1- You can define the lines based on the BUS sets
2- You can define the lines based on their unique sets.
For case 1, please refer to
and go to chapter 4. Please search "model.CONEX".
For case 2, you can write as follows:
MP0.LINE = Set(initialize=[f'LN{i+1}' for i in range(32)], doc='Number of Transmission Lines')
file_path = "your_file.xlsx"
sheet_name = "your_sheet_name"
df = pd.read_excel(file_path, sheet_name=sheet_name)
line_data = {}
for _, row in df.iterrows():
line_id = row["LineID"]
from_bus = row["From"]
to_bus = row["To"]
x_val = row["X"]
limit_val = row["LIMIT"]
line_data[line_id] = {
"from" : from_bus,
"to" : to_bus,
"X" : x_val,
"LIMIT":limit_val
}
MP0.from_bus = Param(MP0.LINE, initialize=lambda m, ln: line_data[ln]["from"], within=Any)
MP0.to_bus = Param(MP0.LINE, initialize=lambda m, ln: line_data[ln]["to"], within=Any)
MP0.X = Param(MP0.LINE, initialize=lambda m, ln: line_data[ln]["X"], doc="Reactance of each line")
MP0.LIMIT = Param(MP0.LINE, initialize=lambda m, ln: line_data[ln]["LIMIT"], doc="Thermal limit of each line")
MP0.Pij = Var(MP0.LINE, MP0.T, domain=Reals, doc='Optimal Transmission DC Power Flow')
Finally, you can use the "fix" keyword to fix a variable:
for i in MP0.I:
for t in MP0.T:
MP0.Pij[i, t].fix(10)