Example #1
0
def simulated_annealing_search_variation1(graph, iterations=500):
    #moguce je zameniti svih n cvorova
    best = list(graph.nodes)
    best_s, best_c = solutionValue(best, graph.adj)
    for i in range(1, iterations):
        current = best
        n = random.randint(0, (len(best) - 1))
        indexes = []
        values = []
        for _ in range(n):
            r = random.randint(0, len(best) - 1)
            indexes.append(r)
            values.append(current[r])
        random.shuffle(indexes)
        random.shuffle(values)

        for j in range(n):
            index = indexes[j]
            current[index] = values[j]

        current_s, current_c = solutionValue(current, graph.adj)
        if (current_c > best_c) or (1 / i > random.random()):
            best = current
            best_c = current_c
            best_s = current_s
    return best_s, best_c
Example #2
0
def simulated_annealing_search_sort(graph, iterations=500):
    best = list(graph.nodes)
    best.sort(key=lambda x: graph.degree[x])
    best_s, best_c = solutionValue(best, graph.adj)
    for i in range(1, iterations):
        current = best
        a = random.randint(0, len(best) - 1)
        b = random.randint(0, len(best) - 1)
        current[a], current[b] = current[b], current[a]
        current_s, current_c = solutionValue(current, graph.adj)
        if (current_c > best_c) or (1 / i > random.random()):
            best = current
            best_c = current_c
            best_s = current_s
    return best_s, best_c
Example #3
0
def simulated_annealing_search(graph, iterations=500):
    best = list(graph.nodes)
    #bolje uzeti cvor sa manjim stepenom na pocetku zato sto je veca sansa da nece moci u niz na kraju
    # best.sort(key=lambda x: graph.degree[x])
    best_s, best_c = solutionValue(best, graph.adj)
    for i in range(1, iterations):
        current = best
        a = random.randint(0, len(best) - 1)
        b = random.randint(0, len(best) - 1)
        current[a], current[b] = current[b], current[a]
        current_s, current_c = solutionValue(current, graph.adj)
        if (current_c > best_c) or (1 / i > random.random()):
            best = current
            best_c = current_c
            best_s = current_s
    return best_s, best_c
 def fitness(self, permutation):
     sol, cardinality = solutionValue(permutation, self._graph.adj)
     if cardinality > self._best_chromosome.fitness:
         self._best_chromosome.fitness = cardinality
         self._best_chromosome.content = sol
         self._solution = sol
     return cardinality
Example #5
0
def brute_force_search(graph):
    nodes = list(graph.nodes)
    adj = graph.adj
    nodes.sort()
    permutations = itertools.permutations(nodes)
    best_c = 0
    best_s = []
    for permutation in permutations:
        print(permutation)
        curr_s, curr_c = solutionValue(permutation, adj)
        if curr_c > best_c:
            best_c = curr_c
            best_s = curr_s
    return best_s, best_c
Example #6
0
def halldorsson(graph):
    best = list(graph.nodes)
    n = len(best)
    num_of_elements = math.ceil(math.log(n))
    print(math.log(n))
    adj = graph.adj
    print(n)
    subsets = math.ceil(n / math.log(n))
    best_c = 0
    best_s = []
    subset = []
    for _ in range(0, subsets):
        subset = random.sample(best, num_of_elements)
        print(subset)
        curr_s, curr_c = solutionValue(subset, adj)
        if curr_c > best_c:
            best_c = curr_c
            best_s = curr_s
    return best_s, best_c