Public API
Automaton{S, T, F <: Union{S, Vector{S}, Set{S}}} <: AbstractAutomaton
A minimal implementation of a deterministic automaton structure.
MDD{S,T} <: AbstractMultivaluedDecisionDiagram
A minimal implementation of a multivalued decision diagram structure.
accept(a::Union{Automaton, MDD}, w)
Return true
if a
accepts the word w
and false
otherwise.
consin(::Any, ::Nothing)
Extends Base.in
(or ∈
) when the set is nothing
. Returns false
.
consisempty(::Nothing)
Extends Base.isempty
when the set is nothing
. Returns true
.
extract_parameters(m::Union{Method, Function}; parameters)
Extracts the intersection between the kargs
of m
and parameters
(defaults to USUAL_CONSTRAINT_PARAMETERS
).
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
.
oversample(X, f)
Oversample elements of X
until the boolean function f
has as many true
and false
configurations.
symcon(s1::Symbol, s2::Symbol, connector::AbstractString="_")
Extends *
to Symbol
s multiplication by connecting the symbols by an _
.
δ_extrema(X...)
Compute both the difference between the maximum and the minimum of over all the collections of X
.
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
ContinuousDomain{T <: Real} <: AbstractDomain
An abstract supertype for all continuous domains.
DiscreteDomain{T <: Number} <: AbstractDomain
An abstract supertype for discrete domains (set, range).
ExploreSettings(domains;
complete_search_limit = 10^6,
max_samplings = sum(domain_size, domains),
search = :flexible,
solutions_limit = floor(Int, sqrt(max_samplings)))
Create an ExploreSettings
object to configure the exploration of a search space composed of a collection of domains.
Arguments
domains
: A collection of domains to be explored.complete_search_limit
: An integer specifying the maximum limit for complete search iterations. Default is 10^6.max_samplings
: An integer specifying the maximum number of samplings. Default is the sum of domain sizes.search
: A symbol indicating the type of search to perform. Default is:flexible
.solutions_limit
: An integer specifying the limit on the number of solutions. Default is the floor of the square root ofmax_samplings
.
Returns
ExploreSettings
object with the specified settings.
RangeDomain
A discrete domain defined by a range <: AbstractRange{Real}
. As ranges are immutable in Julia, changes in RangeDomain
must use set_domain!
.
SetDomain{T <: Number} <: DiscreteDomain{T}
Domain that stores discrete values as a set of (unordered) points.
Base.delete!(d::SetDomain, value)(d::SetDomain, value)
Delete value
from the list of points in d
.
add!(d::SetDomain, value)
Add value
to the list of points in d
.
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)
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).
domain_size(itv::Intervals)
Return the difference between the highest and lowest values in itv
.
domain_size(d <: AbstractDomain)
Fallback method for domain_size(d)
that return length(d)
.
domain_size(d::D) where D <: DiscreteDomain
Return the maximum distance between two points in d
.
explore(domains, concept; settings = ExploreSettings(domains), parameters...)
Search (a part of) a search space and return a pair of vectors of configurations: (solutions, non_solutions)
. The exploration behavior is determined based on the settings
.
Arguments
domains
: A collection of domains to be explored.concept
: The concept representing the constraint to be targeted.settings
: An optionalExploreSettings
object to configure the exploration. Default isExploreSettings(domains)
.parameters...
: Additional parameters for theconcept
.
Returns
- A tuple of sets:
(solutions, non_solutions)
.
generate_parameters(d<:AbstractDomain, param)
Generates random parameters based on the domain d
and the kind of parameters param
.
get_domain(::AbstractDomain)
Access the internal structure of any domain type.
intersect_domains(d₁, d₂)
Compute the intersections of two domains.
merge_domains(d₁::AbstractDomain, d₂::AbstractDomain)
Merge two domains of same nature (discrete/contiuous).
to_domains(args...)
Convert various arguments into valid domains format.
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)
USUAL_SYMMETRIES
A Dictionary that contains the function to apply for each symmetry to avoid searching a whole space.
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.
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)
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.
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
.
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_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_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()
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()
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
.
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.
symmetries(c::Constraint)
Return the list of symmetries of c
.
Composition(f::F, symbols) where {F<:Function}
Construct a Composition
.
struct Composition{F<:Function}
Store the all the information of a composition learned by an ICN.
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)
aggregation_layer()
Generate the layer of aggregations of the ICN. The operations are mutually exclusive, that is only one will be selected.
arithmetic_layer()
Generate the layer of arithmetic operations of the ICN. The operations are mutually exclusive, that is only one will be selected.
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.
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.
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
.
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
composition(c::Composition)
Access the actual method of an ICN composition c
.
composition_to_file!(c::Composition, path, name, language=:Julia)
Write the composition code in a given language
into a file at path
.
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
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.
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)
.
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)
.
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.
nbits(icn)
Return the expected number of bits of a viable weight of an ICN.
regularization(icn)
Return the regularization value of an ICN weights, which is proportional to the normalized number of operations selected in the icn layers.
show_layers(icn)
Return a formatted string with each layers in the icn.
symbols(c::Composition)
Output the composition as a layered collection of Symbol
s.
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.
weights!(icn, weights)
Set the weights of an ICN with a BitVector
.
weights(icn)
Access the current set of weights of an ICN.
weights_bias(x)
A metric that bias x
towards operations with a lower bit. Do not affect the main metric.
QUBO_linear_sum(n, σ)
One valid QUBO matrix given n
variables and parameter σ
for the linear sum constraint.
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.
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
.
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.
train(args...)
Default train
method for any AbstractOptimizer.