Skip to content

Commit

Permalink
Fixes for ConstraintLearning.jl (#53)
Browse files Browse the repository at this point in the history
* Temp save

* Fix for cbls icn

* Temp save

* Update for keyargs parameters

* Tag new version. Update CI
  • Loading branch information
Azzaare committed Jan 19, 2023
1 parent b5045b2 commit 3803fe2
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 67 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
name: CI
on:
- push
- pull_request
push:
branches:
- main
tags: '*'
pull_request:
concurrency:
# Skip intermediate builds: always.
# Cancel intermediate builds: only if it is a pull request build.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
Expand All @@ -11,8 +19,7 @@ jobs:
matrix:
version:
- '1.6'
- '1.7'
- "^1.8.0-0"
- "1.8"
- 'nightly'
os:
- ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CompositionalNetworks"
uuid = "4b67e4b5-442d-4ef5-b760-3f5df3a57537"
authors = ["Jean-François Baffier"]
version = "0.5.2"
version = "0.5.3"

[deps]
ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954"
Expand Down
2 changes: 1 addition & 1 deletion src/aggregation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ function aggregation_layer()
:count_positive => ag_count_positive,
)

return Layer(aggregations, true)
return Layer(true, aggregations, Vector{Symbol}())
end
2 changes: 1 addition & 1 deletion src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ function arithmetic_layer()
:prod => ar_prod,
)

return Layer(arithmetics, true)
return Layer(true, arithmetics, Vector{Symbol}())
end
41 changes: 27 additions & 14 deletions src/comparison.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,41 @@ Return the difference `nvars - x` if positive, `0.0` otherwise, where `nvars` de
"""
co_vars_minus_val(x; param=nothing, dom_size=0, nvars) = co_param_minus_val(x; param=nvars)

"""
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.
"""
function comparison_layer(param=false)
comparisons = LittleDict{Symbol,Function}(

# Parametric layers
make_comparisons(param::Symbol) = make_comparisons(Val(param))

function make_comparisons(::Val{:none})
return LittleDict{Symbol,Function}(
:identity => co_identity,
:euclidian => co_euclidian,
:abs_diff_val_vars => co_abs_diff_val_vars,
:val_minus_vars => co_val_minus_vars,
:vars_minus_val => co_vars_minus_val,
)
end

function make_comparisons(::Val{:val})
return LittleDict{Symbol,Function}(
:abs_diff_val_param => co_abs_diff_val_param,
:val_minus_param => co_val_minus_param,
:param_minus_val => co_param_minus_val,
:euclidian_param => co_euclidian_param,
)
end


"""
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.
"""
function comparison_layer(parameters = Vector{Symbol}())
comparisons = make_comparisons(:none)

if param
comparisons_param = LittleDict{Symbol,Function}(
:abs_diff_val_param => co_abs_diff_val_param,
:val_minus_param => co_val_minus_param,
:param_minus_val => co_param_minus_val,
:euclidian_param => co_euclidian_param,
)
for p in parameters
comparisons_param = make_comparisons(p)
comparisons = LittleDict{Symbol,Function}(union(comparisons, comparisons_param))
end

return Layer(comparisons, true)
return Layer(true, comparisons, parameters)
end
6 changes: 3 additions & 3 deletions src/icn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mutable struct ICN
weigths::BitVector

function ICN(;
param=false,
param=Vector{Symbol}(),
tr_layer=transformation_layer(param),
ar_layer=arithmetic_layer(),
ag_layer=aggregation_layer(),
Expand Down Expand Up @@ -73,7 +73,7 @@ is_viable(icn::ICN) = is_viable(icn, weigths(icn))
Set the weigths of an ICN with a `BitVector`.
"""
function weigths!(icn, weigths)
length(weigths) == nbits(icn) || @warn icn weigths
length(weigths) == nbits(icn) || @warn icn weigths nbits(icn)
@assert length(weigths) == nbits(icn)
return icn.weigths = weigths
end
Expand Down Expand Up @@ -107,7 +107,7 @@ function regularization(icn)
return Σop / (Σmax + 1)
end

max_icn_length(icn=ICN(; param=true)) = length(icn.transformation)
max_icn_length(icn=ICN(; param=[:val])) = length(icn.transformation)

