Skip to content

Commit

Permalink
Included PuLP as a third party dependency and modified it to suite Gr…
Browse files Browse the repository at this point in the history
…idCal needs
  • Loading branch information
SanPen committed Oct 10, 2024
1 parent 55f23a3 commit 8db0db1
Show file tree
Hide file tree
Showing 31 changed files with 7,950 additions and 89 deletions.
109 changes: 66 additions & 43 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/ac_linear_opf_ts_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# get the power flow options from the GUI
solver = SolverType.NONLINEAR_OPF
mip_solver = MIPSolvers.CBC
mip_solver = MIPSolvers.HIGHS
grouping = TimeGrouping.Daily
pf_options = PowerFlowOptions()

Expand Down
2 changes: 1 addition & 1 deletion examples/simple_dispatch_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# get the power flow options from the GUI
solver = SolverType.SIMPLE_OPF
mip_solver = MIPSolvers.CBC
mip_solver = MIPSolvers.HIGHS
grouping = TimeGrouping.Daily
pf_options = PowerFlowOptions()

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ networkx>=2.1
pandas>=1.1
xlwt>=1.3.0
xlrd>=1.1.0
pulp
highspy
matplotlib>=2.1.1
qtconsole>=4.3.1
Expand Down
3 changes: 0 additions & 3 deletions src/GridCal/Gui/Main/SubClasses/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,11 @@ def __init__(self, parent=None):

# the MIP combobox models assigning is done in modify_ui_options_according_to_the_engine
self.mip_solvers_dict = OrderedDict()
self.mip_solvers_dict[MIPSolvers.CBC.value] = MIPSolvers.CBC
self.mip_solvers_dict[MIPSolvers.HIGHS.value] = MIPSolvers.HIGHS
self.mip_solvers_dict[MIPSolvers.GLOP.value] = MIPSolvers.GLOP
self.mip_solvers_dict[MIPSolvers.SCIP.value] = MIPSolvers.SCIP
self.mip_solvers_dict[MIPSolvers.CPLEX.value] = MIPSolvers.CPLEX
self.mip_solvers_dict[MIPSolvers.GUROBI.value] = MIPSolvers.GUROBI
self.mip_solvers_dict[MIPSolvers.XPRESS.value] = MIPSolvers.XPRESS
self.mip_solvers_dict[MIPSolvers.PDLP.value] = MIPSolvers.PDLP

