guofei9987 / guofei9987/scikit-opt

PSO seens cannot treat equal constraints

Open
#181 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
6.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Hi, it seems the equation in PSO doesn't work effectively, this is the code:

```
def demo_func(x):
x1, x2 = x
return -20 * np.exp(-0.2 * np.sqrt(0.5 * (x1 ** 2 + x2 ** 2))) - np.exp(
0.5 * (np.cos(2 * np.pi * x1) + np.cos(2 * np.pi * x2))) + 20 + np.e

# I add this equation
constraint_eq = [
lambda x: 2 - x[0] - x[1],
]

constraint_ueq = (
lambda x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2
,
)

max_iter = 50
pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2],
constraint_eq=constraint_eq,
constraint_ueq=constraint_ueq)
pso.record_mode = True
pso.run()
print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)

# but it seems not effective
print('the sume of x:', sum(pso.gbest_x))
```

the output is
```
best_x is [ 9.51948501e-01 -3.04008149e-04] best_y is [2.57993123]
the sume of x: 0.9516444926553026
```

we can see that `x[0] + x[1] = 2` doesn't work, is there any problem with my code?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.