Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Change indices function in the fixed_om_costs function" #1132

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/objective/fixed_om_costs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,7 @@ function fixed_om_costs(m, t_range)
# an extended period of time with a weight >=1, e.g. a representative month represents 3 months.
* duration(t)
for (u, ng, d) in indices(unit_capacity; unit=indices(fom_cost))
for (u, s, t) in Iterators.flatten(
u in intersect(indices(candidate_units), members(u)) ?
(units_invested_available_indices(m; unit=u, t=t_range),) :
((
(u, s, t) for (u, _n, _d, s, t)
in unit_flow_indices(m; unit=u, node=ng, direction=d, t=t_range)
),)
);
for (u, _n, _d, s, t) in unit_flow_indices(m; unit=u, node=ng, direction=d, t=t_range);
init=0,
)
)
Expand Down
157 changes: 18 additions & 139 deletions test/objective/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#############################################################################

function _test_objective_setup()
@testset "objective" begin
url_in = "sqlite://"
test_data = Dict(
:objects => [
Expand Down Expand Up @@ -66,13 +66,8 @@ function _test_objective_setup()
Dict("type" => "duration", "data" => "1h"),
]],
)
_load_test_data(url_in, test_data)
url_in
end

function test_fom_cost_case_1()
@testset "fom_cost case 1" begin
url_in = _test_objective_setup()
@testset "fixed_om_costs" begin
_load_test_data(url_in, test_data)
unit_capacity = 100
fom_cost = 8
number_of_units = 2
Expand Down Expand Up @@ -109,85 +104,8 @@ function test_fom_cost_case_1()
observed_obj = objective_function(m)
@test observed_obj == expected_obj
end
end

function test_fom_cost_case_2()
@testset "fom_cost case 2" begin
url_in = _test_objective_setup()
unit_capacity = 100
fom_cost = 8
number_of_units = 0
# The template provides the parameter `number_of_units` with a default value of 1.
candidate_units = 3
object_parameter_values = [
["unit", "unit_ab", "fom_cost", fom_cost],
["unit", "unit_ab", "number_of_units", number_of_units],
["unit", "unit_ab", "candidate_units", candidate_units],
]
relationships = [
["unit__investment_temporal_block", ["unit_ab", "hourly"]],
["unit__investment_stochastic_structure", ["unit_ab", "stochastic"]],
]
relationship_parameter_values = [
["unit__to_node", ["unit_ab", "node_b"], "unit_capacity", unit_capacity],
]
SpineInterface.import_data(
url_in;
relationships=relationships,
object_parameter_values=object_parameter_values,
relationship_parameter_values=relationship_parameter_values
)
m = run_spineopt(url_in; log_level=0, optimize=false)
var_units_invested_available = m.ext[:spineopt].variables[:units_invested_available]

duration = length(time_slice(m; temporal_block=temporal_block(:two_hourly)))
scenarios = (stochastic_scenario(:parent), stochastic_scenario(:child))
time_slices = time_slice(m; temporal_block=temporal_block(:hourly))
expected_obj = fom_cost * unit_capacity * duration *
sum(
(number_of_units + var_units_invested_available[unit(:unit_ab), s, t])
for (s, t) in zip(scenarios, time_slices)
)
observed_obj = objective_function(m)
@test observed_obj == expected_obj
end
end

function test_fom_cost_case_3()
@testset "fom_cost case 3" begin
url_in = _test_objective_setup()
unit_capacity = 100
fom_cost = 8
number_of_units = 2
object_parameter_values = [
["unit", "unit_ab", "fom_cost", fom_cost],
["unit", "unit_ab", "number_of_units", number_of_units],
]
relationship_parameter_values = [
["unit__to_node", ["unit_ab", "node_b"], "unit_capacity", unit_capacity],
]
SpineInterface.import_data(
url_in;
object_parameter_values=object_parameter_values,
relationship_parameter_values=relationship_parameter_values
)
m = run_spineopt(url_in; log_level=0, optimize=false)

duration = length(time_slice(m; temporal_block=temporal_block(:two_hourly)))
scenarios = (stochastic_scenario(:parent), stochastic_scenario(:child))
time_slices = time_slice(m; temporal_block=temporal_block(:hourly))
expected_obj = fom_cost * unit_capacity * duration *
sum(
number_of_units for (s, t) in zip(scenarios, time_slices)
)
observed_obj = objective_function(m)
@test observed_obj == expected_obj
end
end

function test_fuel_cost()
@testset "fuel_cost" begin
url_in = _test_objective_setup()
@testset "fuel_costs" begin
_load_test_data(url_in, test_data)
fuel_cost = 125
relationship_parameter_values = [["unit__to_node", ["unit_ab", "node_b"], "fuel_cost", fuel_cost]]
SpineInterface.import_data(url_in; relationship_parameter_values=relationship_parameter_values)
Expand All @@ -201,11 +119,8 @@ function test_fuel_cost()
expected_obj = fuel_cost * sum(unit_flow[(key..., s, t)...] for (s, t) in zip(scenarios, time_slices))
@test observed_obj == expected_obj
end
end

