Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename abstract structs #67

Merged
merged 1 commit into from
Jul 30, 2024
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
3 changes: 1 addition & 2 deletions src/UnsupervisedClustering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export concatenate,
GeneticAlgorithm,
ClusteringChain

abstract type Algorithm end
abstract type Result end
include("abstract.jl")

include("localsearch/gmm.jl")
include("localsearch/kmeans.jl")
Expand Down
2 changes: 2 additions & 0 deletions src/abstract.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
abstract type AbstractAlgorithm end
abstract type AbstractResult end
14 changes: 7 additions & 7 deletions src/concatenate.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# function concatenate_k(result::Result, results::Result...)
# function concatenate_k(result::AbstractResult, results::AbstractResult...)
# return result.k + sum([i.k for i in results])
# end

function concatenate_assignments(result::Result, results::Result...)
function concatenate_assignments(result::AbstractResult, results::AbstractResult...)
size = length(results)
accumulated = zeros(Int, size)
for i in 1:size
Expand Down Expand Up @@ -32,23 +32,23 @@ function concatenate_clusters(result::KmedoidsResult, results::KmedoidsResult...
return vcat(result.clusters, [results[i].clusters .+ accumulated[i] for i in 1:size]...)
end

function concatenate_objective(result::Result, results::Result...)
function concatenate_objective(result::AbstractResult, results::AbstractResult...)
return result.objective + sum([i.objective for i in results])
end

function concatenate_objective_per_cluster(result::Result, results::Result...)
function concatenate_objective_per_cluster(result::AbstractResult, results::AbstractResult...)
return vcat(result.objective_per_cluster, [i.objective_per_cluster for i in results]...)
end

function concatenate_iterations(result::Result, results::Result...)
function concatenate_iterations(result::AbstractResult, results::AbstractResult...)
return result.iterations + sum([i.iterations for i in results])
end

function concatenate_elapsed(result::Result, results::Result...)
function concatenate_elapsed(result::AbstractResult, results::AbstractResult...)
return result.elapsed + sum([i.elapsed for i in results])
end

function concatenate_converged(result::Result, results::Result...)
function concatenate_converged(result::AbstractResult, results::AbstractResult...)
return result.converged && all([i.converged for i in results])
end

Expand Down
8 changes: 4 additions & 4 deletions src/ensemble/chain.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
@doc """
ClusteringChain(algorithms::Algorithm...)
ClusteringChain(algorithms::AbstractAlgorithm...)

ClusteringChain represents a chain of clustering algorithms that are executed sequentially. It allows for applying multiple clustering algorithms in a specific order to refine and improve the clustering results.

# Fields
- `algorithms`: the vector of clustering algorithms that will be executed in sequence.
"""
Base.@kwdef struct ClusteringChain <: Algorithm
algorithms::AbstractVector{<:Algorithm}
Base.@kwdef struct ClusteringChain <: AbstractAlgorithm
algorithms::AbstractVector{<:AbstractAlgorithm}

function ClusteringChain(algorithms::Algorithm...)
function ClusteringChain(algorithms::AbstractAlgorithm...)
return new(collect(algorithms))
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/localsearch/gmm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The GMM is a clustering algorithm that models the underlying data distribution a
Maximum likelihood from incomplete data via the EM algorithm.
Journal of the royal statistical society: series B (methodological) 39.1 (1977): 1-22.
"""
Base.@kwdef mutable struct GMM <: Algorithm
Base.@kwdef mutable struct GMM <: AbstractAlgorithm
estimator::CovarianceMatrixEstimator
verbose::Bool = DEFAULT_VERBOSE
rng::AbstractRNG = Random.GLOBAL_RNG
Expand Down Expand Up @@ -58,7 +58,7 @@ GMMResult struct represents the result of the GMM clustering algorithm.
- `converged`: indicates whether the algorithm has converged to a solution.
- `k`: the number of clusters.
"""
mutable struct GMMResult{I <: Integer, R <: Real} <: Result
mutable struct GMMResult{I <: Integer, R <: Real} <: AbstractResult
assignments::Vector{I}
weights::Vector{R}
clusters::Vector{Vector{R}}
Expand Down
4 changes: 2 additions & 2 deletions src/localsearch/kmeans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The k-means is a clustering algorithm that aims to partition data into clusters
Least squares quantization in PCM.
IEEE transactions on information theory 28.2 (1982): 129-137.
"""
Base.@kwdef mutable struct Kmeans <: Algorithm
Base.@kwdef mutable struct Kmeans <: AbstractAlgorithm
metric::SemiMetric = SqEuclidean()
verbose::Bool = DEFAULT_VERBOSE
rng::AbstractRNG = Random.GLOBAL_RNG
Expand Down Expand Up @@ -56,7 +56,7 @@ KmeansResult struct represents the result of the k-means clustering algorithm.
- `converged`: indicates whether the algorithm has converged to a solution.
- `k`: the number of clusters.
"""
mutable struct KmeansResult{I <: Integer, R <: Real} <: Result
mutable struct KmeansResult{I <: Integer, R <: Real} <: AbstractResult
assignments::Vector{I}
clusters::Matrix{R}
objective::R
Expand Down
4 changes: 2 additions & 2 deletions src/localsearch/kmedoids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The k-medoids is a variation of k-means clustering algorithm that uses actual da

# References
"""
Base.@kwdef mutable struct Kmedoids <: Algorithm
Base.@kwdef mutable struct Kmedoids <: AbstractAlgorithm
verbose::Bool = DEFAULT_VERBOSE
rng::AbstractRNG = Random.GLOBAL_RNG
tolerance::Real = DEFAULT_TOLERANCE
Expand Down Expand Up @@ -47,7 +47,7 @@ KmedoidsResult struct represents the result of the k-medoids clustering algorith
- `converged`: indicates whether the algorithm has converged to a solution.
- `k`: the number of clusters.
"""
mutable struct KmedoidsResult{I <: Integer, R <: Real} <: Result
mutable struct KmedoidsResult{I <: Integer, R <: Real} <: AbstractResult
assignments::Vector{I}
clusters::Vector{I}
objective::R
Expand Down
2 changes: 1 addition & 1 deletion src/localsearch/ksegmentation.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Base.@kwdef mutable struct Ksegmentation <: Algorithm
Base.@kwdef mutable struct Ksegmentation <: AbstractAlgorithm
end

const KsegmentationResult{I <: Integer, R <: Real} = KmeansResult{I, R}
Expand Down
2 changes: 1 addition & 1 deletion src/markov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function stochastic_matrix(k::Integer, from::AbstractVector{<:Integer}, to::Abst
return matrix
end

function stochastic_matrix(from::Result, to::Result)
function stochastic_matrix(from::AbstractResult, to::AbstractResult)
@assert from.k == to.k
return stochastic_matrix(from.k, from.assignments, to.assignments)
end
8 changes: 4 additions & 4 deletions src/metaheuristic/generation.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mutable struct Generation
population::AbstractVector{<:Result}
population::AbstractVector{<:AbstractResult}
empty::Set{Integer}

function Generation()
return new(Vector{Result}(), Set{Int}())
return new(Vector{AbstractResult}(), Set{Int}())
end
end

Expand All @@ -21,7 +21,7 @@ function remove(generation::Generation, i::Integer)
return nothing
end

function add!(generation::Generation, result::Result)
function add!(generation::Generation, result::AbstractResult)
if length(generation.empty) > 0
generation.population[pop!(generation.empty)] = result
else
Expand Down Expand Up @@ -50,7 +50,7 @@ function get_best_solution(generation::Generation)
return best_solution
end

function crossover(parent1::Result, parent2::Result, data::AbstractMatrix{<:Real}, rng::AbstractRNG)
function crossover(parent1::AbstractResult, parent2::AbstractResult, data::AbstractMatrix{<:Real}, rng::AbstractRNG)
k = parent1.k

weights = zeros(k, k)
Expand Down
8 changes: 4 additions & 4 deletions src/metaheuristic/geneticalgorithm.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@doc """
GeneticAlgorithm(
local_search::Algorithm
local_search::AbstractAlgorithm
verbose::Bool = DEFAULT_VERBOSE
max_iterations::Integer = 200
max_iterations_without_improvement::Integer = 150
Expand All @@ -20,8 +20,8 @@ GeneticAlgorithm represents a clustering algorithm that utilizes a genetic algor

# References
"""
Base.@kwdef struct GeneticAlgorithm <: Algorithm
local_search::Algorithm
Base.@kwdef struct GeneticAlgorithm <: AbstractAlgorithm
local_search::AbstractAlgorithm
verbose::Bool = DEFAULT_VERBOSE
max_iterations::Integer = 200
max_iterations_without_improvement::Integer = 150
Expand Down Expand Up @@ -57,7 +57,7 @@ genetic_algorithm = GeneticAlgorithm(local_search = kmeans)
result = fit(genetic_algorithm, data, k)
```
"""
function fit(meta::GeneticAlgorithm, data::AbstractMatrix{<:Real}, k::Integer)::Result
function fit(meta::GeneticAlgorithm, data::AbstractMatrix{<:Real}, k::Integer)::AbstractResult
generation = Generation()

best_objective = 0.0
Expand Down
8 changes: 4 additions & 4 deletions src/metaheuristic/multistart.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@doc """
MultiStart(
local_search::Algorithm
local_search::AbstractAlgorithm
verbose::Bool = DEFAULT_VERBOSE
max_iterations::Integer = 200
)
Expand All @@ -12,8 +12,8 @@ The MultiStart approach repeatedly applies a clustering algorithm to generate mu
- `verbose`: controls whether the algorithm should display additional information during execution.
- `max_iterations`: represents the maximum number of iterations the algorithm will perform before stopping.
"""
Base.@kwdef struct MultiStart <: Algorithm
local_search::Algorithm
Base.@kwdef struct MultiStart <: AbstractAlgorithm
local_search::AbstractAlgorithm
verbose::Bool = DEFAULT_VERBOSE
max_iterations::Integer = 200
end
Expand Down Expand Up @@ -46,7 +46,7 @@ multi_start = MultiStart(local_search = kmeans)
result = fit(multi_start, data, k)
```
"""
function fit(meta::MultiStart, data::AbstractMatrix{<:Real}, k::Integer)::Result
function fit(meta::MultiStart, data::AbstractMatrix{<:Real}, k::Integer)::AbstractResult
best_result = fit(meta.local_search, data, k)

if meta.verbose
Expand Down
8 changes: 4 additions & 4 deletions src/metaheuristic/randomswap.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@doc """
RandomSwap(
local_search::Algorithm
local_search::AbstractAlgorithm
verbose::Bool = DEFAULT_VERBOSE
max_iterations::Integer = 200
max_iterations_without_improvement::Integer = 150
Expand All @@ -19,8 +19,8 @@ RandomSwap is a meta-heuristic approach used for clustering problems. It follows
Efficiency of random swap clustering.
Journal of big data 5.1 (2018): 1-29.
"""
Base.@kwdef struct RandomSwap <: Algorithm
local_search::Algorithm
Base.@kwdef struct RandomSwap <: AbstractAlgorithm
local_search::AbstractAlgorithm
verbose::Bool = DEFAULT_VERBOSE
max_iterations::Integer = 200
max_iterations_without_improvement::Integer = 150
Expand Down Expand Up @@ -54,7 +54,7 @@ random_swap = RandomSwap(local_search = kmeans)
result = fit(random_swap, data, k)
```
"""
function fit(meta::RandomSwap, data::AbstractMatrix{<:Real}, k::Integer)::Result
function fit(meta::RandomSwap, data::AbstractMatrix{<:Real}, k::Integer)::AbstractResult
iterations_without_improvement = 0

best_result = fit(meta.local_search, data, k)
Expand Down
8 changes: 4 additions & 4 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# return (number == 0) ? 1 : (log10(number) + 1)
# end

print_objective(result::Result) = print_objective(result.objective)
print_objective(result::AbstractResult) = print_objective(result.objective)
function print_objective(objective::Real)
@printf("%12.4f ", objective)
return nothing
Expand All @@ -13,13 +13,13 @@ function print_iteration(iteration::Integer)
return nothing
end

print_iterations(result::Result) = print_iterations(result.iterations)
print_iterations(result::AbstractResult) = print_iterations(result.iterations)
function print_iterations(iterations::Integer)
@printf("%8dit ", iterations)
return nothing
end

print_elapsed(result::Result) = print_elapsed(result.elapsed)
print_elapsed(result::AbstractResult) = print_elapsed(result.elapsed)
function print_elapsed(elapsed::Real)
@printf("%10.2fs ", elapsed)
return nothing
Expand All @@ -30,7 +30,7 @@ function print_change(change::Real)
return nothing
end

function print_result(result::Result)
function print_result(result::AbstractResult)
print_objective(result)
print_iterations(result)
print_elapsed(result)
Expand Down
2 changes: 1 addition & 1 deletion src/result/counts.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function counts(result::Result)
function counts(result::AbstractResult)
return StatsBase.counts(result.assignments, result.k)
end
2 changes: 1 addition & 1 deletion src/seed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function seed!(algorithm::Algorithm, seed::Integer)
function seed!(algorithm::AbstractAlgorithm, seed::Integer)
Random.seed!(algorithm.rng, seed)
return nothing
end
Expand Down
2 changes: 1 addition & 1 deletion test/gmmsk.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Base.@kwdef mutable struct GMMSK <: UnsupervisedClustering.Algorithm
Base.@kwdef mutable struct GMMSK <: UnsupervisedClustering.AbstractAlgorithm
verbose::Bool = false
rng::AbstractRNG = Random.GLOBAL_RNG
tolerance::Real = 1e-3
Expand Down
Loading