Skip to content

Commit 9920dab

Browse files
committed
发布0.0.2版本
1. 完善原BAS边界无处理的问题 2. 加demo 3. 依赖astartool工具包 Signed-off-by: A.Star <[email protected]>
1 parent ce217a8 commit 9920dab

File tree

7 files changed

+129
-8
lines changed

7 files changed

+129
-8
lines changed

Diff for: bas/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@
88
# @Software: PyCharm
99

1010
from bas.rbas import RBAS
11-
__version__ = '0.0.1'
11+
from bas.bas import BAS
12+
13+
from astartool.setuptool import get_version
14+
15+
version = (0, 0, 2, 'final', 0)
16+
__version__ = get_version(version)
17+
18+
del get_version

Diff for: bas/bas.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Author: 河北雪域网络科技有限公司 A.Star
4+
# @contact: [email protected]
5+
# @site: www.snowland.ltd
6+
# @file: bas.py
7+
# @time: 2019/7/25 11:15
8+
# @Software: PyCharm
9+
10+
11+
__author__ = 'A.Star'
12+
13+
from matplotlib import pylab as plt
14+
import numpy as np
15+
from copy import deepcopy
16+
from slapy.swarm.bas import BASEngine
17+
18+
npa = np.array
19+
npr = np.random
20+
21+
# 带边界处理的BAS
22+
23+
24+
class BAS(BASEngine):
25+
def __init__(self, steps=100, eps=0.01, chromosome=None, dim=2, bound=None, fitness_function=None, *,
26+
fitness_value=-np.inf, init_method='random', step0=1, c=5, eta=0.95, **kwargs):
27+
super().__init__(steps, eps, chromosome, dim, bound, fitness_function, fitness_value=fitness_value,
28+
init_method=init_method, **kwargs)
29+
self.eta = eta
30+
self.c = c
31+
self.step = step0
32+
33+
def update(self, *args, **kwargs):
34+
d0 = self.step / self.c
35+
dir = npr.rand(self.dim) - 1
36+
dir = dir / (self.eps + np.linalg.norm(dir))
37+
xleft = self.chromosome + dir * d0
38+
fleft = self.fitness_function(xleft)
39+
xright = self.chromosome - dir * d0
40+
fright = self.fitness_function(xright)
41+
self.chromosome -= self.step * dir * np.sign(-fleft + fright)
42+
# 边界处理规则
43+
self.transboundary()
44+
45+
if self.fitness() > self.gbest.fitness_value:
46+
self.gbest = deepcopy(self)
47+
self.step *= self.eta
48+
49+
def fitness(self, *args, **kwargs):
50+
return super(BASEngine, self).fitness(*args, **kwargs)
51+
52+
def initialize(self, *args, **kwargs):
53+
super(BASEngine, self).initialize(*args, **kwargs)
54+
self.gbest = deepcopy(self)
55+
self.best = []
56+
self.path = []
57+
58+
def analysis(self, *args, **kwargs):
59+
print('-' * 15)
60+
print("best:", self.best)
61+
print('-' * 15)
62+
print("path:", self.path)
63+
print('-' * 15)
64+
plt.figure(1)
65+
path = npa(self.path)
66+
plt.plot(path[:, 0], path[:, 1], 'r+-')
67+
plt.figure(2)
68+
plt.title('itor in each steps')
69+
plt.plot(self.best, 'r-', label='best')
70+
plt.xlabel("Iteration")
71+
plt.ylabel("function value")
72+
plt.show()
73+
print(self.gbest.chromosome)
74+
75+
def record(self, *args, **kwargs):
76+
print(len(self.path))
77+
self.path.append(deepcopy(self.chromosome))
78+
self.best.append(self.fitness_value)

Diff for: bas/rbas.py

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
npr = np.random
1616

1717

18+
19+
# 模拟退火结合的BAS
20+
21+
1822
class RBASEngine(BASEngine):
1923
def __init__(self, steps=100, eps=0.01, chromosome=None, dim=2, bound=None, fitness_function=None, *,
2024
fitness_value=-np.inf, init_method='random', step0=1, c=5, eta=0.95, **kwargs):

Diff for: bas/demo.py renamed to demo/demo.py

File renamed without changes.

Diff for: demo/demo2.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Author: 河北雪域网络科技有限公司 A.Star
4+
# @contact: [email protected]
5+
# @site: www.snowland.ltd
6+
# @file: demo2.py
7+
# @time: 2019/7/25 10:52
8+
# @Software: PyCharm
9+
10+
11+
__author__ = 'A.Star'
12+
13+
from bas import BAS
14+
15+
16+
def f(x):
17+
c, d = x
18+
return 1 / ((1 + (c + d + 1) ** 2 * (19 - 14 * c + 3 * d ** 2 - 14 * d + 6 * c * d + 3 * d ** 2)) * (
19+
30 + (2 * c - 3 * d) ** 2 * (18 - 32 * c + 12 * c ** 2 + 48 * d - 36 * c * d + 27 * d ** 2)))
20+
21+
22+
if __name__ == '__main__':
23+
bas_engine = BAS(
24+
steps=500,
25+
dim=2,
26+
fitness_function=f,
27+
step=1,
28+
eta=0.95,
29+
bound=[[-2, 2], [-2, 2]],
30+
transboundary_rules='mod'
31+
)
32+
bas_engine.run()

Diff for: requirements.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
snowland-algorithm==0.0.7
2-
numpy>=1.0.0
1+
snowland-algorithm>=0.0.7
2+
numpy>=1.0.0
3+
astartool>=0.0.2

Diff for: setup.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
from setuptools import setup, find_packages
1313
import bas
14+
from astartool.setuptool import load_install_requires
15+
1416

1517
setup(
1618
name="pybas",
1719
version=bas.__version__,
1820
description=(
19-
'Python implementation algorithm'
21+
'Beetle Antennae Search Algorithm for python 3.x'
2022
),
2123
long_description=open('README.rst').read(),
2224
author='A.Star',
@@ -43,8 +45,5 @@
4345
'Programming Language :: Python :: 3.7',
4446
'Topic :: Software Development :: Libraries'
4547
],
46-
install_requires=[
47-
'snowland-algorithm==0.0.7',
48-
'numpy>=1.0.0'
49-
],
48+
install_requires=load_install_requires(),
5049
)

0 commit comments

Comments
 (0)