Skip to content

Commit 3803fe2

Browse files
authored
Fixes for ConstraintLearning.jl (#53)
* Temp save * Fix for cbls icn * Temp save * Update for keyargs parameters * Tag new version. Update CI
1 parent b5045b2 commit 3803fe2

File tree

11 files changed

+116
-67
lines changed

11 files changed

+116
-67
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
name: CI
22
on:
3-
- push
4-
- pull_request
3+
push:
4+
branches:
5+
- main
6+
tags: '*'
7+
pull_request:
8+
concurrency:
9+
# Skip intermediate builds: always.
10+
# Cancel intermediate builds: only if it is a pull request build.
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}
513
jobs:
614
test:
715
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
@@ -11,8 +19,7 @@ jobs:
1119
matrix:
1220
version:
1321
- '1.6'
14-
- '1.7'
15-
- "^1.8.0-0"
22+
- "1.8"
1623
- 'nightly'
1724
os:
1825
- ubuntu-latest

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CompositionalNetworks"
22
uuid = "4b67e4b5-442d-4ef5-b760-3f5df3a57537"
33
authors = ["Jean-François Baffier"]
4-
version = "0.5.2"
4+
version = "0.5.3"
55

66
[deps]
77
ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954"

src/aggregation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ function aggregation_layer()
2020
:count_positive => ag_count_positive,
2121
)
2222

23-
return Layer(aggregations, true)
23+
return Layer(true, aggregations, Vector{Symbol}())
2424
end

src/arithmetic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ function arithmetic_layer()
2020
:prod => ar_prod,
2121
)
2222

23-
return Layer(arithmetics, true)
23+
return Layer(true, arithmetics, Vector{Symbol}())
2424
end

src/comparison.jl

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,41 @@ Return the difference `nvars - x` if positive, `0.0` otherwise, where `nvars` de
5656
"""
5757
co_vars_minus_val(x; param=nothing, dom_size=0, nvars) = co_param_minus_val(x; param=nvars)
5858

59-
"""
60-
comparison_layer(param = false)
61-
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.
62-
"""
63-
function comparison_layer(param=false)
64-
comparisons = LittleDict{Symbol,Function}(
59+
60+
# Parametric layers
61+
make_comparisons(param::Symbol) = make_comparisons(Val(param))
62+
63+
function make_comparisons(::Val{:none})
64+
return LittleDict{Symbol,Function}(
6565
:identity => co_identity,
6666
:euclidian => co_euclidian,
6767
:abs_diff_val_vars => co_abs_diff_val_vars,
6868
:val_minus_vars => co_val_minus_vars,
6969
:vars_minus_val => co_vars_minus_val,
7070
)
71+
end
72+
73+
function make_comparisons(::Val{:val})
74+
return LittleDict{Symbol,Function}(
75+
:abs_diff_val_param => co_abs_diff_val_param,
76+
:val_minus_param => co_val_minus_param,
77+
:param_minus_val => co_param_minus_val,
78+
:euclidian_param => co_euclidian_param,
79+
)
80+
end
81+
82+
83+
"""
84+
comparison_layer(param = false)
85+
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.
86+
"""
87+
function comparison_layer(parameters = Vector{Symbol}())
88+
comparisons = make_comparisons(:none)
7189

72-
if param
73-
comparisons_param = LittleDict{Symbol,Function}(
74-
:abs_diff_val_param => co_abs_diff_val_param,
75-
:val_minus_param => co_val_minus_param,
76-
:param_minus_val => co_param_minus_val,
77-
:euclidian_param => co_euclidian_param,
78-
)
90+
for p in parameters
91+
comparisons_param = make_comparisons(p)
7992
comparisons = LittleDict{Symbol,Function}(union(comparisons, comparisons_param))
8093
end
8194

82-
return Layer(comparisons, true)
95+
return Layer(true, comparisons, parameters)
8396
end

src/icn.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mutable struct ICN
1717
weigths::BitVector
1818

1919
function ICN(;
20-
param=false,
20+
param=Vector{Symbol}(),
2121
tr_layer=transformation_layer(param),
2222
ar_layer=arithmetic_layer(),
2323
ag_layer=aggregation_layer(),
@@ -73,7 +73,7 @@ is_viable(icn::ICN) = is_viable(icn, weigths(icn))
7373
Set the weigths of an ICN with a `BitVector`.
7474
"""
7575
function weigths!(icn, weigths)
76-
length(weigths) == nbits(icn) || @warn icn weigths
76+
length(weigths) == nbits(icn) || @warn icn weigths nbits(icn)
7777
@assert length(weigths) == nbits(icn)
7878
return icn.weigths = weigths
7979
end
@@ -107,7 +107,7 @@ function regularization(icn)
107107
return Σop / (Σmax + 1)
108108
end
109109

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

112112
"""
113113
_compose(icn)

src/layer.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
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.
44
"""
55
struct Layer
6-
functions::LittleDict{Symbol, Function}
76
exclusive::Bool
7+
functions::LittleDict{Symbol, Function}
8+
parameters::Vector{Symbol}
89
end
910

