@@ -13,45 +13,45 @@ def __init__(self, cities, pop_size, generations, mutation_rate, elite_size):
13
13
self .generations = generations
14
14
self .mutation_rate = mutation_rate
15
15
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 )
20
20
21
21
def calculate_fitness (self ):
22
22
fitness = []
23
23
for individual in self .population :
24
24
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 )
29
29
return fitness
30
30
31
31
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 )]
36
36
return parents
37
37
38
38
def crossover (self , parents ):
39
39
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 )
48
48
return children
49
49
50
50
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 ]
55
55
return children
56
56
57
57
def create_circuit (self ):
@@ -124,17 +124,18 @@ def __str__(self):
124
124
125
125
126
126
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 )
133
134
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