diff --git a/src/builders/compare.jl b/src/builders/compare.jl index 7666f0b7e..5705bea04 100644 --- a/src/builders/compare.jl +++ b/src/builders/compare.jl @@ -29,7 +29,7 @@ $(TYPEDSIGNATURES) A constraint that makes sure that the values of `a` and `b` are the same. """ -same_value_constraint(a, b) = difference_constraint(a, b, 0) +equal_value_constraint(a, b) = difference_constraint(a, b, 0) """ $(TYPEDSIGNATURES) @@ -39,9 +39,9 @@ value of `a`. Names in the output `ConstraintTree` match the names in the `tree`. """ -all_same_constraints(a, tree::C.ConstraintTree) = +all_equal_constraints(a, tree::C.ConstraintTree) = C.map(tree) do b - same_value_constraint(a, b) + equal_value_constraint(a, b) end """ diff --git a/src/builders/unsigned.jl b/src/builders/unsigned.jl index fb0a46a91..ff796f81e 100644 --- a/src/builders/unsigned.jl +++ b/src/builders/unsigned.jl @@ -32,7 +32,7 @@ sign_split_constraints(; signed::C.ConstraintTree, ) = C.zip(positive, negative, signed, C.Constraint) do p, n, s - C.Constraint(s.value + n.value - p.value, 0.0) + equal_value_constraint(s.value + n.value, p.value, 0.0) end #TODO the construction needs an example in the docs. @@ -43,7 +43,10 @@ positive_bound_contribution(b::C.Between) = b.lower >= 0 && b.upper >= 0 ? b : b.lower <= 0 && b.upper <= 0 ? C.EqualTo(0) : C.Between(max(0, b.lower), max(0, b.upper)) -# TODO binary doesn't really fit here but it would be great if it could. +positive_bound_contribution(b::C.Switch) = + let upper_bound = max(b.a, b.b) + upper_bound > 0 ? C.Between(0.0, upper_bound) : C.EqualTo(0.0) + end unsigned_positive_contribution_variables(cs::C.ConstraintTree) = C.variables_for(c -> positive_bound_contribution(c.bound), cs) diff --git a/src/solver.jl b/src/solver.jl index ad8032eef..7cb6ea903 100644 --- a/src/solver.jl +++ b/src/solver.jl @@ -25,9 +25,15 @@ variable, and the value is constrained to equal `a + boolean_var * (b-a)`. Switches can be offset by adding real numbers, negated, and multiplied and divided by scalar constraints. For optimizing some special cases, multiplying by exact zero returns an equality bound to zero. + +# Fields +$(TYPEDFIELDS) """ -struct Switch <: C.Bound +Base.@kwdef mutable struct Switch + "One choice" a::Float64 + + "The other choice" b::Float64 end