Beispiel #1
0
 def initialize_random(self):
     return [
         Graph([
             self._nodes_left.pop(
                 self._random.randrange(len(self._nodes_left)))
         ], self._distance_matrix) for _ in range(self._num_of_clusters)
     ]
Beispiel #2
0
 def __init__(self,
              nodes,
              distance_matrix,
              rand,
              num_of_clusters=None,
              starting_points=None,
              init_by_range=False,
              online_draw=False,
              position_data=None):
     self._distance_matrix = distance_matrix
     self._nodes_left = list(nodes)
     self._num_of_clusters = num_of_clusters
     self._online_draw = online_draw
     self._position_data = position_data
     self._random = rand
     assert bool(num_of_clusters is None) ^ bool(
         starting_points is None)  # xor
     if starting_points is None:
         self._clusters = self.initialize_by_range(
         ) if init_by_range else self.initialize_random()
     else:
         self._clusters = [
             Graph(list(point), distance_matrix)
             for point in starting_points
         ]
     for i in range(len(self._clusters)):
         self._clusters[i].id = i
Beispiel #3
0
    def initialize_by_range(self):
        dims = len(self._position_data[0])
        self._product = 1
        for i in range(dims):
            min_i = np.amin(self._position_data[:, i - 1], axis=0)
            max_i = np.amax(self._position_data[:, i - 1], axis=0)
            rang_i = max_i - min_i
            self._product *= rang_i

        self._product /= self._num_of_clusters
        # root of dims degree
        self._product = math.pow(self._product, 1 / dims)
        self._product *= math.sqrt(dims)  # times diagonal

        self._slope_level = 1
        self._threshold = 0

        # indices = np.unravel_index(np.argmax(self._distance_matrix), self._distance_matrix.shape)
        # points = [ind for ind in indices]

        points = [self._random.randint(0, 201)]

        while len(points) < self._num_of_clusters:
            candidates = [
                ([self._distance_matrix[i, point] for point in points], i)
                for i in range(len(self._distance_matrix)) if i not in points
            ]

            cand_vals = [
                (
                    mean(list(map(
                        self.func,
                        cand[0])))  # - var(self._distance_matrix[:, cand[1]])
                    ,
                    cand[1]) for cand in candidates
            ]

            sum_dist, point = max(cand_vals, key=lambda t: t[0])
            points.append(point)
        self._nodes_left = [
            node for node in self._nodes_left if node not in points
        ]
        return [
            Graph([points[i]], self._distance_matrix)
            for i in range(self._num_of_clusters)
        ]
Beispiel #4
0
def recombine(parent1 : LocalSearch, parent2 : LocalSearch, k=20):
    points = list(range(len(parent1.nearest._distance_matrix)))
    graphs = []
    while(len(graphs) < k):
        point = np.random.choice(points)
        minGroup = getMinGroup(point, parent1, parent2)
        points = difference(points, minGroup)
        graphTemp = Graph(minGroup, parent1.nearest._distance_matrix, len(graphs))
        graphs.append(graphTemp)
    nearest = Nearest(points, parent1.nearest._distance_matrix, None, k, position_data=parent1.nearest._position_data, online_draw=False, createClusters=False, givenClusters=graphs)
    while (len(nearest._nodes_left) > 0):
        nearest.distribute_random_point()
    localSearch = LocalSearch(nearest._clusters, nearest, useCache=False, useCandidateMoves=True, distance_matrix=parent1.nearest._distance_matrix,
                              k_candidates=21)
    localSearch.greedy()
    localSearch.countMetric(True)

    return localSearch
Beispiel #5
0
    def __init__(self,
                 nodes,
                 distance_matrix,
                 rand=None,
                 num_of_clusters=None,
                 starting_points=None,
                 init_by_range=False,
                 online_draw=False,
                 position_data=None,
                 nearest_obj=None,
                 createClusters=True,
                 givenClusters=None):
        self._distance_matrix = distance_matrix
        self._nodes_left = list(nodes)
        self._num_of_clusters = num_of_clusters
        self._online_draw = online_draw
        self._position_data = position_data
        self._random = rand if rand is not None else random.Random()
        assert bool(num_of_clusters is None) ^ bool(
            starting_points is None)  # xor
        if createClusters:
            if starting_points is None:
                if nearest_obj is not None:
                    self._clusters = self.initialize_by_another_obj(
                        nearest_obj)
                else:
                    self._clusters = self.initialize_by_range(
                    ) if init_by_range else self.initialize_random()
            else:
                self._clusters = [
                    Graph(list(point), distance_matrix)
                    for point in starting_points
                ]

            for i in range(len(self._clusters)):
                self._clusters[i].id = i
        if givenClusters is not None:
            self._clusters = givenClusters
Beispiel #6
0
from Code.Graph import Graph
from Code.Data.DistanceMatrix import getDistanceMatrix
from Code.Data.LoadData import readData
from Code.drawGraph import visualizeData
from functools import reduce
from operator import add
position_data = readData(path="../objects.data")
matrix = getDistanceMatrix(position_data)
all_nodes = len(position_data)

g = Graph(list(range(all_nodes)), matrix)
edges_cut = []
# cutting max edges
for i in range(9):
    edges_cut.append(g.get_max_edge())
    g.remove_edge([edges_cut[i]])
print(g.compute_edges_length())
graphs = []
visualizeData(position_data, [g.points], g.edges)
# building clusters from disconnected graphs
for i in range(10):
    points = g.get_first_connected_subgraph()
    g.removePoints(points)
    graphs.append(Graph(points, matrix))

    visualizeData(position_data, [graphs[i].points], graphs[i].edges)

visualizeData(position_data, [graph.points for graph in graphs],
              reduce(add, [graph.edges for graph in graphs]))
Beispiel #7
0
 def initialize_by_another_obj(self, nearest):
     self._nodes_left = [nd for nd in nearest._nodes_left]
     return [
         Graph([pt for pt in graph.points], self._distance_matrix,
               int(graph.id)) for graph in nearest._clusters
     ]