From 4e44bed2c619eb09167e4a5c7ef5ea7126c76c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=BD=87=E5=B3=B0?= <1809996465@qq.com> Date: Tue, 24 Oct 2023 16:24:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=B8=BADE=E3=80=81GA=E3=80=81PSO=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=8E=A7=E5=88=B6=E5=A4=9A?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B/=E5=A4=9A=E8=BF=9B=E7=A8=8B=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84cpu=E6=A0=B8=E6=95=B0=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sko/DE.py | 5 +++-- sko/GA.py | 15 ++++++++++----- sko/PSO.py | 6 ++++-- sko/tools.py | 16 +++++++++++----- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/sko/DE.py b/sko/DE.py index e2b028c..8a9faf4 100644 --- a/sko/DE.py +++ b/sko/DE.py @@ -15,9 +15,10 @@ class DE(GeneticAlgorithmBase): def __init__(self, func, n_dim, F=0.5, size_pop=50, max_iter=200, prob_mut=0.3, lb=-1, ub=1, - constraint_eq=tuple(), constraint_ueq=tuple()): + constraint_eq=tuple(), constraint_ueq=tuple(), + n_processes=0): super().__init__(func, n_dim, size_pop, max_iter, prob_mut, - constraint_eq=constraint_eq, constraint_ueq=constraint_ueq) + constraint_eq=constraint_eq, constraint_ueq=constraint_ueq, n_processes=n_processes) self.F = F self.V, self.U = None, None diff --git a/sko/GA.py b/sko/GA.py index 8a90f24..8e89311 100644 --- a/sko/GA.py +++ b/sko/GA.py @@ -14,8 +14,8 @@ class GeneticAlgorithmBase(SkoBase, metaclass=ABCMeta): 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) + constraint_eq=tuple(), constraint_ueq=tuple(), early_stop=None, n_processes=0): + self.func = func_transformer(func, n_processes) assert size_pop % 2 == 0, 'size_pop must be even integer' self.size_pop = size_pop # size of population self.max_iter = max_iter @@ -133,6 +133,8 @@ class GA(GeneticAlgorithmBase): Max of iter prob_mut : float between 0 and 1 Probability of mutation + n_processes : int + Number of processes, 0 means use all cpu Attributes ---------------------- Lind : array_like @@ -151,8 +153,8 @@ def __init__(self, func, n_dim, prob_mut=0.001, lb=-1, ub=1, constraint_eq=tuple(), constraint_ueq=tuple(), - precision=1e-7, early_stop=None): - super().__init__(func, n_dim, size_pop, max_iter, prob_mut, constraint_eq, constraint_ueq, early_stop) + precision=1e-7, early_stop=None, n_processes=0): + super().__init__(func, n_dim, size_pop, max_iter, prob_mut, constraint_eq, constraint_ueq, early_stop, n_processes=n_processes) self.lb, self.ub = np.array(lb) * np.ones(self.n_dim), np.array(ub) * np.ones(self.n_dim) self.precision = np.array(precision) * np.ones(self.n_dim) # works when precision is int, float, list or array @@ -277,6 +279,8 @@ class RCGA(GeneticAlgorithmBase): The lower bound of every variables of func ub : array_like The upper bound of every variables of func + n_processes : int + Number of processes, 0 means use all cpu """ def __init__(self, func, n_dim, @@ -284,8 +288,9 @@ def __init__(self, func, n_dim, prob_mut=0.001, prob_cros=0.9, lb=-1, ub=1, + n_processes=0 ): - super().__init__(func, n_dim, size_pop, max_iter, prob_mut) + super().__init__(func, n_dim, size_pop, max_iter, prob_mut, n_processes=n_processes) self.lb, self.ub = np.array(lb) * np.ones(self.n_dim), np.array(ub) * np.ones(self.n_dim) self.prob_cros = prob_cros self.crtbp() diff --git a/sko/PSO.py b/sko/PSO.py index fbb7657..eb64112 100644 --- a/sko/PSO.py +++ b/sko/PSO.py @@ -61,6 +61,8 @@ class PSO(SkoBase): equal constraint. Note: not available yet. constraint_ueq : tuple unequal constraint + n_processes : int + Number of processes, 0 means use all cpu Attributes ---------------------- pbest_x : array_like, shape is (pop,dim) @@ -82,11 +84,11 @@ class PSO(SkoBase): 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): + , dim=None, n_processes=0): n_dim = n_dim or dim # support the earlier version - self.func = func_transformer(func) + self.func = func_transformer(func, n_processes) self.w = w # inertia self.cp, self.cg = c1, c2 # parameters to control personal best, global best respectively self.pop = pop # number of particles diff --git a/sko/tools.py b/sko/tools.py index 4039366..4a02076 100644 --- a/sko/tools.py +++ b/sko/tools.py @@ -27,7 +27,7 @@ def set_run_mode(func, mode): return -def func_transformer(func): +def func_transformer(func, n_processes): ''' transform this kind of function: ``` @@ -91,18 +91,24 @@ def func_warped(X): return func_warped elif mode == 'multithreading': + assert n_processes >= 0, 'n_processes should >= 0' from multiprocessing.dummy import Pool as ThreadPool - - pool = ThreadPool() + if n_processes == 0: + pool = ThreadPool() + else: + pool = ThreadPool(n_processes) def func_transformed(X): return np.array(pool.map(func, X)) return func_transformed elif mode == 'multiprocessing': + assert n_processes >= 0, 'n_processes should >= 0' from multiprocessing import Pool - pool = Pool() - + if n_processes == 0: + pool = Pool() + else: + pool = Pool(n_processes) def func_transformed(X): return np.array(pool.map(func, X)) From 17c85a81b2d34e69ac9d2b845dad612f90837962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=BD=87=E5=B3=B0?= <1809996465@qq.com> Date: Tue, 24 Oct 2023 16:45:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/args.md | 3 +++ docs/zh/args.md | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/docs/en/args.md b/docs/en/args.md index 39d14ff..9edf197 100644 --- a/docs/en/args.md +++ b/docs/en/args.md @@ -30,6 +30,7 @@ help(sko.AFSA.AFSA) | constraint\_eq | tuple() | equal constraint | | constraint\_ueq | tuple() | unequal constraint | | precision | 1e\-7 | precision,int/float or list | +| n_processes | 0 | number of processes, 0 means use all cpu | ### GA_TSP @@ -57,6 +58,7 @@ help(sko.AFSA.AFSA) | c1 | 0\.5 | cognitive parameter | | c2 | 0\.5 | social parameter | | constraint\_ueq | tuple() | unequal constraint | +| n_processes | 0 | number of processes, 0 means use all cpu | ### DE @@ -72,6 +74,7 @@ help(sko.AFSA.AFSA) | ub | 1 | upper bound of variables | | constraint\_eq | tuple() | equal constraint | | constraint\_ueq | tuple() | unequal constraint | +| n_processes | 0 | number of processes, 0 means use all cpu | ### SA diff --git a/docs/zh/args.md b/docs/zh/args.md index b0b340e..26fe17f 100644 --- a/docs/zh/args.md +++ b/docs/zh/args.md @@ -30,6 +30,7 @@ help(sko.AFSA.AFSA) | constraint\_eq | 空元组 | 等式约束 | | constraint\_ueq | 空元组 | 不等式约束 | | precision | 1e\-7 | 精准度,int/float或者它们组成的列表 | +| n_processes | 0 | 使用的核数, 0表示使用全部的核 | ### GA_TSP @@ -57,6 +58,8 @@ help(sko.AFSA.AFSA) | c1 | 0\.5 | 个体记忆 | | c2 | 0\.5 | 集体记忆 | | constraint\_ueq | 空元组 | 不等式约束 | +| n_processes | 0 | 使用的核数, 0表示使用全部的核 | + ### DE @@ -72,6 +75,8 @@ help(sko.AFSA.AFSA) | ub | 1 | 每个自变量的最大值 | | constraint\_eq | 空元组 | 等式约束 | | constraint\_ueq | 空元组 | 不等式约束 | +| n_processes | 0 | 使用的核数, 0表示使用全部的核 | + ### SA