-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
131 lines (98 loc) · 4.43 KB
/
main.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
import random
import time
from deap import base, creator, tools
# Predator libraries
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import numpy as np
def prey_evaluation(individual):
return 1
def predator_evaluation(predator_output, expected):
correct_predictions = np.sum(predator_output.round() == expected)
fitness_value = correct_predictions / len(expected)
return fitness_value
# Define the individual and population
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
# Create a toolbox with the required evolutionary operators
toolbox = base.Toolbox()
toolbox.register("attribute", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attribute, n=10)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", prey_evaluation)
# ============== PREY ==============
# Create an initial population
population = toolbox.population(n=70)
# Set the algorithm parameters
CXPB, MUTPB, NGEN = 0.7, 0.2, 50
# Evaluate the entire population
fitnesses = list(map(toolbox.evaluate, population))
for ind, fit in zip(population, fitnesses):
ind.fitness.values = (fit,) # Remove brackets when the prey function has been sorted
def train_prey():
# Begin the evolution
for gen in range(NGEN):
# Select the next generation individuals
offspring = toolbox.select(population, len(population))
# Clone the selected individuals
offspring = list(map(toolbox.clone, offspring))
# Apply crossover and mutation on the offspring
for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < CXPB:
toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
for mutant in offspring:
if random.random() < MUTPB:
toolbox.mutate(mutant)
del mutant.fitness.values
# Evaluate the individuals with invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = (fit, )
# Replace the old population by the offspring
population[:] = offspring
best_ind = tools.selBest(population, 1)[0]
print(f"Best individual: {best_ind}, Fitness: {best_ind.fitness.values[0]}")
# ============== PREDATOR ==============
# Define the model globally
predator = Sequential()
predator.add(Dense(units=4, input_dim=10, activation='relu'))
predator.add(Dense(units=1, activation='sigmoid'))
predator.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
num_samples = 5
array_size = 10
def generate_data(num_samples, array_size):
given = np.random.choice(['A', 'B', 'C'], size=(num_samples, array_size))
expected = (given == 'B').astype(int) + (2 * (given == 'A').astype(int))
given = (given == 'B').astype(int) + (2 * (given == 'C').astype(int))
return given, expected
def train_predators(model, given, expected_output, epochs=4):
model.fit(given, expected_output, epochs=epochs, verbose=2)
if __name__ == "__main__":
given, expected = generate_data(num_samples, array_size)
print (given)
print(expected)
for epoc in range(5):
# Train the predator
start_time = time.time()
train_predators(predator, given, expected)
# Test the predator
predictions = predator.predict(given)
fitness_value = predator_evaluation(predictions, expected)
# Train the prey
train_prey()
prey_end_time = time.time()
fitness_value = predator_evaluation(predictions.round(), expected)
predator_end_time = time.time()
print(f"Epoch time (Prey): {prey_end_time - start_time} seconds")
print(f"Epoch time (Predator): {predator_end_time - prey_end_time} seconds")
print(f"Epoch time (Combined): {predator_end_time - start_time} seconds")
print(f"Predator Fitness: {fitness_value}")
print(f"Predator Predicted: {predictions}")
print(f"Predator Expected: {expected}")