Skip to content

Commit

Permalink
Merge branch 'master' into constraint_mp_min_res_gen_to_demand_ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Aug 7, 2023
2 parents fd109de + 245605c commit f3a609e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
12 changes: 4 additions & 8 deletions src/data_structure/postprocess_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,13 @@ function postprocess_results!(m::Model)
end

function save_connection_avg_throughflow!(m::Model)
if haskey(m.ext[:spineopt].values, :connection_flow)
@fetch connection_flow = m.ext[:spineopt].values
_save_connection_avg_throughflow!(m, :connection_avg_throughflow, connection_flow)
end
@fetch connection_flow = m.ext[:spineopt].values
_save_connection_avg_throughflow!(m, :connection_avg_throughflow, connection_flow)
end

function save_connection_avg_intact_throughflow!(m::Model)
if haskey(m.ext[:spineopt].values, :connection_intact_flow)
@fetch connection_intact_flow = m.ext[:spineopt].values
_save_connection_avg_throughflow!(m, :connection_avg_intact_throughflow, connection_intact_flow)
end
@fetch connection_intact_flow = m.ext[:spineopt].values
_save_connection_avg_throughflow!(m, :connection_avg_intact_throughflow, connection_intact_flow)
end

function _save_connection_avg_throughflow!(m::Model, key, connection_flow)
Expand Down
22 changes: 21 additions & 1 deletion src/data_structure/preprocess_data_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function preprocess_data_structure(; log_level=3)
generate_report()
generate_report__output()
generate_model__report()
add_required_outputs()
process_lossless_bidirectional_connections()
# NOTE: generate direction before doing anything that calls `connection__from_node` or `connection__to_node`,
# so we don't corrupt the lookup cache
Expand Down Expand Up @@ -747,7 +748,26 @@ Generate a default `report` object, only if no report objects exist.
"""
function generate_report()
isempty(report()) || return
add_objects!(report, [Object(:default_report)])
add_objects!(report, [Object(:default_report, :report)])
end

"""
add_required_outputs()
Add outputs that are required for calculating other outputs.
"""
function add_required_outputs()
required_output_names = Dict(
:connection_avg_throughflow => :connection_flow,
:connection_avg_intact_throughflow => :connection_intact_flow,
)
for r in report()
new_output_names = (get(required_output_names, out.name, nothing) for out in report__output(report=r))
new_output_names = [n for n in new_output_names if n !== nothing]
isempty(new_output_names) && continue
add_objects!(output, [Object(n, :output) for n in new_output_names])
add_relationships!(report__output, [(r, output(n)) for n in new_output_names])
end
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/run_spineopt_benders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function rerun_spineopt_benders!(
m_mp; log_level=log_level
)
end
optimize_model!(m_mp; log_level=log_level) || break
optimize_model!(m_mp; log_level=log_level, save_outputs=false) || break
@timelog log_level 2 "Processing master problem solution" process_master_problem_solution!(m_mp)
k = 1
subproblem_solved = nothing
Expand Down
10 changes: 6 additions & 4 deletions src/run_spineopt_standard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ end
Optimize the given model.
If an optimal solution is found, save results and return `true`, otherwise return `false`.
"""
function optimize_model!(m::Model; log_level=3, calculate_duals=false, iterations=nothing)
function optimize_model!(m::Model; log_level=3, calculate_duals=false, save_outputs=true, iterations=nothing)
write_mps_file(model=m.ext[:spineopt].instance) == :write_mps_always && write_to_file(m, "model_diagnostics.mps")
# NOTE: The above results in a lot of Warning: Variable connection_flow[...] is mentioned in BOUNDS,
# but is not mentioned in the COLUMNS section.
Expand All @@ -359,7 +359,10 @@ function optimize_model!(m::Model; log_level=3, calculate_duals=false, iteration
m; iterations=iterations
)
calculate_duals && _calculate_duals(m; log_level=log_level)
@timelog log_level 2 "Saving outputs..." _save_outputs!(m; iterations=iterations)
if save_outputs
@timelog log_level 2 "Postprocessing results..." postprocess_results!(m)
@timelog log_level 2 "Saving outputs..." _save_outputs!(m; iterations=iterations)
end
else
m.ext[:spineopt].has_results[] = false
@warn "no solution available for window $(current_window(m)) - moving on..."
Expand Down Expand Up @@ -391,7 +394,6 @@ Save a model results: first postprocess results, then save variables and objecti
function _save_model_results!(m; iterations=nothing)
_save_variable_values!(m)
_save_objective_values!(m)
postprocess_results!(m)
end

"""
Expand Down Expand Up @@ -555,7 +557,7 @@ function _save_output!(m, out, value_or_param, crop_to_window; iterations=nothin
by_entity_non_aggr = _value_by_entity_non_aggregated(m, value_or_param, crop_to_window)
for (entity, by_analysis_time_non_aggr) in by_entity_non_aggr
if !isnothing(iterations)
# FIXME: Needs to be done, befooooore we execute solve, as we need to set objective for this solve
# FIXME: Needs to be done before we solve, as we need to set objective for this solve
new_mga_name = Symbol(string("mga_it_", iterations))
if mga_iteration(new_mga_name) == nothing
new_mga_i = Object(new_mga_name)
Expand Down

0 comments on commit f3a609e

Please sign in to comment.