Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 02ecab7

Browse files
committed
Hot Fix
1 parent ceb2b44 commit 02ecab7

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

Quantum_Logistics_Solvers/Quantum_Genetic_Algorithm.py

+38-37
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,45 @@ def __init__(self, cities, pop_size, generations, mutation_rate, elite_size):
1313
self.generations = generations
1414
self.mutation_rate = mutation_rate
1515
self.elite_size = elite_size
16-
self.population = [np.random.permutation (len (cities)) for _ in range (pop_size)]
17-
self.fitness = self.calculate_fitness ()
18-
self.best_individual = self.population [np.argmin (self.fitness)]
19-
self.best_fitness = np.min (self.fitness)
16+
self.population = [np.random.permutation(len(cities)) for _ in range(pop_size)]
17+
self.fitness = self.calculate_fitness()
18+
self.best_individual = self.population[np.argmin(self.fitness)]
19+
self.best_fitness = np.min(self.fitness)
2020

2121
def calculate_fitness(self):
2222
fitness = []
2323
for individual in self.population:
2424
distance = 0
25-
for i in range (len (individual) - 1):
26-
distance += np.linalg.norm (self.cities [individual [i]] - self.cities [individual [i + 1]])
27-
distance += np.linalg.norm (self.cities [individual [-1]] - self.cities [individual [0]])
28-
fitness.append (distance)
25+
for i in range(len(individual) - 1):
26+
distance += np.linalg.norm(self.cities[individual[i]] - self.cities[individual[i + 1]])
27+
distance += np.linalg.norm(self.cities[individual[-1]] - self.cities[individual[0]])
28+
fitness.append(distance)
2929
return fitness
3030

3131
def select_parents(self):
32-
fitness = 1 / np.array (self.fitness)
33-
fitness /= np.sum (fitness)
34-
parents = [self.population [i] for i in
35-
numpy.random.Generator (len (self.population), self.elite_size, p=fitness, replace=False)]
32+
fitness = 1 / np.array(self.fitness)
33+
fitness /= np.sum(fitness)
34+
rng = np.random.default_rng()
35+
parents = [self.population[i] for i in rng.choice(len(self.population), self.elite_size, p=fitness, replace=False)]
3636
return parents
3737

3838
def crossover(self, parents):
3939
children = []
40-
for i in range (self.pop_size - self.elite_size):
41-
parent1 = parents [numpy.random.Generator (len (parents))]
42-
parent2 = parents [numpy.random.Generator (len (parents))]
43-
child = np.copy (parent1)
44-
for j in range (len (child)):
45-
if np.random.rand () < 0.5:
46-
child [j] = parent2 [j]
47-
children.append (child)
40+
for i in range(self.pop_size - self.elite_size):
41+
parent1 = parents[rng.choice(len(parents))]
42+
parent2 = parents[rng.choice(len(parents))]
43+
child = np.copy(parent1)
44+
for j in range(len(child)):
45+
if np.random.rand() < 0.5:
46+
child[j] = parent2[j]
47+
children.append(child)
4848
return children
4949

5050
def mutate(self, children):
51-
for i in range (len (children)):
52-
if np.random.rand () < self.mutation_rate:
53-
index1, index2 = np.random.choice (len (children [i]), 2, replace=False)
54-
children [i] [index1], children [i] [index2] = children [i] [index2], children [i] [index1]
51+
for i in range(len(children)):
52+
if np.random.rand() < self.mutation_rate:
53+
index1, index2 = np.random.choice(len(children[i]), 2, replace=False)
54+
children[i][index1], children[i][index2] = children[i][index2], children[i][index1]
5555
return children
5656

5757
def create_circuit(self):
@@ -124,17 +124,18 @@ def __str__(self):
124124

125125

126126
if __name__ == "__main__":
127-
cities = numpy.random.Generator (10, 2)
128-
tsp = QuantumTSP (cities, 100, 100, 0.01, 10)
129-
for _ in range (tsp.generations):
130-
parents = tsp.select_parents ()
131-
children = tsp.crossover (parents)
132-
children = tsp.mutate (children)
127+
rng = np.random.default_rng()
128+
cities = rng.random((10, 2))
129+
tsp = QuantumTSP(cities, 100, 100, 0.01, 10)
130+
for _ in range(tsp.generations):
131+
parents = tsp.select_parents()
132+
children = tsp.crossover(parents)
133+
children = tsp.mutate(children)
133134
tsp.population = parents + children
134-
tsp.fitness = tsp.calculate_fitness ()
135-
best_index = np.argmin (tsp.fitness)
136-
if tsp.fitness [best_index] < np.min (tsp.best_fitness):
137-
tsp.best_individual = tsp.population [best_index]
138-
tsp.best_fitness = tsp.fitness [best_index]
139-
print (tsp.best_individual, tsp.best_fitness)
140-
QuantumTSP.plot_route (cities, tsp.best_individual) # visualize the best route
135+
tsp.fitness = tsp.calculate_fitness()
136+
best_index = np.argmin(tsp.fitness)
137+
if tsp.fitness[best_index] < tsp.best_fitness:
138+
tsp.best_individual = tsp.population[best_index]
139+
tsp.best_fitness = tsp.fitness[best_index]
140+
print(tsp.best_individual, tsp.best_fitness)
141+
QuantumTSP.plot_route(cities, tsp.best_individual) # visualize the best route

0 commit comments

Comments
 (0)