Skip to content

Commit

Permalink
Core MOI wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Azzaare committed Sep 26, 2024
1 parent b777563 commit 62bebd3
Show file tree
Hide file tree
Showing 24 changed files with 2,793 additions and 1 deletion.
679 changes: 679 additions & 0 deletions Manifest.toml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Jean-François BAFFIER (@Azzaare)"]
version = "0.0.1"

[deps]
ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954"
ConstraintDomains = "5800fd60-8556-4464-8d61-84ebf7a0bedb"
Constraints = "30f324ab-b02d-43f0-b619-e131c61659f7"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
Expand All @@ -13,6 +14,7 @@ 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"
Expand Down
27 changes: 26 additions & 1 deletion src/ConstraintExplorer.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
module ConstraintExplorer

#SECTION - Imports
import ConstraintDomains
import ConstraintCommons
import ConstraintDomains: AbstractDomain, ExploreSettings, explore
import Constraints
import JuMP
import MathOptInterface as MOI

import TestItems: @testitem

#SECTION - Abbr
const VOV = MOI.VectorOfVariables

#SECTION - Exports

#SECTION - Includes
include("MOI_wrapper.jl")
include("constraints.jl")
include("constraints/all_different.jl")
include("constraints/all_equal.jl")
include("constraints/cardinality.jl")
include("constraints/channel.jl")
include("constraints/circuit.jl")
include("constraints/count.jl")
include("constraints/cumulative.jl")
include("constraints/element.jl")
include("constraints/extension.jl")
include("constraints/instantiation.jl")
include("constraints/intention.jl")
include("constraints/maximum.jl")
include("constraints/mdd.jl")
include("constraints/minimum.jl")
include("constraints/n_values.jl")
include("constraints/no_overlap.jl")
include("constraints/ordered.jl")
include("constraints/regular.jl")
include("constraints/sum.jl")

#SECTION - Main function (optional)

Expand Down
157 changes: 157 additions & 0 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
"""
JuMP.build_variable(::Function, info::JuMP.VariableInfo, set::T) where T <: MOI.AbstractScalarSet
Create a variable constrained by a scalar set.
# Arguments
- `info::JuMP.VariableInfo`: Information about the variable to be created.
- `set::T where T <: MOI.AbstractScalarSet`: The set defining the constraints on the variable.
# Returns
- `JuMP.VariableConstrainedOnCreation`: A variable constrained by the specified set.
"""
function JuMP.build_variable(
::Function,
info::JuMP.VariableInfo,
set::T,
) where {T <: MOI.AbstractScalarSet}
return JuMP.VariableConstrainedOnCreation(JuMP.ScalarVariable(info), set)
end

"""
Optimizer <: MOI.AbstractOptimizer
Defines an optimizer for CBLS.
# Fields
- `solver::LS.MainSolver`: The main solver used for local search.
- `int_vars::Set{Int}`: Set of integer variables.
- `compare_vars::Set{Int}`: Set of variables to compare.
"""
mutable struct Optimizer <: MOI.AbstractOptimizer
concepts::Dict{Int, Tuple{Function, Vector{Int}}}
configurations::Tuple{Set{Vector{Number}}, Set{Vector{Number}}}
domains::Dict{Int, AbstractDomain}
settings::Union{Nothing, ExploreSettings}

c_max::Int
d_max::Int
end

function Optimizer(;
concepts = Dict{Int, Function}(),
configurations = (Set{Vector{Number}}(), Set{Vector{Number}}()),
domains = Dict{Int, AbstractDomain}(),
settings = nothing,
)
c_max = maximum(keys(concepts); init = 0)
d_max = maximum(keys(domains); init = 0)
return Optimizer(concepts, configurations, domains, parameters, settings, c_max, d_max)
end

MOI.get(::Optimizer, ::MOI.SolverName) = "Constraint Explorer"

function MOI.empty!(explorer::Optimizer)
empty!(explorer.concepts)
empty!(explorer.configurations)
empty!(explorer.domains)
explorer.settings = nothing

explorer.c_max = 0
explorer.d_max = 0
return
end

function MOI.is_empty(explorer::Optimizer)
return isempty(explorer.concepts) && isempty(explorer.configurations) &&
isempty(explorer.domains) && explorer.settings === nothing
end

struct CompleteSearchLimit <: MOI.AbstractModelAttribute end

function MOI.set(model::Optimizer, ::CompleteSearchLimit, n::Integer)
settings = ExploreSettings(
n,
model.settings.max_samplings,
model.settings.search,
model.settings.solutions_limit,
)
model.settings = settings
return
end

function MOI.get(model::Optimizer, ::CompleteSearchLimit)
return model.settings.complete_search_limit
end

struct MaxSamplings <: MOI.AbstractModelAttribute end

function MOI.set(model::Optimizer, ::MaxSamplings, n::Integer)
settings = ExploreSettings(
model.settings.complete_search_limit,
n,
model.settings.search,
model.settings.solutions_limit,
)
model.settings = settings
return
end

function MOI.get(model::Optimizer, ::MaxSamplings)
return model.settings.max_samplings
end

struct Search <: MOI.AbstractModelAttribute end

function MOI.set(model::Optimizer, ::Search, s::Symbol)
settings = ExploreSettings(
model.settings.complete_search_limit,
model.settings.max_samplings,
s,
model.settings.solutions_limit,
)
model.settings = settings
return
end

function MOI.get(model::Optimizer, ::Search)
return model.settings.search
end

struct NumberOfSolutions <: MOI.AbstractModelAttribute end

function MOI.set(model::Optimizer, ::NumberOfSolutions, n::Integer)
settings = ExploreSettings(
model.settings.complete_search_limit,
model.settings.max_samplings,
model.settings.search,
n,
)
model.settings = settings
return
end

function MOI.get(model::Optimizer, ::NumberOfSolutions)
return model.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(model::Optimizer, src::MOI.ModelLike)
return MOIU.default_copy_to(model, src)
end

function optimize!(model::Optimizer)
c = reduce(&, model.concepts; init = true)
return explore(model.domains, c; settings = model.settings)
end

function MOI.get(::Optimizer, ::MOI.SolverVersion)
deps = Pkg.dependencies()
_uuid = Base.UUID("5800fd60-8556-4464-8d61-84ebf7a0bedb")
return "v" * string(deps[_uuid].version)
end
Loading

0 comments on commit 62bebd3

Please sign in to comment.