From 37c69fce67b348d08dc6e02eb29a210644ac1d3a Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Sun, 5 Jan 2025 12:39:09 +0100 Subject: [PATCH] use the broadcasting to make variables() nicer --- src/constraint_tree.jl | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/constraint_tree.jl b/src/constraint_tree.jl index 1493864..189b615 100644 --- a/src/constraint_tree.jl +++ b/src/constraint_tree.jl @@ -1,5 +1,5 @@ -# Copyright (c) 2023-2024, University of Luxembourg +# Copyright (c) 2023-2025, University of Luxembourg # Copyright (c) 2023, Heinrich-Heine University Duesseldorf # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -291,25 +291,14 @@ $(TYPEDSIGNATURES) Make a trivial constraint system that creates variables with indexes in range `1:length(keys)` named in order as given by `keys`. -Parameter `bounds` is either `nothing` for creating variables without bounds -assigned to them, a single bound for creating variables with the same constraint -assigned to them all, or an iterable object of same length as `keys` with -individual bounds for each variable in the same order as `keys`. - -The individual bounds should be subtypes of [`Bound`](@ref), or nothing. To pass -a single bound for all variables, use e.g. `bounds = EqualTo(0)`. -""" -function variables(weight = 1.0; keys::AbstractVector{Symbol}, bounds = nothing) - bs = - isnothing(bounds) ? Base.Iterators.cycle(tuple(nothing)) : - length(bounds) == 1 ? Base.Iterators.cycle(tuple(bounds)) : - length(bounds) == length(keys) ? bounds : - error("lengths of bounds and keys differ for allocated variables") - ConstraintTree( - k => variable(weight; idx = i, bound = b) for - ((i, k), b) in Base.zip(enumerate(keys), bs) - ) -end +The individual bounds should be subtypes of [`Bound`](@ref), or nothing (which +is the default). The bounds are broadcasted; to pass a single bound for all +variables, one can use e.g. `bounds = EqualTo(0)`. +""" +variables(weight = 1.0; keys::AbstractVector{Symbol}, bounds = Ref(nothing)) = + let go((i, k), b) = k => variable(weight, idx = i, bound = b) + ConstraintTree(go.(enumerate(keys), bounds)...) + end """ $(TYPEDSIGNATURES)