diff --git a/gastop/progmon.py b/gastop/progmon.py index 5520e9c..8f7dda9 100644 --- a/gastop/progmon.py +++ b/gastop/progmon.py @@ -11,16 +11,26 @@ 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, num_generations) - plt.ylabel('fitscore') - plt.xlabel('iteration') - plt.xlim(0,num_generations) + 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): @@ -28,10 +38,35 @@ 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: - print(current_gen,np.amin(fitscore)) - elif self.progress_display == 2: - #print(current_gen,min(fitscore)) - self.ax1.scatter(current_gen,np.amin(fitscore),c=[0,0,0]) #plot minimum fitscore for current gen in black + #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 diff --git a/junk/susan_scratch.py b/junk/susan_scratch.py index 1c4c225..6ee5cd9 100644 --- a/junk/susan_scratch.py +++ b/junk/susan_scratch.py @@ -4,6 +4,7 @@ from matplotlib import style import time from tqdm import tqdm +import numpy as np ##plt.ion() #look into multithreading this #style.use('fivethirtyeight') @@ -17,25 +18,38 @@ def run(self): print(y) def counter(n): - y = 0 + y0 = np.array([1,2,3]) style.use('fivethirtyeight') fig = plt.figure() - ax1 = fig.add_subplot(1,1,1) + ax1 = fig.add_subplot(1,2,1) plt.ylabel('convergence') plt.xlabel('iteration') + plt.xlim(0,n) + ax2 = fig.add_subplot(1,2,2) + plt.ylabel('test') + plt.xlabel('iteration') + #plt.yscale('log') + #plt.text(2,4,"0") + plt.xlim(0,n) + #plt.ylim(0,12) + test = [] - for i in tqdm(range(n)): - y = 2.0*i - progress(i,y,ax1) - #print(y) + for i in tqdm(range(n)): + y = 10.0-i*y0 + if i == 0: + test = np.min(y) + # else: + # test.append(test[i-1]) + progress(i,y,ax1,ax2,n,test) + print(np.min(y)) plt.show() return y -def progress(i,y,ax1): +def progress(i,y,ax1,ax2,n,test): # if i==1: #style.use('fivethirtyeight') #fig = plt.figure() @@ -43,9 +57,17 @@ def progress(i,y,ax1): #plt.ylabel('convergence') #plt.xlabel('iteration') # else: - ax1.scatter(i,y,c=[0,0,0]) - plt.pause(0.0001) #time it waits for plot to update + err_range = (np.amax(y) - np.amin(y))/2.0 + ax1.errorbar(i, np.mean(y), yerr=err_range, fmt='o') + #ax2.text(n,4,np.amin(y),bbox=dict(facecolor='white', alpha=1)) + ax2.text(n-1, test-1, np.amin(y), + bbox=dict(facecolor='white', alpha=1)) + ax2.scatter(i,np.amin(y),c=[0,0,0]) + + + #ax2.text() + plt.pause(0.5) #time it waits for plot to update -y = counter(10) +y = counter(4) print(y)