Skip to content

Commit

Permalink
Use MOI.CountDistinct (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Oct 9, 2024
1 parent 71ca3c7 commit 2fb38a2
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 36 deletions.
4 changes: 2 additions & 2 deletions docs/src/mappings/hakank.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ All the constraints that are currently defined in [hakank's constraints_utils.jl
* `decreasing_strict`: `Strictly(Decreasing)`
* `all_different_except_c`: `AllDifferentExceptConstants`
* `count_ctr`: `Count` (except for operators)
* `count_ctr2`: `CountDistinct` (except for operators)
* `count_ctr2`: `MOI.CountDistinct` (except for operators)
* `global_cardinality_count`: `Count`
* `either_eq`: `Disjunction`
* `is_member_of`: `Domain` and `Membership`
Expand All @@ -29,4 +29,4 @@ All the constraints that are currently defined in [hakank's constraints_utils.jl
* `lex_less_eq`: `LexicographicallyLessThan`
* `among`: `Count`

Functions: TODO
Functions: TODO
1 change: 0 additions & 1 deletion docs/src/reference/sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ KnapsackValueType
```@docs
Count
CountCompare
CountDistinct
```

### Global cardinality
Expand Down
8 changes: 4 additions & 4 deletions src/Test/test_countdistinct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function test_countdistinct_vectorofvariables(
) where {T <: Real}
@MOIT.requires MOI.supports_constraint(model, MOI.VariableIndex, MOI.Integer) # x1, x2, x3, x4
@MOIT.requires MOI.supports_constraint(model, MOI.VariableIndex, MOI.EqualTo{T}) # c1, c2, c3
@MOIT.requires MOI.supports_constraint(model, MOI.VectorOfVariables, CP.CountDistinct) # c4
@MOIT.requires MOI.supports_constraint(model, MOI.VectorOfVariables, MOI.CountDistinct) # c4

x1, _ = MOI.add_constrained_variable(model, MOI.Integer())
x2, _ = MOI.add_constrained_variable(model, MOI.Integer())
Expand All @@ -15,7 +15,7 @@ function test_countdistinct_vectorofvariables(
c2 = MOI.add_constraint(model, x2, MOI.EqualTo(1))
c3 = MOI.add_constraint(model, x3, MOI.EqualTo(2))

c4 = MOI.add_constraint(model, MOI.VectorOfVariables([x4, x1, x2, x3]), CP.CountDistinct(3))
c4 = MOI.add_constraint(model, MOI.VectorOfVariables([x4, x1, x2, x3]), MOI.CountDistinct(4))

@test MOI.is_valid(model, x1)
@test MOI.is_valid(model, x2)
Expand Down Expand Up @@ -57,7 +57,7 @@ function test_countdistinct_vectoraffinefunction(
) where {T <: Real}
@MOIT.requires MOI.supports_constraint(model, MOI.VariableIndex, MOI.Integer) # x1, x2, x3, x4
@MOIT.requires MOI.supports_constraint(model, MOI.ScalarAffineFunction{T}, MOI.EqualTo{T}) # c1, c2, c3
@MOIT.requires MOI.supports_constraint(model, MOI.VectorAffineFunction{T}, CP.CountDistinct) # c4
@MOIT.requires MOI.supports_constraint(model, MOI.VectorAffineFunction{T}, MOI.CountDistinct) # c4

x1, _ = MOI.add_constrained_variable(model, MOI.Integer())
x2, _ = MOI.add_constrained_variable(model, MOI.Integer())
Expand All @@ -68,7 +68,7 @@ function test_countdistinct_vectoraffinefunction(
c2 = MOI.add_constraint(model, one(T) * x2, MOI.EqualTo(1))
c3 = MOI.add_constraint(model, one(T) * x3, MOI.EqualTo(2))

c4 = MOI.add_constraint(model, MOIU.vectorize(one(T) .* [x4, x1, x2, x3]), CP.CountDistinct(3))
c4 = MOI.add_constraint(model, MOIU.vectorize(one(T) .* [x4, x1, x2, x3]), MOI.CountDistinct(4))

@test MOI.is_valid(model, x1)
@test MOI.is_valid(model, x2)
Expand Down
26 changes: 1 addition & 25 deletions src/sets_count.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,31 +253,7 @@ end

MOI.dimension(set::CountCompare) = 2 * set.dimension + 1

"""
CountDistinct(dimension::Int)
The first variable in the set is forced to be the number of distinct values in
the rest of the expressions.
This is a relaxed version of `MOI.AllDifferent`; it encodes an `MOI.AllDifferent`
constraint when the first variable is the number of variables in the set.
Also called `nvalues`.
## Example
[x, y, z] in CountDistinct(3)
# x = 1 if y == z, x = 2 if y != z
"""
struct CountDistinct <: MOI.AbstractVectorSet
dimension::Int
end

MOI.dimension(set::CountDistinct) = set.dimension + 1

# isbits types, nothing to copy
function copy(
set::Union{CountCompare, CountDistinct},
)
function copy(set::CountCompare)
return set
end
4 changes: 0 additions & 4 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
# Just a dimension.
@testset "$(S)" for S in [
CP.AllEqual,
MOI.AllDifferent,
CP.SymmetricAllDifferent,
CP.Membership,
CP.ElementVariableArray,
CP.CountDistinct,
CP.Inverse,
CP.Contiguity,
CP.LexicographicallyLessThan,
Expand All @@ -54,7 +52,6 @@

if S in [
CP.AllEqual,
MOI.AllDifferent,
CP.SymmetricAllDifferent,
CP.Membership,
CP.Contiguity,
Expand All @@ -64,7 +61,6 @@
@test MOI.dimension(S(2)) == 2
@test MOI.dimension(S(3)) == 3
elseif S in [
CP.CountDistinct,
CP.MinimumAmong,
CP.MaximumAmong,
CP.ArgumentMinimumAmong,
Expand Down

0 comments on commit 2fb38a2

Please sign in to comment.