-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic_algorithms.py
152 lines (119 loc) · 6.19 KB
/
genetic_algorithms.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
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
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import pandas as pd
import random
from pyeasyga import pyeasyga
from sklearn.metrics import mean_squared_error
import math
import scipy.optimize as optimize
from odesolver import solveODES
#Genetic Algorithm using pyeasyga
class GA():
def __init__(self, data):
self.data = data
def create_individual(self, data):
random_list=np.around(np.arange(0, 100, 0.01), decimals=2)
random_list_2 = np.around(np.arange(0, 10, 0.05), decimals=2)
individual = [random.choice(random_list_2), random.choice(random_list_2), random.choice(random_list_2)]
return individual
def fitness(self, individual, data):
fitness_val = self.findlsq(individual)
return fitness_val
def findlsq(self, individual):
individual=list(individual)
ODE = solveODES(self.data, individual[0], individual[1], individual[2])
ODEsoln = ODE.solve()
error_sum = mean_squared_error(self.data['MKKK'], ODEsoln[:, 0]) + mean_squared_error(self.data['MKKK_P'],
ODEsoln[:, 1]) + mean_squared_error(
self.data['MKK'], ODEsoln[:, 2]) + mean_squared_error(self.data['MKK_P'], ODEsoln[:, 3]) + mean_squared_error(self.data['MKK_PP'], ODEsoln[:,
4]) + mean_squared_error(
self.data['MAPK'], ODEsoln[:, 5]) + mean_squared_error(self.data['MAPK_P'], ODEsoln[:, 6]) + mean_squared_error(self.data['MAPK_PP'],
ODEsoln[:, 7])
return error_sum
def run_ga(self):
seed_data = [0.00, 0.00, 0.00]
ga = pyeasyga.GeneticAlgorithm(seed_data,
population_size=1000,
generations=5,
crossover_probability=0.8,
mutation_probability=0.05,
elitism=True,
maximise_fitness=False) # initialise the GA with data
ga.create_individual = self.create_individual
ga.fitness_function = self.fitness # set the GA's fitness function
ga.run() # run the GA
#print(ga.best_individual()) # print the GA's best solution
return(ga.best_individual())
#I tried to write my own GA and failed
class GA_custom():
def __init__(self, data):
self.data = data
def create_individual(self):
random_list=np.around(np.arange(0, 10000, 1), decimals=2)
random_list_2 = np.around(np.arange(0, 1, 0.01), decimals=2)
individual = [bin(random.choice(random_list))[2:], bin(random.choice(random_list))[2:], bin(random.choice(random_list))[2:]]
return individual
def create_population(self, pop_size):
population=[]
for i in range(pop_size):
population.append(self.create_individual())
return population
def crossover(self, individual1, individual2):
decider=np.random.random()
if(decider<=0.33):
individual1[0], individual2[0] = individual2[0], individual1[0]
elif(0.33<decider<=0.66):
individual1[1], individual2[1] = individual2[1], individual1[1]
elif(decider>0.66):
individual1[2], individual2[2] = individual2[2], individual1[2]
return(individual1, individual2)
def fitness(self, individual):
fitness_value = self.findlsq(individual)
return fitness_value
def findlsq(self, individual):
ODE = solveODES(self.data, int(individual[0], 2)/100, int(individual[1], 2)/100, int(individual[2], 2)/100)
ODEsoln = ODE.solve()
error_sum = mean_squared_error(self.data['MKKK'], ODEsoln[:, 0]) + mean_squared_error(self.data['MKKK_P'],
ODEsoln[:,
1]) + mean_squared_error(
self.data['MKK'], ODEsoln[:, 2]) + mean_squared_error(self.data['MKK_P'],
ODEsoln[:, 3]) + mean_squared_error(
self.data['MKK_PP'], ODEsoln[:,
4]) + mean_squared_error(
self.data['MAPK'], ODEsoln[:, 5]) + mean_squared_error(self.data['MAPK_P'],
ODEsoln[:, 6]) + mean_squared_error(
self.data['MAPK_PP'],ODEsoln[:, 7])
return error_sum
def run_GA(self, crossover_thresh):
pop_size=1024
population=self.create_population(pop_size)
print(population)
n=1
for i in range(n):
for j in range(len(population)):
crossover_point=np.random.uniform(0,1)
if (crossover_point>crossover_thresh):
player1 = np.random.randint(len(population))
player2 = np.random.randint(len(population))
population[player1], population[player2] = self.crossover(population[player1], population[player2])
fitness_val = []
new_population=[]
#print(len(population))
for j in range(len(population)):
fitness_val.append(self.fitness(population[j]))
#print(fitness_val[j])
while(len(population)>1):
print(len(population))
player1 = np.random.randint(len(population)-1)
player2 = np.random.randint(len(population)-1)
#print(player1, player2)
if not player1==player2:
if fitness_val[player1]<fitness_val[player2]:
new_population.append(population[player1])
else:
new_population.append(population[player2])
population.pop(player1)
population.pop(player2)
population=new_population
return population