Skip to content

Commit

Permalink
Change the name convention (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
datejada committed Oct 4, 2023
1 parent 2608212 commit 6c3d2a2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
4 changes: 2 additions & 2 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ solution = optimise_investments(parameters, sets)
SUITE["io"]["output"] = @benchmarkable begin
save_solution_to_file(
$OUTPUT_FOLDER,
$(sets.s_assets_investment),
$(sets.assets_investment),
$(solution.v_investment),
$(parameters.p_unit_capacity),
$(parameters.unit_capacity),
)
end
76 changes: 38 additions & 38 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,84 +22,84 @@ function create_parameters_and_sets_from_file(input_folder::AbstractString)
rep_period_df = CSV.read(rep_period_file, DataFrames.DataFrame; header = 2)

# Sets and subsets that depend on input data
A = s_assets = nodes_data_df[nodes_data_df.active.==true, :].name #assets in the energy system that are active
Ap = s_assets_producer = nodes_data_df[nodes_data_df.type.=="producer", :].name #producer assets in the energy system
Ac = s_assets_consumer = nodes_data_df[nodes_data_df.type.=="consumer", :].name #consumer assets in the energy system
s_assets_investment = nodes_data_df[nodes_data_df.investable.==true, :].name #assets with investment method in the energy system
s_representative_periods = unique(nodes_profiles_df.rep_period_id) #representative periods
s_time_steps = unique(nodes_profiles_df.time_step) #time steps in the RP (e.g., hours)
A = assets = nodes_data_df[nodes_data_df.active.==true, :].name #assets in the energy system that are active
Ap = assets_producer = nodes_data_df[nodes_data_df.type.=="producer", :].name #producer assets in the energy system
Ac = assets_consumer = nodes_data_df[nodes_data_df.type.=="consumer", :].name #consumer assets in the energy system
assets_investment = nodes_data_df[nodes_data_df.investable.==true, :].name #assets with investment method in the energy system
rep_periods = unique(nodes_profiles_df.rep_period_id) #representative periods
time_steps = unique(nodes_profiles_df.time_step) #time steps in the RP (e.g., hours)

# Parameters for system
p_rp_weight = Dict((row.id) => row.weight for row in eachrow(rep_period_df)) #representative period weight [h]
rep_weight = Dict((row.id) => row.weight for row in eachrow(rep_period_df)) #representative period weight [h]

# Parameters for assets
p_profile = Dict(
profile = Dict(
(A[row.id], row.rep_period_id, row.time_step) => row.value for
row in eachrow(nodes_profiles_df)
) # asset profile [p.u.]

# Parameters for producers
p_variable_cost = Dict{String,Float64}()
p_investment_cost = Dict{String,Float64}()
p_unit_capacity = Dict{String,Float64}()
p_init_capacity = Dict{String,Float64}()
variable_cost = Dict{String,Float64}()
investment_cost = Dict{String,Float64}()
unit_capacity = Dict{String,Float64}()
init_capacity = Dict{String,Float64}()
for row in eachrow(nodes_data_df)
if row.name in Ap
p_variable_cost[row.name] = row.variable_cost
p_investment_cost[row.name] = row.investment_cost
p_unit_capacity[row.name] = row.capacity
p_init_capacity[row.name] = row.initial_capacity
variable_cost[row.name] = row.variable_cost
investment_cost[row.name] = row.investment_cost
unit_capacity[row.name] = row.capacity
init_capacity[row.name] = row.initial_capacity
end
end

# Parameters for consumers
p_peak_demand = Dict{String,Float64}()
peak_demand = Dict{String,Float64}()
for row in eachrow(nodes_data_df)
if row.name in Ac
p_peak_demand[row.name] = row.peak_demand
peak_demand[row.name] = row.peak_demand
end
end

params = (
p_init_capacity = p_init_capacity,
p_investment_cost = p_investment_cost,
p_peak_demand = p_peak_demand,
p_profile = p_profile,
p_rp_weight = p_rp_weight,
p_unit_capacity = p_unit_capacity,
p_variable_cost = p_variable_cost,
init_capacity = init_capacity,
investment_cost = investment_cost,
peak_demand = peak_demand,
profile = profile,
rep_weight = rep_weight,
unit_capacity = unit_capacity,
variable_cost = variable_cost,
)
sets = (
s_assets = s_assets,
s_assets_consumer = s_assets_consumer,
s_assets_investment = s_assets_investment,
s_assets_producer = s_assets_producer,
s_representative_periods = s_representative_periods,
s_time_steps = s_time_steps,
assets = assets,
assets_consumer = assets_consumer,
assets_investment = assets_investment,
assets_producer = assets_producer,
rep_periods = rep_periods,
time_steps = time_steps,
)

return params, sets
end

"""
save_solution_to_file(output_file, v_investment, p_unit_capacity)
save_solution_to_file(output_file, v_investment, unit_capacity)
Saves the solution variable v_investment to a file "investments.csv" inside `output_file`.
The format of each row is `a,v,p*v`, where `a` is the asset indexing `v_investment`, `v`
is corresponding `v_investment` value, and `p` is the corresponding `p_unit_capacity` value.
is corresponding `v_investment` value, and `p` is the corresponding `unit_capacity` value.
"""
function save_solution_to_file(
output_folder,
s_assets_investment,
assets_investment,
v_investment,
p_unit_capacity,
unit_capacity,
)
# Writing the investment results to a CSV file
output_file = joinpath(output_folder, "investments.csv")
output_table = DataFrame(;
a = s_assets_investment,
InstalUnits = [v_investment[a] for a in s_assets_investment],
InstalCap_MW = [p_unit_capacity[a] * v_investment[a] for a in s_assets_investment],
a = assets_investment,
InstalUnits = [v_investment[a] for a in assets_investment],
InstalCap_MW = [unit_capacity[a] * v_investment[a] for a in assets_investment],
)
CSV.write(output_file, output_table)

Expand Down
24 changes: 12 additions & 12 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Create and solve the model using the `graph` structure, the parameters and sets.
"""
function optimise_investments(graph, params, sets; verbose = false)
# Sets unpacking
A = sets.s_assets
Ac = sets.s_assets_consumer
# Ap = sets.s_assets_producer
Ai = sets.s_assets_investment
A = sets.assets
Ac = sets.assets_consumer
# Ap = sets.assets_producer
Ai = sets.assets_investment
F = [(A[e.src], A[e.dst]) for e in edges(graph)]
K = sets.s_time_steps
RP = sets.s_representative_periods
K = sets.time_steps
RP = sets.rep_periods

# Model
model = Model(HiGHS.Optimizer)
Expand All @@ -27,15 +27,15 @@ function optimise_investments(graph, params, sets; verbose = false)
e_investment_cost = @expression(
model,
sum(
params.p_investment_cost[a] * params.p_unit_capacity[a] * v_investment[a]
for a in Ai
params.investment_cost[a] * params.unit_capacity[a] * v_investment[a] for
a in Ai
)
)

e_variable_cost = @expression(
model,
sum(
params.p_rp_weight[rp] * params.p_variable_cost[a] * v_flow[f, rp, k] for
params.rep_weight[rp] * params.variable_cost[a] * v_flow[f, rp, k] for
a in A, f in F, rp in RP, k in K if f[1] == a
)
)
Expand All @@ -49,16 +49,16 @@ function optimise_investments(graph, params, sets; verbose = false)
model,
c_balance[a in Ac, rp in RP, k in K],
sum(v_flow[f, rp, k] for f in F if f[2] == a) ==
params.p_profile[a, rp, k] * params.p_peak_demand[a]
params.profile[a, rp, k] * params.peak_demand[a]
)

# - maximum generation
@constraint(
model,
c_max_prod[a in Ai, f in F, rp in RP, k in K; f[1] == a],
v_flow[f, rp, k] <=
get(params.p_profile, (a, rp, k), 1.0) *
(params.p_init_capacity[a] + params.p_unit_capacity[a] * v_investment[a])
get(params.profile, (a, rp, k), 1.0) *
(params.init_capacity[a] + params.unit_capacity[a] * v_investment[a])
)

# print lp file
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const OUTPUT_FOLDER = joinpath(@__DIR__, "outputs")
@test solution.objective_value 269238.43825 atol = 1e-5
save_solution_to_file(
OUTPUT_FOLDER,
sets.s_assets_investment,
sets.assets_investment,
solution.v_investment,
parameters.p_unit_capacity,
parameters.unit_capacity,
)
end

Expand Down

0 comments on commit 6c3d2a2

Please sign in to comment.