def set_velocity(self, **kwargs): if 'velocity' in kwargs: self.velocity = kwargs.get('velocity') if 'uppervs' in kwargs and 'lowervs' in kwargs: self.velocity = helper.init_vector(self.n, kwargs['uppervs'], kwargs['lowervs'])
def __init__( self, _np: int, n: int, ub, # upper boundary lb, # low boundary procedure: callable, max_gen: int = 500, optimal_minimal: bool = True, boundary_strategy=Boundary.BOUNDARY, random_state: int = None): self.np = _np self.n = n self.ub = np.array(ub) self.lb = np.array(lb) self.procedure = procedure self.max_gen = max_gen self.optimal_minimal = optimal_minimal # bs: Boundary Strategy self.bs = Boundary.make_strategy(boundary_strategy) self.random_state = random_state if self.random_state: np.random.seed(self.random_state) # sc: Solution Collection self.sc = np.array( [init_vector(self.n, self.ub, self.lb) for _ in range(self.np)]) # fc: fitness Collection self.fc = None # hbsc: history best solution collection self.hbsc = [] self.fsort = True self.fappend = True
def create(self, class_str, all_zero=False): # 对可行解进行初始化 vector = np.zeros(self.n) if all_zero else helper.init_vector( self.n, self.us, self.ls) _class = Solution if class_str == 'TrialSolution': _class = TrialSolution if class_str == 'LabelSolution': _class = LabelSolution if class_str == 'VelocitySolution': _class = VelocitySolution return _class(vector)
def __init__(self, *args, vlb=None, vub=None, **kwargs): super(PSO, self).__init__(*args, **kwargs) self.vlb = vlb if vlb else ([-5] * self.n) self.vub = vub if vub else ([5] * self.n) self.vc = np.array([init_vector(self.n, self.vub, self.vlb) for _ in range(self.np)]) # self.vcg = lambda cg: self.vc - self.vc * cg / self.max_gen # 记录每一个体的历史最优 self.psc = deepcopy(self.sc) # 参数生成 self.wg = lambda cg: 0.9 - cg * 0.4 / self.max_gen self.r1g = lambda: 0.05 self.r2g = lambda: 0.05
def create(n, us, ls): return Solution(helper.init_vector(n, us, ls))
def scouter_stage(self, g): for i, nc in enumerate(self.ncs.flatten().astype(int)): if nc >= self.nc: self.sc[i] = init_vector(self.n, self.ub, self.lb) self.ncs[i] = 0