Skip to content

Commit

Permalink
clean up Switch in split-sign constraints, also equal_value_constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
exaexa committed Jan 17, 2024
1 parent b6b6845 commit abb81e4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/builders/compare.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

"""
Expand Down
7 changes: 5 additions & 2 deletions src/builders/unsigned.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion src/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit abb81e4

Please sign in to comment.