I would like to (1) use PowerModels to construct a base DCOPF model and (2) make some modifications to the model (such as allowing load shed by adding new variables and modifying cosntraints). Beginning with an instance
file = "case30.m"
nd = PowerModels.parse_file(file)
pm = instantiate_model(network_data, DCPPowerModel, PowerModels.build_opf)
# ... modify `pm` to add new variables and constraints
is there a way to retrieve constraint names for each constraint and their duals/values? I see report_duals in InfrastructureModels, but I am not sure how to obtain a DCOPF model with constraint names and duals names beginning from my nd instance. Are the arguments I can pass to instantiate_model to do this?
In most cases the constraints do not have names and they are not stored, so you cannot access their dual values. (There are some exceptions.)
See these issues:
In most cases, if you want specific control over the subproblem and the duals, you should use PowerModels to parse the data and code your own implementation.
This is exactly what motivated us to build PGLearn.jl, we similarly wanted 1) duals and 2) the ability to easily inspect/modify the formulations.
In PGLearn.jl each formulation is contained in one file – the one corresponding to PowerModels’ DCPPowerModel is DCOPF in dcp.jl. In that file you’ll find three functions – build_opf for building the model (which has all the constraints written plainly, so you can find/fork+modify them easily), extract_primal to get the primal solution, and extract_dual for the dual solution.
Here’s what your snippet would look like:
using PGLearn, PowerModels
# use PowerModels to parse the data like usual
file = "case30.m"
data = PowerModels.make_basic_network(PowerModels.parse_file(file))
# build the model
opf = PGLearn.build_opf(
PGLearn.DCOPF,
PGLearn.OPFData(data),
nothing, # optimizer constructor, e.g. Ipopt.Optimizer
)
# solve the model
PGLearn.solve!(opf)
# extract the metadata/primal/dual solutions
results = PGLearn.extract_result(opf)
The documentation is currently quite sparse, focusing mostly on the math. But if it’s a good fit for your use case I’m happy to help you get started using it!