From fd5095b0d3ed45fba27cc6a95a19b621ac0575c6 Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Tue, 20 Feb 2024 17:51:52 +0000
Subject: [PATCH] Treat TR as normal variable

---
 src/containers/sequences.jl | 43 ++++---------------------------------
 1 file changed, 4 insertions(+), 39 deletions(-)

diff --git a/src/containers/sequences.jl b/src/containers/sequences.jl
index b191cc9..3ff4625 100644
--- a/src/containers/sequences.jl
+++ b/src/containers/sequences.jl
@@ -4,7 +4,7 @@ Defines [`BaseSequence`](@ref) and [`Sequence`](@ref)
 module Sequences
 import StaticArrays: SVector
 import JuMP: @constraint
-import ...Variables: get_free_variable, TR, VariableType, duration, variables, VariableNotAvailable, Variables
+import ...Variables: get_free_variable, TR, VariableType, duration, variables, VariableNotAvailable, Variables, set_simple_constraints!
 import ...BuildSequences: global_model
 import ...Components: EventComponent
 import ..Abstract: ContainerBlock, start_time
@@ -96,49 +96,15 @@ Defines an MRI sequence from a vector of building blocks.
 """
 struct Sequence{S, N} <: BaseSequence{N}
     blocks :: SVector{N, Pair{<:Union{Symbol, Nothing}, <:ContainerBlock}}
-    TR :: VariableType
 end
 
-function Sequence(blocks::AbstractVector; TR=:min, name=:Sequence)
+function Sequence(blocks::AbstractVector; name=:Sequence, variables...)
     blocks = to_block_pair.(blocks)
-    actual_duration = sum(pair -> duration(pair[2]), blocks; init=0.)
-    if TR == :min
-        TR = actual_duration
-    end
-    res = Sequence{name, length(blocks)}(SVector{length(blocks)}(blocks), get_free_variable(TR))
-    if !(res.TR isa Number) || !(duration(res) isa Number)
-        @constraint global_model() res.TR >= actual_duration
-    end
+    res = Sequence{name, length(blocks)}(SVector{length(blocks)}(blocks))
+    set_simple_constraints!(res, variables)
     return res
 end
 
-for fn in keys(variables)
-    if fn in (:duration, :TR)
-        continue
-    end
-    @eval function Variables.$fn(seq::Sequence)
-        res = []
-        for (name, block) in seq.blocks
-            if isnothing(name)
-                continue
-            end
-            try
-                push!(res, name => Variables.$fn(block))
-            catch e
-                if e isa VariableNotAvailable
-                    continue
-                end
-                rethrow()
-            end
-        end
-        if length(res) > 0
-            return NamedTuple(res)
-        else
-            error("None of the building blocks of $(typeof(seq)) define $(Variables.$fn)")
-        end
-    end
-end
-
 Base.show(io::IO, ::Type{<:Sequence{S, N}}) where {S, N} = print(io, S, "{$N}")
 
 Sequence(blocks...; kwargs...) = Sequence([blocks...]; kwargs...)
@@ -146,7 +112,6 @@ Sequence(blocks...; kwargs...) = Sequence([blocks...]; kwargs...)
 get_index_single_TR(s::Sequence, i::Integer) = s.blocks[i][2]
 Base.getindex(seq::Sequence, sym::Symbol) = seq[findfirst(p -> p[1] == sym, seq.blocks)]
 nrepeat(::Sequence) = 0
-TR(s::Sequence) = s.TR
 
 to_block_pair(pair::Pair) = pair[1] => to_block(pair[2])
 to_block_pair(other) = nothing => to_block(other)
-- 
GitLab