1011
"""

src/learn.jl

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@ Create an ICN, optimize it, and return its composition.
1414
function learn_compose(
1515
solutions,
1616
non_sltns,
17-
dom_size,
18-
param=nothing;
17+
dom_size;
1918
metric=:hamming,
2019
optimizer,
21-
X_test = nothing,
20+
X_test=nothing,
21+
parameters...
2222
)
23-
icn = ICN(; param=!isnothing(param))
23+
icn = ICN(; parameters...)
2424
_, weigths = optimize!(
2525
icn,
2626
solutions,
2727
non_sltns,
2828
dom_size,
29-
param,
3029
metric,
31-
optimizer,
30+
optimizer;
31+
parameters...
3232
)
3333
compositions = Dictionary{Composition,Int}()
34+
3435
for (bv, occurences) in pairs(weigths)
3536
set!(compositions, compose(deepcopy(icn), bv), occurences)
3637
end
@@ -57,22 +58,26 @@ Explore a search space, learn a composition from an ICN, and compose an error fu
5758
function explore_learn_compose(
5859
domains,
5960
concept;
60-
param=nothing,
61-
configurations=explore(domains, concept; param),
61+
configurations=nothing,
6262
metric=:hamming,
6363
optimizer,
64-
X_test = nothing,
64+
X_test=nothing,
65+
parameters...
6566
)
67+
if isnothing(configurations)
68+
configurations = explore(domains, concept; parameters...)
69+
end
70+
6671
dom_size = maximum(length, domains)
6772
solutions, non_sltns = configurations
6873
return learn_compose(
6974
solutions,
7075
non_sltns,
71-
dom_size,
72-
param;
76+
dom_size;
7377
metric,
7478
optimizer,
7579
X_test,
80+
parameters...
7681
)
7782
end
7883

@@ -99,22 +104,26 @@ function compose_to_file!(
99104
concept,
100105
name,
101106
path;
102-
param=nothing,
103-
configurations=explore(domains, concept; param),
107+
configurations=nothing,
104108
domains,
105109
language=:Julia,
106110
metric=:hamming,
107111
optimizer,
108112
X_test=nothing,
113+
parameters...
109114
)
115+
if isnothing(configurations)
116+
configurations = explore(domains, concept; parameters...)
117+
end
118+
110119
compo, icn, _ = explore_learn_compose(
111120
domains,
112121
concept;
113122
configurations,
114123
metric,
115124
optimizer,
116-
param,
117125
X_test,
126+
parameters...
118127
)
119128
composition_to_file!(compo, path, name, language)
120129
return icn

src/transformation.jl

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,11 @@ end
212212
# Generating vetorized versions
213213
lazy(tr_contiguous_vals_minus, tr_contiguous_vals_minus_rev)
214214

215+
# Parametric layers
216+
make_transformations(param::Symbol) = make_transformations(Val(param))
215217

216-
"""
217-
transformation_layer(param = false)
218-
Generate the layer of transformations functions of the ICN. Iff `param` value is true, also includes all the parametric transformations.
219-
"""
220-
function transformation_layer(param=false)
221-
transformations = LittleDict{Symbol,Function}(
218+
function make_transformations(::Val{:none})
219+
return LittleDict{Symbol,Function}(
222220
:identity => tr_identity,
223221
:count_eq => tr_count_eq,
224222
:count_eq_left => tr_count_eq_left,
@@ -232,18 +230,35 @@ function transformation_layer(param=false)
232230
:contiguous_vals_minus => tr_contiguous_vals_minus,
233231
:contiguous_vals_minus_rev => tr_contiguous_vals_minus_rev,
234232
)
233+
end
234+
235+
function make_transformations(::Val{:val})
236+
return LittleDict{Symbol,Function}(
237+
:count_eq_param => tr_count_eq_param,
238+
:count_l_param => tr_count_l_param,
239+
:count_g_param => tr_count_g_param,
240+
:count_bounding_param => tr_count_bounding_param,
241+
:val_minus_param => tr_val_minus_param,
242+
:param_minus_val => tr_param_minus_val,
243+
)
244+
end
245+
246+
function make_transformations(::Val)
247+
return LittleDict{Symbol,Function}()
248+
end
249+
250+
251+
"""
252+
transformation_layer(param = false)
253+
Generate the layer of transformations functions of the ICN. Iff `param` value is true, also includes all the parametric transformations.
254+
"""
255+
function transformation_layer(parameters = Vector{Symbol}())
256+
transformations = make_transformations(:none)
235257

236-
if param
237-
transformations_param = LittleDict{Symbol,Function}(
238-
:count_eq_param => tr_count_eq_param,
239-
:count_l_param => tr_count_l_param,
240-
:count_g_param => tr_count_g_param,
241-
:count_bounding_param => tr_count_bounding_param,
242-
:val_minus_param => tr_val_minus_param,
243-
:param_minus_val => tr_param_minus_val,
244-
)
258+
for p in parameters
259+
transformations_param = make_transformations(p)
245260
transformations = LittleDict(union(transformations, transformations_param))
246261
end
247262

248-
return Layer(transformations, false)
263+
return Layer(false, transformations, parameters)
249264
end

0 commit comments

Comments
 (0)