Skip to content

Commit

Permalink
always add variables with historical_time_slices
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhjy committed May 22, 2024
1 parent 2cefffa commit ca43ceb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 41 deletions.
28 changes: 0 additions & 28 deletions src/data_structure/preprocess_data_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function preprocess_data_structure()
generate_unit_flow_capacity()
generate_connection_flow_capacity()
generate_unit_commitment_parameters()
generate_representative_temporal_block_label()
end

"""
Expand Down Expand Up @@ -992,30 +991,3 @@ function generate_unit_commitment_parameters()
export has_out_of_service_variable
end
end

function generate_representative_temporal_block_label()
_iter_explicit_repr_temporal_block = Iterators.flatten(
(
# represented temporal blocks
indices(representative_periods_mapping),
# representing temporal blocks which may be used as a group
Set(
Iterators.flatten(
groups(temporal_block.(representative_periods_mapping(temporal_block=tb).values))
for tb in indices(representative_periods_mapping)
)
),
)
)
pname = :explicit_representative_temporal_block
add_object_parameter_values!(
temporal_block,
Dict(tb => Dict(pname => parameter_value(true)) for tb in Set(_iter_explicit_repr_temporal_block))
)
add_object_parameter_defaults!(temporal_block, Dict(pname => parameter_value(false)))
explicit_representative_temporal_block = Parameter(pname, [temporal_block])
@eval begin
explicit_representative_temporal_block = $explicit_representative_temporal_block
export explicit_representative_temporal_block
end
end
8 changes: 0 additions & 8 deletions src/data_structure/temporal_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,6 @@ function unit_dynamic_time_indices(
(unit=u, t_before=tb, t_after=ta)
for (u, blk) in units_on__temporal_block(unit=unit, _compact=false)
for (tb, ta) in dynamic_time_indices(m, blk; t_before=t_before, t_after=t_after)
# When representative temporal structure is used, the dynamic time indices of a unit must stay within the
# represented temporal blocks, including the corresponding representing blocks in case any of them leads to
# time slices outside the represented blocks. The reason is that the constraints about unit state transition
# may use variables (e.g. units_on) with time indices only inside the represented temporal blocks, otherwise
# the model can't find a mapping to representative blocks for the outside time indices.
if !(explicit_representative_temporal_block(temporal_block=blk)) || tb in time_slice(
m; temporal_block=members(blk)
)
)
end

Expand Down
11 changes: 6 additions & 5 deletions src/variables/variable_common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ function add_variable!(
initial_value = _nothing_if_empty(initial_value)
fix_value = _nothing_if_empty(fix_value)
internal_fix_value = _nothing_if_empty(internal_fix_value)
# The syntax below will add one extra time slice ahead the defined time indices to ensure mathcing
# the dynamic time indices at the beginning of model horizon.
t = vcat(history_time_slices, time_slice(m))
first_ind = iterate(indices(m; t=t))
K = first_ind === nothing ? Any : typeof(first_ind[1])
# Some indices functions may use as default the temporal_blocks that exclude the history_time_slices.
# This could cause trouble for variables in some constraints (e.g. units_on in constraint_unit_state_transition)
# when using representiative temporal structure.
_iter_indices = Iterators.flatten((indices(m; t=t), indices(m; temporal_block=anything, t=history_time_slices)))
vars = m.ext[:spineopt].variables[name] = Dict{K,Union{VariableRef,AffExpr,Call}}(
ind => _add_variable!(m, name, ind, replacement_value) for ind in indices(m; t=t) if !haskey(ind_map, ind)
ind => _add_variable!(m, name, ind, replacement_value) for ind in Set(_iter_indices) if !haskey(ind_map, ind)
)
inverse_ind_map = Dict(ref_ind => (ind, 1 / coeff) for (ind, (ref_ind, coeff)) in ind_map)
Threads.@threads for ind in collect(keys(vars))
Expand Down Expand Up @@ -110,8 +112,7 @@ function add_variable!(
end
isempty(SpineInterface.indices(representative_periods_mapping)) || merge!(
# When a representative termporal structure is used, the syntax will generate representative periods mapping
# only for the given indices, which miss the head time slice internally generated above to match the dynamic
# time indices at the beginning of model horizon.
# only for the given indices, excluding the internally generated history_time_slice.
vars, _representative_periods_mapping(m, vars, indices)
)
vars
Expand Down

0 comments on commit ca43ceb

Please sign in to comment.