Skip to content

Commit

Permalink
version 0.5.0: Differential Evolution is available
Browse files Browse the repository at this point in the history
  • Loading branch information
guofei9987 committed Dec 2, 2019
1 parent 33b95f4 commit 10bf651
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 41 deletions.
72 changes: 57 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ pip install .
```

## News:
All algorithms will be available on ~~TensorFlow/Spark~~ **pytorch** on version 0.4, getting parallel performance.
DE(Differential Evolution Algorithm) will be complete on version 0.5
All algorithms will be available on ~~TensorFlow/Spark~~ **pytorch** on version 1.0.0, getting parallel performance.
Have fun!


Expand Down Expand Up @@ -130,7 +129,50 @@ ga.run(20)
```

# Quick start
## 1. Genetic Algorithm

## 1. Differential Evolution
**Step1**:define your problem
-> Demo code: [examples/demo_de.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_de.py#L1)
```python
'''
min f(x1, x2, x3) = x1^2 + x2^2 + x3^2
s.t.
x1x2 >= 1
x1x2 <= 5
x2+x3 = 1
0 <= x1, x2, x3 <= 5
'''


def obj_func(p):
x1, x2, x3 = p
return x1 ** 2 + x2 ** 2 + x3 ** 2


constraint_eq = [
lambda x: 1 - x[1] - x[2]
]

constraint_ueq = [
lambda x: 1 - x[0] * x[1],
lambda x: x[0] * x[1] - 5
]

```

**Step2**: do Differential Evolution
-> Demo code: [examples/demo_de.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_de.py#L25)
```python
from sko.DE import DE

de = DE(func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5],
constraint_eq=constraint_eq, constraint_ueq=constraint_ueq)

best_x, best_y = de.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)
```

## 2. Genetic Algorithm

**Step1**:define your problem
-> Demo code: [examples/demo_ga.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga.py#L1)
Expand All @@ -150,7 +192,7 @@ def schaffer(p):

```

**Step2**: do GA
**Step2**: do Genetic Algorithm
-> Demo code: [examples/demo_ga.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga.py#L14)
```python
from sko.GA import GA
Expand All @@ -176,7 +218,7 @@ plt.show()

![Figure_1-1](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/ga_1.png?raw=true)

### 1.1 Genetic Algorithm for TSP(Travelling Salesman Problem)
### 2.2 Genetic Algorithm for TSP(Travelling Salesman Problem)
Just import the `GA_TSP`, it overloads the `crossover`, `mutation` to solve the TSP

**Step1**: define your problem. Prepare your points coordinate and the distance matrix.
Expand Down Expand Up @@ -227,9 +269,9 @@ plt.show()
![GA_TPS](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/ga_tsp.png?raw=true)


## 2. PSO(Particle swarm optimization)
## 3. PSO(Particle swarm optimization)

### 2.1 PSO with constraint
### 3.1 PSO with constraint
**Step1**: define your problem:
-> Demo code: [examples/demo_pso.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L1)
```python
Expand Down Expand Up @@ -268,16 +310,16 @@ plt.show()
![pso_ani](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/pso.gif?raw=true)
**see [examples/demo_pso_ani.py](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso_ani.py)**

### 2.2 PSO without constraint
### 3.2 PSO without constraint
-> Demo code: [examples/demo_pso.py#s4](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L19)
```python
pso = PSO(func=demo_func, dim=3)
fitness = pso.run()
print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)
```

## 3. SA(Simulated Annealing)
### 3.1 SA for multiple function
## 4. SA(Simulated Annealing)
### 4.1 SA for multiple function
**Step1**: define your problem
-> Demo code: [examples/demo_sa.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa.py#L1)
```python
Expand Down Expand Up @@ -309,7 +351,7 @@ plt.show()


Moreover, scikit-opt provide 3 types of Simulated Annealing: Fast, Boltzmann, Cauchy. See [more sa](https://scikit-opt.github.io/scikit-opt/#/en/more_sa)
### 3.2 SA for TSP
### 4.2 SA for TSP
**Step1**: oh, yes, define your problems. To boring to copy this step.

**Step2**: DO SA for TSP
Expand Down Expand Up @@ -355,7 +397,7 @@ More: Plot the animation:



## 4. ACA (Ant Colony Algorithm) for tsp
## 5. ACA (Ant Colony Algorithm) for tsp
-> Demo code: [examples/demo_aca_tsp.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_aca_tsp.py#L23)
```python
from sko.ACA import ACA_TSP
Expand All @@ -371,13 +413,13 @@ best_x, best_y = aca.run()
![ACA](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/aca_tsp.png?raw=true)


## 5. immune algorithm (IA)
## 6. immune algorithm (IA)
-> Demo code: [examples/demo_ia.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ia.py#L6)
```python

from sko.IA import IA_TSP

ia_tsp = IA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=500, max_iter=2000, prob_mut=0.2,
ia_tsp = IA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=500, max_iter=800, prob_mut=0.2,
T=0.7, alpha=0.95)
best_points, best_distance = ia_tsp.run()
print('best routine:', best_points, 'best_distance:', best_distance)
Expand All @@ -386,7 +428,7 @@ print('best routine:', best_points, 'best_distance:', best_distance)

![IA](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/ia2.png?raw=true)

## 6. artificial fish swarm algorithm (AFSA)
## 7. artificial fish swarm algorithm (AFSA)
-> Demo code: [examples/demo_asfs.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_asfs.py#L1)
```python
def func(x):
Expand Down
69 changes: 56 additions & 13 deletions docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,50 @@ ga.run(20)
```

# Quick start
## 1. Genetic Algorithm

## 1. Differential Evolution
**Step1**:define your problem
-> Demo code: [examples/demo_de.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_de.py#L1)
```python
'''
min f(x1, x2, x3) = x1^2 + x2^2 + x3^2
s.t.
x1x2 >= 1
x1x2 <= 5
x2+x3 = 1
0 <= x1, x2, x3 <= 5
'''


def obj_func(p):
x1, x2, x3 = p
return x1 ** 2 + x2 ** 2 + x3 ** 2


constraint_eq = [
lambda x: 1 - x[1] - x[2]
]

constraint_ueq = [
lambda x: 1 - x[0] * x[1],
lambda x: x[0] * x[1] - 5
]

```

**Step2**: do Differential Evolution
-> Demo code: [examples/demo_de.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_de.py#L25)
```python
from sko.DE import DE

de = DE(func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5],
constraint_eq=constraint_eq, constraint_ueq=constraint_ueq)

best_x, best_y = de.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)
```

## 2. Genetic Algorithm

**Step1**:define your problem
-> Demo code: [examples/demo_ga.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga.py#L1)
Expand All @@ -130,7 +173,7 @@ def schaffer(p):

```

**Step2**: do GA
**Step2**: do Genetic Algorithm
-> Demo code: [examples/demo_ga.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga.py#L14)
```python
from sko.GA import GA
Expand All @@ -156,7 +199,7 @@ plt.show()

![Figure_1-1](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/ga_1.png?raw=true)

### 1.1 Genetic Algorithm for TSP(Travelling Salesman Problem)
### 2.2 Genetic Algorithm for TSP(Travelling Salesman Problem)
Just import the `GA_TSP`, it overloads the `crossover`, `mutation` to solve the TSP

**Step1**: define your problem. Prepare your points coordinate and the distance matrix.
Expand Down Expand Up @@ -207,9 +250,9 @@ plt.show()
![GA_TPS](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/ga_tsp.png?raw=true)


## 2. PSO(Particle swarm optimization)
## 3. PSO(Particle swarm optimization)

### 2.1 PSO with constraint
### 3.1 PSO with constraint
**Step1**: define your problem:
-> Demo code: [examples/demo_pso.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L1)
```python
Expand Down Expand Up @@ -248,16 +291,16 @@ plt.show()
![pso_ani](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/pso.gif?raw=true)
**see [examples/demo_pso_ani.py](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso_ani.py)**

### 2.2 PSO without constraint
### 3.2 PSO without constraint
-> Demo code: [examples/demo_pso.py#s4](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L19)
```python
pso = PSO(func=demo_func, dim=3)
fitness = pso.run()
print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)
```

## 3. SA(Simulated Annealing)
### 3.1 SA for multiple function
## 4. SA(Simulated Annealing)
### 4.1 SA for multiple function
**Step1**: define your problem
-> Demo code: [examples/demo_sa.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa.py#L1)
```python
Expand Down Expand Up @@ -289,7 +332,7 @@ plt.show()


Moreover, scikit-opt provide 3 types of Simulated Annealing: Fast, Boltzmann, Cauchy. See [more sa](https://scikit-opt.github.io/scikit-opt/#/en/more_sa)
### 3.2 SA for TSP
### 4.2 SA for TSP
**Step1**: oh, yes, define your problems. To boring to copy this step.

**Step2**: DO SA for TSP
Expand Down Expand Up @@ -335,7 +378,7 @@ More: Plot the animation:



## 4. ACA (Ant Colony Algorithm) for tsp
## 5. ACA (Ant Colony Algorithm) for tsp
-> Demo code: [examples/demo_aca_tsp.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_aca_tsp.py#L23)
```python
from sko.ACA import ACA_TSP
Expand All @@ -351,13 +394,13 @@ best_x, best_y = aca.run()
![ACA](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/aca_tsp.png?raw=true)


## 5. immune algorithm (IA)
## 6. immune algorithm (IA)
-> Demo code: [examples/demo_ia.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ia.py#L6)
```python

from sko.IA import IA_TSP

ia_tsp = IA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=500, max_iter=2000, prob_mut=0.2,
ia_tsp = IA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=500, max_iter=800, prob_mut=0.2,
T=0.7, alpha=0.95)
best_points, best_distance = ia_tsp.run()
print('best routine:', best_points, 'best_distance:', best_distance)
Expand All @@ -366,7 +409,7 @@ print('best routine:', best_points, 'best_distance:', best_distance)

![IA](https://github.com/guofei9987/pictures_for_blog/blob/master/heuristic_algorithm/ia2.png?raw=true)

## 6. artificial fish swarm algorithm (AFSA)
## 7. artificial fish swarm algorithm (AFSA)
-> Demo code: [examples/demo_asfs.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_asfs.py#L1)
```python
def func(x):
Expand Down
Loading

0 comments on commit 10bf651

Please sign in to comment.