Skip to content

Commit

Permalink
Refactor solve
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliette-Gerbaux committed Aug 2, 2024
1 parent f1f08e8 commit d5f1535
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 38 deletions.
18 changes: 16 additions & 2 deletions src/andromede/thermal_heuristic/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from math import ceil
from pathlib import Path
from typing import List, Optional
import ortools.linear_solver.pywraplp as pywraplp

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -68,14 +69,21 @@ def __init__(
self.id_thermal_cluster_model = id_thermal_cluster_model
self.database = self.get_database(data_dir, "components.yml", fast)

def get_main_resolution_step(self, index: WeekScenarioIndex) -> ResolutionStep:
def get_main_resolution_step(
self,
index: WeekScenarioIndex,
solver_parameters: pywraplp.MPSolverParameters = pywraplp.MPSolverParameters(),
expected_status: str = pywraplp.Solver.OPTIMAL,
) -> ResolutionStep:
main_resolution_step = ResolutionStep(
timesteps=timesteps(index, self.time_scenario_hour_parameter),
scenarios=[index.scenario],
database=self.database,
network=self.network,
)

main_resolution_step.solve(solver_parameters, expected_status)

return main_resolution_step

def update_database_accurate(
Expand Down Expand Up @@ -173,7 +181,12 @@ def update_database_fast_after_heuristic(
)

def get_resolution_step_heuristic(
self, index: WeekScenarioIndex, id: str, model: Model
self,
index: WeekScenarioIndex,
id: str,
model: Model,
solver_parameters: pywraplp.MPSolverParameters = pywraplp.MPSolverParameters(),
expected_status: str = pywraplp.Solver.OPTIMAL,
) -> ResolutionStep:
cluster = create_component(model=model, id=id)

Expand All @@ -187,6 +200,7 @@ def get_resolution_step_heuristic(
network=network,
)

resolution_step.solve(solver_parameters, expected_status)
return resolution_step

def compute_delta(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ def test_fast_heuristic(data_path: str) -> None:
).model,
)

resolution_step_heuristic.solve()

thermal_problem_builder.update_database_fast_after_heuristic(
resolution_step_heuristic.output, week_scenario_index, [cluster]
)
Expand Down
8 changes: 0 additions & 8 deletions tests/functional/test_thermal_heuristic_one_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def test_milp_version(
main_resolution_step = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
main_resolution_step.solve()

assert main_resolution_step.objective == 16805387

Expand Down Expand Up @@ -153,7 +152,6 @@ def test_lp_version(
main_resolution_step = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
main_resolution_step.solve()

assert main_resolution_step.objective == pytest.approx(16802840.55)

Expand Down Expand Up @@ -192,7 +190,6 @@ def test_accurate_heuristic(
resolution_step_1 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
resolution_step_1.solve()

# Get number of on units and round it to integer
thermal_problem_builder.update_database_accurate(
Expand All @@ -216,7 +213,6 @@ def test_accurate_heuristic(
model=HeuristicAccurateModelBuilder(THERMAL_CLUSTER_MODEL_MILP).model,
)
)
resolution_step_accurate_heuristic.solve()

thermal_problem_builder.update_database_accurate(
resolution_step_accurate_heuristic.output, week_scenario_index, None
Expand All @@ -236,7 +232,6 @@ def test_accurate_heuristic(
resolution_step_2 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
resolution_step_2.solve()
assert resolution_step_2.objective == 16805387

expected_output = ExpectedOutput(
Expand Down Expand Up @@ -295,7 +290,6 @@ def test_fast_heuristic(
resolution_step_1 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
resolution_step_1.solve()

thermal_problem_builder.update_database_fast_before_heuristic(
resolution_step_1.output, week_scenario_index
Expand All @@ -308,7 +302,6 @@ def test_fast_heuristic(
number_hours, delta=thermal_problem_builder.compute_delta(cluster)
).model,
)
resolution_step_heuristic.solve()
thermal_problem_builder.update_database_fast_after_heuristic(
resolution_step_heuristic.output, week_scenario_index, None
)
Expand All @@ -327,7 +320,6 @@ def test_fast_heuristic(
resolution_step_2 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
resolution_step_2.solve()
assert resolution_step_2.objective == pytest.approx(16850000)

expected_output = ExpectedOutput(
Expand Down
4 changes: 1 addition & 3 deletions tests/functional/test_thermal_heuristic_six_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def test_accurate_heuristic(
index=week_scenario_index,
id=cluster,
model=HeuristicAccurateModelBuilder(THERMAL_CLUSTER_MODEL_MILP).model,
solver_parameters=solver_parameters,
)
)
resolution_step_accurate_heuristic.solve(solver_parameters)

nb_on_heuristic = np.transpose(
np.ceil(
Expand Down Expand Up @@ -170,8 +170,6 @@ def test_fast_heuristic(data_path: str, week_scenario_index: WeekScenarioIndex)
)
)

resolution_step_heuristic.solve()

thermal_problem_builder.update_database_fast_after_heuristic(
resolution_step_heuristic.output, week_scenario_index, [cluster]
)
Expand Down
19 changes: 6 additions & 13 deletions tests/functional/test_thermal_heuristic_three_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@ def test_milp_version(
for week in range(thermal_problem_builder.time_scenario_hour_parameter.week):
week_scenario_index = WeekScenarioIndex(week, scenario)
resolution_step = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
week_scenario_index, solver_parameters=solver_parameters
)

resolution_step.solve(solver_parameters)

expected_output = ExpectedOutput(
mode="milp",
index=week_scenario_index,
Expand Down Expand Up @@ -127,9 +125,8 @@ def test_accurate_heuristic(
week_scenario_index = WeekScenarioIndex(week, scenario)
# First optimization
resolution_step_1 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
week_scenario_index, solver_parameters=solver_parameters
)
resolution_step_1.solve(solver_parameters)

thermal_problem_builder.update_database_accurate(
resolution_step_1.output, week_scenario_index, None
Expand All @@ -144,19 +141,18 @@ def test_accurate_heuristic(
model=HeuristicAccurateModelBuilder(
THERMAL_CLUSTER_MODEL_MILP
).model,
solver_parameters=solver_parameters,
)
)
resolution_step_accurate_heuristic.solve(solver_parameters)

thermal_problem_builder.update_database_accurate(
resolution_step_accurate_heuristic.output, week_scenario_index, [g]
)

# Second optimization with lower bound modified
resolution_step_2 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
week_scenario_index, solver_parameters=solver_parameters
)
resolution_step_2.solve(solver_parameters)

expected_output = ExpectedOutput(
mode="accurate",
Expand Down Expand Up @@ -202,9 +198,8 @@ def test_fast_heuristic(
week_scenario_index = WeekScenarioIndex(week, scenario)
# First optimization
resolution_step_1 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
week_scenario_index, solver_parameters=solver_parameters
)
resolution_step_1.solve(solver_parameters)

thermal_problem_builder.update_database_fast_before_heuristic(
resolution_step_1.output, week_scenario_index
Expand All @@ -221,17 +216,15 @@ def test_fast_heuristic(
).model,
)
)
resolution_step_heuristic.solve()

thermal_problem_builder.update_database_fast_after_heuristic(
resolution_step_heuristic.output, week_scenario_index, [g]
)

# Second optimization with lower bound modified
resolution_step_2 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
week_scenario_index, solver_parameters=solver_parameters
)
resolution_step_2.solve(solver_parameters)

expected_output = ExpectedOutput(
mode="fast",
Expand Down
12 changes: 2 additions & 10 deletions tests/functional/test_thermal_heuristic_two_clusters_with_bc.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def test_milp_version(
main_resolution_step = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
main_resolution_step.solve()

assert main_resolution_step.objective == 16822864

Expand Down Expand Up @@ -116,7 +115,6 @@ def test_lp_version(
main_resolution_step = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
main_resolution_step.solve()

assert main_resolution_step.objective == pytest.approx(16802840.55)

Expand Down Expand Up @@ -155,7 +153,6 @@ def test_accurate_heuristic(
resolution_step_1 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
resolution_step_1.solve()

# Get number of on units and round it to integer
thermal_problem_builder.update_database_accurate(
Expand All @@ -176,7 +173,6 @@ def test_accurate_heuristic(
model=HeuristicAccurateModelBuilder(THERMAL_CLUSTER_MODEL_MILP).model,
)
)
resolution_step_accurate_heuristic.solve()

thermal_problem_builder.update_database_accurate(
resolution_step_accurate_heuristic.output, week_scenario_index, [g]
Expand All @@ -189,9 +185,8 @@ def test_accurate_heuristic(

# Second optimization with lower bound modified
resolution_step_2 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
week_scenario_index, expected_status=pywraplp.Solver.INFEASIBLE
)
resolution_step_2.solve(expected_status=pywraplp.Solver.INFEASIBLE)


def test_fast_heuristic(
Expand All @@ -216,7 +211,6 @@ def test_fast_heuristic(
resolution_step_1 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
)
resolution_step_1.solve()

thermal_problem_builder.update_database_fast_before_heuristic(
resolution_step_1.output, week_scenario_index
Expand All @@ -233,7 +227,6 @@ def test_fast_heuristic(
).model,
)
)
resolution_step_heuristic.solve()
thermal_problem_builder.update_database_fast_after_heuristic(
resolution_step_heuristic.output, week_scenario_index, [g]
)
Expand All @@ -249,6 +242,5 @@ def test_fast_heuristic(

# Second optimization with lower bound modified
resolution_step_2 = thermal_problem_builder.get_main_resolution_step(
week_scenario_index
week_scenario_index, expected_status=pywraplp.Solver.INFEASIBLE
)
resolution_step_2.solve(expected_status=pywraplp.Solver.INFEASIBLE)

0 comments on commit d5f1535

Please sign in to comment.