From 24b80f6df8853f194d16919bbdb9ecf2a3775dae Mon Sep 17 00:00:00 2001 From: Juliette-Gerbaux Date: Tue, 10 Dec 2024 19:12:37 +0100 Subject: [PATCH] Clean src --- src/display.py | 14 +--- src/estimation.py | 4 +- src/functions_iterative.py | 1 - src/hyperplane_interpolation.py | 4 - src/launch_calculation.py | 4 +- src/multi_stock_bellman_value_calculation.py | 84 ++++---------------- src/optimization.py | 8 +- src/requirements.txt | 6 -- tests/test_iterative_two_stocks.py | 6 -- 9 files changed, 25 insertions(+), 106 deletions(-) delete mode 100644 src/requirements.txt diff --git a/src/display.py b/src/display.py index 8612dcf..d2a2a4f 100644 --- a/src/display.py +++ b/src/display.py @@ -62,9 +62,7 @@ def draw_usage_values( marker=dict(symbol="circle"), showlegend=True, ) - for i, (area, mng) in enumerate( - multi_stock_management.dict_reservoirs.items() - ) + for i, (_, _) in enumerate(multi_stock_management.dict_reservoirs.items()) ] + [ go.Scatter( @@ -76,9 +74,7 @@ def draw_usage_values( line=dict(dash="dash"), showlegend=True, ) - for i, (area, mng) in enumerate( - multi_stock_management.dict_reservoirs.items() - ) + for i, (_, mng) in enumerate(multi_stock_management.dict_reservoirs.items()) ] + [ go.Scatter( @@ -90,9 +86,7 @@ def draw_usage_values( line=dict(dash="dash"), showlegend=True, ) - for i, (area, mng) in enumerate( - multi_stock_management.dict_reservoirs.items() - ) + for i, (_, mng) in enumerate(multi_stock_management.dict_reservoirs.items()) ], layout=dict(title=f"Usage Values"), ) @@ -166,7 +160,7 @@ def draw_uvs_sddp( showlegend=True, visible=(r == 0), ) - for r, res in enumerate(reservoirs) + for r, _ in enumerate(reservoirs) ] + [ go.Scatter( diff --git a/src/estimation.py b/src/estimation.py index c5bbed5..f1eef49 100644 --- a/src/estimation.py +++ b/src/estimation.py @@ -311,7 +311,6 @@ def __init__( costs: np.ndarray, duals: np.ndarray, correlations: Optional[np.ndarray] = None, - interp_mode: bool = False, ) -> None: """ Instanciates a LinearCostEstimator @@ -385,7 +384,6 @@ def update( inputs: np.ndarray, costs: np.ndarray, duals: np.ndarray, - interp_mode: bool = False, ) -> None: """ Updates the parameters of the Linear Interpolators @@ -407,7 +405,7 @@ def update( duals=duals, ) - def enrich_estimator(self, n_splits: int = 3) -> None: + def enrich_estimator(self) -> None: """ Adds 'mid_cuts' to our cost estimator to smoothen the curves and (hopefully) accelerate convergence diff --git a/src/functions_iterative.py b/src/functions_iterative.py index 36ea514..6f9aca2 100644 --- a/src/functions_iterative.py +++ b/src/functions_iterative.py @@ -391,7 +391,6 @@ def init_iterative_calculation( stock_discretization=X, ) - i = 0 gap = 1e3 fin = time() tot_t.append(fin - debut) diff --git a/src/hyperplane_interpolation.py b/src/hyperplane_interpolation.py index 2745cb9..21e3bfc 100644 --- a/src/hyperplane_interpolation.py +++ b/src/hyperplane_interpolation.py @@ -122,10 +122,6 @@ def interpolate_between( return mlr, data -mlrs: list = [] -datas: list = [] - - def enrich_by_interpolation( controls_init: np.ndarray, costs: np.ndarray, slopes: np.ndarray, n_splits: int = 3 ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: diff --git a/src/launch_calculation.py b/src/launch_calculation.py index b0b7cfa..60b0431 100644 --- a/src/launch_calculation.py +++ b/src/launch_calculation.py @@ -50,7 +50,7 @@ def calculate_bellman_values( elif method == "precalculated": # or with precalulated reward - vb, G = calculate_bellman_value_with_precalculated_reward( + vb, _ = calculate_bellman_value_with_precalculated_reward( len_controls=len_controls, param=param, reservoir_management=reservoir_management, @@ -61,7 +61,7 @@ def calculate_bellman_values( elif method == "iterative": # or with iterative algorithm - vb, G, _, _, controls_upper, traj = itr_control( + vb, _, _, _, _, _ = itr_control( param=param, reservoir_management=reservoir_management, output_path=output_path, diff --git a/src/multi_stock_bellman_value_calculation.py b/src/multi_stock_bellman_value_calculation.py index f42eb98..aa57ac0 100644 --- a/src/multi_stock_bellman_value_calculation.py +++ b/src/multi_stock_bellman_value_calculation.py @@ -18,7 +18,7 @@ solve_problem_with_multivariate_bellman_values, solve_for_optimal_trajectory, ) -from typing import Annotated, List, Literal, Dict, Optional, Any +from typing import Annotated, Literal, Dict, Optional, Any import numpy.typing as npt import numpy as np @@ -289,7 +289,6 @@ def get_bellman_values_from_costs( trajectory: np.ndarray, correlations: np.ndarray, divisor: dict[str, float] = {"euro": 1e8, "energy": 1e4}, - rounding: int = 6, verbose: bool = False, ) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, list[LinearInterpolator]]: """ @@ -322,7 +321,6 @@ def get_bellman_values_from_costs( name_solver=name_solver, divisor=divisor, ) - # problem.parameters.SetDoubleParam(problem.parameters.PRESOLVE, problem.parameters.PRESOLVE_ON) # Keeping in memory all future costs approximations future_costs_approx_l = [future_costs_approx] @@ -406,10 +404,8 @@ def get_bellman_values_from_costs( def initialize_future_costs( - param: TimeScenarioParameter, starting_pt: np.ndarray, multi_stock_management: MultiStockManagement, - costs_approx: Estimator, mult: float = 0.0, ) -> LinearInterpolator: """ @@ -531,7 +527,6 @@ def get_all_costs( """ tot_iter = 0 times = [] - n_reservoirs = len(multi_stock_management.dict_reservoirs) if keep_intermed_res or already_init: assert saving_dir is not None filename = saving_dir + "/get_all_costs_run.pkl" @@ -562,10 +557,6 @@ def get_all_costs( controls_list=controls_list[week, scenario], ) except ValueError: - reservoirs = [ - mng.reservoir - for mng in multi_stock_management.dict_reservoirs.values() - ] print( f"Failed at week {week}, the conditions on control were: {controls_list[week, scenario]}" ) @@ -700,7 +691,6 @@ def generate_controls( for mng in multi_stock_management.dict_reservoirs.values() ] ) - mean_res = (max_res + min_res) / 2 # Points to start lines at start_pts = [ @@ -756,7 +746,7 @@ def generate_controls( manag.reservoir.max_generating, xNsteps, ) - for area, manag in multi_stock_management.dict_reservoirs.items() + for _, manag in multi_stock_management.dict_reservoirs.items() ] ) ] @@ -798,9 +788,6 @@ def precalculated_method( ------- Bellman Values:Dict[int, Dict[str, npt.NDArray[np.float32]]]: Bellman values """ - # __________________________________________________________________________________________ - # PRECALCULATION PART - # __________________________________________________________________________________________ # Initialize the problems list_models = initialize_antares_problems( @@ -830,15 +817,11 @@ def precalculated_method( verbose=verbose, ) - # print("=======================[ Reward approximation ]=======================") # Initialize cost functions costs_approx = LinearCostEstimator( param=param, controls=controls_list, costs=costs, duals=slopes ) - # __________________________________________________________________________________________ - # BELLMAN CALCULATION PART - # __________________________________________________________________________________________ print( "=======================[ Dynamic Programming ]=======================" ) @@ -851,9 +834,7 @@ def precalculated_method( ) future_costs_approx = initialize_future_costs( - param=param, multi_stock_management=multi_stock_management, - costs_approx=costs_approx, starting_pt=starting_pt, ) @@ -861,7 +842,6 @@ def precalculated_method( trajectory = np.swapaxes(trajectory, 1, 2) correlations = get_correlation_matrix( - param=param, multi_stock_management=multi_stock_management, corr_type="no_corrs", ) @@ -1028,7 +1008,6 @@ def select_controls_to_explore( def get_correlation_matrix( - param: TimeScenarioParameter, multi_stock_management: MultiStockManagement, adjacency_mat: Optional[np.ndarray] = None, corr_type: str = "no_corrs", @@ -1067,7 +1046,6 @@ def compute_usage_values_from_costs( nSteps_bellman: int, correlations: np.ndarray, divisor: dict[str, float] = {"euro": 1e8, "energy": 1e4}, - rounding: int = 6, verbose: bool = False, ) -> tuple[dict[str, np.ndarray], np.ndarray]: """For now we want this function to solve a lot of optimization problems for different initial values of stock @@ -1242,9 +1220,7 @@ def cutting_plane_method( saving_dir: str, maxiter: Optional[int] = None, precision: float = 5e-2, - interp_mode: bool = False, divisor: dict[str, float] = {"euro": 1e8, "energy": 1e4}, - rounding: int = 6, output_path: str = "", verbose: bool = False, ) -> tuple[ @@ -1274,12 +1250,6 @@ def cutting_plane_method( iter = 0 opt_gap = 1.0 maxiter = int(1e2) if maxiter is None else maxiter - inflows = np.array( - [ - mng.reservoir.inflow[: param.len_week, : param.len_scenario] - for mng in multi_stock_management.dict_reservoirs.values() - ] - ).T # Init trajectory trajectory = np.array([[starting_pt] * param.len_scenario] * param.len_week) trajectory = np.swapaxes(trajectory, 1, 2) @@ -1301,8 +1271,8 @@ def cutting_plane_method( ( levels, bellman_costs, - bellman_duals, - bellman_controls, + _, + _, future_costs_approx_l, ) = get_bellman_values_from_costs( param=param, @@ -1315,24 +1285,19 @@ def cutting_plane_method( trajectory=trajectory, correlations=correlations, divisor=divisor, - rounding=rounding, verbose=verbose, ) future_costs_approx = future_costs_approx_l[0] # Evaluate optimal - trajectory, pseudo_opt_controls, pseudo_opt_costs = ( - solve_for_optimal_trajectory( - param=param, - multi_stock_management=multi_stock_management, - costs_approx=costs_approx, - future_costs_approx_l=future_costs_approx_l, - inflows=inflows, - starting_pt=starting_pt, - name_solver=name_solver, - divisor=divisor, - verbose=verbose, - ) + trajectory, pseudo_opt_controls, _ = solve_for_optimal_trajectory( + param=param, + multi_stock_management=multi_stock_management, + costs_approx=costs_approx, + future_costs_approx_l=future_costs_approx_l, + starting_pt=starting_pt, + name_solver=name_solver, + divisor=divisor, ) # Beware, some trajectories seem to be overstep the bounds with values such as -2e-12 @@ -1368,9 +1333,7 @@ def cutting_plane_method( max_gap=max_gap, ) - costs_approx.update( - inputs=controls_list, costs=costs, duals=slopes, interp_mode=interp_mode - ) + costs_approx.update(inputs=controls_list, costs=costs, duals=slopes) costs_approx.remove_redundants(tolerance=1e-2) # If we want to look at the usage values evolution @@ -1386,7 +1349,6 @@ def cutting_plane_method( nSteps_bellman=nSteps_bellman, correlations=correlations, divisor=divisor, - rounding=rounding, ) draw_usage_values( usage_values=usage_values, @@ -1415,14 +1377,9 @@ def iter_bell_vals( saving_dir: str, name_solver: str = "CLP", precision: float = 1e-2, - maxiter: int = 2, correlations: Optional[np.ndarray] = None, - interp_mode: bool = False, divisor: dict[str, float] = {"euro": 1e8, "energy": 1e4}, - rounding: int = 6, verbose: bool = False, - already_init: bool = False, - keep_intermed_res: bool = False, ) -> tuple[Any, Any, Any, Any, Any, Any]: """ In a similar fashion to Kelley's algorithm (1960), the idea is to approximate the (convex) cost function @@ -1488,15 +1445,12 @@ def iter_bell_vals( # Initialize our approximation on future costs future_costs_approx = initialize_future_costs( - param=param, starting_pt=starting_pt, multi_stock_management=multi_stock_management, - costs_approx=costs_approx, ) # Correlations matrix correlations = get_correlation_matrix( - param=param, multi_stock_management=multi_stock_management, corr_type="no_corrs", ) @@ -1516,16 +1470,14 @@ def iter_bell_vals( method=method, correlations=correlations, precision=precision, - interp_mode=interp_mode, divisor=divisor, - rounding=rounding, output_path=output_path, verbose=verbose, ) ) # Deducing usage values - usage_values, levels_imposed = compute_usage_values_from_costs( + usage_values, _ = compute_usage_values_from_costs( param=param, multi_stock_management=multi_stock_management, name_solver=name_solver, @@ -1535,7 +1487,6 @@ def iter_bell_vals( nSteps_bellman=101, correlations=correlations, divisor=divisor, - rounding=rounding, ) return ( @@ -1560,7 +1511,6 @@ def sddp_cutting_planes( normalization: Dict[str, float], maxiter: Optional[int] = None, precision: float = 1e-2, - interp_mode: bool = False, verbose: bool = False, ) -> tuple[np.ndarray, np.ndarray, LinearCostEstimator, list[np.ndarray]]: @@ -1681,9 +1631,7 @@ def sddp_cutting_planes( ) pbar.update(precision=opt_gap) - costs_approx.update( - inputs=controls, costs=costs, duals=slopes, interp_mode=interp_mode - ) + costs_approx.update(inputs=controls, costs=costs, duals=slopes) costs_approx.remove_redundants(tolerance=1e-2) usage_values, bellman_costs = jl_sddp.get_usage_values( param.len_week, param.len_scenario, jl_reservoirs, model, norms, 101 @@ -1702,7 +1650,6 @@ def iter_bell_vals_v2( name_solver: str = "CLP", precision: float = 1e-2, maxiter: int = 2, - interp_mode: bool = False, verbose: bool = False, ) -> Any: @@ -1740,7 +1687,6 @@ def iter_bell_vals_v2( precision=precision, normalization=normalization, maxiter=maxiter, - interp_mode=interp_mode, verbose=verbose, ) diff --git a/src/optimization.py b/src/optimization.py index 62e0a04..6ab6376 100644 --- a/src/optimization.py +++ b/src/optimization.py @@ -1,11 +1,11 @@ import re -from typing import Any, Optional +from typing import Optional from calculate_reward_and_bellman_values import ( BellmanValueCalculation, MultiStockBellmanValueCalculation, MultiStockManagement, ) -from estimation import Estimator, LinearInterpolator, LinearCostEstimator +from estimation import Estimator, LinearInterpolator from read_antares_data import TimeScenarioParameter import numpy as np from time import time @@ -444,7 +444,7 @@ def solve_with_predefined_controls( for area in self.range_reservoir: self.set_constraints_predefined_control(control[area], area) try: - beta, lamb, final_level, _, _, itr, computing_time = self.solve_problem( + beta, lamb, _, _, _, itr, computing_time = self.solve_problem( direct_bellman_mode=False ) # print(f"✔ for controls {control}") @@ -1714,11 +1714,9 @@ def solve_for_optimal_trajectory( multi_stock_management: MultiStockManagement, costs_approx: Estimator, future_costs_approx_l: list[LinearInterpolator], - inflows: np.ndarray, starting_pt: np.ndarray, name_solver: str, divisor: dict[str, float], - verbose: bool = False, ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: """Finds the optimal trajectory starting from starting_pts diff --git a/src/requirements.txt b/src/requirements.txt deleted file mode 100644 index 966cb43..0000000 --- a/src/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -juliacall==0.9.22 -numpy==2.1.2 -ortools==9.9.9999 -plotly==5.23.0 -scipy==1.14.1 -tqdm==4.66.5 diff --git a/tests/test_iterative_two_stocks.py b/tests/test_iterative_two_stocks.py index 9611a0e..bf7e5cd 100644 --- a/tests/test_iterative_two_stocks.py +++ b/tests/test_iterative_two_stocks.py @@ -59,16 +59,11 @@ def test_bellman_value_iterative_method() -> None: for mng in multi_management.dict_reservoirs.values() ] ), - maxiter=10, precision=1e-3, method="lines", correlations=None, - interp_mode=False, divisor={"euro": 1e8, "energy": 1e4}, - rounding=6, verbose=False, - keep_intermed_res=False, - already_init=True, ) assert bell_costs == pytest.approx( @@ -262,7 +257,6 @@ def test_bellman_value_iterative_method_with_sddp() -> None: name_solver="CLP", maxiter=3, precision=1e-2, - interp_mode=False, verbose=False, )