Skip to content

Commit

Permalink
Saving cuts in dev directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliette-Gerbaux committed Dec 10, 2024
1 parent 5034eaa commit dd4f35f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/multi_stock_bellman_value_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ def get_all_costs(
list_models: Dict[TimeScenarioIndex, AntaresProblem],
multi_stock_management: MultiStockManagement,
controls_list: np.ndarray,
saving_dir: Optional[str] = None,
verbose: bool = False,
already_init: bool = False,
keep_intermed_res: bool = False,
Expand All @@ -535,7 +536,9 @@ def get_all_costs(
tot_iter = 0
times = []
n_reservoirs = len(multi_stock_management.dict_reservoirs)
filename = "get_all_costs_run.pkl"
if keep_intermed_res or already_init:
assert saving_dir is not None
filename = saving_dir + "/get_all_costs_run.pkl"

# Initializing the n_weeks*n_scenarios*n_controls*n_stocks values to fill
shape_controls = list(controls_list.shape)
Expand Down Expand Up @@ -594,7 +597,7 @@ def Lget_costs(
load_from_protos: bool = False,
prefix: str = "",
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
filename = f"{prefix}get_all_costs_run_{output_path.replace('/','_')[-27:]}.pkl"
filename = f"{saving_directory}/{prefix}get_all_costs_run_{output_path.replace('/','_')[-27:]}.pkl"

# Initializing the n_weeks*n_scenarios*n_controls(*n_stocks) values to fill
shape_controls = list(controls_list.shape)
Expand Down Expand Up @@ -1691,6 +1694,7 @@ def sddp_cutting_planes(
param.len_scenario,
julia_reservoirs,
julia_capp,
saving_dir,
normalization["euro"],
normalization["energy"],
)
Expand All @@ -1704,6 +1708,7 @@ def sddp_cutting_planes(
param.len_scenario,
julia_reservoirs,
julia_capp,
saving_dir,
normalization["euro"],
normalization["energy"],
)
Expand Down
14 changes: 7 additions & 7 deletions src/sddp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Normalizer
end

# Function to convert Python data to Julia structures
function formater(n_weeks, n_scenarios, reservoirs_data, costs_approx_data, norm_euros::Float64=1e7, norm_enrgy::Float64=1e5)
function formater(n_weeks, n_scenarios, reservoirs_data, costs_approx_data, saving_dir::String, norm_euros::Float64=1e7, norm_enrgy::Float64=1e5)
round_energy = 4
round_euro = 8
round_price = 5
Expand Down Expand Up @@ -64,7 +64,7 @@ function formater(n_weeks, n_scenarios, reservoirs_data, costs_approx_data, norm
round.(pyconvert(Matrix, costs_approx_data[i,j]["duals"]) /norm_price, digits=round_price), # € / MWh
) for j in 1:size_ca_data[2]] for i in 1:size_ca_data[1]]

return (n_weeks, n_scenarios, reservoirs, costs_approx, norms)
return (n_weeks, n_scenarios, reservoirs, costs_approx, norms, saving_dir)
end


Expand Down Expand Up @@ -163,17 +163,17 @@ function stability_report(model)
return SDDP.numerical_stability_report(model)
end

function reinit_cuts(n_weeks::Int, n_scenarios::Int, reservoirs::Vector{Main.Jl_SDDP.Reservoir}, costs_approx::Vector{Vector{Main.Jl_SDDP.LinInterp}}, norms::Normalizer)
function reinit_cuts(n_weeks::Int, n_scenarios::Int, reservoirs::Vector{Main.Jl_SDDP.Reservoir}, costs_approx::Vector{Vector{Main.Jl_SDDP.LinInterp}}, norms::Normalizer, saving_dir::String)
model = generate_model(n_weeks, n_scenarios, reservoirs, costs_approx, norms)
SDDP.write_cuts_to_file(model, "sddp_current_cuts")
SDDP.write_cuts_to_file(model, saving_dir*"/sddp_current_cuts")
end

function manage_reservoirs(n_weeks::Int, n_scenarios::Int, reservoirs::Vector{Main.Jl_SDDP.Reservoir}, costs_approx::Vector{Vector{Main.Jl_SDDP.LinInterp}}, norms::Normalizer)
function manage_reservoirs(n_weeks::Int, n_scenarios::Int, reservoirs::Vector{Main.Jl_SDDP.Reservoir}, costs_approx::Vector{Vector{Main.Jl_SDDP.LinInterp}}, norms::Normalizer, saving_dir::String)
model = generate_model(n_weeks, n_scenarios, reservoirs, costs_approx, norms)
SDDP.read_cuts_from_file(model, "sddp_current_cuts")
SDDP.read_cuts_from_file(model, saving_dir*"/sddp_current_cuts")
# Training the model
SDDP.train(model, stopping_rules = [SDDP.BoundStalling(40, 1e1)], iteration_limit = 700, cut_type = SDDP.MULTI_CUT)
SDDP.write_cuts_to_file(model, "sddp_current_cuts")
SDDP.write_cuts_to_file(model, saving_dir*"/sddp_current_cuts")

#Simulating
simulation_results = get_trajectory(n_weeks, n_scenarios, reservoirs, model, norms)
Expand Down

0 comments on commit dd4f35f

Please sign in to comment.