KeyError
AnsweredHello,
I am writing the code for the optimization problem, but here is the error message I have:
KeyError: 'L4'
Please help me figure out how to solve it.
import gurobipy as gp
from gurobipy import GRB
# Data
generating_units = {
'g1': {'node': 'n1', 'generator_capacity': 300, 'production_cost': 18},
'g2': {'node': 'n2', 'generator_capacity': 250, 'production_cost': 25},
'g3': {'node': 'n3', 'generator_capacity': 400, 'production_cost': 16},
'g4': {'node': 'n4', 'generator_capacity': 300, 'production_cost': 32},
'g5': {'node': 'n5', 'generator_capacity': 150, 'production_cost': 35}
}
demands = {
'd1': {'node': 'n1', 'demand': 200, 'shedding_cost': 18},
'd2': {'node': 'n4', 'demand': 150, 'shedding_cost': 25},
'd3': {'node': 'n5', 'demand': 100, 'shedding_cost': 16},
'd4': {'node': 'n6', 'demand': 200, 'shedding_cost': 32}
}
existing_lines = {
'l1': {'sender': 'n1', 'receiver': 'n2', 'susceptance': 500, 'line_capacity': 150},
'l2': {'sender': 'n1', 'receiver': 'n3', 'susceptance': 500, 'line_capacity': 150},
'l3': {'sender': 'n4', 'receiver': 'n5', 'susceptance': 500, 'line_capacity': 150}
}
prospective_lines = {
'L4': {'sender': 'n2', 'receiver': 'n3', 'susceptance': 500, 'line_capacity': 150, 'line_build_cost': 700000},
'L5': {'sender': 'n2', 'receiver': 'n4', 'susceptance': 500, 'line_capacity': 200, 'line_build_cost': 1400000},
'L6': {'sender': 'n3', 'receiver': 'n4', 'susceptance': 500, 'line_capacity': 200, 'line_build_cost': 1800000},
'L7': {'sender': 'n3', 'receiver': 'n6', 'susceptance': 500, 'line_capacity': 200, 'line_build_cost': 1600000},
'L8': {'sender': 'n4', 'receiver': 'n6', 'susceptance': 500, 'line_capacity': 150, 'line_build_cost': 800000},
'L9': {'sender': 'n5', 'receiver': 'n6', 'susceptance': 500, 'line_capacity': 150, 'line_build_cost': 700000}
}
investment_budget = 3000000
sigma = 8760
planning_horizon = 20
base_power = 1
base_voltage = 1
# Create a model
model = gp.Model("TEP")
# Decision variables
build_line = model.addVars(transmission_lines_prospective, vtype=GRB.BINARY, name="Build_Line")
power_generated = model.addVars(generating_units, vtype=GRB.CONTINUOUS, name="Power_Generated")
power_flow_existing = model.addVars(existing_lines, vtype=GRB.CONTINUOUS, name="{Power_flow_Existing}")
power_flow_candidate = model.addVars(prospective_lines, vtype=GRB.CONTINUOUS, name="Power_flow_candidate")
power_demand = model.addVars(demands, vtype=GRB.CONTINUOUS, name="Power_Demand")
load_shedding = model.addVars(demands, vtype=GRB.CONTINUOUS, name="Load_Shedding")
model.update()
#Objective Function
investment_cost = gp.quicksum(line_build_cost[line] * build_line[line] for line in prospective_lines)
operating_cost = gp.quicksum(production_cost[generator] * generator_capacity[generator] for generator in generating_units)
load_shedding_cost = gp.quicksum(shedding_cost[demand] * demands[demand] for demand in demands)
model.setObjective((investment_cost + sigma(operating_cost + shedding_cost), GRB.MINIMIZE))
# Constraints
# Budget constraint
# model.addConstr(gp.quicksum(prospective_lines[line]['line_build_cost'] * build_line[line] for line in prospective_lines) <= investment_budget)
model.addConstr(gp.quicksum(build_line[line] * prospective_lines[line] for line in prospective_lines) <= investment_budget, "Budget")
0
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Our Tutorial: Preparing a Minimal Reproducible Example – Gurobi Help Center discusses how to identify the cause of a Key error. If this does not help, please make your example reproducible.
0
Post is closed for comments.
Comments
2 comments