Skip to content

Commit

Permalink
Fix Aqua and JET tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Azzaare committed Oct 4, 2024
1 parent 2f004e0 commit 59da623
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 56 deletions.
26 changes: 14 additions & 12 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ Constraints = "30f324ab-b02d-43f0-b619-e131c61659f7"
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"

[compat]
Aqua = "0.8.7"
ConstraintCommons = "0.2.3"
ConstraintDomains = "0.3.13"
Constraints = "0.5.8"
ExplicitImports = "1.9.0"
Intervals = "1.10.0"
JET = "0.9.9"
JuMP = "1.23.2"
MathOptInterface = "1.31.2"
Test = "1.11.0"
TestItemRunner = "1.0.5"
TestItems = "1.0.0"
Aqua = "0.8"
ConstraintCommons = "0.2"
ConstraintDomains = "0.3.14"
Constraints = "0.5"
ExplicitImports = "1"
Intervals = "1"
JET = "0.9"
JuMP = "1"
MathOptInterface = "1"
Pkg = "1"
Test = "1"
TestItemRunner = "1"
TestItems = "1"
julia = "1.10"

[extras]
Expand Down
22 changes: 17 additions & 5 deletions src/ConstraintExplorer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module ConstraintExplorer

#SECTION - Imports
import ConstraintCommons
import ConstraintDomains: AbstractDomain, domain, RangeDomain, intersect_domains
import ConstraintDomains: AbstractDomain, RangeDomain
import ConstraintDomains: intersect_domains, domain
import ConstraintDomains: Explorer, ExploreSettings, ExplorerState, explore!
import Constraints: concept
import Constraints: concept, USUAL_CONSTRAINTS
import JuMP
import MathOptInterface as MOI
import Intervals: Interval, Closed, Open
import Pkg

import TestItems: @testitem

Expand All @@ -31,8 +33,7 @@ const VAR_TYPES = Union{MOI.ZeroOne, MOI.Integer}
export DiscreteSet

# Export: Constraints
export Error
export Intention, Predicate
export Intention

export AllDifferent
export AllEqual
Expand All @@ -54,6 +55,9 @@ export Ordered
export Regular
export Sum

#Exports: Explorer
export configurations, solutions, non_solutions

#SECTION - Includes
include("MOI_wrapper.jl")
include("variables.jl")
Expand Down Expand Up @@ -82,15 +86,23 @@ include("constraints/sum.jl")
@testitem "ConstraintExplorer" default_imports=false begin
using ConstraintDomains
using ConstraintExplorer
import MathOptInterface as MOI
using JuMP
using Test

explorer = Model(ConstraintExplorer.Optimizer)

@variable(explorer, 1X[1:4]4, Int)

# @constraint(explorer, X in AllDifferent())
@constraint(explorer, X in AllDifferent())
@constraint(explorer, X in DistDifferent())

optimize!(explorer)

@test MOI.get(explorer, MOI.SolverName()) == "Constraint Explorer"

X, X̅ = configurations(explorer)
@info "All Different" X X̅
end

#SECTION - Main function (optional)
Expand Down
18 changes: 10 additions & 8 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end

MOI.is_empty(model::Optimizer) = model.c_max == 0 && model.d_max == 0

struct CompleteSearchLimit <: MOI.AbstractModelAttribute end
struct CompleteSearchLimit <: MOI.AbstractOptimizerAttribute end

