Skip to content

Commit 97937b3

Browse files
committed
version: 0.6.1, PSO with nonlinear constraint is available.
1 parent d38edec commit 97937b3

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ plt.show()
294294

295295
## 3. PSO(Particle swarm optimization)
296296

297-
### 3.1 PSO with constraint
297+
### 3.1 PSO
298298
**Step1**: define your problem:
299299
-> Demo code: [examples/demo_pso.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L1)
300300
```python
@@ -328,6 +328,21 @@ plt.show()
328328

329329
![PSO_TPS](https://img1.github.io/heuristic_algorithm/pso.png)
330330

331+
### 3.2 PSO with nonlinear constraint
332+
333+
If you need nolinear constraint like `(x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2<=0`
334+
Codes are like this:
335+
```python
336+
constraint_ueq = (
337+
lambda x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2
338+
,
339+
)
340+
pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2]
341+
, constraint_ueq=constraint_ueq)
342+
```
343+
344+
Note that, you can add more then one nonlinear constraint. Just add it to `constraint_ueq`
345+
331346

332347
![pso_ani](https://img1.github.io/heuristic_algorithm/pso.gif)
333348
**see [examples/demo_pso_ani.py](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso_ani.py)**

docs/en/args.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ help(sko.AFSA.AFSA)
5656
| w | 0\.8 | inertia weight |
5757
| c1 | 0\.5 | cognitive parameter |
5858
| c2 | 0\.5 | social parameter |
59-
59+
| constraint\_ueq | tuple() | unequal constraint |
6060

6161
### DE
6262

docs/zh/README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ plt.show()
262262
## 3. 粒子群算法
263263
(PSO, Particle swarm optimization)
264264

265-
### 3.1 带约束的粒子群算法
265+
### 3.1 粒子群算法
266266
**第一步**,定义问题
267267
-> Demo code: [examples/demo_pso.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L1)
268268
```python
@@ -296,6 +296,21 @@ plt.show()
296296
![pso_ani](https://img1.github.io/heuristic_algorithm/pso.gif)
297297
**see [examples/demo_pso.py](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso_ani.py)**
298298

299+
### 3.2 带非线性约束的粒子群算法
300+
301+
### 3.2 PSO with nonlinear constraint
302+
303+
加入你的非线性约束是个圆内的面积 `(x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2<=0`
304+
这样写代码:
305+
```python
306+
constraint_ueq = (
307+
lambda x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2
308+
,
309+
)
310+
pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2]
311+
, constraint_ueq=constraint_ueq)
312+
```
313+
可以有多个非线性约束,向 `constraint_ueq` 加就行了。
299314

300315
## 4. 模拟退火算法
301316
(SA, Simulated Annealing)

docs/zh/args.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ help(sko.AFSA.AFSA)
5656
| w | 0\.8 | 惯性权重 |
5757
| c1 | 0\.5 | 个体记忆 |
5858
| c2 | 0\.5 | 集体记忆 |
59-
59+
| constraint\_ueq | 空元组 | 非线性约束 |
6060

6161
### DE
6262

sko/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '0.5.9'
1+
__version__ = '0.6.1'
22

33
from . import DE, GA, PSO, SA, ACA, AFSA, IA
44

0 commit comments

Comments
 (0)