Skip to content

Commit 358152d

Browse files
authored
Merge pull request #276 from hktkzyx/master
2 parents 2777968 + a968451 commit 358152d

File tree

236 files changed

+16448
-14650
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

236 files changed

+16448
-14650
lines changed

.flake8

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[flake8]
22
extend-ignore = D1, D204, W292, W293
3-
docstring-convention = numpy
43
max-complexity = 10
54
max-line-length = 119
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: build-and-test
5+
6+
on:
7+
push:
8+
branches: [master]
9+
pull_request:
10+
branches: [master]
11+
12+
jobs:
13+
build:
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
os: [ubuntu-latest, windows-latest]
18+
python-version: ["3.6"]
19+
defaults:
20+
run:
21+
shell: bash
22+
runs-on: ${{ matrix.os }}
23+
timeout-minutes: 30
24+
steps:
25+
- uses: actions/checkout@v3
26+
- name: Set up Python ${{ matrix.python-version }}
27+
uses: actions/setup-python@v3
28+
with:
29+
python-version: ${{ matrix.python-version }}
30+
- name: Install dependencies
31+
run: |
32+
pip install cython setuptools
33+
pip install -r requirements.txt
34+
pip install pytest
35+
- name: Build
36+
run: python setup.py install
37+
- name: Test with pytest
38+
run: |
39+
python -m geatpy --version
40+
pytest

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
.idea/
33
.vscode/
44

5+
# Auto generated folder
6+
core/
7+
58
# Byte-compiled / optimized / DLL files
69
__pycache__/
710
*.py[cod]

.travis.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# **Geatpy2**
1+
# **Geatpy2**
22
The Genetic and Evolutionary Algorithm Toolbox for Python with high performance.
33

