Skip to content

Commit

Permalink
Merge pull request #221 from ZXF1001/master
Browse files Browse the repository at this point in the history
Parameters for the number of cpu cores used
  • Loading branch information
guofei9987 authored Nov 19, 2023
2 parents d36e32a + 17c85a8 commit 7e5a1d1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
3 changes: 3 additions & 0 deletions docs/en/args.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions docs/zh/args.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ help(sko.AFSA.AFSA)
| constraint\_eq | 空元组 | 等式约束 |
| constraint\_ueq | 空元组 | 不等式约束 |
| precision | 1e\-7 | 精准度,int/float或者它们组成的列表 |
| n_processes | 0 | 使用的核数, 0表示使用全部的核 |


### GA_TSP
Expand Down Expand Up @@ -57,6 +58,8 @@ help(sko.AFSA.AFSA)
| c1 | 0\.5 | 个体记忆 |
| c2 | 0\.5 | 集体记忆 |
| constraint\_ueq | 空元组 | 不等式约束 |
| n_processes | 0 | 使用的核数, 0表示使用全部的核 |


### DE

Expand All @@ -72,6 +75,8 @@ help(sko.AFSA.AFSA)
| ub | 1 | 每个自变量的最大值 |
| constraint\_eq | 空元组 | 等式约束 |
| constraint\_ueq | 空元组 | 不等式约束 |
| n_processes | 0 | 使用的核数, 0表示使用全部的核 |


### SA

Expand Down
5 changes: 3 additions & 2 deletions sko/DE.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions sko/GA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -339,15 +341,18 @@ 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,
size_pop=50, max_iter=200,
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()
Expand Down
6 changes: 4 additions & 2 deletions sko/PSO.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
16 changes: 11 additions & 5 deletions sko/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 7e5a1d1

Please sign in to comment.