"""
_compose(icn)
Expand Down
3 changes: 2 additions & 1 deletion src/layer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
A structure to store a `LittleDict` of operations that can be selected during the learning phase of an ICN. If the layer is exclusive, only one operation can be selected at a time.
"""
struct Layer
functions::LittleDict{Symbol, Function}
exclusive::Bool
functions::LittleDict{Symbol, Function}
parameters::Vector{Symbol}
end

"""
Expand Down
37 changes: 23 additions & 14 deletions src/learn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ Create an ICN, optimize it, and return its composition.
function learn_compose(
solutions,
non_sltns,
dom_size,
param=nothing;
dom_size;
metric=:hamming,
optimizer,
X_test = nothing,
X_test=nothing,
parameters...
)
icn = ICN(; param=!isnothing(param))
icn = ICN(; parameters...)
_, weigths = optimize!(
icn,
solutions,
non_sltns,
dom_size,
param,
metric,
optimizer,
optimizer;
parameters...
)
compositions = Dictionary{Composition,Int}()

for (bv, occurences) in pairs(weigths)
set!(compositions, compose(deepcopy(icn), bv), occurences)
end
Expand All @@ -57,22 +58,26 @@ Explore a search space, learn a composition from an ICN, and compose an error fu
function explore_learn_compose(
domains,
concept;
param=nothing,
configurations=explore(domains, concept; param),
configurations=nothing,
metric=:hamming,
optimizer,
X_test = nothing,
X_test=nothing,
parameters...
)
if isnothing(configurations)
configurations = explore(domains, concept; parameters...)
end

dom_size = maximum(length, domains)
solutions, non_sltns = configurations
return learn_compose(
solutions,
non_sltns,
dom_size,
param;
dom_size;
metric,
optimizer,
X_test,
parameters...
)
end

Expand All @@ -99,22 +104,26 @@ function compose_to_file!(
concept,
name,
path;
param=nothing,
configurations=explore(domains, concept; param),
configurations=nothing,
domains,
language=:Julia,
metric=:hamming,
optimizer,
X_test=nothing,
parameters...
)
if isnothing(configurations)
configurations = explore(domains, concept; parameters...)
end

compo, icn, _ = explore_learn_compose(
domains,
concept;
configurations,
metric,
optimizer,
param,
X_test,
parameters...
)
composition_to_file!(compo, path, name, language)
return icn
Expand Down
47 changes: 31 additions & 16 deletions src/transformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,11 @@ end
# Generating vetorized versions
lazy(tr_contiguous_vals_minus, tr_contiguous_vals_minus_rev)

# Parametric layers
make_transformations(param::Symbol) = make_transformations(Val(param))

"""
transformation_layer(param = false)
Generate the layer of transformations functions of the ICN. Iff `param` value is true, also includes all the parametric transformations.
"""
function transformation_layer(param=false)
transformations = LittleDict{Symbol,Function}(
function make_transformations(::Val{:none})
return LittleDict{Symbol,Function}(
:identity => tr_identity,
:count_eq => tr_count_eq,
:count_eq_left => tr_count_eq_left,
Expand All @@ -232,18 +230,35 @@ function transformation_layer(param=false)
:contiguous_vals_minus => tr_contiguous_vals_minus,
:contiguous_vals_minus_rev => tr_contiguous_vals_minus_rev,
)
end

function make_transformations(::Val{:val})
return LittleDict{Symbol,Function}(
:count_eq_param => tr_count_eq_param,
:count_l_param => tr_count_l_param,
:count_g_param => tr_count_g_param,
:count_bounding_param => tr_count_bounding_param,
:val_minus_param => tr_val_minus_param,
:param_minus_val => tr_param_minus_val,
)
end

function make_transformations(::Val)
return LittleDict{Symbol,Function}()
end


"""
transformation_layer(param = false)
Generate the layer of transformations functions of the ICN. Iff `param` value is true, also includes all the parametric transformations.
"""
function transformation_layer(parameters = Vector{Symbol}())
transformations = make_transformations(:none)

if param
transformations_param = LittleDict{Symbol,Function}(
:count_eq_param => tr_count_eq_param,
:count_l_param => tr_count_l_param,
:count_g_param => tr_count_g_param,
:count_bounding_param => tr_count_bounding_param,
:val_minus_param => tr_val_minus_param,
:param_minus_val => tr_param_minus_val,
)
for p in parameters
transformations_param = make_transformations(p)
transformations = LittleDict(union(transformations, transformations_param))
end

return Layer(transformations, false)
return Layer(false, transformations, parameters)
end
Loading

0 comments on commit 3803fe2

Please sign in to comment.