Exemple #1
0
    def __init__(self, func, n_dim, size_pop=50, max_iter=200, w=0.8, c1=0.1, c2=0.1):
        self.func = func_transformer(func)
        self.func_raw = func
        self.n_dim = n_dim
        self.size_pop = size_pop
        self.max_iter = max_iter

        self.w = w
        self.cp = c1
        self.cg = c2

        self.X = self.crt_X()
        self.Y = self.cal_y()
        self.pbest_x = self.X.copy()
        self.pbest_y = np.array([[np.inf]] * self.size_pop)

        self.gbest_x = self.pbest_x[0, :]
        self.gbest_y = np.inf
        self.gbest_y_hist = []
        self.update_gbest()
        self.update_pbest()

        # record verbose values
        self.record_mode = False
        self.record_value = {'X': [], 'V': [], 'Y': []}
        self.verbose = False
Exemple #2
0
    def __init__(self,
                 func,
                 n_dim,
                 size_pop=50,
                 max_iter=200,
                 prob_mut=0.001,
                 constraint_eq=[],
                 constraint_ueq=[]):
        self.func = func_transformer(func)
        self.size_pop = size_pop  # size of population
        self.max_iter = max_iter
        self.prob_mut = prob_mut  # probability of mutation
        self.n_dim = n_dim

        # constraint:
        self.has_constraint = len(constraint_eq) > 0 or len(constraint_ueq) > 0
        self.constraint_eq = constraint_eq  # a list of unequal constraint functions with c[i] <= 0
        self.constraint_ueq = constraint_ueq  # a list of equal functions with ceq[i] = 0

        self.Chorm = None
        self.X = None  # shape = (size_pop, n_dim)
        self.Y_raw = None  # shape = (size_pop,) , value is f(x)
        self.Y = None  # shape = (size_pop,) , value is f(x) + penalty for constraint
        self.FitV = None  # shape = (size_pop,)

        # self.FitV_history = []
        self.generation_best_X = []
        self.generation_best_Y = []

        self.all_history_Y = []
        self.all_history_FitV = []
Exemple #3
0
    def __init__(self, func, dim, pop=40, max_iter=150, lb=None, ub=None):
        self.func = func_transformer(func)
        self.w = 0.8  # inertia
        self.cp, self.cg = 0.5, 0.5  # parameters to control personal best,global best respectively
        self.pop = pop  # number of particles
        self.dim = dim  # dimension of particles, which is the number of variables of func
        self.max_iter = max_iter  # max iter

        self.has_constraints=not(lb is None and ub is None)
        self.lb = -np.ones(self.dim) if lb is None else np.array(lb)
        self.ub = np.ones(self.dim) if ub is None else np.array(ub)

        assert self.dim == len(self.lb) == len(self.ub), 'dim == len(lb) == len(ub) must holds'
        assert np.all(self.ub > self.lb), 'All upper-bound values must be greater than lower-bound values'

        # self.X = np.random.rand(self.pop, self.dim) * (self.ub - self.lb) + self.lb  # location of particles
        self.X = np.random.uniform(low=self.lb, high=self.ub, size=(self.pop, self.dim))
        # self.V = np.random.rand(self.pop, self.dim)  # speed of particles
        v_high = self.ub - self.lb
        self.V = np.random.uniform(low=-v_high, high=v_high, size=(self.pop, self.dim))  # speed of particles
        self.Y = self.cal_y()  # image of function corresponding to every particles for one generation
        self.pbest_x = self.X.copy()  # personal best location of every particle in history
        self.pbest_y = self.Y.copy()  # best image of every particle in history
        self.gbest_x = np.zeros((1, self.dim))  # global best location for all particles in history
        self.gbest_y = np.inf  # general best image  for all particles in history
        self.gbest_y_hist = []  # gbest_y of every iteration
        self.update_gbest()
    def __init__(self, cost_func, dimension, population_size=40, max_iteration=150, lb=None, ub=None, w=0.8, c1=0.5, c2=0.5):
        self.func = func_transformer(cost_func)
        
        self.w = w                      # inertia weight
        self.c1, self.c2 = c1, c2       # parameters to control personal best, global best respectively
        self.N = population_size        # number of particles
        self.dimension = dimension      # dimension of particles, which is the number of variables of func
        self.max_iter = max_iteration   # max iter

        self.has_constraints = not (lb is None and ub is None)
        self.lb = -np.ones(self.dimension) if lb is None else np.array(lb)
        self.ub = np.ones(self.dimension) if ub is None else np.array(ub)
        v_high = self.ub - self.lb
      
        assert self.dimension == len(self.lb) == len(self.ub), 'dimension == len(lb) == len(ub) is not True'
        assert np.all(self.ub > self.lb), 'upper-bound must be greater than lower-bound'

        self.X = np.random.uniform(low=self.lb, high=self.ub, size=(self.N, self.dimension)) # position of particles
        self.V = np.random.uniform(low=-v_high, high=v_high, size=(self.N, self.dimension))  # speed of particles
        self.Y = self.calculate_objfunc_Y()             # y = f(x) for all particles
        
        self.pbest_x = self.X.copy()                    # personal best location of every particle in history
        self.pbest_y = self.Y.copy()                    # best objective cost of every particle in history
        
        self.gbest_x = np.zeros((1, self.dimension))    # global best location for all particles
        self.gbest_y = np.inf                           # global best objective cost for all particles
        
        self.gbest_y_hist = []                          # gbest_y of every iteration
        self.update_gbest()

        # record verbose values
        self.record_mode = False
        self.record_value = {'X': [], 'V': [], 'Y': []}
