Skip to content

Commit

Permalink
Update project
Browse files Browse the repository at this point in the history
  • Loading branch information
raphasampaio committed Feb 5, 2025
1 parent 113685d commit 35cdd17
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/assignments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ function assignment_step!(::BalancedKmeans; result::KmeansResult, distances::Abs
end

function assignment_step!(::Kmedoids; result::KmedoidsResult, distances::AbstractMatrix{<:Real}, medoids::AbstractVector{<:Vector{Int}})
k = length(result.clusters)
n = size(distances, 1)

for i in 1:k
empty!(medoids[i])
end

for i in 1:n
cluster, distance = kmedoids_assign(i, result.clusters, distances)
push!(medoids[cluster], i)
Expand Down
17 changes: 6 additions & 11 deletions src/localsearch/kmedoids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,22 @@ function fit!(kmedoids::AbstractKmedoids, distances::AbstractMatrix{<:Real}, res
for iteration in 1:kmedoids.max_iterations
previous_objective = result.objective

# assignment step
result.objective = 0
for i in 1:k
empty!(medoids[i])
clusters_size[i] = 0
result.objective_per_cluster[i] = 0
end

assignment_step!(kmedoids; result, distances, medoids)

for (i, medoid) in enumerate(medoids)
cluster = result.clusters[i]

result.objective_per_cluster[i] = 0
for point in medoid
distance = distances[point, cluster]

clusters_size[i] += 1
result.objective += distance
result.objective_per_cluster[i] += distance
end

clusters_size[i] = length(medoid)
end

result.objective = sum(result.objective_per_cluster)

change = abs(result.objective - previous_objective)

if kmedoids.verbose
Expand Down

0 comments on commit 35cdd17

Please sign in to comment.