Skip to content

Commit

Permalink
Fix treatment of the 'next t' in constraint unit flow capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Dec 5, 2023
1 parent bdecc3f commit f92cdae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
25 changes: 18 additions & 7 deletions src/constraints/constraint_unit_flow_capacity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function add_constraint_unit_flow_capacity!(m::Model)
) = m.ext[:spineopt].variables
t0 = _analysis_time(m)
m.ext[:spineopt].constraints[:unit_flow_capacity] = Dict(
(unit=u, node=ng, direction=d, stochastic_path=s, t=t, case=case, part=part) => @constraint(
(unit=u, node=ng, direction=d, stochastic_path=s, t=t, t_next=t_next, case=case, part=part) => @constraint(
m,
expr_sum(
unit_flow[u, n, d, s, t_over] * overlap_duration(t_over, t)
Expand All @@ -119,9 +119,7 @@ function add_constraint_unit_flow_capacity!(m::Model)
* _unit_flow_capacity(u, ng, d, s, t0, t)
* units_shut_down[u, s, t_after]
* duration(t_after)
for (u, s, t_after) in units_on_indices(
m; unit=u, stochastic_scenario=s, t=t_before_t(m; t_before=t)
);
for (u, s, t_after) in units_on_indices(m; unit=u, stochastic_scenario=s, t=t_next);
init=0
)
+ expr_sum(
Expand All @@ -145,7 +143,7 @@ function add_constraint_unit_flow_capacity!(m::Model)
init=0
)
)
for (u, ng, d, s, t, case, part) in constraint_unit_flow_capacity_indices(m)
for (u, ng, d, s, t, t_next, case, part) in constraint_unit_flow_capacity_indices(m)
)
end

Expand All @@ -171,17 +169,18 @@ end

function constraint_unit_flow_capacity_indices(m::Model)
unique(
(unit=u, node=ng, direction=d, stochastic_path=subpath, t=t, case=case, part=part)
(unit=u, node=ng, direction=d, stochastic_path=subpath, t=t, t_next=t_next, case=case, part=part)
for (u, ng, d) in indices(unit_capacity)
for t in t_highest_resolution(
Iterators.flatten(
((t for (u, t) in unit_time_indices(m; unit=u)), (t for (ng, t) in node_time_indices(m; node=ng)))
)
)
for t_next in _t_next(m, u, t)
for path in active_stochastic_paths(
m,
[
units_on_indices(m; unit=u, t=[t_overlaps_t(m; t=t); t_before_t(m; t_before=t)]);
units_on_indices(m; unit=u, t=[t_overlaps_t(m; t=t); t_next]);
unit_flow_indices(m; unit=u, node=ng, direction=d, t=t_overlaps_t(m; t=t));
nonspin_units_shut_down_indices(m; unit=u, t=t_overlaps_t(m; t=t))
]
Expand All @@ -192,6 +191,18 @@ function constraint_unit_flow_capacity_indices(m::Model)
)
end

function _t_next(m, u, t)
t_next = unit_time_indices(m; unit=u, t=t_before_t(m; t_before=t))
if isempty(t_next)
# Nothing next, return a tuple with a dummy TimeSlice so things work,
# but essentially it will be as if there is nothing next indeed.
(TimeSlice(end_(t), end_(t)),)
else
(t for (u, t) in t_next)
end
end


"""
_unit_capacity_constraint_subpaths(path, u, t)
Expand Down
6 changes: 2 additions & 4 deletions src/data_structure/temporal_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,7 @@ Generate an `Array` of all valid `(node, t)` `NamedTuples` with keyword argument
function node_time_indices(m::Model; node=anything, temporal_block=anything, t=anything)
unique(
(node=n, t=t1)
for tb in intersect(SpineOpt.temporal_block(), temporal_block)
for (n, tb) in node__temporal_block(node=node, temporal_block=tb, _compact=false)
for (n, tb) in node__temporal_block(node=node, temporal_block=temporal_block, _compact=false)
for t1 in time_slice(m; temporal_block=members(tb), t=t)
)
end
Expand Down Expand Up @@ -627,8 +626,7 @@ function unit_time_indices(
)
unique(
(unit=u, t=t1)
for tb in intersect(SpineOpt.temporal_block(), temporal_block)
for (u, tb) in units_on__temporal_block(unit=unit, temporal_block=tb, _compact=false)
for (u, tb) in units_on__temporal_block(unit=unit, temporal_block=temporal_block, _compact=false)
for t1 in time_slice(m; temporal_block=members(tb), t=t)
)
end
Expand Down
9 changes: 6 additions & 3 deletions test/constraints/constraint_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function test_constraint_unit_flow_capacity()
case_part = (Object(:min_up_time_gt_time_step, :case), Object(:one, :part))
@testset for con_key in keys(constraint)
con = constraint[con_key]
u, n, d, s, t, case, part = con_key
u, n, d, s, t, t_after, case, part = con_key
@test u.name == :unit_ab
@test (n.name, d.name) in ((:node_group_a, :from_node), (:node_group_bc, :to_node))
@test case.name == case_name
Expand All @@ -292,10 +292,13 @@ function test_constraint_unit_flow_capacity()
(:node_group_bc, [s_parent, s_child], t1h1),
(:node_group_bc, [s_parent, s_child], t1h2),
)
t_after = t == t1h1 ? t1h2 : nothing
var_u_on_t = var_units_on[u, s_by_t[t], t]
var_u_su_t = var_units_started_up[u, s_by_t[t], t]
var_u_sd_t_after = isnothing(t_after) ? 0 : var_units_shut_down[u, s_by_t[t_after], t_after]
var_u_sd_t_after = try
var_units_shut_down[u, s_by_t[t_after], t_after]
catch KeyError
0
end
lhs = if n.name == :node_group_a
var_u_flow_a = var_unit_flow[u, node(:node_a), d, s_by_t[t], t]
var_u_flow_reserves_a = dr ? var_unit_flow[u, node(:reserves_a), d, s_by_t[t], t] : 0
Expand Down

0 comments on commit f92cdae

Please sign in to comment.