Skip to content

Commit

Permalink
Create timing dict for EnergyProblem and time constructor (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsiqueira authored Jun 27, 2024
1 parent 88f2d7e commit 82446a2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ function create_model!(energy_problem; kwargs...)
energy_problem.objective_value = NaN
end

energy_problem.time_create_model = elapsed_time_create_model
energy_problem.timings["creating the model"] = elapsed_time_create_model

return energy_problem
end
Expand Down
8 changes: 2 additions & 6 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ the `EnergyProblem` structure.
Set `strict = true` to error if assets are missing from partition data.
"""
function create_energy_problem_from_csv_folder(input_folder::AbstractString; strict = false)
elapsed_time_read_data = @elapsed begin
connection = create_connection_and_import_from_csv_folder(input_folder)
energy_problem = EnergyProblem(connection; strict = strict)
end
energy_problem.time_read_data = elapsed_time_read_data
return energy_problem
connection = create_connection_and_import_from_csv_folder(input_folder)
return EnergyProblem(connection; strict = strict)
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/solve-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function solve_model!(
end
end

energy_problem.time_solve_model = elapsed_time_solve_model
energy_problem.timings["solving the model"] = elapsed_time_solve_model

return energy_problem.solution
end
Expand Down
46 changes: 29 additions & 17 deletions src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,7 @@ It hides the complexity behind the energy problem, making the usage more friendl
- `solved`: A boolean indicating whether the `model` has been solved or not.
- `objective_value`: The objective value of the solved problem.
- `termination_status`: The termination status of the optimization model.
- `time_read_data`: Time taken for reading the data (in seconds).
- `time_create_model`: Time taken for creating the model (in seconds).
- `time_solve_model`: Time taken for solving the model (in seconds).
- `timings`: Dictionary of elapsed time for various parts of the code (in seconds).
# Constructor
- `EnergyProblem(graph, representative_periods, timeframe)`: Constructs a new `EnergyProblem` object with the given graph, representative periods, and timeframe. The `constraints_partitions` field is computed from the `representative_periods`, and the other fields are initialized with default values.
Expand Down Expand Up @@ -289,9 +286,7 @@ mutable struct EnergyProblem
solved::Bool
objective_value::Float64
termination_status::JuMP.TerminationStatusCode
time_read_data::Float64
time_create_model::Float64
time_solve_model::Float64
timings::Dict{String,Float64}

"""
EnergyProblem(connection)
Expand All @@ -300,11 +295,17 @@ mutable struct EnergyProblem
This will call relevant functions to generate all input that is required for the model creation.
"""
function EnergyProblem(connection; strict = false)
table_tree = create_input_dataframes(connection; strict = strict)
graph, representative_periods, timeframe = create_internal_structures(table_tree)
constraints_partitions = compute_constraints_partitions(graph, representative_periods)
elapsed_time_df = @elapsed begin
table_tree = create_input_dataframes(connection; strict = strict)
end
elapsed_time_internal = @elapsed begin
graph, representative_periods, timeframe = create_internal_structures(table_tree)
end
elapsed_time_cons = @elapsed begin
constraints_partitions = compute_constraints_partitions(graph, representative_periods)
end

return new(
energy_problem = new(
connection,
table_tree,
graph,
Expand All @@ -317,22 +318,33 @@ mutable struct EnergyProblem
false,
NaN,
JuMP.OPTIMIZE_NOT_CALLED,
NaN,
NaN,
NaN,
Dict(
"creating input dataframes" => elapsed_time_df,
"creating internal structures" => elapsed_time_internal,
"computing constraints partitions" => elapsed_time_cons,
),
)

return energy_problem
end
end

function Base.show(io::IO, ep::EnergyProblem)
status_model_creation = !isnothing(ep.model)
status_model_solved = ep.solved

timing_str(prefix, field) = begin
t = get(ep.timings, field, "-")
"$prefix $field (in seconds): $t"
end

println(io, "EnergyProblem:")
println(io, " - Time for reading the data (in seconds): ", ep.time_read_data)
println(io, " - ", timing_str("Time", "creating input dataframes"))
println(io, " - ", timing_str("Time", "creating internal structures"))
println(io, " - ", timing_str("Time", "computing constraints partitions"))
if status_model_creation
println(io, " - Model created!")
println(io, " - Time for creating the model (in seconds): ", ep.time_create_model)
println(io, " - ", timing_str("Time for ", "creating the model"))
println(io, " - Number of variables: ", JuMP.num_variables(ep.model))
println(
io,
Expand All @@ -350,7 +362,7 @@ function Base.show(io::IO, ep::EnergyProblem)
end
if status_model_solved
println(io, " - Model solved! ")
println(io, " - Time for solving the model (in seconds): ", ep.time_solve_model)
println(io, " - ", timing_str("Time for ", "solving the model"))
println(io, " - Termination status: ", ep.termination_status)
println(io, " - Objective value: ", ep.objective_value)
elseif !status_model_solved && ep.termination_status == JuMP.INFEASIBLE
Expand Down

2 comments on commit 82446a2

@datejada
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/109992

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.0 -m "<description of version>" 82446a2720416ab00753d6b76b37610cbfa2eecd
git push origin v0.9.0

Please sign in to comment.