Public API
ConstraintCommons.Automaton Type
Automaton{S, T, F <: Union{S, Vector{S}, Set{S}}} <: AbstractAutomaton
A minimal implementation of a deterministic automaton structure.
ConstraintCommons.MDD Type
MDD{S,T} <: AbstractMultivaluedDecisionDiagram
A minimal implementation of a multivalued decision diagram structure.
ConstraintCommons.accept Method
accept(a::Union{Automaton, MDD}, w)
Return true
if a
accepts the word w
and false
otherwise.
ConstraintCommons.consin Method
consin(::Any, ::Nothing)
Extends Base.in
(or ∈
) when the set is nothing
. Returns false
.
ConstraintCommons.consisempty Method
consisempty(::Nothing)
Extends Base.isempty
when the set is nothing
. Returns true
.
ConstraintCommons.extract_parameters Method
extract_parameters(m::Union{Method, Function}; parameters)
Extracts the intersection between the kargs
of m
and parameters
(defaults to USUAL_CONSTRAINT_PARAMETERS
).
ConstraintCommons.incsert! Function
incsert!(d::Union{AbstractDict, AbstractDictionary}, ind, val = 1)
Increase or insert a counter in a dictionary-based collection. The counter insertion defaults to val = 1
.
ConstraintCommons.oversample Method
oversample(X, f)
Oversample elements of X
until the boolean function f
has as many true
and false
configurations.
ConstraintCommons.symcon Function
symcon(s1::Symbol, s2::Symbol, connector::AbstractString="_")
Extends *
to Symbol
s multiplication by connecting the symbols by an _
.
ConstraintCommons.δ_extrema Method
δ_extrema(X...)
Compute both the difference between the maximum and the minimum of over all the collections of X
.
ConstraintDomains.AbstractDomain Type
AbstractDomain
An abstract super type for any domain type. A domain type D <: AbstractDomain
must implement the following methods to properly interface AbstractDomain
.
Base.∈(val, ::D)
Base.rand(::D)
Base.length(::D)
that is the number of elements in a discrete domain, and the distance between bounds or similar for a continuous domain
Additionally, if the domain is used in a dynamic context, it can extend
add!(::D, args)
delete!(::D, args)
where args
depends on D
's structure
ConstraintDomains.ContinuousDomain Type
ContinuousDomain{T <: Real} <: AbstractDomain
An abstract supertype for all continuous domains.
ConstraintDomains.DiscreteDomain Type
DiscreteDomain{T <: Number} <: AbstractDomain
An abstract supertype for discrete domains (set, range).
ConstraintDomains.ExploreSettings Method
ExploreSettings(
domains;
complete_search_limit = 10^6,
max_samplings = sum(domain_size, domains),
search = :flexible,
solutions_limit = floor(Int, sqrt(max_samplings)),
)
Create settings for the exploration of a search space composed by a collection of domains.
Arguments
domains
: A collection of domains representing the search space.complete_search_limit
: Maximum size of the search space for complete search.max_samplings
: Maximum number of samples to take during partial search.search
: Search strategy (:flexible
,:complete
, or:partial
).solutions_limit
: Maximum number of solutions to store.
Returns
An ExploreSettings
object.
Example
domains = [domain([1, 2, 3]), domain([4, 5, 6])]
settings = ExploreSettings(domains, search = :complete)
ConstraintDomains.Explorer Type
Explorer(concepts, domains, objective = nothing; settings = ExploreSettings(domains))
Create an Explorer object for searching a constraint satisfaction problem space.
Arguments
concepts
: A collection of tuples, each containing a concept function and its associated variable indices.domains
: A collection of domains representing the search space.objective
: An optional objective function for optimization problems.settings
: AnExploreSettings
object to configure the exploration process.
Returns
An Explorer
object ready for exploration.
Example
domains = [domain([1, 2, 3]), domain([4, 5, 6])]
concepts = [(sum, [1, 2])]
objective = x -> x[1] + x[2]
explorer = Explorer(concepts, domains, objective)
ConstraintDomains.RangeDomain Type
RangeDomain
A discrete domain defined by a range <: AbstractRange{Real}
. As ranges are immutable in Julia, changes in RangeDomain
must use set_domain!
.
ConstraintDomains.SetDomain Type
SetDomain{T <: Number} <: DiscreteDomain{T}
Domain that stores discrete values as a set of (unordered) points.
Base.delete! Method
Base.delete!(d::SetDomain, value)(d::SetDomain, value)
Delete value
from the list of points in d
.
ConstraintDomains.domain Method
domain(values)
domain(range::R) where {T <: Real, R <: AbstractRange{T}}
Construct either a SetDomain
or a RangeDomain
.
d1 = domain(1:5)
d2 = domain([53.69, 89.2, 0.12])
d3 = domain([2//3, 89//123])
d4 = domain(4.3)
d5 = domain(1,42,86.9)
ConstraintDomains.domain Method
domain(a::Tuple{T, Bool}, b::Tuple{T, Bool}) where {T <: Real}
domain(intervals::Vector{Tuple{Tuple{T, Bool},Tuple{T, Bool}}}) where {T <: Real}
Construct a domain of continuous interval(s).
ConstraintDomains.domain_size Method
domain_size(itv::Intervals)
Return the difference between the highest and lowest values in itv
.
ConstraintDomains.domain_size Method
domain_size(d <: AbstractDomain)
Fallback method for domain_size(d)
that return length(d)
.
ConstraintDomains.domain_size Method
domain_size(d::D) where D <: DiscreteDomain
Return the maximum distance between two points in d
.
ConstraintDomains.explore! Method
explore!(explorer::Explorer)
Perform exploration on the search space defined by the Explorer
object.
This function explores the search space according to the settings specified in the Explorer
object. It updates the Explorer
's state with found solutions and non-solutions.
Arguments
explorer
: AnExplorer
object containing the problem definition and exploration settings.
Returns
Nothing. The Explorer
's state is updated in-place.
Example
explorer = Explorer(concepts, domains)
explore!(explorer)
println("Solutions found: ", length(explorer.state.solutions))
ConstraintDomains.explore Method
explore(domains, concept; settings = ExploreSettings(domains), parameters...)
Explore a search space defined by domains and a concept.
Arguments
domains
: A collection of domains representing the search space.concept
: The concept function defining the constraint.settings
: AnExploreSettings
object to configure the exploration process.parameters
: Additional parameters to pass to the concept function.
Returns
A tuple containing two sets: (solutions, non_solutions).
Example
domains = [domain([1, 2, 3]), domain([4, 5, 6])]
solutions, non_solutions = explore(domains, allunique)
ConstraintDomains.generate_parameters Method
generate_parameters(d<:AbstractDomain, param)
Generates random parameters based on the domain d
and the kind of parameters param
.
ConstraintDomains.get_domain Method
get_domain(::AbstractDomain)
Access the internal structure of any domain type.
ConstraintDomains.intersect_domains Method
intersect_domains(d₁, d₂)
Compute the intersections of two domains.
ConstraintDomains.merge_domains Method
merge_domains(d₁::AbstractDomain, d₂::AbstractDomain)
Merge two domains of same nature (discrete/contiuous).
ConstraintDomains.to_domains Method
to_domains(args...)
Convert various arguments into valid domains format.
Constraints.USUAL_CONSTRAINTS Constant
USUAL_CONSTRAINTS::Dict
Dictionary that contains all the usual constraints defined in Constraint.jl. It is based on XCSP3-core specifications available at https://arxiv.org/abs/2009.00514
Adding a new constraint is as simple as defining a new function with the same name as the constraint and using the @usual
macro to define it. The macro will take care of adding the new constraint to the USUAL_CONSTRAINTS
dictionary.
Example
@usual concept_all_different(x; vals=nothing) = xcsp_all_different(list=x, except=vals)
Constraints.USUAL_SYMMETRIES Constant
USUAL_SYMMETRIES
A Dictionary that contains the function to apply for each symmetry to avoid searching a whole space.
Constraints.Constraint Type
Constraint
Parametric structure with the following fields.
concept
: a Boolean function that, given an assignmentx
, outputstrue
ifx
satisfies the constraint, andfalse
otherwise.error
: a positive function that works as preferences over invalid assignments. Return0.0
if the constraint is satisfied, and a strictly positive real otherwise.
ConstraintCommons.extract_parameters Function
extract_parameters(s::Symbol, constraints_dict=USUAL_CONSTRAINTS; parameters=ConstraintCommons.USUAL_CONSTRAINT_PARAMETERS)
Return the parameters of the constraint s
in constraints_dict
.
Arguments
s::Symbol
: the constraint name.constraints_dict::Dict{Symbol,Constraint}
: dictionary of constraints. Default isUSUAL_CONSTRAINTS
.parameters::Vector{Symbol}
: vector of parameters. Default isConstraintCommons.USUAL_CONSTRAINT_PARAMETERS
.
Example
extract_parameters(:all_different)
Constraints.args Method
args(c::Constraint)
Return the expected length restriction of the arguments in a constraint c
. The value nothing
indicates that any strictly positive number of value is accepted.
Constraints.concept Method
concept(c::Constraint)
Return the concept (function) of constraint c
. concept(c::Constraint, x...; param = nothing) Apply the concept of c
to values x
and optionally param
.
Constraints.concept Method
concept(s::Symbol, args...; kargs...)
Return the concept of the constraint s
applied to args
and kargs
. This is a shortcut for concept(USUAL_CONSTRAINTS[s])(args...; kargs...)
.
Arguments
s::Symbol
: the constraint name.args...
: the arguments to apply the concept to.kargs...
: the keyword arguments to apply the concept to.
Example
concept(:all_different, [1, 2, 3])
Constraints.constraints_descriptions Function
constraints_descriptions(C=USUAL_CONSTRAINTS)
Return a pretty table with the descriptions of the constraints in C
.
Arguments
C::Dict{Symbol,Constraint}
: dictionary of constraints. Default isUSUAL_CONSTRAINTS
.
Example
constraints_descriptions()
Constraints.constraints_parameters Function
constraints_parameters(C=USUAL_CONSTRAINTS)
Return a pretty table with the parameters of the constraints in C
.
Arguments
C::Dict{Symbol,Constraint}
: dictionary of constraints. Default isUSUAL_CONSTRAINTS
.
Example
constraints_parameters()
Constraints.describe Function
describe(constraints::Dict{Symbol,Constraint}=USUAL_CONSTRAINTS; width=150)
Return a pretty table with the description of the constraints in constraints
.
Arguments
constraints::Dict{Symbol,Constraint}
: dictionary of constraints to describe. Default isUSUAL_CONSTRAINTS
.width::Int
: width of the table.
Example
describe()
Constraints.error_f Method
error_f(c::Constraint)
Return the error function of constraint c
. error_f(c::Constraint, x; param = nothing) Apply the error function of c
to values x
and optionally param
.
Constraints.params_length Method
params_length(c::Constraint)
Return the expected length restriction of the arguments in a constraint c
. The value nothing
indicates that any strictly positive number of parameters is accepted.
CompositionalNetworks.Composition Method
Composition(f::F, symbols) where {F<:Function}
Construct a Composition
.
CompositionalNetworks.Composition Type
struct Composition{F<:Function}
Store the all the information of a composition learned by an ICN.
CompositionalNetworks.ICN Type
ICN(; nvars, dom_size, param, transformation, arithmetic, aggregation, comparison)
Construct an Interpretable Compositional Network, with the following arguments:
nvars
: number of variable in the constraintdom_size: maximum domain size of any variable in the constraint
param
: optional parameter (default tonothing
)transformation
: a transformation layer (optional)arithmetic
: a arithmetic layer (optional)aggregation
: a aggregation layer (optional)comparison
: a comparison layer (optional)
CompositionalNetworks.aggregation_layer Method
aggregation_layer()
Generate the layer of aggregations of the ICN. The operations are mutually exclusive, that is only one will be selected.
CompositionalNetworks.arithmetic_layer Method
arithmetic_layer()
Generate the layer of arithmetic operations of the ICN. The operations are mutually exclusive, that is only one will be selected.
CompositionalNetworks.code Function
code(c::Composition, lang=:maths; name="composition")
Access the code of a composition c
in a given language lang
. The name of the generated method is optional.
CompositionalNetworks.comparison_layer Function
comparison_layer(param = false)
Generate the layer of transformations functions of the ICN. Iff param
value is set, also includes all the parametric comparison with that value. The operations are mutually exclusive, that is only one will be selected.
CompositionalNetworks.compose Function
compose(icn, weights=nothing)
Return a function composed by some of the operations of a given ICN. Can be applied to any vector of variables. If weights
are given, will assign to icn
.
CompositionalNetworks.compose_to_file! Method
compose_to_file!(concept, name, path; domains, param = nothing, language = :Julia, search = :complete, global_iter = 10, local_iter = 100, metric = hamming, popSize = 200)
Explore, learn and compose a function and write it to a file.
Arguments:
concept
: the concept to learnname
: the name to give to the constraintpath
: path of the output file
Keywords arguments:
domains
: domains that defines the search spaceparam
: an optional parameter of the constraintlanguage
: the language to export to, default to:julia
search
: either:partial
or:complete
searchglobal_iter
: number of learning iterationlocal_iter
: number of generation in the genetic algorithmmetric
: the metric to measure the distance between a configuration and known solutionspopSize
: size of the population in the genetic algorithm
CompositionalNetworks.composition Method
composition(c::Composition)
Access the actual method of an ICN composition c
.
CompositionalNetworks.composition_to_file! Function
composition_to_file!(c::Composition, path, name, language=:Julia)
Write the composition code in a given language
into a file at path
.
CompositionalNetworks.explore_learn_compose Method
explore_learn_compose(concept; domains, param = nothing, search = :complete, global_iter = 10, local_iter = 100, metric = hamming, popSize = 200, action = :composition)
Explore a search space, learn a composition from an ICN, and compose an error function.
Arguments:
concept
: the concept of the targeted constraintdomains
: domains of the variables that define the training spaceparam
: an optional parameter of the constraintsearch
: eitherflexible
,:partial
or:complete
search. Flexible search will usesearch_limit
andsolutions_limit
to determine if the search space needs to be partially or completely exploredglobal_iter
: number of learning iterationlocal_iter
: number of generation in the genetic algorithmmetric
: the metric to measure the distance between a configuration and known solutionspopSize
: size of the population in the genetic algorithmaction
: either:symbols
to have a description of the composition or:composition
to have the composed function itself
CompositionalNetworks.hamming Method
hamming(x, X)
Compute the hamming distance of x
over a collection of solutions X
, i.e. the minimal number of variables to switch in x
to reach a solution.
CompositionalNetworks.lazy Method
lazy(funcs::Function...)
Generate methods extended to a vector instead of one of its components. A function f
should have the following signature: f(i::Int, x::V)
.
CompositionalNetworks.lazy_param Method
lazy_param(funcs::Function...)
Generate methods extended to a vector instead of one of its components. A function f
should have the following signature: f(i::Int, x::V; param)
.
CompositionalNetworks.learn_compose Method
learn_compose(;
nvars, dom_size, param=nothing, icn=ICN(nvars, dom_size, param),
X, X_sols, global_iter=100, local_iter=100, metric=hamming, popSize=200
)
Create an ICN, optimize it, and return its composition.
CompositionalNetworks.nbits Method
nbits(icn)
Return the expected number of bits of a viable weight of an ICN.
CompositionalNetworks.regularization Method
regularization(icn)
Return the regularization value of an ICN weights, which is proportional to the normalized number of operations selected in the icn layers.
CompositionalNetworks.show_layers Method
show_layers(icn)
Return a formatted string with each layers in the icn.
CompositionalNetworks.symbols Method
symbols(c::Composition)
Output the composition as a layered collection of Symbol
s.
CompositionalNetworks.transformation_layer Function
transformation_layer(param = Vector{Symbol}())
Generate the layer of transformations functions of the ICN. Iff param
value is non empty, also includes all the related parametric transformations.
CompositionalNetworks.weights! Method
weights!(icn, weights)
Set the weights of an ICN with a BitVector
.
CompositionalNetworks.weights_bias Method
weights_bias(x)
A metric that bias x
towards operations with a lower bit. Do not affect the main metric.
QUBOConstraints.QUBO_linear_sum Method
QUBO_linear_sum(n, σ)
One valid QUBO matrix given n
variables and parameter σ
for the linear sum constraint.
QUBOConstraints.binarize Method
binarize(x[, domain]; binarization = :one_hot)
Binarize x
following the binarization
encoding. If x
is a vector (instead of a number per say), domain
is optional.
QUBOConstraints.debinarize Method
debinarize(x[, domain]; binarization = :one_hot)
Transform a binary vector into a number or a set of number. If domain
is not given, it will compute a default value based on binarization
and x
.
QUBOConstraints.is_valid Function
is_valid(x, encoding::Symbol = :none)
Check if x
has a valid format for encoding
.
For instance, if encoding == :one_hot
, at most one bit of x
can be set to 1.