Skip to content

Commit e9705d8

Browse files
committed
removing generic process_results function; retrieve model results (including constraint and objective data) from the integral model, then solve the relaxed-integrality model to get constraint dual values; save that data to DB
1 parent f9bba14 commit e9705d8

1 file changed

Lines changed: 67 additions & 25 deletions

File tree

src/agent_choice.jl

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,26 +124,6 @@ function compute_last_year_results(db, settings, CLI_args, agent_params)
124124
end
125125

126126

127-
function process_results(settings, CLI_args, final_model, final_mode, db, PA_uids, unit_specs)
128-
# Ensure model results data is valid and of correct type
129-
all_results = ABCEfunctions.finalize_results_dataframe(final_model, final_mode, PA_uids)
130-
131-
# Display the results
132-
ABCEfunctions.display_agent_choice_results(CLI_args, final_model, all_results)
133-
134-
# Save newly-selected project alternatives happening in the current period
135-
# to the database
136-
ABCEfunctions.postprocess_agent_decisions(
137-
settings,
138-
all_results,
139-
unit_specs,
140-
db,
141-
CLI_args["current_pd"],
142-
CLI_args["agent_id"],
143-
)
144-
end
145-
146-
147127
function save_intermediate_outputs(settings, CLI_args, adj_system_portfolios, long_econ_results)
148128
# Save all system portfolio forecasts to the cnerg groupspace
149129
pfs = deepcopy(adj_system_portfolios[CLI_args["current_pd"]])
@@ -163,6 +143,34 @@ function save_intermediate_outputs(settings, CLI_args, adj_system_portfolios, lo
163143
end
164144

165145

146+
function get_model_results(settings, model, realized_mode, PA_uids; mode="integral")
147+
# Check solve status of model
148+
status = string(termination_status.(model))
149+
150+
if status == "OPTIMAL"
151+
# If the model solved to optimality:
152+
if mode == "integral"
153+
# Convert the results to type Int64
154+
unit_qty = Int64.(round.(value.(model[:u])))
155+
else
156+
unit_qty = value.(model[:u])
157+
end
158+
obj_val = objective_value(model)
159+
else
160+
# If the model did not solve to optimality for any reason
161+
# (infeasibility or error):
162+
# The agent does nothing. Return a vector of all zeroes instead.
163+
unit_qty = zeros(Int64, size(PA_uids)[1])
164+
obj_val = nothing
165+
end
166+
167+
decision_df = hcat(PA_uids, DataFrame(units_to_execute = unit_qty))
168+
decision_df[!, :mode] .= realized_mode
169+
170+
return decision_df, obj_val
171+
end
172+
173+
166174
function run_agent_choice()
167175
@info "Setting up data..."
168176

@@ -302,12 +310,12 @@ function run_agent_choice()
302310

303311
# Solve the model
304312
@info "Solving optimization problem..."
305-
m = ABCEfunctions.solve_model(m, CLI_args["verbosity"])
313+
optimize!(m)
306314

307315
status = string(termination_status.(m))
308316
if status == "OPTIMAL"
309317
final_model = m
310-
final_mode = "normal"
318+
realized_mode = "normal"
311319
else
312320
m_ret = ABCEfunctions.set_up_model(
313321
settings,
@@ -327,15 +335,49 @@ function run_agent_choice()
327335
mode="ret_only"
328336
)
329337

330-
m_ret = ABCEfunctions.solve_model(m_ret, CLI_args["verbosity"])
338+
optimize!(m_ret)
331339

332340
final_model = m_ret
333-
final_mode = "ret_only"
341+
realized_mode = "ret_only"
334342
end
335343

336344
# Process the model outputs
337345
@debug "Postprocessing model results..."
338-
process_results(settings, CLI_args, final_model, final_mode, db, PA_uids, unit_specs)
346+
decision_df, obj_val = get_model_results(settings, final_model, realized_mode, PA_uids; mode="integral")
347+
ABCEfunctions.display_agent_choice_results(CLI_args, final_model, decision_df)
348+
349+
# Re-solve the model with integrality relaxed
350+
undo_relax = relax_integrality(final_model)
351+
optimize!(final_model)
352+
relax_decision_df, relax_obj_val = get_model_results(settings, final_model, realized_mode, PA_uids; mode="relax")
353+
354+
# Save the constraint data (including dual values) for the relaxed model
355+
ABCEfunctions.save_constraint_data(db, final_model, CLI_args["agent_id"], CLI_args["current_pd"])
356+
357+
# Display the integral model results
358+
ABCEfunctions.display_agent_choice_results(CLI_args, final_model, relax_decision_df)
359+
360+
# Save newly-selected project alternatives happening in the current period
361+
# to the database
362+
ABCEfunctions.postprocess_agent_decisions(
363+
settings,
364+
decision_df,
365+
unit_specs,
366+
db,
367+
CLI_args["current_pd"],
368+
CLI_args["agent_id"],
369+
)
370+
371+
# Save relaxed problem results to DB
372+
ABCEfunctions.save_agent_decisions(db, CLI_args["current_pd"], CLI_args["agent_id"], relax_decision_df; mode="relax")
373+
374+
# Save both problems' final objective values to DB
375+
DBInterface.execute(
376+
db,
377+
"INSERT INTO objective_values VALUES (?, ?, ?, ?)",
378+
(CLI_args["agent_id"], CLI_args["current_pd"], obj_val, relax_obj_val),
379+
)
380+
339381
end
340382

341383

0 commit comments

Comments
 (0)