From 4155ecb9bdb81e40fb722c319e4d4482174fefa1 Mon Sep 17 00:00:00 2001 From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk> Date: Sat, 27 Jan 2024 16:40:01 +0000 Subject: [PATCH] Convert every part of Sequence to BuildingBlocks --- src/build_sequences.jl | 2 +- src/building_blocks.jl | 8 ++++++++ src/containers/sequences.jl | 4 ++-- src/readouts/instant_readouts.jl | 3 ++- src/wait.jl | 11 ++++++++++- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/build_sequences.jl b/src/build_sequences.jl index 84b62f5..8392f06 100644 --- a/src/build_sequences.jl +++ b/src/build_sequences.jl @@ -1,5 +1,5 @@ module BuildSequences -import JuMP: Model, optimizer_with_attributes +import JuMP: Model, optimizer_with_attributes, optimize! import Ipopt import Juniper diff --git a/src/building_blocks.jl b/src/building_blocks.jl index ef275c5..476fa7a 100644 --- a/src/building_blocks.jl +++ b/src/building_blocks.jl @@ -32,6 +32,14 @@ The [`BuildingBlock`](@ref) is defined by one or more indices as defined below. end_time(bb) = duration(bb) +""" + to_block(object) + +Function used internally to convert a wide variety of objects into [`BuildingBlock`](@ref) objects. +""" +function to_block end + + """ scanner_constraints!([model, ]building_block, scanner) diff --git a/src/containers/sequences.jl b/src/containers/sequences.jl index 8c91cf0..c5a6f37 100644 --- a/src/containers/sequences.jl +++ b/src/containers/sequences.jl @@ -2,7 +2,7 @@ module Sequences import JuMP: Model import ...BuildSequences: @global_model_constructor import ...Variables: variables, start_time, duration, VariableType -import ...BuildingBlocks: BuildingBlock +import ...BuildingBlocks: BuildingBlock, to_block """ Sequence(building_blocks...) @@ -17,7 +17,7 @@ end @global_model_constructor Sequence -Sequence(model::Model, blocks...) = Sequence(model, blocks) +Sequence(model::Model, blocks::BuildingBlock...) = Sequence(model, to_block.(blocks)) Base.length(seq::Sequence) = length(seq) Base.getindex(seq::Sequence, index) = seq[index] diff --git a/src/readouts/instant_readouts.jl b/src/readouts/instant_readouts.jl index 6f3bf4c..56727f5 100644 --- a/src/readouts/instant_readouts.jl +++ b/src/readouts/instant_readouts.jl @@ -1,5 +1,5 @@ module InstantReadouts -import ...BuildingBlocks: BuildingBlock +import ...BuildingBlocks: BuildingBlock, to_block import ...ConcreteBlocks: AbstractConcreteBlock, to_concrete_block import ...Variables: variables @@ -15,4 +15,5 @@ end variables(::Type{<:InstantReadout}) = [] to_concrete_block(::InstantReadout) = InstantReadout() +to_block(::Type{<:InstantReadout}) = InstantReadout() end \ No newline at end of file diff --git a/src/wait.jl b/src/wait.jl index 678b7ff..039098e 100644 --- a/src/wait.jl +++ b/src/wait.jl @@ -1,7 +1,7 @@ module Wait import JuMP: Model, @constraint, @variable, VariableRef, owner_model, value import ..Variables: VariableType, variables, duration, get_free_variable -import ..BuildingBlocks: BuildingBlock +import ..BuildingBlocks: BuildingBlock, to_block import ..ConcreteBlocks: to_concrete_block, ConcreteBlock import ..BuildSequences: @global_model_constructor import ...Scanners: Scanner @@ -34,6 +34,15 @@ function WaitBlock(model::Model, duration=nothing) return res end +""" + to_block(JuMP variable/:min or :max/nothing/number) + +Converts object into a [`WaitBlock`](@ref). +- if a Number or a JuMP variable the wait time will match that value. +- if `:min`, the wait time will be minimised in the final sequence (limited by any external constraints). +- if `:max`, the wait time will be maximised in the final sequence (limited by any external constraints). +- if `nothing`, the wait time is only constrained by external factors. +""" to_block(time::Union{VariableType, Symbol, Nothing, Val{:min}, Val{:max}}) = WaitBlock(time) -- GitLab