def stat(self, population): # 分析记录,更新进化记录器,population为传入的种群对象 feasible = np.where( np.all(population.CV <= 0, 1))[0] if population.CV is not None else np.array( range(population.sizes)) # 找到可行解个体的下标 if len(feasible) > 0: tempPop = population[feasible] # 获取可行解个体 self.pop_trace.append(tempPop) # 添加记录(只添加可行解个体到种群记录器中) self.forgetCount = 0 # “遗忘策略”计数器清零 self.passTime += time.time() - self.timeSlot # 更新用时记录 if self.drawing == 2: # 绘制目标空间动态图 self.ax = ea.moeaplot(tempPop.ObjV, 'objective values', False, self.ax, self.currentGen, gridFlag=True) elif self.drawing == 3: # 绘制决策空间动态图 self.ax = ea.varplot(tempPop.Phen, 'decision variables', False, self.ax, self.currentGen, gridFlag=False) self.timeSlot = time.time() # 更新时间戳 else: self.currentGen -= 1 # 忽略这一代 self.forgetCount += 1 # “遗忘策略”计数器加1
def stat( self, population): # 分析记录,更新进化记录器,population为传入的种群对象,NDSet为当代的种群中的非支配个体集 feasible = np.where(np.all(population.CV <= 0, 1))[0] # 找到可行解个体的下标 if len(feasible) > 0: self.pop_trace.append(population) # 添加记录 self.forgetCount = 0 # “遗忘策略”计数器清零 self.passTime += time.time() - self.timeSlot # 更新用时记录 if self.drawing == 2: # 绘制目标空间动态图 self.ax = ea.moeaplot(population.ObjV, 'objective values', False, self.ax, self.currentGen, gridFlag=True) elif self.drawing == 3: # 绘制决策空间动态图 self.ax = ea.varplot(population.Phen, 'decision variables', False, self.ax, self.currentGen, gridFlag=False) self.timeSlot = time.time() # 更新时间戳 else: self.currentGen -= 1 # 忽略这一代 self.forgetCount += 1 # “遗忘策略”计数器加1
def draw(self, pop, EndFlag=False): """ 描述: 该函数用于在进化过程中进行绘图。该函数在stat()以及finishing函数里面被调用。 输入参数: pop : class <Population> - 种群对象。 EndFlag : bool - 表示是否是最后一次调用该函数。 输出参数: 无输出参数。 """ if not EndFlag: self.passTime += time.time() - self.timeSlot # 更新用时记录,不计算画图的耗时 # 绘制动画 if self.drawing == 2: # 绘制目标空间动态图 self.ax = ea.moeaplot(pop.ObjV, 'objective values', False, self.ax, self.currentGen, gridFlag=True) elif self.drawing == 3: # 绘制决策空间动态图 self.ax = ea.varplot(pop.Phen, 'decision variables', False, self.ax, self.currentGen, gridFlag=False) self.timeSlot = time.time() # 更新时间戳 else: # 绘制最终结果图 if self.drawing != 0: if pop.ObjV.shape[1] == 2 or pop.ObjV.shape[1] == 3: ea.moeaplot(pop.ObjV, 'Pareto Front', saveFlag=True, gridFlag=True) else: ea.moeaplot(pop.ObjV, 'Value Path', saveFlag=True, gridFlag=False)
def draw(self, pop, EndFlag=False): """ 描述: 该函数用于在进化过程中进行绘图。该函数在stat()以及finishing函数里面被调用。 输入参数: pop : class <Population> - 种群对象。 EndFlag : bool - 表示是否是最后一次调用该函数。 输出参数: 无输出参数。 """ if not EndFlag: self.passTime += time.time() - self.timeSlot # 更新用时记录,不计算画图的耗时 # 绘制动画 if self.drawing == 2: metric = np.array(self.trace['f_best']).reshape(-1, 1) self.ax = ea.soeaplot(metric, Label='Objective Value', saveFlag=False, ax=self.ax, gen=self.currentGen, gridFlag=False) # 绘制动态图 elif self.drawing == 3: self.ax = ea.varplot(pop.Phen, Label='decision variables', saveFlag=False, ax=self.ax, gen=self.currentGen, gridFlag=False) self.timeSlot = time.time() # 更新时间戳 else: # 绘制最终结果图 if self.drawing != 0: metric = np.vstack([self.trace['f_avg'], self.trace['f_best']]).T ea.trcplot(metric, [['种群个体平均目标函数值', '种群最优个体目标函数值']], xlabels=[['Number of Generation']], ylabels=[['Value']], gridFlags=[[False]])
def stat(self, population): # 分析记录,更新进化记录器,population为传入的种群对象 # 进行进化记录 feasible = np.where( np.all(population.CV <= 0, 1))[0] if population.CV is not None else np.arange( population.sizes) # 找到可行解个体的下标 if len(feasible) > 0: tempPop = population[feasible] bestIdx = np.argmax(tempPop.FitnV) # 获取最优个体的下标 self.obj_trace[self.currentGen, 0] = np.sum( tempPop.ObjV) / tempPop.sizes # 记录种群个体平均目标函数值 self.obj_trace[self.currentGen, 1] = tempPop.ObjV[bestIdx] # 记录当代目标函数的最优值 self.var_trace[self.currentGen, :] = tempPop.Phen[ bestIdx, :] # 记录当代最优的决策变量值 self.forgetCount = 0 # “遗忘策略”计数器清零 if np.abs(self.preObjV - self.obj_trace[self.currentGen, 1]) < self.trappedValue: self.trappedCount += 1 else: self.trappedCount = 0 # 重置进化停滞计数器 self.passTime += time.time() - self.timeSlot # 更新用时记录 if self.drawing == 2: self.ax = ea.soeaplot(self.obj_trace[:, [1]], Label='Objective Value', saveFlag=False, ax=self.ax, gen=self.currentGen, gridFlag=False) # 绘制动态图 elif self.drawing == 3: self.ax = ea.varplot(tempPop.Phen, Label='decision variables', saveFlag=False, ax=self.ax, gen=self.currentGen, gridFlag=False) self.timeSlot = time.time() # 更新时间戳 else: self.currentGen -= 1 # 忽略这一代 self.forgetCount += 1 # “遗忘策略”计数器加1
def stat(self, pop): # 分析记录,更新进化记录器 # 进行进化记录 feasible = np.where(np.all(pop.CV <= 0, 1))[0] # 找到可行解个体的下标 if len(feasible) > 0: tempPop = pop[feasible] bestIdx = np.argmax(tempPop.FitnV) # 获取最优个体的下标 self.obj_trace[self.currentGen, 0] = np.sum( tempPop.ObjV) / tempPop.sizes # 记录种群个体平均目标函数值 self.obj_trace[self.currentGen, 1] = tempPop.ObjV[bestIdx] # 记录当代目标函数的最优值 self.var_trace[self.currentGen, :] = tempPop.Phen[ bestIdx, :] # 记录当代最优的决策变量值 self.forgetCount = 0 # “遗忘策略”计数器清零 self.passTime += time.time() - self.timeSlot # 更新用时记录 if self.drawing == 2: self.ax = ea.soeaplot(self.obj_trace[:, [1]], None, False, self.ax, self.currentGen) # 绘制动态图 elif self.drawing == 3: self.ax = ea.varplot(tempPop.Phen, 'decision variables', False, self.ax, self.currentGen) self.timeSlot = time.time() # 更新时间戳 else: self.currentGen -= 1 # 忽略这一代 self.forgetCount += 1 # “遗忘策略”计数器加1