Skip to content

Commit

Permalink
Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliette-Gerbaux committed Dec 10, 2024
1 parent 0e63c2b commit 74d2fe8
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 175 deletions.
4 changes: 0 additions & 4 deletions src/calculate_reward_and_bellman_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ def get_disc(
n_pts_below + n_pts_in + n_pts_above
) # Make sure total adds up
if method == "lines":
# in_curve_pts = np.linspace(lbs, ubs, xNsteps-2) # Disc-2 * R
# all_pts = np.insert(in_curve_pts, 0, empty, axis=0) # (Disc-1) * R
# all_pts = np.insert(all_pts, all_pts.shape[0], full, axis=0) # Disc * R
above_curve_pts = [
np.linspace(ubs[r], full[r], n_pts_above[r], endpoint=True)
for r in range(n_reservoirs)
Expand All @@ -183,7 +180,6 @@ def get_disc(
for r in range(n_reservoirs)
]
).T
# all_pts = np.concatenate((below_curve_pts, in_curve_pts, above_curve_pts), axis=0) # Disc * R
diffs_to_ref = all_pts[:, None] - reference_pt[None, :] # Disc * R
diffs_to_ref = (
diffs_to_ref[:, :, None] * np.eye(n_reservoirs)[None, :, :]
Expand Down
2 changes: 0 additions & 2 deletions src/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ def draw_usage_values(
closest_level
]

# z = np.maximum(np.zeros(reinterpolated_usage_values[area].T.shape), np.minimum(ub*np.ones(reinterpolated_usage_values[area].T.shape), reinterpolated_usage_values[area].T))
# z = np.maximum(np.zeros(reinterpolated_usage_values[area].T.shape) - ub, np.minimum(ub*np.ones(reinterpolated_usage_values[area].T.shape), reinterpolated_usage_values[area].T))
usage_values_plot = go.Figure(
data=[
go.Heatmap(
Expand Down
7 changes: 0 additions & 7 deletions src/estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ def __init__(
costs:np.ndarray: Cost for every input,
duals:np.ndarray: Duals for every input first dimension should be the same as inputs,
"""
# self.true_controls=controls
# self.true_costs=costs
# self.true_duals=duals
self.estimators = np.array(
[
[
Expand All @@ -335,7 +332,6 @@ def __init__(
costs=costs[week, scenario],
duals=duals[week, scenario],
correlations=correlations,
# interp_mode=interp_mode,
)
for scenario in range(param.len_scenario)
]
Expand Down Expand Up @@ -409,7 +405,6 @@ def update(
inputs=inputs,
costs=costs,
duals=duals,
# interp_mode=interp_mode,
)

def enrich_estimator(self, n_splits: int = 3) -> None:
Expand Down Expand Up @@ -441,8 +436,6 @@ def cleanup_approximations(
for week_estimators in self.estimators:
for estimator in week_estimators:
estimator.remove_incoherence()
# controls=true_controls[week, scenario],
# real_costs=true_costs[week, scenario])

def remove_redundants(
self,
Expand Down
2 changes: 1 addition & 1 deletion src/functions_iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def itr_control(
) = init_iterative_calculation(param, reservoir_management, output_path, X, solver)
i = 0

while (gap >= tol_gap and gap >= 0) and i < N: # and (i<3):
while (gap >= tol_gap and gap >= 0) and i < N:
debut = time()

initial_x, controls = compute_x_multi_scenario(
Expand Down
3 changes: 0 additions & 3 deletions src/hyperplane_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ def BezierAv(
if last_axify:
p[:-1] = (p0[:-1] + p1[:-1]) / 2
p[-1] = p0[-1] * diff_rate + p1[-1] * (1 - diff_rate)
# v_sum = np.sum(v[:-1])
# v[-1] /= v_sum
v = v0 * (t) + v1 * (1 - t)
v[-1] = v0[-1] * (t * (1 - partage) + partage * diff_rate) + v1[-1] * (
(1 - t) * (1 - partage) + partage * (1 - diff_rate)
)
elif norm(v) > 1e-12:
# v = v / sum(v[:-1])
v = v / norm(v) * (n0 + n1) / 2
return np.array([p, v])

Expand Down
108 changes: 1 addition & 107 deletions src/multi_stock_bellman_value_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from estimation import Estimator, LinearCostEstimator, LinearInterpolator
from read_antares_data import TimeScenarioParameter, TimeScenarioIndex

# from julia_sddp import python_to_julia_data, manage_reservoirs_py
from optimization import (
AntaresProblem,
WeeklyBellmanProblem,
Expand All @@ -23,8 +22,6 @@
import numpy.typing as npt
import numpy as np

# from scipy.interpolate import interp1d
# from functions_iterative import compute_upper_bound
from display import ConvergenceProgressBar, draw_usage_values, draw_uvs_sddp
from scipy.stats import random_correlation
import pickle as pkl
Expand Down Expand Up @@ -383,7 +380,6 @@ def get_bellman_values_from_costs(
f"We were using the following future costs estimation: {[f'Cost(lvl) >= {cost} + (lvl_0 - {input[0]})*{duals[0]} + (lvl_1 - {input[1]})*{duals[1]}' for input, cost, duals in zip(future_costs_approx.inputs, future_costs_approx.costs, future_costs_approx.duals)]}"
)
raise ValueError
# assert (np.min(np.abs(duals_wl))>.1 or np.random.rand()> .01)

# Writing down results
controls_w[lvl_id] = controls_wls
Expand Down Expand Up @@ -1025,7 +1021,6 @@ def select_controls_to_explore(
* (2 * n_inputs + 1),
size=(n_weeks, n_scenarios, n_reservoirs),
)
# pseudo_opt_controls += np.random.normal(scale=np.exp(-relative_proximity) * max_var[: ,None ,:]/10, size=(n_weeks, n_scenarios, n_reservoirs))
controls_to_explore[:, :, 0, :] = np.maximum(
np.minimum(pseudo_opt_controls, max_gen[:, None, :]), max_pump[:, None, :]
)
Expand Down Expand Up @@ -1174,11 +1169,8 @@ def compute_usage_values_from_costs(
for j, levels in enumerate(levels_to_test[week, :, i]):
# Remove previous constraints / vars
problem.reset_solver()
# problem.solver.SetSolverSpecificParametersAsString("presolve off")

try:
# Rewrite problem
# print(levels, future_costs_approx_l[week].costs)
problem.write_problem(
week=week,
level_init=levels,
Expand Down Expand Up @@ -1229,13 +1221,10 @@ def get_opt_gap(
]
)
# Computing the optimality gap
# pre_update_tot_costs = np.sum(pre_update_costs, axis=0)
# real_tot_costs = np.sum(costs, axis=0)
costs = np.mean(costs, axis=-1)
opt_gap = max(
1e-10, min(1, np.max(np.mean((costs - pre_update_costs), axis=1) / max_gap))
)
# print(f"Gap {np.max(np.mean((costs - pre_update_costs),axis=1)/max_gap)}")
return opt_gap


Expand Down Expand Up @@ -1298,32 +1287,6 @@ def cutting_plane_method(
max_gap = np.mean(np.max(costs, axis=2) - np.min(costs, axis=2), axis=1)
rng = np.random.default_rng(1234115)

# hlpr = Caller(
# param=param,
# multi_stock_management=multi_stock_management,
# name_solver=name_solver,
# list_models=list_models,
# starting_pt=starting_pt,
# inflows=inflows,
# trajectory=trajectory,
# max_gap=max_gap,
# costs_approx=costs_approx,
# future_costs_approx=future_costs_approx,
# nSteps_bellman=nSteps_bellman,
# method=method,
# correlations=correlations,
# maxiter=maxiter,
# precision=precision,
# interp_mode=interp_mode,
# opt_gap=opt_gap,
# divisor=divisor,
# rounding=rounding,
# output_path=output_path,
# rng=rng,
# verbose=False,
# prefix="cut_plan")
# prefix="cut_plan"

# To display convergence:
if verbose:
pbar = ConvergenceProgressBar(
Expand All @@ -1335,8 +1298,6 @@ def cutting_plane_method(
if verbose:
pbar.describe("Dynamic Programming")

# levels, bellman_costs, _, _, future_costs_approx_l = hlpr(get_bellman_values_from_costs,\
# ("levels", "bellman_costs", "bellman_duals", "bellman_controls", "future_costs_approx_l", "usage_values"))
(
levels,
bellman_costs,
Expand All @@ -1358,11 +1319,8 @@ def cutting_plane_method(
verbose=verbose,
)
future_costs_approx = future_costs_approx_l[0]
# hlpr.update(future_costs_approx = future_costs_approx)

# Evaluate optimal
# trajectory, _, _ = hlpr(solve_for_optimal_trajectory,\
# ("trajectory", "pseudo_opt_controls", "pseudo_opt_costs"))
trajectory, pseudo_opt_controls, pseudo_opt_costs = (
solve_for_optimal_trajectory(
param=param,
Expand All @@ -1378,7 +1336,6 @@ def cutting_plane_method(
)
# Beware, some trajectories seem to be overstep the bounds with values such as -2e-12

# controls_list = hlpr(select_controls_to_explore, ("controls_list"))
controls_list = select_controls_to_explore(
param=param,
multi_stock_management=multi_stock_management,
Expand All @@ -1390,8 +1347,6 @@ def cutting_plane_method(
pbar.describe("Simulation")

# Evaluating this "optimal" trajectory
# hlpr.update(prefix=f"cut_plan_iter_{iter}_")
# controls_list, costs, slopes = hlpr(Lget_costs, ("controls_list", "costs", "slopes"))
controls, costs, slopes = Lget_costs(
param=param,
multi_stock_management=multi_stock_management,
Expand All @@ -1404,7 +1359,6 @@ def cutting_plane_method(
prefix=f"cut_plan_iter_{iter}_",
)

# opt_gap = hlpr(get_opt_gap, "opt_gap")
opt_gap = get_opt_gap(
param=param,
costs=costs,
Expand All @@ -1422,7 +1376,6 @@ def cutting_plane_method(
# If we want to look at the usage values evolution
if verbose:
pbar.describe("Drawing")
# hlpr.update(optimal_trajectory=trajectory, n_weeks=param.len_week)
usage_values, levels_uv = compute_usage_values_from_costs(
param=param,
multi_stock_management=multi_stock_management,
Expand All @@ -1435,7 +1388,6 @@ def cutting_plane_method(
divisor=divisor,
rounding=rounding,
)
# hlpr(compute_usage_values_from_costs, ("usage_values", "levels_uv"))
draw_usage_values(
usage_values=usage_values,
levels_uv=levels_uv,
Expand All @@ -1445,7 +1397,6 @@ def cutting_plane_method(
trajectory=trajectory,
ub=500,
)
# hlpr(draw_usage_values)
pbar.update(precision=opt_gap)

if verbose:
Expand Down Expand Up @@ -1512,40 +1463,14 @@ def iter_bell_vals(
Returns:
tuple[np.ndarray, Estimator, list[LinearInterpolator], np.ndarray]: Bellman costs, costs approximation, future costs approximation, levels
"""
# Initialize the caller
# hlpr = Caller(
# param=param,
# multi_stock_management=multi_stock_management,
# output_path=output_path,
# n_controls_init=n_controls_init,
# starting_pt=starting_pt,
# nSteps_bellman=nSteps_bellman,
# method=method,
# name_solver=name_solver,
# precision=precision,
# maxiter=maxiter,
# correlations=correlations,
# interp_mode=interp_mode,
# divisor=divisor,
# rounding=rounding,
# verbose=verbose,
# already_init=already_init,
# keep_intermed_res=keep_intermed_res)

# Choose first controls to test
# controls = hlpr(initialize_controls, 'controls_list')
controls_list = initialize_controls(
param=param,
multi_stock_management=multi_stock_management,
n_controls_init=n_controls_init,
)

# #Initialize the Antares problems
# hlpr(initialize_antares_problems, 'list_models')

# #Get hyperplanes resulting from initial controls
# _, duals = hlpr(get_all_costs, ('costs', 'slopes'))
# controls, costs, duals = hlpr(Lget_costs, ('controls_list', 'costs', 'slopes'))
# Get hyperplanes resulting from initial controls
controls, costs, duals = Lget_costs(
param=param,
multi_stock_management=multi_stock_management,
Expand All @@ -1556,16 +1481,12 @@ def iter_bell_vals(
load_from_protos=True,
verbose=verbose,
)
# hlpr.update(duals=duals, controls=controls)
# hlpr.update(list_models=[])

# costs_approx = hlpr(LinearCostEstimator, "costs_approx")
costs_approx = LinearCostEstimator(
param=param, controls=controls, costs=costs, duals=duals
)

# Initialize our approximation on future costs
# hlpr(initialize_future_costs, "future_costs_approx")
future_costs_approx = initialize_future_costs(
param=param,
starting_pt=starting_pt,
Expand All @@ -1574,16 +1495,13 @@ def iter_bell_vals(
)

# Correlations matrix
# hlpr(get_correlation_matrix, "correlations")
correlations = get_correlation_matrix(
param=param,
multi_stock_management=multi_stock_management,
corr_type="no_corrs",
)

# Iterative part
# hlpr(cutting_plane_method, ("bellman_costs", "levels", "future_costs_approx_l",
# "costs_approx", "optimal_trajectory"))
bellman_costs, levels, future_costs_approx_l, costs_approx, optimal_trajectory = (
cutting_plane_method(
param=param,
Expand All @@ -1607,8 +1525,6 @@ def iter_bell_vals(
)

# Deducing usage values
# hlpr.update(nSteps_bellman=101)
# hlpr(compute_usage_values_from_costs, ('usage_values', 'levels_imposed'))
usage_values, levels_imposed = compute_usage_values_from_costs(
param=param,
multi_stock_management=multi_stock_management,
Expand All @@ -1622,10 +1538,6 @@ def iter_bell_vals(
rounding=rounding,
)

# returns = ("bellman_costs", "costs_approx", "future_costs_approx_l", "levels", "optimal_trajectory",\
# "usage_values")
# bellman_costs, costs_approx, future_costs_approx_l,\
# levels, optimal_trajectory, usage_values = hlpr.get(returns)
return (
bellman_costs,
costs_approx,
Expand Down Expand Up @@ -1801,24 +1713,6 @@ def iter_bell_vals_v2(
n_controls_init=n_controls_init,
)

# #Initialize the Antares problems
# list_models = initialize_antares_problems(
# param=param,
# multi_stock_management=multi_stock_management,
# output_path=output_path,
# name_solver=name_solver,
# direct_bellman_calc=False,
# verbose=verbose,
# )

# Get hyperplanes resulting from initial
# costs, duals = get_all_costs(
# param=param,
# list_models=list_models,
# multi_stock_management=multi_stock_management,
# controls_list=controls_list,
# verbose=verbose,
# )
controls_list, costs, duals = Lget_costs(
param=param,
multi_stock_management=multi_stock_management,
Expand Down
Loading

0 comments on commit 74d2fe8

Please sign in to comment.