def onpick(self, event): #description = [] #for objective_index in ordered_list_of_objectives: # description = description + self.objective_column_headers[objective_index] + ': ' + str( ind = event.ind[0] if len(self.ordered_list_of_objectives) == 2: #2D print '2D picker not implemented' elif len(self.ordered_list_of_objectives) >= 3: #3D or 4D plot x, y, z = event.artist._offsets3d candidate_solution_indices_per_objective = [] idx = np.where(self.objective_values_matrix[0] == x[ind]) idy = np.where(self.objective_values_matrix[1] == y[ind]) idz = np.where(self.objective_values_matrix[2] == z[ind]) SolutionIndex = np.intersect1d(idx[0], np.intersect1d(idy[0], idz[0]))[0] ThisSolution = self.legal_solutions[SolutionIndex] print ThisSolution.description for objIndex in range(0, len(self.objective_column_headers)): if self.objective_column_headers[objIndex] == 'Flight time (days)' and self.TimeUnit == 0: print 'Flight time (years)', ': ', ThisSolution.objective_values[objIndex] / 365.25 elif self.objective_column_headers[objIndex] == 'Launch epoch (MJD)' and self.EpochUnit == 0: dt = datetime.datetime.fromtimestamp(wx.DateTimeFromJDN(ThisSolution.objective_values[objIndex] + 2400000.5).GetTicks()) print 'Launch Epoch (TDB Gregorian):', dt.strftime('%m/%d/%Y') elif self.objective_column_headers[objIndex] == 'Thruster preference': print 'Thruster type: ', ThisSolution.thruster elif self.objective_column_headers[objIndex] == 'Launch vehicle preference': print 'Launch vehicle: ', ThisSolution.launch_vehicle else: print self.objective_column_headers[objIndex], ': ', ThisSolution.objective_values[objIndex] print '---------------------------------------------------------------------------------------------'
def process_solution(solution): if max(solution.objective_values) < 1.0e+99: solution.Legal_Solution = True if not (self.LowerBounds == [] or self.UpperBounds == []): #if bounds were supplied, check to see if the solution fits inside the bounds for obj in range(0, len(self.ordered_list_of_objectives)): if solution.objective_values[ ordered_list_of_objectives[ obj]] < self.LowerBounds[ obj] or solution.objective_values[ ordered_list_of_objectives[ obj]] > self.UpperBounds[obj]: solution.Legal_Solution = False break if solution.Legal_Solution: self.legal_solutions.append(solution) if self.objective_column_headers[self.ordered_list_of_objectives[ objective_index]] == 'Flight time (days)' and self.TimeUnit == 0: objective_values_vector.append( solution.objective_values[ ordered_list_of_objectives[objective_index]] / 365.25) elif self.objective_column_headers[self.ordered_list_of_objectives[ objective_index]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: objective_values_vector.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[ ordered_list_of_objectives[ objective_index]] + 2400000.5).GetTicks()))) else: objective_values_vector.append( copy.deepcopy( float(solution.objective_values[ ordered_list_of_objectives[ objective_index]])))
def plot_solution_points(self): self.colorbar = None self.solution_names = [] for solution in self.solutions: if solution.Legal_Solution: self.solution_names.append(solution.description) X = [] Y = [] Z = [] C = [] if self.objective_column_headers[ self.ordered_list_of_objectives[ 0]] == 'Flight time (days)' and self.TimeUnit == 0: X = solution.objective_values[ self.ordered_list_of_objectives[0]] / 365.25 elif self.objective_column_headers[ self.ordered_list_of_objectives[ 0]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: X = dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN(solution.objective_values[0] + 2400000.5).GetTicks())) else: X = solution.objective_values[ self.ordered_list_of_objectives[0]] if self.objective_column_headers[ self.ordered_list_of_objectives[ 1]] == 'Flight time (days)' and self.TimeUnit == 0: Y = solution.objective_values[ self.ordered_list_of_objectives[1]] / 365.25 elif self.objective_column_headers[ self.ordered_list_of_objectives[ 1]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: Y = dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN(solution.objective_values[1] + 2400000.5).GetTicks())) else: Y = solution.objective_values[ self.ordered_list_of_objectives[1]] if len(self.ordered_list_of_objectives) > 2: if self.objective_column_headers[ self.ordered_list_of_objectives[ 2]] == 'Flight time (days)' and self.TimeUnit == 0: Z = solution.objective_values[ self.ordered_list_of_objectives[2]] / 365.25 elif self.objective_column_headers[ self.ordered_list_of_objectives[ 2]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: Z = dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[2] + 2400000.5).GetTicks())) else: Z = solution.objective_values[ self.ordered_list_of_objectives[2]] if len(self.ordered_list_of_objectives) > 3: if self.objective_column_headers[ self.ordered_list_of_objectives[ 3]] == 'Flight time (days)' and self.TimeUnit == 0: C = solution.objective_values[ self.ordered_list_of_objectives[3]] / 365.25 elif self.objective_column_headers[ self.ordered_list_of_objectives[ 3]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: C = dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[3] + 2400000.5).GetTicks())) else: C = solution.objective_values[ self.ordered_list_of_objectives[3]] if len(self.ordered_list_of_objectives) == 2: #2D solution.point = self.PopulationAxes.scatter(X, Y, s=20, c='b', marker='o', lw=0, picker=1) elif len(self.ordered_list_of_objectives) == 3: #3D solution.point = self.PopulationAxes.scatter(X, Y, Z, s=20, c='b', marker='o', lw=0, picker=1) else: #4D if self.colorbar is None: solution.point = self.PopulationAxes.scatter( X, Y, Z, s=20, c=C, marker='o', lw=0, picker=1) solution.point.set_clim( vmin=self.lowerbounds[ self.ordered_list_of_objectives[3]], vmax=self.upperbounds[ self.ordered_list_of_objectives[3]]) if self.objective_column_headers[ self.ordered_list_of_objectives[ 3]] == 'Flight time (days)' and self.TimeUnit == 0: self.colorbar = self.PopulationFigure.colorbar( solution.point, label='Flight time (years)') elif self.objective_column_headers[ self.ordered_list_of_objectives[ 3]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: self.PopulationAxes.set_xlabel( 'Launch Epoch (TDB Gregorian)') else: self.colorbar = self.PopulationFigure.colorbar( solution.point, label=self.objective_column_headers[ self.ordered_list_of_objectives[3]]) else: solution.point = self.PopulationAxes.scatter( X, Y, Z, s=20, c=C, marker='o', lw=0, picker=1) solution.point.set_clim( vmin=self.lowerbounds[ self.ordered_list_of_objectives[3]], vmax=self.upperbounds[ self.ordered_list_of_objectives[3]]) self.picker = self.PopulationFigure.canvas.mpl_connect( 'pick_event', self.onpick) if len(self.ordered_list_of_objectives) == 4: self.updater = self.PopulationFigure.canvas.mpl_connect( 'draw_event', self.force_update)
def plot_population(self, ordered_list_of_objectives, LowerBounds=None, UpperBounds=None, TimeUnit=1, EpochUnit=1): self.ordered_list_of_objectives = ordered_list_of_objectives self.LowerBounds = LowerBounds self.UpperBounds = UpperBounds self.TimeUnit = TimeUnit self.EpochUnit = EpochUnit #first check to see if the correct number of objective function indices were supplied if len(self.ordered_list_of_objectives) < 2 or len( self.ordered_list_of_objectives) > 4: print "NSGAII_outerloop_population::plot_population ERROR. You must specify between two and four objective functions to plot." return self.PopulationFigure = matplotlib.pyplot.figure() self.PopulationFigure.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99) if len(ordered_list_of_objectives) == 2: self.PopulationAxes = self.PopulationFigure.add_subplot(111) else: self.PopulationAxes = self.PopulationFigure.add_subplot( 111, projection='3d') #build up a list of objective values to be plotted self.objective_values_matrix = [] for objective_index in range(0, len(self.ordered_list_of_objectives)): objective_values_vector = [] for solution in self.solutions: if max(solution.objective_values) < 1.0e+99: solution.Legal_Solution = True if not (self.LowerBounds == None or self.UpperBounds == None): #if bounds were supplied, check to see if the solution fits inside the bounds for obj in range(0, len(self.ordered_list_of_objectives)): if solution.objective_values[ obj] < self.LowerBounds[ obj] or solution.objective_values[ obj] > self.UpperBounds[obj]: solution.Legal_Solution = False break if solution.Legal_Solution: if self.objective_column_headers[self.ordered_list_of_objectives[ objective_index]] == 'Flight time (days)' and self.TimeUnit == 0: objective_values_vector.append( solution.objective_values[objective_index] / 365.25) elif self.objective_column_headers[self.ordered_list_of_objectives[ objective_index]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: objective_values_vector.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution. objective_values[objective_index] + 2400000.5).GetTicks()))) else: objective_values_vector.append( copy.deepcopy( solution.objective_values[objective_index]) ) self.objective_values_matrix.append( np.array(objective_values_vector)) #determine upper and lower bounds on each objective self.upperbounds = [] self.lowerbounds = [] for objective_index in range(0, len(self.ordered_list_of_objectives)): self.upperbounds.append( self.objective_values_matrix[objective_index].max()) self.lowerbounds.append( self.objective_values_matrix[objective_index].min()) #plot each solution self.plot_solution_points() #set the axes labels if self.objective_column_headers[self.ordered_list_of_objectives[ 0]] == 'Flight time (days)' and self.TimeUnit == 0: self.PopulationAxes.set_xlabel('Flight time (years)') elif self.objective_column_headers[self.ordered_list_of_objectives[ 0]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: self.PopulationAxes.set_xlabel('Launch Epoch (TDB Gregorian)') self.PopulationAxes.w_xaxis.set_major_formatter( ticker.FuncFormatter(self.format_date)) else: self.PopulationAxes.set_xlabel(self.objective_column_headers[ self.ordered_list_of_objectives[0]]) if self.objective_column_headers[self.ordered_list_of_objectives[ 1]] == 'Flight time (days)' and self.TimeUnit == 0: self.PopulationAxes.set_ylabel('Flight time (years)') elif self.objective_column_headers[self.ordered_list_of_objectives[ 1]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: self.PopulationAxes.set_ylabel('Launch Epoch (TDB Gregorian)') self.PopulationAxes.w_yaxis.set_major_formatter( ticker.FuncFormatter(self.format_date)) else: self.PopulationAxes.set_ylabel(self.objective_column_headers[ self.ordered_list_of_objectives[1]]) if len(ordered_list_of_objectives) > 2: if self.objective_column_headers[self.ordered_list_of_objectives[ 2]] == 'Flight time (days)' and self.TimeUnit == 0: self.PopulationAxes.set_zlabel('Flight time (years)') elif self.objective_column_headers[self.ordered_list_of_objectives[ 2]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: self.PopulationAxes.set_zlabel('Launch Epoch (TDB Gregorian)') self.PopulationAxes.w_zaxis.set_major_formatter( ticker.FuncFormatter(self.format_date)) else: self.PopulationAxes.set_zlabel(self.objective_column_headers[ self.ordered_list_of_objectives[2]]) self.PopulationAxes.autoscale_view(tight=True, scalex=True, scaley=True, scalez=True) else: self.PopulationAxes.autoscale_view(tight=True, scalex=True, scaley=True) self.PopulationAxes.grid(b=True) self.PopulationFigure.show()
def plot_solution_points(self): self.colorbar = None self.solution_names = [] X = [] Y = [] Z = [] C = [] S = [] for solution in self.solutions: if solution.Legal_Solution: if self.objective_column_headers[ self.ordered_list_of_objectives[ 0]] == 'Flight time (days)' and self.TimeUnit == 0: X.append(solution.objective_values[ self.ordered_list_of_objectives[0]] / 365.25) elif self.objective_column_headers[ self.ordered_list_of_objectives[ 0]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: X.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[0] + 2400000.5).GetTicks()))) else: X.append(solution.objective_values[ self.ordered_list_of_objectives[0]]) if self.objective_column_headers[ self.ordered_list_of_objectives[ 1]] == 'Flight time (days)' and self.TimeUnit == 0: Y.append(solution.objective_values[ self.ordered_list_of_objectives[1]] / 365.25) elif self.objective_column_headers[ self.ordered_list_of_objectives[ 1]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: Y.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[1] + 2400000.5).GetTicks()))) else: Y.append(solution.objective_values[ self.ordered_list_of_objectives[1]]) if len(self.ordered_list_of_objectives) > 2: if self.objective_column_headers[ self.ordered_list_of_objectives[ 2]] == 'Flight time (days)' and self.TimeUnit == 0: Z.append(solution.objective_values[ self.ordered_list_of_objectives[2]] / 365.25) elif self.objective_column_headers[ self.ordered_list_of_objectives[ 2]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: Z.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[2] + 2400000.5).GetTicks()))) else: Z.append(solution.objective_values[ self.ordered_list_of_objectives[2]]) else: Z.append(0.0) if len(self.ordered_list_of_objectives) > 3: if self.objective_column_headers[ self.ordered_list_of_objectives[ 3]] == 'Flight time (days)' and self.TimeUnit == 0: C.append(solution.objective_values[ self.ordered_list_of_objectives[3]] / 365.25) elif self.objective_column_headers[ self.ordered_list_of_objectives[ 3]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: C.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[3] + 2400000.5).GetTicks()))) else: C.append(solution.objective_values[ self.ordered_list_of_objectives[3]]) else: C.append(0.0) #if there is a fifth objective, size the markers to reflect it if len(self.ordered_list_of_objectives) > 4: S.append(self.BaseMarkerSize * solution.objective_values[ self.ordered_list_of_objectives[4]] / (self.upperbounds[4] - self.lowerbounds[4])) else: S.append(self.BaseMarkerSize) solution.points = self.PopulationAxes.scatter(X, Y, Z, s=S, c=C, marker='o', edgecolors='none', picker=1) self.picker = self.PopulationFigure.canvas.mpl_connect( 'pick_event', self.onpick) if len(self.ordered_list_of_objectives) > 3: self.PopulationFigure.colorbar( solution.points, label=self.objective_column_headers[ self.ordered_list_of_objectives[3]])
def onpick(self, event): ind = event.ind[0] if not self.OutputWindow == []: if len(self.ordered_list_of_objectives) == 2: #2D self.OutputWindow.WriteText('2D picker not implemented\n') elif len(self.ordered_list_of_objectives) >= 3: #3D or 4D plot x, y, z = event.artist._offsets3d idx = np.where(self.objective_values_matrix[0] == x[ind]) idy = np.where(self.objective_values_matrix[1] == y[ind]) idz = np.where(self.objective_values_matrix[2] == z[ind]) SolutionIndex = np.intersect1d(idx[0], np.intersect1d(idy[0], idz[0]))[0] ThisSolution = self.legal_solutions[SolutionIndex] self.OutputWindow.WriteText('Description: ' + ThisSolution.description + '\n') if ThisSolution.outputfilename != '': if platform.system() == 'Windows': self.OutputWindow.WriteText( 'Output file: ' + ThisSolution.outputfilename.replace('//', '\\') + '.emtg\n') else: self.OutputWindow.WriteText( 'Output file: ' + ThisSolution.outputfilename + '.emtg\n') for objIndex in range(0, len(self.objective_column_headers)): if self.objective_column_headers[ objIndex] == 'Flight time (days)' and self.TimeUnit == 0: self.OutputWindow.WriteText( 'Flight time (years): ' + str(ThisSolution.objective_values[objIndex] / 365.25) + '\n') elif self.objective_column_headers[ objIndex] == 'Launch epoch (MJD)' and self.EpochUnit == 0: dt = datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( ThisSolution.objective_values[objIndex] + 2400000.5).GetTicks()) self.OutputWindow.WriteText( 'Launch Epoch (TDB Gregorian): ' + dt.strftime('%m/%d/%Y') + '\n') elif self.objective_column_headers[ objIndex] == 'Thruster preference': self.OutputWindow.WriteText('Thruster type: ' + ThisSolution.thruster + '\n') elif self.objective_column_headers[ objIndex] == 'Launch vehicle preference': self.OutputWindow.WriteText( 'Launch vehicle: ' + ThisSolution.launch_vehicle + '\n') else: self.OutputWindow.WriteText( str(self.objective_column_headers[objIndex]) + ': ' + str(ThisSolution.objective_values[objIndex]) + '\n') self.OutputWindow.WriteText( '---------------------------------------------------------------------------------------------\n' )
def plot_solution_points(self): self.solution_names = [] X = [] Y = [] Z = [] C = [] S = [] for solution in self.solutions: if solution.Legal_Solution: if self.objective_column_headers[ self.ordered_list_of_objectives[ 0]] == 'Flight time (days)' and self.TimeUnit == 0: X.append(solution.objective_values[ self.ordered_list_of_objectives[0]] / 365.25) elif self.objective_column_headers[ self.ordered_list_of_objectives[ 0]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: X.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[0] + 2400000.5).GetTicks()))) else: X.append(solution.objective_values[ self.ordered_list_of_objectives[0]]) if self.objective_column_headers[ self.ordered_list_of_objectives[ 1]] == 'Flight time (days)' and self.TimeUnit == 0: Y.append(solution.objective_values[ self.ordered_list_of_objectives[1]] / 365.25) elif self.objective_column_headers[ self.ordered_list_of_objectives[ 1]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: Y.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[1] + 2400000.5).GetTicks()))) else: Y.append(solution.objective_values[ self.ordered_list_of_objectives[1]]) if len(self.ordered_list_of_objectives) > 2: if self.objective_column_headers[ self.ordered_list_of_objectives[ 2]] == 'Flight time (days)' and self.TimeUnit == 0: Z.append(solution.objective_values[ self.ordered_list_of_objectives[2]] / 365.25) elif self.objective_column_headers[ self.ordered_list_of_objectives[ 2]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: Z.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[2] + 2400000.5).GetTicks()))) else: Z.append(solution.objective_values[ self.ordered_list_of_objectives[2]]) else: Z.append(0.0) if len(self.ordered_list_of_objectives) > 3: if self.objective_column_headers[ self.ordered_list_of_objectives[ 3]] == 'Flight time (days)' and self.TimeUnit == 0: C.append(solution.objective_values[ self.ordered_list_of_objectives[3]] / 365.25) elif self.objective_column_headers[ self.ordered_list_of_objectives[ 3]] == 'Launch epoch (MJD)' and self.EpochUnit == 0: C.append( dates.date2num( datetime.datetime.fromtimestamp( wx.DateTimeFromJDN( solution.objective_values[3] + 2400000.5).GetTicks()))) else: C.append(solution.objective_values[ self.ordered_list_of_objectives[3]]) else: C.append(0.0) #if there is a fifth objective, size the markers to reflect it if len(self.ordered_list_of_objectives) > 4: S.append( self.BaseMarkerSize * solution.objective_values[ self.ordered_list_of_objectives[4]] ) # / (self.upperbounds[4] - self.lowerbounds[4])) else: S.append(self.BaseMarkerSize) if not self.solution_points == []: self.solution_points.remove() self.solution_points = self.PopulationAxes.scatter(X, Y, Z, s=S, c=C, marker='o', edgecolors='none', picker=1) self.number_of_feasible_solutions = len(X) self.OutputWindow.WriteText( str(self.number_of_feasible_solutions) + ' solutions meet the applied filters.\n') self.picker = self.PopulationFigure.canvas.mpl_connect( 'pick_event', self.onpick) if len(self.ordered_list_of_objectives) > 3 and len( self.legal_solutions) > 0: if not self.colorbar == []: self.PopulationFigure.delaxes(self.PopulationFigure.axes[1]) self.PopulationAxes.set_position(self.pre_colorbar_position) self.pre_colorbar_position = self.PopulationAxes.get_position() self.colorbar = self.PopulationFigure.colorbar( self.solution_points, label=self.objective_column_headers[ self.ordered_list_of_objectives[3]], shrink=0.9)