@@ -40,10 +40,10 @@ function _cb(lx::AbstractArray{Tx}, ux::AbstractArray{Tx}, lc::AbstractVector{Tc
4040end
4141
4242Base. eltype (:: Type{ConstraintBounds{T}} ) where T = T
43- Base. eltype (cb:: ConstraintBounds ) = eltype (typeof (cb))
4443
44+ Base. convert (:: Type{ConstraintBounds{T}} , cb:: ConstraintBounds{T} ) where {T} = cb
4545Base. convert (:: Type{ConstraintBounds{T}} , cb:: ConstraintBounds{S} ) where {T,S} =
46- ConstraintBounds (cb. nc, cb. eqx, convert (Vector{T}, cb. valx),
46+ ConstraintBounds {T} (cb. nc, cb. eqx, convert (Vector{T}, cb. valx),
4747 cb. ineqx, cb. σx, convert (Vector{T}, cb. bx),
4848 cb. eqc, convert (Vector{T}, cb. valc), cb. ineqc,
4949 cb. σc, convert (Vector{T}, cb. bc))
@@ -80,11 +80,11 @@ function Base.show(io::IO, cb::ConstraintBounds)
8080 indent = " "
8181 print (io, " ConstraintBounds:" )
8282 print (io, " \n Variables:" )
83- showeq (io, indent, cb. eqx, cb. valx, ' x' , :bracket )
84- showineq (io, indent, cb. ineqx, cb. σx, cb. bx, ' x' , :bracket )
83+ showeq (io, indent, cb. eqx, cb. valx, ' x' , true ) # bracket
84+ showineq (io, indent, cb. ineqx, cb. σx, cb. bx, ' x' , true ) # bracket
8585 print (io, " \n Linear/nonlinear constraints:" )
86- showeq (io, indent, cb. eqc, cb. valc, ' c' , :subscript )
87- showineq (io, indent, cb. ineqc, cb. σc, cb. bc, ' c' , :subscript )
86+ showeq (io, indent, cb. eqc, cb. valc, ' c' , false ) # subscript
87+ showineq (io, indent, cb. ineqc, cb. σc, cb. bc, ' c' , false ) # subscript
8888 nothing
8989end
9090
@@ -281,17 +281,17 @@ corresponding entry in `ineq`/`σ`/`b`.
281281T is the element-type of the non-Int outputs
282282"""
283283function parse_constraints (:: Type{T} , l, u) where T
284- size (l) == size (u) || throw (DimensionMismatch (" l and u must be the same size, got $(size (l)) and $(size (u)) " ))
284+ if size (l) != size (u)
285+ throw (DimensionMismatch (LazyString (" Size of lower bounds (" , size (l), " ) and number of upper bounds (" , size (u), " ) must be equal." )))
286+ end
285287 eq, ineq = Int[], Int[]
288+ σ = Int8[]
286289 val, b = T[], T[]
287- σ = Array {Int8} (undef, 0 )
288- for i = 1 : length (l)
289- li, ui = l[i], u[i]
290- li <= ui || throw (ArgumentError (" l must be smaller than u, got $li , $ui " ))
290+ for (i, (li, ui)) in enumerate (zip (l, u))
291291 if li == ui
292292 push! (eq, i)
293293 push! (val, ui)
294- else
294+ elseif li < ui
295295 if isfinite (li)
296296 push! (ineq, i)
297297 push! (σ, 1 )
@@ -302,36 +302,55 @@ function parse_constraints(::Type{T}, l, u) where T
302302 push! (σ, - 1 )
303303 push! (b, ui)
304304 end
305+ else
306+ throw (ArgumentError (LazyString (" Lower bound (" , li, " ) must not be greater than upper bound (" , ui, " )." )))
305307 end
306308 end
307309 eq, val, ineq, σ, b
308310end
309311
310312# ## Compact printing of constraints
311313
312- function showeq (io, indent, eq, val , chr, style )
313- if ! isempty (eq )
314+ function showeq (io:: IO , indent:: String , eqs :: Vector{Int} , vals :: Vector , chr:: Char , bracket :: Bool )
315+ if ! isempty (eqs )
314316 print (io, ' \n ' , indent)
315- if style == :bracket
316- eqstrs = map ((i, v)-> " $chr [$i ]=$v " , eq, val)
317+ if bracket
318+ for (i, (eq, val)) in enumerate (zip (eqs, vals))
319+ if i != 1
320+ print (io, " , " )
321+ end
322+ print (io, chr, " [" , eq, " ]=" , val)
323+ end
317324 else
318- eqstrs = map ((i, v)-> " $(chr) _$i =$v " , eq, val)
325+ for (i, (eq, val)) in enumerate (zip (eqs, vals))
326+ if i != 1
327+ print (io, " , " )
328+ end
329+ print (io, chr, " _" , eq, " =" , val)
330+ end
319331 end
320- foreach (s-> print (io, s * " , " ), eqstrs[1 : end - 1 ])
321- print (io, eqstrs[end ])
322332 end
333+ return nothing
323334end
324335
325- function showineq (io, indent, ineqs, σs, bs, chr, style )
336+ function showineq (io:: IO , indent:: String , ineqs:: Vector{Int} , σs:: Vector{Int8} , bs:: Vector , chr:: Char , bracket :: Bool )
326337 if ! isempty (ineqs)
327338 print (io, ' \n ' , indent)
328- if style == :bracket
329- ineqstrs = map ((i, σ, b)-> " $chr [$i ]" * ineqstr (σ, b), ineqs, σs, bs)
339+ if bracket
340+ for (i, (ineq, σ, b)) in enumerate (zip (ineqs, σs, bs))
341+ if i != 1
342+ print (io, " , " )
343+ end
344+ print (io, chr, " [" , ineq, " ]" , σ > 0 ? ' ≥' : ' ≤' , b)
345+ end
330346 else
331- ineqstrs = map ((i, σ, b)-> " $(chr) _$i " * ineqstr (σ, b), ineqs, σs, bs)
347+ for (i, (ineq, σ, b)) in enumerate (zip (ineqs, σs, bs))
348+ if i != 1
349+ print (io, " , " )
350+ end
351+ print (io, chr, " _" , ineq, σ > 0 ? ' ≥' : ' ≤' , b)
352+ end
332353 end
333- foreach (s-> print (io, s * " , " ), ineqstrs[1 : end - 1 ])
334- print (io, ineqstrs[end ])
335354 end
355+ return nothing
336356end
337- ineqstr (σ, b) = σ > 0 ? " ≥$b " : " ≤$b "
0 commit comments