Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/aggregation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
_sum(x::V)
Aggregate through `+` a vector into a single scalar.
"""
_sum(x::V) where {T <: Number, V <: AbstractVector{T}} = reduce(+, x)
_ag_sum(x) = reduce(+, x)

"""
_count_positive(x::V)
Count the number of strictly positive elements of `x`.
"""
_count_positive(x::V) where {T <: Number, V <: AbstractVector{T}} = count(y -> y > 0.0, x)
_ag_count_positive(x) = count(y -> y > 0.0, x)

"""
aggregation_layer()
Generate the layer of aggregation functions of the ICN.
"""
function aggregation_layer()
aggregations = LittleDict{Symbol, Function}(
:sum => _sum,
:count_positive => _count_positive,
:sum => _ag_sum,
:count_positive => _ag_count_positive,
)

return Layer(aggregations, true)
Expand Down
12 changes: 4 additions & 8 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
_prod(x::W) where {T <: Number, V <: AbstractVector{T}, W <: AbstractVector{V}}
Reduce `k = length(x)` vectors through sum/product to a single vector.
"""
function _sum(x::W) where {T <: Number, V <: AbstractVector{T}, W <: AbstractVector{V}}
return reduce((y, z) -> y .+ z, x)
end
function _prod(x::W) where {T <: Number, V <: AbstractVector{T}, W <: AbstractVector{V}}
return reduce((y, z) -> y .* z, x)
end
_ar_sum(x) = reduce((y, z) -> y .+ z, x)
_ar_prod(x) = reduce((y, z) -> y .* z, x)

"""
arithmetic_layer()
Generate the layer of arithmetic functions of the ICN.
"""
function arithmetic_layer()
arithmetics = LittleDict{Symbol, Function}(
:sum => _sum,
:prod => _prod,
:sum => _ar_sum,
:prod => _ar_prod,
)

return Layer(arithmetics, true)
Expand Down
46 changes: 18 additions & 28 deletions src/comparison.jl
Original file line number Diff line number Diff line change
@@ -1,71 +1,61 @@
# doc in transformation.jl
_identity(x::T) where T <: Number = identity(x)
_co_identity(x) = identity(x)

"""
_abs_diff_val_param(x::T, param::T)
Return the absolute difference between `x` and `param`.
"""
_abs_diff_val_param(x::T, param::T) where T <: Number = abs(x - param)
_co_abs_diff_val_param(x, param) = abs(x - param)

"""
_val_minus_param(x::T, param::T)
_param_minus_val(x::T, param::T)
Return the difference `x - param` (resp. `param - x`) if positive, `0.0` otherwise.
"""
_val_minus_param(x::T, param::T) where T <: Number = max(0.0, x - param)
_param_minus_val(x::T, param::T) where T <: Number = max(0.0, param - x)
_co_val_minus_param(x, param) = max(0.0, x - param)
_co_param_minus_val(x, param) = max(0.0, param - x)

"""
_euclidian_param(x::T, param::T, dom_size::T2)
_euclidian(x::T, dom_size::T2)
Compute an euclidian norm , possibly weigthed by `param`, on a scalar.
"""
function _euclidian_param(x::T, param::T, dom_size::T2) where {T <: Number, T2 <: Number}
return x == param ? 0.0 : (1.0 + abs(x - param) \ dom_size)
end
function _euclidian(x::T, dom_size::T2) where {T <: Number, T2 <: Number}
return _euclidian_param(x, zero(T), dom_size)
end
_co_euclidian_param(x, param, d_size) = x == param ? 0.0 : (1.0 + abs(x - param) \ d_size)
_co_euclidian(x, d_size) = _co_euclidian_param(x, 0.0, d_size)

"""
_abs_diff_val_vars(x::T, nvars::Int)
Return the absolute difference between `x` and the number of variables.
"""
function _abs_diff_val_vars(x::T, nvars::Int) where {T <: Number}
return abs(x - nvars)
end
_co_abs_diff_val_vars(x, nvars) = abs(x - nvars)

"""
_val_minus_vars(x::T, nvars::Int)
_vars_minus_val(x::T, nvars::Int)
Return the difference `x - nvars` (resp. `nvars - x`) if positive, `0.0` otherwise.
"""
function _val_minus_vars(x::T, nvars::Int) where {T <: Number}
return _val_minus_param(x, nvars)
end
function _vars_minus_val(x::T, nvars::Int) where {T <: Number}
return _param_minus_val(x, nvars)
end
_co_val_minus_vars(x, nvars) = _co_val_minus_param(x, nvars)
_co_vars_minus_val(x, nvars) = _co_param_minus_val(x, nvars)