# opf solvers dictionary
self.nodal_capacity_methods_dict = OrderedDict()
Expand Down
8 changes: 2 additions & 6 deletions src/GridCalEngine/Compilers/circuit_to_newton_pa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,11 +1199,9 @@ def get_newton_pa_nonlinear_opf_options(pf_opt: PowerFlowOptions,
q_control_dict = {False: npa.ReactivePowerControlMode.NoControl,
True: npa.ReactivePowerControlMode.Direct}

solver_dict = {MIPSolvers.CBC: npa.LpSolvers.Highs,
MIPSolvers.HIGHS: npa.LpSolvers.Highs,
solver_dict = {MIPSolvers.HIGHS: npa.LpSolvers.Highs,
MIPSolvers.XPRESS: npa.LpSolvers.Xpress,
MIPSolvers.CPLEX: npa.LpSolvers.CPLEX,
MIPSolvers.GLOP: npa.LpSolvers.Highs,
MIPSolvers.SCIP: npa.LpSolvers.Highs,
MIPSolvers.GUROBI: npa.LpSolvers.Gurobi}

Expand All @@ -1230,11 +1228,9 @@ def get_newton_pa_linear_opf_options(opf_opt: OptimalPowerFlowOptions,
:param area_dict:
:return:
"""
solver_dict = {MIPSolvers.CBC: npa.LpSolvers.Highs,
MIPSolvers.HIGHS: npa.LpSolvers.Highs,
solver_dict = {MIPSolvers.HIGHS: npa.LpSolvers.Highs,
MIPSolvers.XPRESS: npa.LpSolvers.Xpress,
MIPSolvers.CPLEX: npa.LpSolvers.CPLEX,
MIPSolvers.GLOP: npa.LpSolvers.Highs,
MIPSolvers.SCIP: npa.LpSolvers.Scip,
MIPSolvers.GUROBI: npa.LpSolvers.Gurobi}

Expand Down
2 changes: 1 addition & 1 deletion src/GridCalEngine/Simulations/NTC/ntc_opf.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ def add_linear_node_balance(t_idx: int,

def run_linear_ntc_opf_ts(grid: MultiCircuit,
time_indices: Union[IntVec, None],
solver_type: MIPSolvers = MIPSolvers.CBC,
solver_type: MIPSolvers = MIPSolvers.HIGHS,
zonal_grouping: ZonalGrouping = ZonalGrouping.NoGrouping,
skip_generation_limits: bool = False,
consider_contingencies: bool = False,
Expand Down
2 changes: 1 addition & 1 deletion src/GridCalEngine/Simulations/OPF/linear_opf_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ def add_hydro_formulation(t: Union[int, None],

def run_linear_opf_ts(grid: MultiCircuit,
time_indices: Union[IntVec, None],
solver_type: MIPSolvers = MIPSolvers.CBC,
solver_type: MIPSolvers = MIPSolvers.HIGHS,
zonal_grouping: ZonalGrouping = ZonalGrouping.NoGrouping,
skip_generation_limits: bool = False,
consider_contingencies: bool = False,
Expand Down
2 changes: 1 addition & 1 deletion src/GridCalEngine/Simulations/OPF/opf_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self,
solver: SolverType = SolverType.LINEAR_OPF,
time_grouping: TimeGrouping = TimeGrouping.NoGrouping,
zonal_grouping: ZonalGrouping = ZonalGrouping.NoGrouping,
mip_solver=MIPSolvers.CBC,
mip_solver=MIPSolvers.HIGHS,
power_flow_options: Union[None, PowerFlowOptions] = None,
consider_contingencies=False,
contingency_groups_used: List[ContingencyGroup] = (),
Expand Down
22 changes: 22 additions & 0 deletions src/GridCalEngine/ThirdParty/pulp/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2002-2005, Jean-Sebastien Roy
Modifications Copyright (c) 2007- Stuart Anthony Mitchell

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

39 changes: 39 additions & 0 deletions src/GridCalEngine/ThirdParty/pulp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# PuLP : Python LP Modeler
# Version 1.20

# Copyright (c) 2002-2005, Jean-Sebastien Roy ([email protected])
# Modifications Copyright (c) 2007- Stuart Anthony Mitchell ([email protected])
# $Id: __init__.py 1791 2008-04-23 22:54:34Z smit023 $

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:

# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
Module file that imports all of the pulp functions
Copyright 2007- Stuart Mitchell ([email protected])
"""
from .constants import VERSION

from GridCalEngine.ThirdParty.pulp.pulp import *
from GridCalEngine.ThirdParty.pulp.apis import *
from GridCalEngine.ThirdParty.pulp.utilities import *
from GridCalEngine.ThirdParty.pulp.constants import *

__version__ = VERSION
137 changes: 137 additions & 0 deletions src/GridCalEngine/ThirdParty/pulp/apis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import json
from GridCalEngine.ThirdParty.pulp.apis.cplex_api import *
from GridCalEngine.ThirdParty.pulp.apis.gurobi_api import *
from GridCalEngine.ThirdParty.pulp.apis.scip_api import *
from GridCalEngine.ThirdParty.pulp.apis.xpress_api import *
from GridCalEngine.ThirdParty.pulp.apis.highs_api import *
from GridCalEngine.ThirdParty.pulp.apis.copt_api import *
from GridCalEngine.ThirdParty.pulp.apis.core import *

_all_solvers = [
CPLEX_CMD,
CPLEX_PY,
GUROBI,
GUROBI_CMD,
XPRESS,
XPRESS_CMD,
XPRESS_PY,
SCIP_CMD,
FSCIP_CMD,
SCIP_PY,
HiGHS,
HiGHS_CMD,
COPT,
COPT_DLL,
COPT_CMD,
]

# Default solver selection
LpSolverDefault = HiGHS()


def setConfigInformation(**keywords):
"""
set the data in the configuration file
at the moment will only edit things in [locations]
the keyword value pairs come from the keywords dictionary
"""
# TODO: extend if we ever add another section in the config file
# read the old configuration
config = Parser()
config.read(config_filename)
# set the new keys
for key, val in keywords.items():
config.set("locations", key, val)
# write the new configuration
fp = open(config_filename, "w")
config.write(fp)
fp.close()


def configSolvers():
"""
Configure the path the the solvers on the command line
Designed to configure the file locations of the solvers from the
command line after installation
"""
configlist = [
(cplex_dll_path, "cplexpath", "CPLEX: "),
(coinMP_path, "coinmppath", "CoinMP dll (windows only): "),
]
print(
"Please type the full path including filename and extension \n"
+ "for each solver available"
)
configdict = {}
for default, key, msg in configlist:
value = input(msg + "[" + str(default) + "]")
if value:
configdict[key] = value
setConfigInformation(**configdict)


def getSolver(solver, *args, **kwargs):
"""
Instantiates a solver from its name
:param str solver: solver name to create
:param args: additional arguments to the solver
:param kwargs: additional keyword arguments to the solver
:return: solver of type :py:class:`LpSolver`
"""
mapping = {k.name: k for k in _all_solvers}
try:
return mapping[solver](*args, **kwargs)
except KeyError:
raise PulpSolverError(
"The solver {} does not exist in PuLP.\nPossible options are: \n{}".format(
solver, mapping.keys()
)
)


def getSolverFromDict(data):
"""
Instantiates a solver from a dictionary with its data
:param dict data: a dictionary with, at least an "solver" key with the name
of the solver to create
:return: a solver of type :py:class:`LpSolver`
:raises PulpSolverError: if the dictionary does not have the "solver" key
:rtype: LpSolver
"""
solver = data.pop("solver", None)
if solver is None:
raise PulpSolverError("The json file has no solver attribute.")
return getSolver(solver, **data)


def getSolverFromJson(filename):
"""
Instantiates a solver from a json file with its data
:param str filename: name of the json file to read
:return: a solver of type :py:class:`LpSolver`
:rtype: LpSolver
"""
with open(filename) as f:
data = json.load(f)
return getSolverFromDict(data)


def listSolvers(onlyAvailable=False):
"""
List the names of all the existing solvers in PuLP
:param bool onlyAvailable: if True, only show the available solvers
:return: list of solver names
:rtype: list
"""
result = []
for s in _all_solvers:
solver = s(msg=False)
if (not onlyAvailable) or solver.available():
result.append(solver.name)
del solver
return result
Loading

0 comments on commit 8db0db1

Please sign in to comment.