-
Notifications
You must be signed in to change notification settings - Fork 4
/
progmon.py
72 lines (59 loc) · 3.15 KB
/
progmon.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
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
##plt.ion() #look into multithreading this
style.use('fivethirtyeight')
class ProgMon():
def __init__(self,progress_display,num_generations):
self.progress_display = progress_display
self.num_gens = num_generations
self.orderofgen = int(np.log10(self.num_gens))*10
self.pop_progress = []
self.pop_start = []
if self.progress_display == 2: # check if figure method of progress monitoring is requested
# initialize plot:
fig = plt.figure()
self.ax1 = fig.add_subplot(1,1,1) #does this need self.?
plt.xlim(0, self.num_gens)
plt.ylabel('Minimum Fitness Score')
plt.xlabel('Iteration')
#plt.yscale('log')
#
# self.ax2 = fig.add_subplot(1,2,2) #does this need self.?
# plt.xlim(0, num_generations)
# plt.ylabel('Min Fitness Score')
# plt.xlabel('Iteration')
# plt.xlim(0,num_generations)
#
def progress_monitor(self, current_gen, population):
# three options: plot, progress bar ish thing, no output just append
# calc population diversity and plot stuff or show current results
fitscore = [i.fitness_score for i in population] #extract factor of safety from each truss object in population
fitscore_min = np.amin(fitscore)
self.pop_progress.append(population) #append to history
#if self.progress_display == 1:
# test = np.amin(fitscore)
if self.progress_display == 2:
if current_gen==0:
self.pop_start = fitscore_min # store initial min fitscore (should be worst)
# fitscore_range_scaled = 1.0
# #self.pop_prop(current_gen) = (np.amax(fitscore) - np.amin(fitscore))/2.0
# self.pop_prop.append([(np.amax(fitscore) - np.amin(fitscore))/2.0])
# else:
# fitscore_range = (np.amax(fitscore) - np.amin(fitscore))/2.0
# fitscore_range_scaled = fitscore_range/self.pop_prop[current_gen-1]
# self.pop_prop.append([fitscore_range])
# #self.pop_prop(current_gen) = fitscore_range
#
# fitscore_med = np.median(fitscore)
#self.pop_prop.append(fitscore_range)
#self.ax1.errorbar(current_gen, fitscore_med, yerr=fitscore_range_scaled, fmt='o',c=[0,0,0])
#self.ax1.scatter(current_gen,fitscore_med,c=[0,0,0])
self.ax1.scatter(current_gen,fitscore_min,c=[1,0,0])
# set text with current min fitscore
plot_text=self.ax1.text(self.num_gens-self.orderofgen, self.pop_start, round(fitscore_min,3),bbox=dict(facecolor='white', alpha=1))
# set box to same size
plot_text._bbox_patch._mutation_aspect = 0.1
plot_text.get_bbox_patch().set_boxstyle("square", pad=1)
#self.ax1.scatter(current_gen,np.amin(fitscore),c=[0,0,0]) #plot minimum fitscore for current gen in black
plt.pause(0.0001) #pause for 0.0001s to allow plot to update, can potentially remove this