1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| from functools import reduce import matplotlib.pyplot as plt import numpy as np
def init(pop_size,leng): population = [] for i in range(pop_size): pop = '' for j in range(leng): pop = pop + str(np.random.randint(0,2)) population.append(pop) return population
def compute_fitness(population,weight,profit): weight_list = [] profit_list = [] for pop in population: tmp_w = 0 tmp_p = 0 for idx in range(len(pop)): if pop[idx] == '1': tmp_w += weight[idx] tmp_p += profit[idx] weight_list.append(tmp_w) profit_list.append(tmp_p) return weight_list,profit_list
def select(weight_limit,population,weight,profit): pop,w,p = [],[],[] for idx in range(len(weight)): if weight[idx] <= weight_limit: w.append(weight[idx]) p.append(profit[idx]) pop.append(population[idx]) return pop,w,p
def roulette(pop_size,population,total_profit): sum_profit = reduce(lambda i,j:i+j,total_profit) p = list(map(lambda i:i/sum_profit,total_profit)) new_population = [] while len(new_population) < pop_size: tmp = np.random.choice(a=population,size=1,replace=True,p=p) tmp = tmp.tolist()[0] new_population.append(tmp) return new_population
def ga_cross(new_population,pcross): children = [] leng = len(new_population[0]) while len(children) < len(new_population): if np.random.uniform() < pcross: mo_idx = np.random.randint(0,leng) fa_idx = np.random.randint(0,leng) mo = new_population[mo_idx] fa = new_population[fa_idx] if fa_idx != mo_idx: seg_idx = np.random.randint(0,leng) mo_left = mo[0:seg_idx] mo_right = mo[seg_idx:leng] fa_left = fa[0:seg_idx] fa_right = fa[seg_idx:leng] child1 = mo_left + fa_right child2 = fa_left + mo_right children.append(child1) children.append(child2) return children
def mutation(population,pmutation): new_pop = [] leng = len(population[0]) for pop in population: if np.random.uniform() < pmutation: idx = np.random.randint(0,leng) pop = list(pop) if pop[idx] == '0': pop[idx] = '1' else: pop[idx] = '0' pop = ''.join(pop) new_pop.append(pop) return new_pop
if __name__ == '__main__': pm = 0.2 pc = 0.8 iters = 600 pop_size = 50 n = 14 weight=[5,7,9,8,4,3,10,14,13,9,6,8,5,15] profit=[10,8,15,9,6,5,20,10,13,10,7,12,5,18] weight_limit = 75 population = init(pop_size,n) cur_iter = 0 ans_pop,ans_w,ans_p = [],[],[] while cur_iter < iters: w,p = compute_fitness(population,weight,profit) s_pop,s_w,s_p = select(weight_limit,population,w,p) new_pop = roulette(pop_size,s_pop,s_p) new_pop1 = ga_cross(new_pop,pc) population = mutation(new_pop1,pm) cur_iter += 1 w,p = compute_fitness(population,weight,profit) s_pop,s_w,s_p = select(weight_limit,population,w,p) idx = s_pop.index(max(s_pop)) if len(ans_p)==0 or s_p[idx] > ans_p[-1]: ans_pop.append(s_pop[idx]) ans_p.append(s_p[idx]) ans_w.append(s_w[idx]) idx = population.index(max(population)) print("w_limit:",weight_limit) print("w的变化",ans_w) print("p的变化",ans_p) plt.rcParams['font.sans-serif']=['simsun'] plt.rcParams['axes.unicode_minus'] = False plt.plot(range(len(ans_p)),ans_p,label='装入物品总价值', color='red') for idx,(w,p) in enumerate(zip(ans_w, ans_p)): plt.text(idx, p, f'(w:{w},p:{p})') plt.legend() plt.show()
|