function test_unit_investment_cost()
@testset "unit_investment_cost" begin
url_in = _test_objective_setup()
@testset "investment_costs" begin
_load_test_data(url_in, test_data)
unit_investment_cost = 1000
candidate_units = 3
object_parameter_values = [
Expand All @@ -227,11 +142,8 @@ function test_unit_investment_cost()
)
@test observed_obj == expected_obj
end
end

function test_node_slack_penalty()
@testset "node_slack_penalty" begin
url_in = _test_objective_setup()
_load_test_data(url_in, test_data)
node_a_slack_penalty = 0.6
node_b_slack_penalty = 0.4
object_parameter_values = [
Expand Down Expand Up @@ -260,11 +172,8 @@ function test_node_slack_penalty()
)
@test observed_obj == expected_obj
end
end

function test_user_constraint_slack_penalty()
@testset "user_constraint_slack_penalty" begin
url_in = _test_objective_setup()
_load_test_data(url_in, test_data)
uc_slack_penalty = 0.6
objects = [["user_constraint", "ucx"]]
relationships = [["node__user_constraint", ["node_a", "ucx"]]]
Expand All @@ -291,11 +200,8 @@ function test_user_constraint_slack_penalty()
)
@test observed_obj == expected_obj
end
end

function test_shut_down_cost()
@testset "shut_down_cost" begin
url_in = _test_objective_setup()
@testset "shut_down_costs" begin
_load_test_data(url_in, test_data)
shut_down_cost = 180
object_parameter_values = [["unit", "unit_ab", "shut_down_cost", shut_down_cost]]
SpineInterface.import_data(url_in; object_parameter_values=object_parameter_values)
Expand All @@ -309,11 +215,8 @@ function test_shut_down_cost()
expected_obj = shut_down_cost * units_shut_down[unit(:unit_ab), s_parent, t2h]
@test observed_obj == expected_obj
end
end

function test_start_up_cost()
@testset "start_up_cost" begin
url_in = _test_objective_setup()
@testset "start_up_costs" begin
_load_test_data(url_in, test_data)
start_up_cost = 220
object_parameter_values = [["unit", "unit_ab", "start_up_cost", start_up_cost]]
SpineInterface.import_data(url_in; object_parameter_values=object_parameter_values)
Expand All @@ -327,11 +230,8 @@ function test_start_up_cost()
expected_obj = start_up_cost * units_started_up[unit(:unit_ab), s_parent, t2h]
@test observed_obj == expected_obj
end
end

function test_vom_cost()
@testset "vom_cost" begin
url_in = _test_objective_setup()
_load_test_data(url_in, test_data)
vom_cost = 150
relationship_parameter_values = [["unit__to_node", ["unit_ab", "node_b"], "vom_cost", vom_cost]]
SpineInterface.import_data(url_in; relationship_parameter_values=relationship_parameter_values)
Expand All @@ -345,11 +245,8 @@ function test_vom_cost()
expected_obj = vom_cost * sum(unit_flow[(key..., s, t)...] for (s, t) in zip(scenarios, time_slices))
@test observed_obj == expected_obj
end
end

function test_connection_flow_cost()
@testset "connection_flow_cost" begin
url_in = _test_objective_setup()
@testset "connection_flow_costs" begin
_load_test_data(url_in, test_data)
connection_flow_cost = 185
objects = [["connection", "connection_ab"]]
relationships = [["connection__to_node", ["connection_ab", "node_b"]]]
Expand All @@ -374,11 +271,8 @@ function test_connection_flow_cost()
)
@test observed_obj == expected_obj
end
end

function test_units_on_cost()
@testset "units_on_cost" begin
url_in = _test_objective_setup()
@testset "units_on_costs" begin
_load_test_data(url_in, test_data)
units_on_cost = 913
object_parameter_values = [["unit", "unit_ab", "units_on_cost", units_on_cost]]
SpineInterface.import_data(url_in; object_parameter_values=object_parameter_values)
Expand All @@ -391,18 +285,3 @@ function test_units_on_cost()
@test observed_obj == expected_obj
end
end

@testset "objective" begin
test_fom_cost_case_1()
test_fom_cost_case_2()
test_fom_cost_case_3()
test_fuel_cost()
test_unit_investment_cost()
test_node_slack_penalty()
test_user_constraint_slack_penalty()
test_shut_down_cost()
test_start_up_cost()
test_vom_cost()
test_connection_flow_cost()
test_units_on_cost()
end
Loading