Exemple #5
0
    def __init__(self,
                 func,
                 n_dim=None,
                 pop=40,
                 max_iter=150,
                 lb=-1e5,
                 ub=1e5,
                 w=0.8,
                 c1=0.5,
                 c2=0.5,
                 constraint_eq=tuple(),
                 constraint_ueq=tuple(),
                 verbose=False,
                 dim=None):

        n_dim = n_dim or dim  # support the earlier version

        self.func = func_transformer(func)
        self.w = w  # inertia
        self.cp, self.cg = c1, c2  # parameters to control personal best, global best respectively
        self.pop = pop  # number of particles
        self.n_dim = n_dim  # dimension of particles, which is the number of variables of func
        self.max_iter = max_iter  # max iter
        self.verbose = verbose  # print the result of each iter or not

        self.lb, self.ub = np.array(lb) * np.ones(
            self.n_dim), np.array(ub) * np.ones(self.n_dim)
        assert self.n_dim == len(self.lb) == len(
            self.ub), 'dim == len(lb) == len(ub) is not True'
        assert np.all(
            self.ub > self.lb), 'upper-bound must be greater than lower-bound'

        self.has_constraint = bool(constraint_ueq)
        self.constraint_ueq = constraint_ueq
        self.is_feasible = np.array([True] * pop)

        self.X = np.random.uniform(low=self.lb,
                                   high=self.ub,
                                   size=(self.pop, self.n_dim))
        v_high = self.ub - self.lb
        self.V = np.random.uniform(low=-v_high,
                                   high=v_high,
                                   size=(self.pop,
                                         self.n_dim))  # speed of particles
        self.Y = self.cal_y()  # y = f(x) for all particles
        self.pbest_x = self.X.copy(
        )  # personal best location of every particle in history
        self.pbest_y = np.array([[np.inf]] *
                                pop)  # best image of every particle in history
        self.gbest_x = self.pbest_x.mean(axis=0).reshape(
            1, -1)  # global best location for all particles
        self.gbest_y = np.inf  # global best y for all particles
        self.gbest_y_hist = []  # gbest_y of every iteration
        self.update_gbest()

        # record verbose values
        self.record_mode = False
        self.record_value = {'X': [], 'V': [], 'Y': []}
        self.best_x, self.best_y = self.gbest_x, self.gbest_y  # history reasons, will be deprecated