function MOI.set(model::Optimizer, ::CompleteSearchLimit, n::Integer)
settings = ExploreSettings(
Expand All @@ -51,7 +51,7 @@ function MOI.get(model::Optimizer, ::CompleteSearchLimit)
return model.explorer.settings.complete_search_limit
end

struct MaxSamplings <: MOI.AbstractModelAttribute end
struct MaxSamplings <: MOI.AbstractOptimizerAttribute end

function MOI.set(model::Optimizer, ::MaxSamplings, n::Integer)
settings = ExploreSettings(
Expand All @@ -68,7 +68,7 @@ function MOI.get(model::Optimizer, ::MaxSamplings)
return model.explorer.settings.max_samplings
end

struct Search <: MOI.AbstractModelAttribute end
struct Search <: MOI.AbstractOptimizerAttribute end

function MOI.set(model::Optimizer, ::Search, s::Symbol)
settings = ExploreSettings(
Expand All @@ -85,7 +85,7 @@ function MOI.get(model::Optimizer, ::Search)
return model.explorer.settings.search
end

struct NumberOfSolutions <: MOI.AbstractModelAttribute end
struct NumberOfSolutions <: MOI.AbstractOptimizerAttribute end

function MOI.set(model::Optimizer, ::NumberOfSolutions, n::Integer)
settings = ExploreSettings(
Expand All @@ -102,10 +102,6 @@ function MOI.get(model::Optimizer, ::NumberOfSolutions)
return model.explorer.settings.solutions_limit
end

struct Configurations <: MOI.AbstractModelAttribute end

MOI.get(model::Optimizer, ::Configurations) = model.configurations

MOI.supports_incremental_interface(::Optimizer) = true

function MOI.copy_to(dest::Optimizer, src::MOI.ModelLike)
Expand All @@ -119,3 +115,9 @@ function MOI.get(::Optimizer, ::MOI.SolverVersion)
_uuid = Base.UUID("5800fd60-8556-4464-8d61-84ebf7a0bedb")
return "v" * string(deps[_uuid].version)
end

solutions(model) = model.moi_backend.optimizer.model.explorer.state.solutions

non_solutions(model) = model.moi_backend.optimizer.model.explorer.state.non_solutions

configurations(model) = solutions(model), non_solutions(model)
8 changes: 1 addition & 7 deletions src/constraints.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
function constraint!(optimizer::Optimizer, f, vars)
cid = optimizer.c_max + 1
optimizer.c_max = cid
@info "debug" f vars
push!(optimizer.concepts, cid => (f, vars))
return cid
end
constraint!(model::Optimizer, f, vars) = push!(model.explorer, (f, vars))

struct MOIIntention{F <: Function} <: MOI.AbstractVectorSet
f::F
Expand Down
4 changes: 2 additions & 2 deletions src/constraints/all_equal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function MOI.add_constraint(
end

function Base.copy(set::MOIAllEqual)
return MOIAllEqual(
copy(set.op), copy(set.pair_vars), copy(set.val), copy(set.dimension),)
val = set.val === nothing ? nothing : copy(set.val)
return MOIAllEqual(set.op, copy(set.pair_vars), val, copy(set.dimension))
end

struct AllEqual{F <: Function, T1 <: Number, T2 <: Union{Nothing, Number}} <:
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/circuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function MOI.add_constraint(
end

function Base.copy(set::MOICircuit)
return MOICircuit(copy(set.op), copy(set.val), copy(set.dimension))
return MOICircuit(set.op, copy(set.val), copy(set.dimension))
end

struct Circuit{F <: Function, T <: Number} <: JuMP.AbstractVectorSet
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/count.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end

function Base.copy(set::MOICount)
return MOICount(
copy(set.op), copy(set.val), copy(set.vals), copy(set.dimension),)
set.op, copy(set.val), copy(set.vals), copy(set.dimension),)
end

struct Count{F <: Function, T1 <: Number, T2 <: Number} <: JuMP.AbstractVectorSet
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/cumulative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

function Base.copy(set::MOICumulative)
return MOICumulative(
copy(set.op), copy(set.pair_vars), copy(set.val), copy(set.dimension),)
set.op, copy(set.pair_vars), copy(set.val), copy(set.dimension),)
end

struct Cumulative{F <: Function, T1 <: Number, T2 <: Number, V <: VecOrMat{T1}} <:
Expand Down
3 changes: 2 additions & 1 deletion src/constraints/element.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function MOI.add_constraint(
end

function Base.copy(set::MOIElement)
return MOIElement(copy(set.id), copy(set.op), copy(set.val), copy(set.dimension))
val = set.val === nothing ? nothing : copy(set.val)
return MOIElement(copy(set.id), set.op, val, copy(set.dimension))
end

struct Element{I <: Integer, F <: Function, T <: Union{Nothing, Number}} <:
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/maximum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function MOI.add_constraint(
end

function Base.copy(set::MOIMaximum)
return MOIMaximum(copy(set.op), copy(set.val), copy(set.dimension))
return MOIMaximum(set.op, copy(set.val), copy(set.dimension))
end

struct Maximum{F <: Function, T <: Number} <: JuMP.AbstractVectorSet
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/minimum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function MOI.add_constraint(
end

function Base.copy(set::MOIMinimum)
return MOIMinimum(copy(set.op), copy(set.val), copy(set.dimension))
return MOIMinimum(set.op, copy(set.val), copy(set.dimension))
end

struct Minimum{F <: Function, T <: Number} <: JuMP.AbstractVectorSet
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/n_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function MOI.add_constraint(
end

function Base.copy(set::MOINValues)
return MOINValues(copy(set.op), copy(set.val), copy(set.vals), copy(set.dimension))
return MOINValues(set.op, copy(set.val), copy(set.vals), copy(set.dimension))
end

struct NValues{F <: Function, T1 <: Number, T2 <: Number, V <: Vector{T2}} <:
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/ordered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function MOI.add_constraint(
end

function Base.copy(set::MOIOrdered)
return MOIOrdered(copy(set.op), copy(set.pair_vars), copy(set.dimension))
return MOIOrdered(set.op, copy(set.pair_vars), copy(set.dimension))
end

struct Ordered{F <: Function, T <: Number, V <: Vector{T}} <:
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/sum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function MOI.add_constraint(
end

function Base.copy(set::MOISum)
return MOISum(copy(set.op), copy(set.pair_vars), copy(set.val), copy(set.dimension))
return MOISum(set.op, copy(set.pair_vars), copy(set.val), copy(set.dimension))
end

struct Sum{F <: Function, T1 <: Number, T2 <: Number, V <: Vector{T1}} <:
Expand Down
21 changes: 11 additions & 10 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function update_domain!(model::Optimizer, vidx, d)
if haskey(model.explorer.domains, vidx)
d₁ = model.explorer.domains[vidx]
d₂ = isempty(d₁) ? d : intersect_domains(d₁, d)
@info "debug 3" d d₁ d₂
model.explorer.domains[vidx] = d₂
else
push!(model.explorer.domains, vidx => d)
Expand All @@ -21,7 +20,9 @@ struct DiscreteSet{T <: Number} <: MOI.AbstractScalarSet
values::Vector{T}
end
DiscreteSet(values) = DiscreteSet(collect(values))
DiscreteSet(values::T...) where {T <: Number} = DiscreteSet(collect(values))
function DiscreteSet(v::T, values::T...) where {T <: Number}
return DiscreteSet(collect(Iterators.flatten([v, values])))
end

Base.copy(set::DiscreteSet) = DiscreteSet(copy(set.values))

Expand Down Expand Up @@ -64,7 +65,6 @@ end

function MOI.add_constraint(optimizer::Optimizer, v::VI, lt::MOI.LessThan{T},
) where {T <: AbstractFloat}
@info "Entering LessThan" v.value
vidx = v.value
push!(optimizer.compare_vars, vidx)
if vidx optimizer.int_vars
Expand All @@ -73,14 +73,12 @@ function MOI.add_constraint(optimizer::Optimizer, v::VI, lt::MOI.LessThan{T},
a = Float64(floatmin(Float32))
d = domain(Interval{Open, Closed}(a, lt.upper))
end
@info "debug" d
update_domain!(optimizer, vidx, d)
return CI{VI, MOI.LessThan{T}}(vidx)
end

function MOI.add_constraint(optimizer::Optimizer, v::VI, gt::MOI.GreaterThan{T},
) where {T <: AbstractFloat}
@info "Entering GreaterThan" v.value
vidx = v.value
push!(optimizer.compare_vars, vidx)
if vidx optimizer.int_vars
Expand All @@ -96,16 +94,20 @@ end
function MOI.add_constraint(optimizer::Optimizer, v::VI, i::MOI.Interval{T},
) where {T <: Real}
vidx = v.value
is_int = MOI.is_valid(optimizer, CI{VI, MOI.Integer}(vidx))
d = make_domain(i.lower, i.upper, Val(is_int ? :range : :inter))
_set_domain!(optimizer, vidx, d)
a, b = i.lower, i.upper
d = if MOI.is_valid(optimizer, CI{VI, MOI.Integer}(vidx))
domain(Int(a):Int(b))
else
domain(Interval{Closed, Closed}(a, b))
end
optimizer.explorer.domains[vidx] = d
return CI{VI, MOI.Interval{T}}(vidx)
end

function MOI.add_constraint(optimizer::Optimizer, v::VI, et::MOI.EqualTo{T},
) where {T <: Number}
vidx = v.value
_set_domain!(optimizer, vidx, et.value)
optimizer.explorer.domains[vidx] = domain(et.value)
return CI{VI, MOI.EqualTo{T}}(vidx)
end

Expand All @@ -116,7 +118,6 @@ function MOI.add_constraint(model::Optimizer, v::VI, ::MOI.Integer)
push!(model.int_vars, vidx)
if vidx model.compare_vars
x = model.explorer.domains[vidx]
@info x.domain model.compare_vars model.int_vars
model.explorer.domains[vidx] = convert(RangeDomain, x)
end
return MOI.ConstraintIndex{VI, MOI.Integer}(vidx)
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ using Test
using TestItemRunner

@testset "Package tests: ConstraintExplorer" begin
# include("Aqua.jl")
# include("ExplicitImports.jl")
# include("JET.jl")
include("Aqua.jl")
include("ExplicitImports.jl")
include("JET.jl")
include("TestItemRunner.jl")
end

0 comments on commit 59da623

Please sign in to comment.