4-
![Travis](https://travis-ci.org/geatpy-dev/geatpy.svg?branch=master)
4+
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/geatpy-dev/geatpy/build-and-test)](https://github.com/geatpy-dev/geatpy/actions)
55
[![Package Status](https://img.shields.io/pypi/status/geatpy.svg)](https://pypi.org/project/geatpy/)
66
![Python](https://img.shields.io/badge/python->=3.5-green.svg)
77
![Pypi](https://img.shields.io/badge/pypi-2.7.0-blue.svg)
@@ -11,7 +11,7 @@ The Genetic and Evolutionary Algorithm Toolbox for Python with high performance.
1111

1212
## Introduction
1313
* **Website (including documentation)**: http://www.geatpy.com
14-
* **Demo** : https://github.com/geatpy-dev/geatpy/tree/master/geatpy/demo
14+
* **Demo** : https://github.com/geatpy-dev/geatpy/tree/master/demo
1515
* **Pypi page** : https://pypi.org/project/geatpy/
1616
* **Contact us**: http://geatpy.com/index.php/about/
1717
* **Bug reports**: https://github.com/geatpy-dev/geatpy/issues
@@ -145,7 +145,7 @@ if __name__ == '__main__':
145145

146146
Run the "main.py" and the part of the result is:
147147

148-
![image](https://github.com/geatpy-dev/geatpy/blob/master/geatpy/testbed/moea_test/result/Pareto%20Front%20Plot.svg)
148+
![image](https://github.com/geatpy-dev/geatpy/blob/master/testbed/moea_test/result/Pareto%20Front%20Plot.svg)
149149

150150
Execution time: 0.3650233745574951 s
151151

@@ -213,7 +213,7 @@ if __name__ == '__main__':
213213

214214
Part of the result is:
215215

216-
![image](https://github.com/geatpy-dev/geatpy/blob/master/geatpy/testbed/soea_test/result/Trace%20Plot.svg)
216+
![image](https://github.com/geatpy-dev/geatpy/blob/master/testbed/soea_test/result/Trace%20Plot.svg)
217217

218218
Execution time: 0.256328821182251 s
219219

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
import numpy as np
3-
import geatpy as ea
2+
"""Demo.
43
5-
"""
64
min f1 = -25 * (x1 - 2)**2 - (x2 - 2)**2 - (x3 - 1)**2 - (x4 - 4)**2 - (x5 - 1)**2
75
min f2 = (x1 - 1)**2 + (x2 - 1)**2 + (x3 - 1)**2 + (x4 - 1)**2 + (x5 - 1)**2
86
s.t.
@@ -14,9 +12,13 @@
1412
(x5 - 3)**2 + x4 - 4 >= 0
1513
x1,x2,x3,x4,x5 ∈ {0,1,2,3,4,5,6,7,8,9,10}
1614
"""
15+
import numpy as np
16+
17+
import geatpy as ea
1718

1819

1920
class MyProblem(ea.Problem): # 继承Problem父类
21+
2022
def __init__(self, M=2):
2123
name = 'MyProblem' # 初始化name(函数名称,可以随意设置)
2224
Dim = 5 # 初始化Dim(决策变量维数)
@@ -27,16 +29,27 @@ def __init__(self, M=2):
2729
lbin = [1] * Dim # 决策变量下边界(0表示不包含该变量的下边界,1表示包含)
2830
ubin = [1] * Dim # 决策变量上边界(0表示不包含该变量的上边界,1表示包含)
2931
# 调用父类构造方法完成实例化
30-
ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin)
32+
ea.Problem.__init__(self,
33+
name,
34+
M,
35+
maxormins,
36+
Dim,
37+
varTypes,
38+
lb,
39+
ub,
40+
lbin,
41+
ubin)
3142

3243
def evalVars(self, Vars): # 目标函数
3344
x1 = Vars[:, [0]]
3445
x2 = Vars[:, [1]]
3546
x3 = Vars[:, [2]]
3647
x4 = Vars[:, [3]]
3748
x5 = Vars[:, [4]]
38-
f1 = -25 * (x1 - 2) ** 2 - (x2 - 2) ** 2 - (x3 - 1) ** 2 - (x4 - 4) ** 2 - (x5 - 1) ** 2
39-
f2 = (x1 - 1) ** 2 + (x2 - 1) ** 2 + (x3 - 1) ** 2 + (x4 - 1) ** 2 + (x5 - 1) ** 2
49+
f1 = -25 * (x1 - 2)**2 - (x2 - 2)**2 - (x3 - 1)**2 - (x4 - 4)**2 - (
50+
x5 - 1)**2
51+
f2 = (x1 - 1)**2 + (x2 - 1)**2 + (x3 - 1)**2 + (x4 - 1)**2 + (x5
52+
- 1)**2
4053
# # 利用罚函数法处理约束条件
4154
# idx1 = np.where(x1 + x2 < 2)[0]
4255
# idx2 = np.where(x1 + x2 > 6)[0]
@@ -48,11 +61,12 @@ def evalVars(self, Vars): # 目标函数
4861
# f1[exIdx] = f1[exIdx] + np.max(f1) - np.min(f1)
4962
# f2[exIdx] = f2[exIdx] + np.max(f2) - np.min(f2)
5063
# 利用可行性法则处理约束条件
51-
CV = np.hstack([2 - x1 - x2,
52-
x1 + x2 - 6,
53-
-2 - x1 + x2,
54-
x1 - 3 * x2 - 2,
55-
(x3 - 3) ** 2 + x4 - 4,
56-
4 - (x5 - 3) ** 2 - x4])
64+
CV = np.hstack([
65+
2 - x1 - x2,
66+
x1 + x2 - 6,
67+
-2 - x1 + x2,
68+
x1 - 3 * x2 - 2, (x3 - 3)**2 + x4 - 4,
69+
4 - (x5 - 3)**2 - x4
70+
])
5771
f = np.hstack([f1, f2])
5872
return f, CV

demo/moea_demo/moea_demo1/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
"""该案例展示了一个离散决策变量的最小化目标的双目标优化问题的求解。问题的定义详见MyProblem.py."""
3+
from MyProblem import MyProblem # 导入自定义问题接口
4+
5+
import geatpy as ea # import geatpy
6+
7+
if __name__ == '__main__':
8+
# 实例化问题对象
9+
problem = MyProblem()
10+
# 构建算法
11+
algorithm = ea.moea_NSGA2_templet(
12+
problem,
13+
ea.Population(Encoding='BG', NIND=50),
14+
MAXGEN=200, # 最大进化代数
15+
logTras=0) # 表示每隔多少代记录一次日志信息,0表示不记录。
16+
algorithm.mutOper.Pm = 0.2 # 修改变异算子的变异概率
17+
algorithm.recOper.XOVR = 0.9 # 修改交叉算子的交叉概率
18+
# 求解
19+
res = ea.optimize(algorithm,
20+
verbose=False,
21+
drawing=1,
22+
outputMsg=True,
23+
drawLog=False,
24+
saveFlag=False)
25+
print(res)
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# -*- coding: utf-8 -*-
2-
import numpy as np
3-
import geatpy as ea
2+
"""Demo.
43
5-
"""
64
min f1 = X**2
75
min f2 = (X - 2)**2
86
s.t.
97
X**2 - 2.5 * X + 1.5 >= 0
108
10 <= Xi <= 10, (i = 1,2,3,...)
119
"""
10+
import numpy as np
11+
12+
import geatpy as ea
1213

1314

1415
class MyProblem(ea.Problem): # 继承Problem父类
16+
1517
def __init__(self, M=2):
1618
name = 'MyProblem' # 初始化name(函数名称,可以随意设置)
1719
Dim = 1 # 初始化Dim(决策变量维数)
@@ -22,16 +24,25 @@ def __init__(self, M=2):
2224
lbin = [1] * Dim # 决策变量下边界(0表示不包含该变量的下边界,1表示包含)
2325
ubin = [1] * Dim # 决策变量上边界(0表示不包含该变量的上边界,1表示包含)
2426
# 调用父类构造方法完成实例化
25-
ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin)
27+
ea.Problem.__init__(self,
28+
name,
29+
M,
30+
maxormins,
31+
Dim,
32+
varTypes,
33+
lb,
34+
ub,
35+
lbin,
36+
ubin)
2637

2738
def evalVars(self, Vars): # 目标函数
28-
f1 = Vars ** 2
29-
f2 = (Vars - 2) ** 2
39+
f1 = Vars**2
40+
f2 = (Vars - 2)**2
3041
# # 利用罚函数法处理约束条件
3142
# exIdx = np.where(Vars**2 - 2.5 * Vars + 1.5 < 0)[0] # 获取不满足约束条件的个体在种群中的下标
3243
# f1[exIdx] = f1[exIdx] + np.max(f1) - np.min(f1)
3344
# f2[exIdx] = f2[exIdx] + np.max(f2) - np.min(f2)
3445
# 利用可行性法则处理约束条件
35-
CV = -Vars ** 2 + 2.5 * Vars - 1.5
46+
CV = -Vars**2 + 2.5 * Vars - 1.5
3647
ObjV = np.hstack([f1, f2])
3748
return ObjV, CV

demo/moea_demo/moea_demo2/main.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
"""该案例展示了一个带约束连续决策变量的最小化目标的双目标优化问题的求解。详见MyProblem.py."""
3+
from MyProblem import MyProblem # 导入自定义问题接口
4+
5+
import geatpy as ea # import geatpy
6+
7+
if __name__ == '__main__':
8+
# 实例化问题对象
9+
problem = MyProblem()
10+
# 构建算法
11+
algorithm = ea.moea_NSGA2_templet(
12+
problem,
13+
ea.Population(Encoding='RI', NIND=50),
14+
MAXGEN=200, # 最大进化代数
15+
logTras=0) # 表示每隔多少代记录一次日志信息,0表示不记录。
16+
# 求解
17+
res = ea.optimize(algorithm,
18+
verbose=False,
19+
drawing=1,
20+
outputMsg=True,
21+
drawLog=False,
22+
saveFlag=False)
23+
print(res)

geatpy/demo/moea_demo/moea_demo3/MyProblem.py renamed to demo/moea_demo/moea_demo3/MyProblem.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
import numpy as np
3-
import geatpy as ea
2+
"""一个带约束的多目标背包问题.
43
5-
"""
6-
一个带约束的多目标背包问题:
74
假设有5类物品,每类物品中包含着四个具体的物品,要求从这五种类别的物品中分别选择一个物品放进背包,
85
使背包内的物品总价最高,总体积最小,且背包的总质量不能超过92kg。用矩阵P代表背包中物品的价值;
96
矩阵R代表背包中物品的体积;矩阵C代表物品的质量。P,R,C的取值如下:
@@ -17,9 +14,13 @@
1714
们可以用0, 1, 2, 3来表示具体选择哪件物品。因此染色体可以编码为一个元素属于{0, 1, 2, 3}的1x5Numpy ndarray一维数组,
1815
比如:[0,0,0,0,0]表示从这五类物品中各选取第一个物品。
1916
"""
17+
import numpy as np
18+
19+
import geatpy as ea
2020

2121

2222
class MyProblem(ea.Problem): # 继承Problem父类
23+
2324
def __init__(self, M=2):
2425
name = 'MyProblem' # 初始化name(函数名称,可以随意设置)
2526
maxormins = [-1, 1] # 初始化maxormins(目标最小最大化标记列表,1:最小化该目标;-1:最大化该目标)
@@ -30,20 +31,25 @@ def __init__(self, M=2):
3031
lbin = [1] * Dim # 决策变量下边界(0表示不包含该变量的下边界,1表示包含)
3132
ubin = [1] * Dim # 决策变量上边界(0表示不包含该变量的上边界,1表示包含)
3233
# 调用父类构造方法完成实例化
33-
ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin)
34+
ea.Problem.__init__(self,
35+
name,
36+
M,
37+
maxormins,
38+
Dim,
39+
varTypes,
40+
lb,
41+
ub,
42+
lbin,
43+
ubin)
3444
# 添加几个属性来存储P、R、C
35-
self.P = np.array([[3, 4, 9, 15, 2],
36-
[4, 6, 8, 10, 2.5],
37-
[5, 7, 10, 12, 3],
38-
[3, 5, 10, 10, 2]])
39-
self.R = np.array([[0.2, 0.3, 0.4, 0.6, 0.1],
40-
[0.25, 0.35, 0.38, 0.45, 0.15],
41-
[0.3, 0.37, 0.5, 0.5, 0.2],
45+
self.P = np.array([[3, 4, 9, 15, 2], [4, 6, 8, 10, 2.5],
46+
[5, 7, 10, 12, 3], [3, 5, 10, 10, 2]])
47+
self.R = np.array([[0.2, 0.3, 0.4, 0.6,
48+
0.1], [0.25, 0.35, 0.38, 0.45,
49+
0.15], [0.3, 0.37, 0.5, 0.5, 0.2],
4250
[0.3, 0.32, 0.45, 0.6, 0.2]])
43-
self.C = np.array([[10, 13, 24, 32, 4],
44-
[12, 15, 22, 26, 5.2],
45-
[14, 18, 25, 28, 6.8],
46-
[14, 14, 28, 32, 6.8]])
51+
self.C = np.array([[10, 13, 24, 32, 4], [12, 15, 22, 26, 5.2],
52+
[14, 18, 25, 28, 6.8], [14, 14, 28, 32, 6.8]])
4753

4854
def evalVars(self, Vars): # 目标函数
4955
x = Vars.astype(int)

0 commit comments

Comments
 (0)