Exemple #6
0
    def __init__(self,
                 func,
                 dim,
                 pop=40,
                 max_iter=150,
                 lb=None,
                 ub=None,
                 w=0.8,
                 c1=0.5,
                 c2=0.5,
                 verbose=False):
        self.func = func_transformer(func)
        self.w = w  # inertia
        self.cp, self.cg = c1, c2  # parameters to control personal best, global best respectively
        self.pop = pop  # number of particles
        self.dim = dim  # dimension of particles, which is the number of variables of func
        self.max_iter = max_iter  # max iter
        self.verbose = verbose  # print the result of each iter or not

        self.has_constraints = not (lb is None and ub is None)
        self.lb = -np.ones(self.dim) if lb is None else np.array(lb)
        self.ub = np.ones(self.dim) if ub is None else np.array(ub)
        assert self.dim == len(self.lb) == len(
            self.ub), 'dim == len(lb) == len(ub) is not True'
        assert np.all(
            self.ub > self.lb), 'upper-bound must be greater than lower-bound'

        self.X = np.random.uniform(low=self.lb,
                                   high=self.ub,
                                   size=(self.pop, self.dim))
        v_high = self.ub - self.lb
        self.V = np.random.uniform(low=-v_high,
                                   high=v_high,
                                   size=(self.pop,
                                         self.dim))  # speed of particles
        self.Y = self.cal_y()  # y = f(x) for all particles
        self.pbest_x = self.X.copy(
        )  # personal best location of every particle in history
        self.pbest_y = self.Y.copy()  # best image of every particle in history
        self.gbest_x = np.zeros(
            (1, self.dim))  # global best location for all particles
        self.gbest_y = np.inf  # global best y for all particles
        self.gbest_y_hist = []  # gbest_y of every iteration
        self.update_gbest()

        # record verbose values
        self.record_mode = False
        self.record_value = {'X': [], 'V': [], 'Y': []}
        self.best_x, self.best_y = self.gbest_x, self.gbest_y  # history reasons, will be deprecated
Exemple #7
0
    def __init__(self, func, n_dim,
                 size_pop=50, max_iter=200,
                 prob_mut=0.001, **kwargs):
        self.func = func_transformer(func)
        self.size_pop = size_pop  # size of population
        self.max_iter = max_iter
        self.prob_mut = prob_mut  # probability of mutation
        self.n_dim = n_dim

        self.define_chrom(kwargs)
        self.crtbp(self.size_pop, self.len_chrom)

        self.X = None  # shape is size_pop, n_dim (it is all x of func)
        self.Y = None  # shape is size_pop,
        self.FitV = None

        # self.FitV_history = []
        self.generation_best_X = []
        self.generation_best_Y = []
        self.all_history_Y = []
Exemple #8
0
 def __init__(self, func, dim, pop=40, max_iter=150):
     self.func = func_transformer(func)
     self.w = 0.8  # 惯性权重
     self.cp, self.cg = 2, 2  # 加速常数,一般取2附近. p代表个体记忆,g代表集体记忆
     self.pop = pop  # number of particles
     self.dim = dim  # dimension of particles, which is the number of variables of func
     self.max_iter = max_iter  # max iter
     self.X = np.random.rand(
         self.pop, self.dim
     )  # location of particles, which is the value of variables of func
     self.V = np.random.rand(self.pop, self.dim)  # speed of particles
     self.Y = self.cal_y(
     )  # image of function corresponding to every particles for one generation
     self.pbest_x = self.X.copy(
     )  # best location of every particle in history
     self.pbest_y = self.Y.copy()  # best image of every particle in history
     self.gbest_x = np.zeros(
         (1,
          self.dim))  # general best location for all particles in history
     self.gbest_y = np.inf  # general best image  for all particles in history
     self.gbest_y_hist = []  # gbest_y of every iteration
     self.update_gbest()
Exemple #9
0
    def __init__(self,
                 func,
                 n_dim,
                 size_pop=50,
                 max_iter=200,
                 prob_mut=0.001,
                 constraint_eq=tuple(),
                 constraint_ueq=tuple(),
                 early_stop=None):
        self.func = func_transformer(func)
        assert size_pop % 2 == 0, 'size_pop must be even integer'
        self.size_pop = size_pop  # size of population
        self.max_iter = max_iter
        self.prob_mut = prob_mut  # probability of mutation
        self.n_dim = n_dim
        self.early_stop = early_stop

        # constraint:
        self.has_constraint = len(constraint_eq) > 0 or len(constraint_ueq) > 0
        self.constraint_eq = list(
            constraint_eq)  # a list of equal functions with ceq[i] = 0
        self.constraint_ueq = list(
            constraint_ueq
        )  # a list of unequal constraint functions with c[i] <= 0

        self.Chrom = None
        self.X = None  # shape = (size_pop, n_dim)
        self.Y_raw = None  # shape = (size_pop,) , value is f(x)
        self.Y = None  # shape = (size_pop,) , value is f(x) + penalty for constraint
        self.FitV = None  # shape = (size_pop,)

        # self.FitV_history = []
        self.generation_best_X = []
        self.generation_best_Y = []

        self.all_history_Y = []
        self.all_history_FitV = []

        self.best_x, self.best_y = None, None