-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleExploratoryAttacker.py
158 lines (130 loc) · 5.77 KB
/
simpleExploratoryAttacker.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
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 2 19:31:41 2019
@author: Gerry Dozier
"""
import os
import random
import sys
import math
import matplotlib.pyplot as plt
# Only for osX
import matplotlib
matplotlib.use('TkAgg')
from mpl_toolkits.mplot3d import Axes3D
#
# A Simple Steady-State, Real-Coded Genetic Algorithm
#
class anIndividual:
def __init__(self, specified_chromosome_length):
self.chromosome = []
self.fitness = 0
self.chromosome_length = specified_chromosome_length
def randomly_generate(self,lb, ub):
for i in range(self.chromosome_length):
self.chromosome.append(random.uniform(lb, ub))
self.fitness = 0
def calculate_fitness(self):
x2y2 = self.chromosome[0]**2 + self.chromosome[1]**2
self.fitness = 0.5 + (math.sin(math.sqrt(x2y2))**2 - 0.5) / (1+0.001*x2y2)**2
def print_individual(self, i):
print("Chromosome "+str(i) +": " + str(self.chromosome) + " Fitness: " + str(self.fitness))
class aSimpleExploratoryAttacker:
def __init__(self, population_size, chromosome_length, mutation_rate, lb, ub):
if (population_size < 2):
print("Error: Population Size must be greater than 2")
sys.exit()
self.population_size = population_size
self.chromosome_length = chromosome_length
self.mutation_amt = mutation_rate
self.lb = lb
self.ub = ub
self.mutation_amt = mutation_rate * (ub - lb)
self.population = []
self.hacker_tracker_x = []
self.hacker_tracker_y = []
self.hacker_tracker_z = []
def generate_initial_population(self):
for i in range(self.population_size):
individual = anIndividual(self.chromosome_length)
individual.randomly_generate(self.lb,self.ub)
individual.calculate_fitness()
self.hacker_tracker_x.append(individual.chromosome[0])
self.hacker_tracker_y.append(individual.chromosome[1])
self.hacker_tracker_z.append(individual.fitness)
self.population.append(individual)
def get_worst_fit_individual(self):
worst_fitness = 999999999.0 # For Maximization
worst_individual = -1
for i in range(self.population_size):
if (self.population[i].fitness < worst_fitness):
worst_fitness = self.population[i].fitness
worst_individual = i
return worst_individual
def get_best_fitness(self):
best_fitness = -99999999999.0
best_individual = -1
for i in range(self.population_size):
if self.population[i].fitness > best_fitness:
best_fitness = self.population[i].fitness
best_individual = i
return best_fitness
def evolutionary_cycle(self):
mom = random.randint(0,self.population_size-1)
dad = random.randint(0,self.population_size-1)
kid = self.get_worst_fit_individual()
for j in range(self.chromosome_length):
self.population[kid].chromosome[j] = random.uniform(self.population[mom].chromosome[j],self.population[dad].chromosome[j])
self.population[kid].chromosome[j] += self.mutation_amt * random.gauss(0,1.0)
if self.population[kid].chromosome[j] > self.ub:
self.population[kid].chromosome[j] = self.ub
if self.population[kid].chromosome[j] < self.lb:
self.population[kid].chromosome[j] = self.lb
self.population[kid].calculate_fitness()
self.hacker_tracker_x.append(self.population[kid].chromosome[0])
self.hacker_tracker_y.append(self.population[kid].chromosome[1])
self.hacker_tracker_z.append(self.population[kid].fitness)
def print_population(self):
for i in range(self.population_size):
self.population[i].print_individual(i)
def print_best_max_fitness(self):
best_fitness = -999999999.0 # For Maximization
best_individual = -1
for i in range(self.population_size):
if self.population[i].fitness > best_fitness:
best_fitness = self.population[i].fitness
best_individual = i
print("Best Indvidual: ",str(i)," ", self.population[i].chromosome, " Fitness: ", str(best_fitness))
def plot_evolved_candidate_solutions(self):
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1,projection='3d')
ax1.scatter(self.hacker_tracker_x,self.hacker_tracker_y,self.hacker_tracker_z)
plt.title("Evolved Candidate Solutions")
ax1.set_xlim3d(-100.0,100.0)
ax1.set_ylim3d(-100.0,100.0)
ax1.set_zlim3d(0.2,1.0)
plt.show()
ChromLength = 2
ub = 100.0
lb = -100.0
MaxEvaluations = 4000
plot = 0
PopSize = 3
mu_amt = 0.0075
simple_exploratory_attacker = aSimpleExploratoryAttacker(PopSize,ChromLength,mu_amt,lb,ub)
simple_exploratory_attacker.generate_initial_population()
simple_exploratory_attacker.print_population()
for i in range(MaxEvaluations-PopSize+1):
simple_exploratory_attacker.evolutionary_cycle()
if (i % PopSize == 0):
if (plot == 1):
simple_exploratory_attacker.plot_evolved_candidate_solutions()
print("At Iteration: " + str(i))
simple_exploratory_attacker.print_population()
if (simple_exploratory_attacker.get_best_fitness() >= 0.99754):
break
print("\nFinal Population\n")
simple_exploratory_attacker.print_population()
simple_exploratory_attacker.print_best_max_fitness()
print("Function Evaluations: " + str(PopSize+i))
simple_exploratory_attacker.plot_evolved_candidate_solutions()