Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable AC polar formulation for OPF #122

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/gurobi_optimods/opf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@

import logging

import gurobipy as gp

from gurobi_optimods.opf import converters, grbformulator, violations
from gurobi_optimods.utils import optimod

logger = logging.getLogger(__name__)

GUROBI_MAJOR_VERSION, *_ = gp.gurobi.version()


@optimod()
def solve_opf(
Expand Down Expand Up @@ -79,18 +83,30 @@ def solve_opf(
opftype = "ac"
useef = True
usejabr = True
polar = False
default_solver_params = {"MIPGap": 1e-3, "OptimalityTol": 1e-3}
# Exact polar AC
elif opftype.lower() == "acpolar":
opftype = "ac"
useef = False
usejabr = False
polar = True
default_solver_params = {"MIPGap": 1e-3, "OptimalityTol": 1e-3}
if GUROBI_MAJOR_VERSION >= 11:
default_solver_params["FuncNonLinear"] = 1
# AC relaxation using the JABR inequality
elif opftype.lower() == "acrelax":
opftype = "ac"
useef = False
usejabr = True
polar = False
default_solver_params = {"MIPGap": 1e-3, "OptimalityTol": 1e-3}
# DC linear approximation (ef & jabr are irrelevant)
elif opftype.lower() == "dc":
opftype = "dc"
useef = False
usejabr = False
polar = False
default_solver_params = {"MIPGap": 1e-4, "OptimalityTol": 1e-4}
else:
raise ValueError(f"Unknown opftype '{opftype}'")
Expand All @@ -105,8 +121,8 @@ def solve_opf(
branchswitching=branch_switching,
usemipstart=use_mip_start,
minactivebranches=min_active_branches,
polar=False,
ivtype="aggressive",
polar=polar,
ivtype="aggressive", # ignored, no IV formulation used
useactivelossineqs=False,
)

Expand Down
1 change: 1 addition & 0 deletions tests/opf/test_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def test_ivopf(self):
self.assertLess(abs(solution["branch"][4]["Qt"] + 18.431373), 1e1)


@unittest.skip
class TestFingerprints(unittest.TestCase):
# Check model fingerprints for some specific cases. Useful while
# refactoring the code, but
Expand Down
22 changes: 22 additions & 0 deletions tests/opf/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from gurobi_optimods.datasets import load_opf_example
from gurobi_optimods.opf import solve_opf

GUROBI_MAJOR_VERSION, *_ = gp.gurobi.version()


def size_limited_license():
with gp.Env(params={"OutputFlag": 0}) as env, gp.Model(env=env) as model:
Expand Down Expand Up @@ -179,6 +181,26 @@ def test_ac_branchswitching_infeasible(self):
with self.assertRaisesRegex(ValueError, "Infeasible model"):
solve_opf(self.case, opftype="AC", branch_switching=True)

@unittest.skipIf(GUROBI_MAJOR_VERSION >= 11, "v10 specific test case")
def test_ac_polar_piecewise(self):
# We need to be fairly forgiving with v10 PWL approximations
kwargs = dict(
opftype="acpolar",
solver_params={"MIPGap": 2e-2, "TimeLimit": 60},
)
solution = solve_opf(self.case, **kwargs)
self.assertLess(solution["f"], 5450.0)

@unittest.skipIf(GUROBI_MAJOR_VERSION < 11, "v11 specific test case")
def test_ac_polar_nonlinear(self):
# v11 brings home the bacon
kwargs = dict(
opftype="acpolar",
solver_params={"MIPGap": 1e-3, "TimeLimit": 60},
)
solution = solve_opf(self.case, **kwargs)
self.assertLess(solution["f"], 5300.0)


class TestComputeVoltageAnglesBug(unittest.TestCase):
# Reordering the buses on input (which does not change the network
Expand Down