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

Refactoring of linopy solvers to object oriented architecture #349

Merged
merged 21 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c9b5b34
Refactor solvers.py to object-oriented implementation of Solvers whic…
Sep 18, 2024
44b7bd4
Adjustments to execution via direct API for highs and gurobi solvers
Sep 19, 2024
d78b7fe
add unit tests for solver classes and solving from lp file and direct
Sep 19, 2024
b037aa6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 20, 2024
e3253e4
fix of path_to_str type of Path
Sep 20, 2024
593c1fe
Merge remote-tracking branch 'origin/linopy_solver_class' into linopy…
Sep 20, 2024
584fcdf
fix of path_to_str type of Path back to before
Sep 20, 2024
dc157d2
read sense and io_api from problem file
Sep 20, 2024
40de2b8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 20, 2024
53d6f3f
solve typing issues and move util methods to functions in solvers.py
Sep 23, 2024
a274b57
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 23, 2024
44f42dc
Changed architecture of direct execution and execution from problem f…
Sep 24, 2024
002564f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2024
ce0f185
get rid of unused type: ignore comment in solvers.py
Sep 24, 2024
7adf10f
refactor #349
lkstrp Oct 1, 2024
d5ba71c
Merge pull request #1 from lkstrp/linopy_solver_class
daniel-rdt Oct 15, 2024
a6b6968
Merge branch 'master' into linopy_solver_class
daniel-rdt Oct 15, 2024
0f04096
fix bug in maybe_adjust_objective_function and adjust docstrings
Oct 18, 2024
3668a42
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2024
f06bfb3
pin coptpy version away from 7.2.1 due to bug
Oct 18, 2024
34e91af
add pytest for not direct solvers NotImplementedError
Oct 18, 2024
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
2 changes: 1 addition & 1 deletion linopy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Result:

status: Status
solution: Union[Solution, None] = None
solver_model: Union[Any, None] = None
solver_model: Any = None

def __repr__(self) -> str:
solver_model_string = (
Expand Down
46 changes: 34 additions & 12 deletions linopy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
)
from linopy.matrices import MatrixAccessor
from linopy.objective import Objective
from linopy.solvers import available_solvers, quadratic_solvers
from linopy.solvers import IO_APIS, available_solvers, quadratic_solvers
from linopy.types import (
ConstantLike,
ConstraintLike,
Expand Down Expand Up @@ -79,6 +79,9 @@ class Model:
the optimization process.
"""

solver_model: Any
solver_name: str

__slots__ = (
# containers
"_variables",
Expand Down Expand Up @@ -1015,6 +1018,12 @@ def solve(
# clear cached matrix properties potentially present from previous solve commands
self.matrices.clean_cached_properties()

# check io_api
if io_api is not None and io_api not in IO_APIS:
raise ValueError(
f"Keyword argument `io_api` has to be one of {IO_APIS} or None"
)

if remote:
solved = remote.solve_on_remote(
self,
Expand Down Expand Up @@ -1075,19 +1084,32 @@ def solve(
)

try:
func = getattr(solvers, f"run_{solver_name}")
result = func(
self,
io_api=io_api,
problem_fn=to_path(problem_fn),
solution_fn=to_path(solution_fn),
log_fn=to_path(log_fn),
warmstart_fn=to_path(warmstart_fn),
basis_fn=to_path(basis_fn),
keep_files=keep_files,
env=env,
solver_class = getattr(solvers, f"{solvers.SolverName(solver_name).name}")
# initialize the solver as object of solver subclass <solver_class>
solver = solver_class(
**solver_options,
)
if io_api == "direct":
# no problem file written and direct model is set for solver
result = solver.solve_problem_from_model(
model=self,
solution_fn=to_path(solution_fn),
log_fn=to_path(log_fn),
warmstart_fn=to_path(warmstart_fn),
basis_fn=to_path(basis_fn),
env=env,
)
else:
problem_fn = self.to_file(to_path(problem_fn), io_api)
result = solver.solve_problem_from_file(
problem_fn=to_path(problem_fn),
solution_fn=to_path(solution_fn),
log_fn=to_path(log_fn),
warmstart_fn=to_path(warmstart_fn),
basis_fn=to_path(basis_fn),
env=env,
)

finally:
for fn in (problem_fn, solution_fn):
if fn is not None and (os.path.exists(fn) and not keep_files):
Expand Down
Loading