"""
comparison_layer(nvars, dom_size, param = nothing)
Generate the layer of transformations functions of the ICN. Iff `param` value is set, also includes all the parametric transformation with that value.
"""
function comparison_layer(nvars, dom_size, param = nothing)
comparisons = LittleDict{Symbol, Function}(
:identity => _identity,
:euclidian => (x -> _euclidian(x, dom_size)),
:abs_diff_val_vars => (x -> _abs_diff_val_vars(x, nvars)),
:val_minus_vars => (x -> _val_minus_vars(x, nvars)),
:vars_minus_val => (x -> _vars_minus_val(x, nvars)),
:identity => _co_identity,
:euclidian => (x -> _co_euclidian(x, dom_size)),
:abs_diff_val_vars => (x -> _co_abs_diff_val_vars(x, nvars)),
:val_minus_vars => (x -> _co_val_minus_vars(x, nvars)),
:vars_minus_val => (x -> _co_vars_minus_val(x, nvars)),
)

if !isnothing(param)
comparisons_param = LittleDict{Symbol, Function}(
:abs_diff_val_param => (x -> _abs_diff_val_param(x, param)),
:val_minus_param => (x -> _val_minus_param(x, param)),
:param_minus_val => (x -> _param_minus_val(x, param)),
:euclidian_param => (x -> _euclidian_param(x, param, dom_size)),
:abs_diff_val_param => (x -> _co_abs_diff_val_param(x, param)),
:val_minus_param => (x -> _co_val_minus_param(x, param)),
:param_minus_val => (x -> _co_param_minus_val(x, param)),
:euclidian_param => (x -> _co_euclidian_param(x, param, dom_size)),
)
comparisons = LittleDict{Symbol, Function}(union(comparisons, comparisons_param))
end
Expand Down
4 changes: 2 additions & 2 deletions src/io.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function csv2space(file; filter = :none)
function csv2space(file; filter=:none)
df = DataFrame(CSV.File(file))
filter == :solutions && (df = df[df.concept .== true,:])
return Vector(map(Vector,eachrow(df[!, Not(:concept)])))
return Vector(map(Vector, eachrow(df[!, Not(:concept)])))
end
170 changes: 69 additions & 101 deletions src/transformation.jl
Original file line number Diff line number Diff line change
@@ -1,120 +1,88 @@
"""
_identity(x::V) where {T <: Number,V <: AbstractVector{T}}
_identity(x::T) where T <: Number = identity(x)
_tr_identity(x::V) where {T <: Number,V <: AbstractVector{T}}
_tr_identity(x::T) where T <: Number = identity(x)
Identity function. Already defined in Julia as `identity`, specialized for vectors and scalars.
"""
_identity(x::V) where {T <: Number,V <: AbstractVector{T}} = identity(x)

_identity(i::T, x::V) where {T <: Number,V <: AbstractVector{T}} = identity(i)
_tr_identity(x) = identity(x)
_tr_identity(i, x) = identity(i)

