Skip to content

Commit

Permalink
Introduce window_sum to fix min_res_gen constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Aug 9, 2023
1 parent 64bb32a commit 628ad67
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 63 deletions.
27 changes: 3 additions & 24 deletions src/constraints/constraint_mp_any_invested_cuts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ function add_constraint_mp_any_invested_cuts!(m::Model)
+ units_invested_available[u, s, t]
- internal_fix_units_invested_available(unit=u, stochastic_scenario=s, t=t)
)
* sum(
Iterators.filter(
!isnan,
units_invested_available_mv(unit=u, stochastic_scenario=s, t=t)
for t in t_highest_resolution(t_overlaps_t(m; t))
);
init=0
)
* window_sum(units_invested_available_mv(unit=u, stochastic_scenario=s), t)
for (u, s, t) in units_invested_available_indices(m)
init=0,
)
Expand All @@ -62,14 +55,7 @@ function add_constraint_mp_any_invested_cuts!(m::Model)
+ connections_invested_available[c, s, t]
- internal_fix_connections_invested_available(connection=c, stochastic_scenario=s, t=t)
)
* sum(
Iterators.filter(
!isnan,
connections_invested_available_mv(connection=c, stochastic_scenario=s, t=t)
for t in t_highest_resolution(t_overlaps_t(m; t))
);
init=0
)
* window_sum(connections_invested_available_mv(connection=c, stochastic_scenario=s))
for (c, s, t) in connections_invested_available_indices(m)
init=0,
)
Expand All @@ -79,14 +65,7 @@ function add_constraint_mp_any_invested_cuts!(m::Model)
+ storages_invested_available[n, s, t]
- internal_fix_storages_invested_available(node=n, stochastic_scenario=s, t=t)
)
* sum(
Iterators.filter(
!isnan,
storages_invested_available_mv(node=n, stochastic_scenario=s, t=t)
for t in t_highest_resolution(t_overlaps_t(m; t))
);
init=0
)
* window_sum(storages_invested_available_mv(node=n, stochastic_scenario=s))
for (n, s, t) in storages_invested_available_indices(m)
init=0,
)
Expand Down
57 changes: 19 additions & 38 deletions src/constraints/constraint_mp_min_res_gen_to_demand_ratio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,65 +38,46 @@ function add_constraint_mp_min_res_gen_to_demand_ratio!(m::Model)
(commodity=comm,) => @constraint(
m,
+ sum(
Iterators.filter(
!isnan,
sp_unit_flow(unit=u, node=n, direction=d, stochastic_scenario=s, t=t, _default=0)
for (u, n, d, s, t) in unit_flow_indices(
m;
unit=unit(is_renewable=true),
node=node__commodity(commodity=comm),
direction=direction(:to_node)
)
);
window_sum(sp_unit_flow(unit=u, node=n, direction=d, stochastic_scenario=s), current_window(m))
for (u, s) in unit_stochastic_indices(m; unit=unit(is_renewable=true))
for (u, n, d) in unit__to_node(unit=u, node=node__commodity(commodity=comm), _compact=false);
init=0
)
+ sum(
(
+ units_invested_available[u, s, t]
- sum(
Iterators.filter(
!isnan,
internal_fix_units_invested_available(unit=u, stochastic_scenario=s, t=t, _default=0)
)
)
- internal_fix_units_invested_available(unit=u, stochastic_scenario=s, t=t, _default=0)
)
* sum(
Iterators.filter(
!isnan,
+ unit_availability_factor(unit=u, stochastic_scenario=s, t=t_short)
* unit_capacity(unit=u, node=n, direction=d, stochastic_scenario=s, t=t_short)
* unit_conv_cap_to_flow(unit=u, node=n, direction=d, stochastic_scenario=s, t=t_short)
for (u, n, d, s, t_short) in unit_flow_indices(
m;
unit=u,
node=node__commodity(commodity=comm),
direction=direction(:to_node),
stochastic_scenario=s,
t=t_in_t(m; t_long=t)
)
);
init=0
* window_sum(
+ unit_availability_factor(unit=u, stochastic_scenario=s)
* unit_capacity(unit=u, node=n, direction=d, stochastic_scenario=s)
* unit_conv_cap_to_flow(unit=u, node=n, direction=d, stochastic_scenario=s),
t
)
for (u, s, t) in units_invested_available_indices(m; unit=unit(is_renewable=true));
for (u, s, t) in units_invested_available_indices(m; unit=unit(is_renewable=true))
for (u, n, d) in unit__to_node(unit=u, node=node__commodity(commodity=comm), _compact=false);
init=0,
)
>=
+ mp_min_res_gen_to_demand_ratio(commodity=comm)
* (
sum(
demand(node=n, stochastic_scenario=s, t=t, _default=0)
for (n, s, t) in node_stochastic_time_indices(
window_sum(demand(node=n, stochastic_scenario=s), current_window(m))
for (n, s) in node_stochastic_indices(
m; node=intersect(indices(demand), node__commodity(commodity=comm))
);
init=0
)
+ sum(
+ fractional_demand(node=n, stochastic_scenario=s, t=t, _default=0)
* demand(node=ng, stochastic_scenario=s, t=t)
for (n, s, t) in node_stochastic_time_indices(
window_sum(
fractional_demand(node=n, stochastic_scenario=s) * demand(node=ng, stochastic_scenario=s),
current_window(m)
)
for (n, s) in node_stochastic_indices(
m; node=intersect(indices(fractional_demand), node__commodity(commodity=comm))
)
for ng in groups(n);
init=0
)
)
)
Expand Down
24 changes: 24 additions & 0 deletions src/data_structure/stochastic_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,30 @@ function _active_stochastic_paths(m, unique_active_scenarios)
unique(intersect(path, unique_active_scenarios) for path in full_stochastic_paths)
end

function node_stochastic_indices(m::Model; node=anything, stochastic_scenario=anything)
unique(
(node=n, stochastic_scenario=s)
for n in intersect(SpineOpt.node(), node)
for (m_, ss) in model__stochastic_structure(
model=m.ext[:spineopt].instance, stochastic_structure=node__stochastic_structure(node=n), _compact=false,
)
for s in stochastic_structure__stochastic_scenario(stochastic_structure=ss)
)
end

function unit_stochastic_indices(m::Model; unit=anything, stochastic_scenario=anything)
unique(
(unit=u, stochastic_scenario=s)
for u in intersect(SpineOpt.unit(), unit)
for (m_, ss) in model__stochastic_structure(
model=m.ext[:spineopt].instance,
stochastic_structure=units_on__stochastic_structure(unit=u),
_compact=false,
)
for s in stochastic_structure__stochastic_scenario(stochastic_structure=ss)
)
end

function stochastic_time_indices(
m::Model;
stochastic_scenario=anything,
Expand Down
5 changes: 4 additions & 1 deletion src/util/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,7 @@ function print_solution(m, variable_patterns...)
println(v, " = ", value(v))
end
println()
end
end

window_sum(ts::TimeSeries, window; init=0) = sum(v for (t, v) in ts if iscontained(t, window) && !isnan(v); init=init)
window_sum(x::Number, window; init=0) = x + init

0 comments on commit 628ad67

Please sign in to comment.