|
49 | 49 | ) |
50 | 50 | from linopy.matrices import MatrixAccessor |
51 | 51 | from linopy.objective import Objective |
52 | | -from linopy.solvers import available_solvers, quadratic_solvers |
| 52 | +from linopy.solvers import IO_APIS, available_solvers, quadratic_solvers |
53 | 53 | from linopy.types import ( |
54 | 54 | ConstantLike, |
55 | 55 | ConstraintLike, |
@@ -79,6 +79,9 @@ class Model: |
79 | 79 | the optimization process. |
80 | 80 | """ |
81 | 81 |
|
| 82 | + solver_model: Any |
| 83 | + solver_name: str |
| 84 | + |
82 | 85 | __slots__ = ( |
83 | 86 | # containers |
84 | 87 | "_variables", |
@@ -1015,6 +1018,12 @@ def solve( |
1015 | 1018 | # clear cached matrix properties potentially present from previous solve commands |
1016 | 1019 | self.matrices.clean_cached_properties() |
1017 | 1020 |
|
| 1021 | + # check io_api |
| 1022 | + if io_api is not None and io_api not in IO_APIS: |
| 1023 | + raise ValueError( |
| 1024 | + f"Keyword argument `io_api` has to be one of {IO_APIS} or None" |
| 1025 | + ) |
| 1026 | + |
1018 | 1027 | if remote: |
1019 | 1028 | solved = remote.solve_on_remote( |
1020 | 1029 | self, |
@@ -1075,19 +1084,32 @@ def solve( |
1075 | 1084 | ) |
1076 | 1085 |
|
1077 | 1086 | try: |
1078 | | - func = getattr(solvers, f"run_{solver_name}") |
1079 | | - result = func( |
1080 | | - self, |
1081 | | - io_api=io_api, |
1082 | | - problem_fn=to_path(problem_fn), |
1083 | | - solution_fn=to_path(solution_fn), |
1084 | | - log_fn=to_path(log_fn), |
1085 | | - warmstart_fn=to_path(warmstart_fn), |
1086 | | - basis_fn=to_path(basis_fn), |
1087 | | - keep_files=keep_files, |
1088 | | - env=env, |
| 1087 | + solver_class = getattr(solvers, f"{solvers.SolverName(solver_name).name}") |
| 1088 | + # initialize the solver as object of solver subclass <solver_class> |
| 1089 | + solver = solver_class( |
1089 | 1090 | **solver_options, |
1090 | 1091 | ) |
| 1092 | + if io_api == "direct": |
| 1093 | + # no problem file written and direct model is set for solver |
| 1094 | + result = solver.solve_problem_from_model( |
| 1095 | + model=self, |
| 1096 | + solution_fn=to_path(solution_fn), |
| 1097 | + log_fn=to_path(log_fn), |
| 1098 | + warmstart_fn=to_path(warmstart_fn), |
| 1099 | + basis_fn=to_path(basis_fn), |
| 1100 | + env=env, |
| 1101 | + ) |
| 1102 | + else: |
| 1103 | + problem_fn = self.to_file(to_path(problem_fn), io_api) |
| 1104 | + result = solver.solve_problem_from_file( |
| 1105 | + problem_fn=to_path(problem_fn), |
| 1106 | + solution_fn=to_path(solution_fn), |
| 1107 | + log_fn=to_path(log_fn), |
| 1108 | + warmstart_fn=to_path(warmstart_fn), |
| 1109 | + basis_fn=to_path(basis_fn), |
| 1110 | + env=env, |
| 1111 | + ) |
| 1112 | + |
1091 | 1113 | finally: |
1092 | 1114 | for fn in (problem_fn, solution_fn): |
1093 | 1115 | if fn is not None and (os.path.exists(fn) and not keep_files): |
|
0 commit comments