Skip to content

Commit

Permalink
Merge branch 'main' into add-exchange-heuristic-restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
matt035343 authored Jun 13, 2024
2 parents c34e104 + 74fdf6a commit 2d2d548
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
9 changes: 9 additions & 0 deletions anti_clustering/_cluster_swap_heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ def _get_random_clusters(self, num_groups: int, num_elements: int) -> npt.NDArra
)

return cluster_assignment

def _calculate_objective(self, cluster_assignment: npt.NDArray[bool], distance_matrix: npt.NDArray[float]) -> float:
"""
Calculate objective value
:param cluster_assignment: Cluster assignment matrix
:param distance_matrix: Cost matrix
:return: Objective value
"""
return np.multiply(cluster_assignment, distance_matrix).sum()
10 changes: 0 additions & 10 deletions anti_clustering/exchange_heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
Psychological Methods, 26(2), 161–174. https://doi.org/10.1037/met0000301
"""

import numpy as np
import numpy.typing as npt
from anti_clustering._cluster_swap_heuristic import ClusterSwapHeuristic

Expand Down Expand Up @@ -82,12 +81,3 @@ def _solve(self, distance_matrix: npt.NDArray[float], num_groups: int) -> npt.ND
_, best_cluster_assignment = max(candidate_solutions, key=lambda x: x[0])

return best_cluster_assignment

def _calculate_objective(self, cluster_assignment: npt.NDArray[bool], distance_matrix: npt.NDArray[float]) -> float:
"""
Calculate objective value
:param cluster_assignment: Cluster assignment matrix
:param distance_matrix: Cost matrix
:return: Objective value
"""
return np.multiply(cluster_assignment, distance_matrix).sum()
16 changes: 14 additions & 2 deletions anti_clustering/naive_random_heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@ class NaiveRandomHeuristicAntiClustering(ClusterSwapHeuristic):
The naive randomized way of solving the anti-clustering problem.
"""

def __init__(self, verbose: bool = False, random_seed: int = None):
def __init__(self, verbose: bool = False, random_seed: int = None, iterations: int = 1000):
super().__init__(verbose=verbose, random_seed=random_seed)
self.iterations = iterations

def _solve(self, distance_matrix: npt.NDArray[float], num_groups: int) -> npt.NDArray[bool]:
return self._get_random_clusters(num_groups=num_groups, num_elements=len(distance_matrix))
best_candidate = self._get_random_clusters(num_groups=num_groups, num_elements=len(distance_matrix))
best_objective = self._calculate_objective(best_candidate, distance_matrix)

for _ in range(self.iterations):
candidate = self._get_random_clusters(num_groups=num_groups, num_elements=len(distance_matrix))
objective = self._calculate_objective(candidate, distance_matrix)

if objective > best_objective:
best_candidate = candidate
best_objective = objective

return best_candidate
10 changes: 0 additions & 10 deletions anti_clustering/simulated_annealing_heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"""

import math
import numpy as np
import numpy.typing as npt
from anti_clustering._cluster_swap_heuristic import ClusterSwapHeuristic

Expand Down Expand Up @@ -92,15 +91,6 @@ def _solve(self, distance_matrix: npt.NDArray[float], num_groups: int) -> npt.ND

return best_cluster_assignment

def _calculate_objective(self, cluster_assignment: npt.NDArray[bool], distance_matrix: npt.NDArray[float]) -> float:
"""
Calculate objective value
:param cluster_assignment: Cluster assignment
:param distance_matrix: Distance matrix
:return: Objective value
"""
return np.multiply(cluster_assignment, distance_matrix).sum()

def _accept(self, delta: float, temperature: float) -> bool:
"""
Simulated annealing acceptance function. Notice d/t is used instead of -d/t because we are maximizing.
Expand Down

0 comments on commit 2d2d548

Please sign in to comment.