Skip to content

Commit

Permalink
Remove test on iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliette-Gerbaux committed Jan 8, 2025
1 parent 8cd4546 commit 212da0e
Showing 1 changed file with 0 additions and 153 deletions.
153 changes: 0 additions & 153 deletions tests/test_cutting_plane_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,156 +663,3 @@ def test_get_opt_gap() -> None:
)

assert opt_gap == pytest.approx(3.1317827969145355e-10)


def test_iterations() -> None:

max_gap = np.array(
[1.26173053e09, 5.44868087e09, 6.87689705e09, 6.94865731e09, 2.40167721e09]
)
opt_gap = 1.0

future_costs_approx = expected_future_costs_approx
trajectory = np.array([[starting_pt] * param.len_scenario] * param.len_week)
trajectory = np.swapaxes(trajectory, 1, 2)

for iter in range(4):
(
levels,
bellman_costs,
_,
_,
future_costs_approx_l,
) = get_bellman_values_from_costs(
param=param,
multi_stock_management=multi_management,
costs_approx=costs_approx,
future_costs_approx=future_costs_approx,
nSteps_bellman=nSteps_bellman,
name_solver=name_solver,
method=method,
trajectory=trajectory,
correlations=expected_correlations,
divisor=divisor,
verbose=False,
)

assert levels[0][0] == pytest.approx(
np.array(
[
[0.0, 628377.6569],
[0.0, 396540.564],
[0.0, 396540.564],
[0.0, 473537.71],
]
)[iter]
)
assert bellman_costs[0][0] == pytest.approx(
np.array(
[
1.0000001741522894e16,
1.0000009309663566e16,
1.0000012027346378e16,
1.0000011011170042e16,
]
)[iter],
)

future_costs_approx = future_costs_approx_l[0]

# Evaluate optimal
trajectory, pseudo_opt_controls, _ = solve_for_optimal_trajectory(
param=param,
multi_stock_management=multi_management,
costs_approx=costs_approx,
future_costs_approx_l=future_costs_approx_l,
starting_pt=starting_pt,
name_solver=name_solver,
divisor=divisor,
)

assert trajectory[0][0] == pytest.approx(
np.array([277853.0681, 277853.0681, 277853.0681, 277853.0681])[iter]
)
assert pseudo_opt_controls[0][0] == pytest.approx(
np.array(
[
[45619.31803823, 246455.966],
[0.0, 246455.966],
[0.0, 246455.966],
[0.0, 246455.966],
]
)[iter]
)

# Beware, some trajectories seem to be overstep the bounds with values such as -2e-12

controls_list = select_controls_to_explore(
param=param,
multi_stock_management=multi_management,
pseudo_opt_controls=pseudo_opt_controls,
costs_approx=costs_approx,
)

assert controls_list[0][0] == pytest.approx(
np.array(
[
[[-0.0, 419664.0]],
[[-0.0, 419664.0]],
[[-0.0, 419664.0]],
[[-0.0, 419664.0]],
]
)[iter]
)

# Evaluating this "optimal" trajectory
controls, costs, slopes = Lget_costs(
param=param,
multi_stock_management=multi_management,
controls_list=controls_list,
saving_directory=saving_dir,
output_path=output_path,
name_solver=name_solver,
verbose=False,
load_from_protos=True,
prefix=f"test_cut_plan_iter_{iter}_",
)

assert costs[0][0] == pytest.approx(
np.array(
[8719418.3931212, 8719418.3931212, 8719418.3931212, 8719418.3931212]
)[iter]
)
assert slopes[0][0][0] == pytest.approx(
np.array(
[
[-1.19160982e02, 4.05834900e-04],
[-1.19160982e02, 4.05834900e-04],
[-1.19160982e02, 4.05834900e-04],
[-1.19160982e02, 4.05834900e-04],
]
)[iter]
)

opt_gap = get_opt_gap(
param=param,
costs=costs,
costs_approx=costs_approx,
controls_list=controls,
opt_gap=opt_gap,
max_gap=max_gap,
)

assert opt_gap == pytest.approx(
np.array(
[
3.124352836612804e-10,
2.2105123247407198e-09,
1e-10,
1e-10,
]
)[iter]
)

costs_approx.update(controls=controls_list, costs=costs, duals=slopes)
costs_approx.remove_redundants(tolerance=1e-2)

0 comments on commit 212da0e

Please sign in to comment.