"""
_count_eq(i::Int, x::V)
_count_eq_right(i::Int, x::V)
_count_eq_left(i::Int, x::V)
_tr_count_eq(i::Int, x::V)
_tr_count_eq_right(i::Int, x::V)
_tr_count_eq_left(i::Int, x::V)
Count the number of elements equal to `x[i]` (optionally to the right/left of `x[i]`). Extended method to vector `x::V` are generated.
"""
function _count_eq(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return count(y -> x[i] == y, x) - 1
end
function _count_eq_right(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return _count_eq(1, @view x[i:end])
end
function _count_eq_left(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return _count_eq(i, @view x[1:i])
end
_tr_count_eq(i, x) = count(y -> x[i] == y, x) - 1
_tr_count_eq_right(i, x) = _tr_count_eq(1, @view x[i:end])
_tr_count_eq_left(i, x) = _tr_count_eq(i, @view x[1:i])

# Generating vetorized versions
lazy(_count_eq, _count_eq_left, _count_eq_right)
lazy(_tr_count_eq, _tr_count_eq_left, _tr_count_eq_right)

"""
_count_greater(i::Int, x::V)
_count_lesser(i::Int, x::V)
_count_g_left(i::Int, x::V)
_count_l_left(i::Int, x::V)
_count_g_right(i::Int, x::V)
_count_l_right(i::Int, x::V)
_tr_count_greater(i::Int, x::V)
_tr_count_lesser(i::Int, x::V)
_tr_count_g_left(i::Int, x::V)
_tr_count_l_left(i::Int, x::V)
_tr_count_g_right(i::Int, x::V)
_tr_count_l_right(i::Int, x::V)
Count the number of elements greater/lesser than `x[i]` (optionally to the left/right of `x[i]`). Extended method to vector with sig `(x::V)` are generated.
"""
function _count_greater(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return count(y -> x[i] < y, x)
end
function _count_lesser(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return count(y -> x[i] > y, x)
end
function _count_g_left(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return _count_greater(i, @view x[1:i])
end
function _count_l_left(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return _count_lesser(i, @view x[1:i])
end
function _count_g_right(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return _count_greater(1, @view x[i:end])
end
function _count_l_right(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return _count_lesser(1, @view x[i:end])
end
_tr_count_greater(i, x) = count(y -> x[i] < y, x)
_tr_count_lesser(i, x) = count(y -> x[i] > y, x)
_tr_count_g_left(i, x) = _tr_count_greater(i, @view x[1:i])
_tr_count_l_left(i, x) = _tr_count_lesser(i, @view x[1:i])
_tr_count_g_right(i, x) = _tr_count_greater(1, @view x[i:end])
_tr_count_l_right(i, x) = _tr_count_lesser(1, @view x[i:end])

# Generating vetorized versions
lazy(_count_greater, _count_g_left, _count_g_right)
lazy(_count_lesser, _count_l_left, _count_l_right)
lazy(_tr_count_greater, _tr_count_g_left, _tr_count_g_right)
lazy(_tr_count_lesser, _tr_count_l_left, _tr_count_l_right)

"""
_count_eq_param(i::Int, x::V, param::T)
_count_l_param(i::Int, x::V, param::T)
_count_g_param(i::Int, x::V, param::T)
_tr_count_eq_param(i::Int, x::V, param::T)
_tr_count_l_param(i::Int, x::V, param::T)
_tr_count_g_param(i::Int, x::V, param::T)
Count the number of elements equal to (resp. lesser/greater than) `x[i] + param`. Extended method to vector with sig `(x::V, param::T)` are generated.
"""
function _count_eq_param(i::Int, x::V, param::T) where {T <: Number,V <: AbstractVector{T}}
return count(y -> y == x[i] + param, x)
end
function _count_l_param(i::Int, x::V, param::T) where {T <: Number,V <: AbstractVector{T}}
return count(y -> y < x[i] + param, x)
end
function _count_g_param(i::Int, x::V, param::T) where {T <: Number,V <: AbstractVector{T}}
return count(y -> y > x[i] + param, x)
end
_tr_count_eq_param(i, x, param) = count(y -> y == x[i] + param, x)
_tr_count_l_param(i, x, param) = count(y -> y < x[i] + param, x)
_tr_count_g_param(i, x, param) = count(y -> y > x[i] + param, x)

# Generating vetorized versions
lazy_param(_count_eq_param, _count_l_param, _count_g_param)
lazy_param(_tr_count_eq_param, _tr_count_l_param, _tr_count_g_param)

"""
_count_bounding_param(i::Int, x::V, param::T)
_tr_count_bounding_param(i::Int, x::V, param::T)
Count the number of elements bounded (not strictly) by `x[i]` and `x[i] + param`. An extended method to vector with sig `(x::V, param::T)` is generated.
"""
function _count_bounding_param(i::Int, x::V, param::T
) where {T <: Number,V <: AbstractVector{T}}
return count(y -> x[i] ≤ y ≤ x[i] + param, x)
end
_tr_count_bounding_param(i, x, param) = count(y -> x[i] ≤ y ≤ x[i] + param, x)

# Generating vetorized versions
lazy_param(_count_bounding_param)
lazy_param(_tr_count_bounding_param)

"""
_val_minus_param(i::Int, x::V, param::T)
_param_minus_val(i::Int, x::V, param::T)
_tr_val_minus_param(i::Int, x::V, param::T)
_tr_param_minus_val(i::Int, x::V, param::T)
Return the difference `x[i] - param` (resp. `param - x[i]`) if positive, `0.0` otherwise. Extended method to vector with sig `(x::V, param::T)` are generated.
"""
function _val_minus_param(i::Int, x::V, param::T
) where {T <: Number,V <: AbstractVector{T}}
return max(0, x[i] - param)
end
function _param_minus_val(i::Int, x::V, param::T
) where {T <: Number,V <: AbstractVector{T}}
return max(0, param - x[i])
end
_tr_val_minus_param(i, x, param) = max(0, x[i] - param)
_tr_param_minus_val(i, x, param) = max(0, param - x[i])

# Generating vetorized versions
lazy_param(_val_minus_param, _param_minus_val)
lazy_param(_tr_val_minus_param, _tr_param_minus_val)

"""
_contiguous_vals_minus(i::Int, x::V)
_contiguous_vals_minus_rev(i::Int, x::V)
_tr_contiguous_vals_minus(i::Int, x::V)
_tr_contiguous_vals_minus_rev(i::Int, x::V)
Return the difference `x[i] - x[i + 1]` (resp. `x[i + 1] - x[i]`) if positive, `0.0` otherwise. Extended method to vector with sig `(x::V)` are generated.
"""
function _contiguous_vals_minus(i::Int, x::V) where {T <: Number,V <: AbstractVector{T}}
return length(x) == i ? 0 : _val_minus_param(i, x, x[i + 1])
end
function _contiguous_vals_minus_rev(i::Int, x::V
) where {T <: Number,V <: AbstractVector{T}}
return length(x) == i ? 0 : _param_minus_val(i, x, x[i + 1])
_tr_contiguous_vals_minus(i, x) = length(x) == i ? 0 : _tr_val_minus_param(i, x, x[i + 1])
function _tr_contiguous_vals_minus_rev(i, x)
return length(x) == i ? 0 : _tr_param_minus_val(i, x, x[i + 1])
end
# Generating vetorized versions
lazy(_contiguous_vals_minus, _contiguous_vals_minus_rev)
lazy(_tr_contiguous_vals_minus, _tr_contiguous_vals_minus_rev)


"""
Expand All @@ -123,28 +91,28 @@ Generate the layer of transformations functions of the ICN. Iff `param` value is
"""
function transformation_layer(param = nothing)
transformations = LittleDict{Symbol, Function}(
:identity => _identity,
:count_eq => _count_eq,
:count_eq_left => _count_eq_left,
:count_eq_right => _count_eq_right,
:count_greater => _count_greater,
:count_lesser => _count_lesser,
:count_g_left => _count_g_left,
:count_l_left => _count_l_left,
:count_g_right => _count_g_right,
:count_l_right => _count_l_right,
:contiguous_vals_minus => _contiguous_vals_minus,
:contiguous_vals_minus_rev => _contiguous_vals_minus_rev,
:identity => _tr_identity,
:count_eq => _tr_count_eq,
:count_eq_left => _tr_count_eq_left,
:count_eq_right => _tr_count_eq_right,
:count_greater => _tr_count_greater,
:count_lesser => _tr_count_lesser,
:count_g_left => _tr_count_g_left,
:count_l_left => _tr_count_l_left,
:count_g_right => _tr_count_g_right,
:count_l_right => _tr_count_l_right,
:contiguous_vals_minus => _tr_contiguous_vals_minus,
:contiguous_vals_minus_rev => _tr_contiguous_vals_minus_rev,
)

if !isnothing(param)
transformations_param = LittleDict{Symbol, Function}(
:count_eq_param => ((x...) -> _count_eq_param(x..., param)),
:count_l_param => ((x...) -> _count_l_param(x..., param)),
:count_g_param => ((x...) -> _count_g_param(x..., param)),
:count_bounding_param => ((x...) -> _count_bounding_param(x..., param)),
:val_minus_param => ((x...) -> _val_minus_param(x..., param)),
:param_minus_val => ((x...) -> _param_minus_val(x..., param)),
:count_eq_param => ((x...) -> _tr_count_eq_param(x..., param)),
:count_l_param => ((x...) -> _tr_count_l_param(x..., param)),
:count_g_param => ((x...) -> _tr_count_g_param(x..., param)),
:count_bounding_param => ((x...) -> _tr_count_bounding_param(x..., param)),
:val_minus_param => ((x...) -> _tr_val_minus_param(x..., param)),
:param_minus_val => ((x...) -> _tr_param_minus_val(x..., param)),
)
transformations = LittleDict(union(transformations, transformations_param))
end
Expand Down
Loading