遺傳算法 (GA) 算法最主要的就是我們要想明白什么是他的 DNA 和怎么樣對個體進行評估 (他們的 Fitness).
Fitness和DNA
這次的編碼 DNA 方式又不一樣, 我們可以嘗試對每一個城市有一個 ID, 那經歷的城市順序就是按 ID 排序咯. 比如說商人要經過3個城市, 我們就有
- 0-1-2
- 0-2-1
- 1-0-2
- 1-2-0
- 2-0-1
- 2-1-0
這6種排列方式. 每一種排列方式我們就能把它當做一種 DNA 序列, 用 numpy 產生這種 DNA 序列的方式很簡單.
>>> np.random.permutation(3) # array([1, 2, 0])
計算 fitness 的時候, 我們只要將 DNA 中這幾個城市連成線, 計算一下總路徑的長度, 根據長度, 我們定下規則, 越短的總路徑越好, 下面的 fitness0
就用來計算 fitness 啦. 因為越短的路徑我們更要價大幅度選擇, 所以這里我用到了 fitness1
這種方式.
fitness0 = 1/total_distance
fitness1 = np.exp(1/total_distance)
交叉和變異
我們要注意的是在 crossover
和 mutate
的時候有一點點不一樣, 因為對於路徑點, 我們不能隨意變化. 比如 如果按平時的 crossover
, 可能會是這樣的結果:
p1=[0,1,2,3]
(爸爸)
p2=[3,2,1,0]
(媽媽)
cp=[m,b,m,b]
(交叉點, m: 媽媽, b: 爸爸)
c1=[3,1,1,3]
(孩子)
那么這樣的 c1
要經過兩次城市 3, 兩次城市1, 而沒有經過 2, 0. 顯然不行. 所以我們 crossover
以及 mutation
都要換一種方式進行. 其中一種可行的方式是這樣. 同樣是上面的例子.
p1=[0,1,2,3]
(爸爸)
cp=[_,b,_,b]
(選好來自爸爸的點)
c1=[1,3,_,_]
(先將爸爸的點填到孩子的前面)
此時除開來自爸爸的 1, 3. 還有0, 2 兩個城市, 但是0,2 的順序就按照媽媽 DNA 的先后順序排列. 也就是 p2=[3,2,1,0]
的 0, 2 兩城市在 p2 中是先有 2, 再有 0. 所以我們就按照這個順序補去孩子的 DNA.
c1=[1,3,2,0]
按照這樣的方式, 我們就能成功避免在 crossover
產生的問題: 訪問多次通過城市的問題了. 用 Python 的寫法很簡單.
if np.random.rand() < self.cross_rate: i_ = np.random.randint(0, self.pop_size, size=1) # select another individual from pop cross_points = np.random.randint(0, 2, self.DNA_size).astype(np.bool) # choose crossover points keep_city = parent[cross_points] # find the city number swap_city = pop[i_, np.isin(pop[i_].ravel(), keep_city, invert=True)] # 找到與爸爸不同的城市 parent[:] = np.concatenate((keep_city, swap_city))
在 mutate
的時候, 也是找到兩個不同的 DNA 點, 然后交換這兩個點就好了.
for point in range(self.DNA_size): if np.random.rand() < self.mutate_rate: swap_point = np.random.randint(0, self.DNA_size) swapA, swapB = child[point], child[swap_point] child[point], child[swap_point] = swapB, swapA
完整代碼:
""" Visualize Genetic Algorithm to find the shortest path for travel sales problem. Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/ """ import matplotlib.pyplot as plt import numpy as np N_CITIES = 20 # DNA size CROSS_RATE = 0.1 MUTATE_RATE = 0.02 POP_SIZE = 500 N_GENERATIONS = 500 class GA(object): def __init__(self, DNA_size, cross_rate, mutation_rate, pop_size, ): self.DNA_size = DNA_size self.cross_rate = cross_rate self.mutate_rate = mutation_rate self.pop_size = pop_size self.pop = np.vstack([np.random.permutation(DNA_size) for _ in range(pop_size)]) def translateDNA(self, DNA, city_position): # get cities' coord in order line_x = np.empty_like(DNA, dtype=np.float64) line_y = np.empty_like(DNA, dtype=np.float64) for i, d in enumerate(DNA): city_coord = city_position[d] line_x[i, :] = city_coord[:, 0] line_y[i, :] = city_coord[:, 1] return line_x, line_y def get_fitness(self, line_x, line_y): total_distance = np.empty((line_x.shape[0],), dtype=np.float64) for i, (xs, ys) in enumerate(zip(line_x, line_y)): total_distance[i] = np.sum(np.sqrt(np.square(np.diff(xs)) + np.square(np.diff(ys)))) fitness = np.exp(self.DNA_size * 2 / total_distance) return fitness, total_distance def select(self, fitness): idx = np.random.choice(np.arange(self.pop_size), size=self.pop_size, replace=True, p=fitness / fitness.sum()) return self.pop[idx] def crossover(self, parent, pop): if np.random.rand() < self.cross_rate: i_ = np.random.randint(0, self.pop_size, size=1) # select another individual from pop cross_points = np.random.randint(0, 2, self.DNA_size).astype(np.bool) # choose crossover points keep_city = parent[~cross_points] # find the city number swap_city = pop[i_, np.isin(pop[i_].ravel(), keep_city, invert=True)] parent[:] = np.concatenate((keep_city, swap_city)) return parent def mutate(self, child): for point in range(self.DNA_size): if np.random.rand() < self.mutate_rate: swap_point = np.random.randint(0, self.DNA_size) swapA, swapB = child[point], child[swap_point] child[point], child[swap_point] = swapB, swapA return child def evolve(self, fitness): pop = self.select(fitness) pop_copy = pop.copy() for parent in pop: # for every parent child = self.crossover(parent, pop_copy) child = self.mutate(child) parent[:] = child self.pop = pop class TravelSalesPerson(object): def __init__(self, n_cities): self.city_position = np.random.rand(n_cities, 2) plt.ion() def plotting(self, lx, ly, total_d): plt.cla() plt.scatter(self.city_position[:, 0].T, self.city_position[:, 1].T, s=100, c='k') plt.plot(lx.T, ly.T, 'r-') plt.text(-0.05, -0.05, "Total distance=%.2f" % total_d, fontdict={'size': 20, 'color': 'red'}) plt.xlim((-0.1, 1.1)) plt.ylim((-0.1, 1.1)) plt.pause(0.01) ga = GA(DNA_size=N_CITIES, cross_rate=CROSS_RATE, mutation_rate=MUTATE_RATE, pop_size=POP_SIZE) env = TravelSalesPerson(N_CITIES) for generation in range(N_GENERATIONS): lx, ly = ga.translateDNA(ga.pop, env.city_position) fitness, total_distance = ga.get_fitness(lx, ly) ga.evolve(fitness) best_idx = np.argmax(fitness) print('Gen:', generation, '| best fit: %.2f' % fitness[best_idx],) env.plotting(lx[best_idx], ly[best_idx], total_distance[best_idx]) plt.ioff() plt.show()