-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGA.py
86 lines (70 loc) · 2.91 KB
/
GA.py
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
#Сopied from https://medium.com/koderunners/genetic-algorithm-part-3-knapsack-problem-b59035ddd1d6 for the comparison purpose
import numpy as np
import pandas as pd
import random as rd
from random import randint
import matplotlib.pyplot as plt
def cal_fitness(weight, value, population, threshold):
fitness = np.empty(population.shape[0])
for i in range(population.shape[0]):
S1 = np.sum(population[i] * value)
S2 = np.sum(population[i] * weight)
if S2 <= threshold:
fitness[i] = S1
else :
fitness[i] = 0
return fitness.astype(int)
def selection(fitness, num_parents, population):
fitness = list(fitness)
parents = np.empty((num_parents, population.shape[1]))
for i in range(num_parents):
max_fitness_idx = np.where(fitness == np.max(fitness))
parents[i,:] = population[max_fitness_idx[0][0], :]
fitness[max_fitness_idx[0][0]] = -999999
return parents
def crossover(parents, num_offsprings):
offsprings = np.empty((num_offsprings, parents.shape[1]))
crossover_point = int(parents.shape[1]/2)
crossover_rate = 0.8
i=0
while (parents.shape[0] < num_offsprings):
parent1_index = i%parents.shape[0]
parent2_index = (i+1)%parents.shape[0]
x = rd.random()
if x > crossover_rate:
continue
parent1_index = i%parents.shape[0]
parent2_index = (i+1)%parents.shape[0]
offsprings[i,0:crossover_point] = parents[parent1_index,0:crossover_point]
offsprings[i,crossover_point:] = parents[parent2_index,crossover_point:]
i=+1
return offsprings
def mutation(offsprings):
mutants = np.empty((offsprings.shape))
mutation_rate = 0.4
for i in range(mutants.shape[0]):
random_value = rd.random()
mutants[i,:] = offsprings[i,:]
if random_value > mutation_rate:
continue
int_random_value = randint(0,offsprings.shape[1]-1)
if mutants[i,int_random_value] == 0 :
mutants[i,int_random_value] = 1
else :
mutants[i,int_random_value] = 0
return mutants
def optimize(weight, value, population, pop_size, num_generations, threshold):
parameters, fitness_history = [], []
num_parents = int(pop_size[0]/2)
num_offsprings = pop_size[0] - num_parents
for i in range(num_generations):
fitness = cal_fitness(weight, value, population, threshold)
fitness_history.append(fitness)
parents = selection(fitness, num_parents, population)
offsprings = crossover(parents, num_offsprings)
mutants = mutation(offsprings)
population[0:parents.shape[0], :] = parents
population[parents.shape[0]:, :] = mutants
fitness_last_gen = cal_fitness(weight, value, population, threshold)
best_fitness = max(fitness_last_gen)
return best_fitness, population[list(fitness_last_gen).index(best_fitness)]