Skip to content

Commit f89f60b

Browse files
committed
NEW
1 parent d726111 commit f89f60b

File tree

5 files changed

+292
-86
lines changed

5 files changed

+292
-86
lines changed

.idea/PythonnotJava.github.io.iml

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

.idea/modules.xml

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

.idea/vcs.xml

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

.idea/workspace.xml

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

extend/backup/遗传算法.md

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
# 遗传算法
2+
3+
## 核心步骤
4+
5+
- 初始化种群:首先,随机生成一个包含多个个体(解)的初始种群。每个个体都代表问题的一个可能解。
6+
- 适应度评估:对种群中的每个个体进行适应度评估,即计算每个个体的适应度分数,该分数表示个体对于问题的解决方案的优劣程度。
7+
- 选择:根据适应度分数选择个体,通常是根据适应度分数的高低进行概率性选择,以便更有可能选择到适应度高的个体。常用的选择方法包括轮盘赌选择、锦标赛选择等。
8+
- 交叉:从被选择的个体中选取一对(或多对)进行交叉操作,生成新的个体。交叉操作模拟了生物界的基因交换过程,可以通过不同的方式进行,如单点交叉、多点交叉、均匀交叉等。
9+
- 变异:对交叉后的个体进行变异操作,以保持种群的多样性。变异操作是随机改变个体染色体中的一些基因,以产生新的解。常见的变异操作包括基因翻转、基因位移、基因重组等。
10+
- 替换:用新生成的个体替换原来的个体,形成新一代种群。替换策略可以是全局替换(直接用新生成的个体替换原有种群)或局部替换(保留一部分原有个体,只替换其中一部分)。
11+
- 重复迭代:重复执行步骤 2 到步骤 6,直到达到停止条件(如达到最大迭代次数、找到满意的解等)为止。
12+
- 输出结果:在停止迭代后,输出最终的优化结果,通常是种群中适应度最高的个体所对应的解。
13+
14+
```mermaid
15+
graph TD
16+
A[初始化种群] --> B[适应度评估]
17+
B --> C[选择]
18+
C --> D[交叉]
19+
D --> E[变异]
20+
E --> F[替换]
21+
F --> G{满足停止条件?}
22+
G -->|是| H[输出结果]
23+
G -->|否| B
24+
```
25+
26+
## 案例代码
27+
28+
```python
29+
import numpy
30+
import math
31+
import random
32+
33+
# 确定一个目标函数, _max=True表示目标最大值,反之最小值
34+
def objectiveFunc(x : numpy.ndarray, _max : bool = True) -> numpy.ndarray: # _max表示目标最大值,反之最小值
35+
d = x * numpy.sin(2 * x) - 5 * x * numpy.cos(x * 2)
36+
return d if _max else -1 * d
37+
38+
# 基于均匀分布随机生成种群
39+
def genPop(size : int, start : float, end : float) -> numpy.ndarray:
40+
return numpy.random.uniform(start, end, size)
41+
42+
# 选择操作,根据method指定的策略来,默认是轮盘赌方式,可以拓展
43+
def select(pop : numpy.ndarray, size : int, method : str = '轮盘赌', _max : bool = True) -> numpy.ndarray:
44+
obj = objectiveFunc(pop, _max)
45+
match method:
46+
case '轮盘赌':
47+
minObj = numpy.abs(numpy.min(obj)) # 用于修正
48+
obj += minObj
49+
posible = obj / numpy.sum(obj)
50+
return numpy.random.choice(pop, size, p=posible, replace=False) # False无放回抽样
51+
case _:
52+
...
53+
54+
# 编码方式
55+
def encode(x: float, start: float, end: float, step: int = 100) -> str:
56+
"""
57+
把数据平分成step份,然后x落在哪部分,获取对应的二进制值
58+
:param x:
59+
:param start:
60+
:param end:
61+
:param step:
62+
:return:
63+
"""
64+
# 先确定二进制范围同时也规定了统一二进制长度,不够补全0
65+
range_bin = math.ceil(math.log2(step)) # 2^0 ~ 2^range_bin继续写
66+
67+
# 计算每个区间的长度
68+
interval_length = (end - start) / step
69+
70+
# 确定 x 属于哪个区间
71+
bin_index = min(int((x - start) / interval_length), step - 1)
72+
73+
# 将区间索引转换为二进制字符串
74+
bin_str = bin(bin_index)[2:].zfill(range_bin)
75+
76+
return bin_str
77+
78+
# 与编码方式对应的解码方式,有瑕疵,因为精度问题
79+
def decode(bin_str: str, start: float, end: float, step: int = 100) -> float:
80+
"""
81+
将二进制字符串解码为原始数值
82+
:param bin_str:
83+
:param start:
84+
:param end:
85+
:param step:
86+
:return:
87+
"""
88+
# 先确定二进制范围同时也规定了统一二进制长度
89+
range_bin = math.ceil(math.log2(step))
90+
91+
# 将二进制字符串转换为区间索引
92+
bin_index = int(bin_str, 2)
93+
94+
# 计算每个区间的长度
95+
interval_length = (end - start) / step
96+
97+
# 根据区间索引计算值
98+
decoded_value = start + bin_index * interval_length + interval_length / 2
99+
100+
if decoded_value < -4:
101+
decoded_value = -4
102+
elif decoded_value > 4:
103+
decoded_value = 4
104+
else:
105+
pass
106+
107+
return decoded_value
108+
109+
# 交叉策略
110+
# 随机从某点开始连续交叉,用random;交叉默认method方式是的第一个跟第二个交叉,第二个跟第三个交叉,以此类推;
111+
# 交叉的时候采用格外策略:每两个交叉的时候生成1~5个新数据用于填放到新种群,最后在新种群中调用选择选择算法
112+
# length也采用随机策略,可以不定个数交叉, range(1, 3)表示1、2、3个交叉的任意一种
113+
# 而且在生成1~5个新数据是有概率的{1 : 0.75, 2 : 0.2, 3 : 0.03, 4 : 0.015, 5 : 0.005}
114+
# 注意:起点+length一定不会超过编码长度
115+
# method指定交叉方式,可以另行拓展
116+
def cross(
117+
start: float, end: float, step: int, size: int,
118+
pop: numpy.ndarray, length: range, method: str = '1-2 2-3 ...', _max : bool = True
119+
) -> numpy.ndarray:
120+
newGensCodes = []
121+
# {1 : 0.75, 2 : 0.2, 3 : 0.03, 4 : 0.015, 5 : 0.005}
122+
lastGenCode = [encode(g, start, end, step) for g in pop]
123+
mexBinCodeLen: int = math.ceil(math.log2(step))
124+
for i in range(len(lastGenCode) - 1):
125+
p1 = list(lastGenCode[i])
126+
p2 = list(lastGenCode[1 + i])
127+
haveGenNumbers = numpy.random.choice(numpy.arange(1, 6), size=1, p=[.75, .2, .03, .015, .005])[0]
128+
# print(haveGenNumbers)
129+
for j in range(haveGenNumbers):
130+
random_location = numpy.random.randint(
131+
0, mexBinCodeLen - random.randint(length.start, length.stop + 1)
132+
)
133+
# print(f'random_location = {random_location}')
134+
# print(f'p1 = {p1}')
135+
# 交叉得到的两个父类另起变量名字,根据策略当孩子
136+
for k in range(0, random_location, 1):
137+
p1[k], p2[k] = p2[k], p1[k]
138+
# 二者选一入新生代
139+
p11 = ''.join(p1)
140+
p21 = ''.join(p2)
141+
newGensCodes.append(random.choice([p11, p21]))
142+
returnewGens = [decode(i, start, end, step) for i in newGensCodes]
143+
return select(numpy.array(returnewGens), size, _max=_max)
144+
145+
# 变异策略,这里更改随机位置的基因
146+
def mutations(pop : numpy.ndarray, start: float, end: float, step: int) -> numpy.ndarray:
147+
mexBinCodeLen : int = math.ceil(math.log2(step))
148+
lastGenCodeLists = [list(encode(g, start, end, step)) for g in pop]
149+
k = []
150+
g : list
151+
for g in lastGenCodeLists:
152+
for i in range(math.ceil(step / pop.size)): # 保证原种群大小不变
153+
_s = g[random.randint(0, mexBinCodeLen - 1)]
154+
s = g.copy()
155+
s[random.randint(0, mexBinCodeLen - 1)] = '0' if _s == '1' else '1'
156+
k.append(s)
157+
return numpy.array(
158+
[decode(d, start, end, step) for d in [''.join(g) for g in k]]
159+
)
160+
161+
162+
# 迭代100次试试(即模拟一个种群演变了100代)
163+
_genPop = genPop(1000, -4, 4)
164+
for i in range(100):
165+
_select = select(_genPop, 100, _max=True)
166+
_cross = cross(-4, 4, 1000, 100, _select, range(1, 3))
167+
_mutations = mutations(_cross, -4, 4, 1000)
168+
_genPop = _mutations
169+
print(_genPop)
170+
171+
172+
"""
173+
[-3.428 -3.428 -3.3 -3.428 -3.94 -1.38 -3.94 -1.38 -3.428 -2.404
174+
-3.364 -3.364 -3.428 -2.34 -3.364 0.732 -3.428 -3.364 -3.38 -3.372
175+
-3.212 -3.18 0.916 -3.436 -3.18 -2.156 -3.18 -3.18 -3.18 -3.18
176+
-3.052 -3.324 -3.196 -3.052 -3.052 -3.06 -3.052 -3.068 -3.068 -3.052
177+
-3.172 -3.172 -1.124 -3.188 0.924 -3.172 -3.172 -3.18 -3.684 -3.684
178+
-3.148 -3.02 -3.084 -2.988 -3.02 -0.972 -0.972 -3.02 -3.148 -3.276
179+
-3.444 -3.364 -3.38 -3.38 -3.38 0.716 -1.332 -3.38 -3.38 -3.38
180+
-3.284 -3.284 -3.284 -3.284 -3.284 -3.348 0.812 -3.284 -3.796 -3.284
181+
-3.14 -2.372 0.7 -3.396 -3.364 -3.396 -3.908 0.7 -3.396 -3.404
182+
-3.38 -3.26 -3.252 -3.236 0.844 0.844 -2.228 -3.252 -3.252 -3.252
183+
-3.236 -3.372 -3.244 -1.196 -3.244 -3.276 -3.244 -3.244 -3.756 -2.988
184+
-3.204 -3.204 -3.204 -3.172 -2.18 -3.204 -3.14 -1.156 -3.204 -3.204
185+
-2.996 -3.252 -3.252 -3.252 -3.38 -3.764 -3.252 -3.252 -3.764 -2.996
186+
-3.556 -0.996 -3.044 -3.044 -3.172 -3.044 -3.044 -3.044 -3.044 -3.052
187+
-3.476 -3.348 -3.86 -3.348 -3.348 -3.348 -3.348 -3.348 0.748 -1.3
188+
-3.06 -3.06 -3.044 -3.044 -3.068 -3.06 -3.06 -2.996 -3.092 -3.06
189+
-3.188 -2.164 -3.22 -3.188 -3.188 -3.188 -3.444 -3.188 -3.188 -3.188
190+
-3.908 -3.396 0.7 -2.372 -3.396 0.7 0.7 -3.14 -2.372 -3.404
191+
-3.236 -3.236 -3.236 -3.236 -2.212 -3.3 -3.236 -3.244 -3.236 -1.188
192+
-3.244 -3.244 -3.756 -3.244 -3.236 -3.244 -3.244 -3.244 -3.244 -3.244
193+
-3.308 -3.308 -3.052 -3.308 -3.308 -3.244 -3.308 -3.308 -3.308 -3.308
194+
-1.14 0.908 -3.188 -3.188 -3.188 -3.188 -3.124 -3.188 -3.188 -3.188
195+
-3.052 -3.3 -3.308 -3.308 -3.3 -3.308 -3.82 -3.308 -3.308 -3.308
196+
-2.724 -3.764 -3.764 -3.492 -3.492 -3.748 -3.236 -3.748 -3.748 -3.748
197+
-3.444 -3.316 -3.316 -1.396 -1.396 -3.428 -2.42 -3.38 -3.188 -3.444
198+
-2.028 -3.18 -3.052 -3.052 -3.564 -3.052 -3.052 1.044 -3.052 -3.052
199+
-3.204 -3.204 -3.204 -3.204 -3.204 -3.204 -2.18 -3.076 -3.204 -3.204
200+
-2.02 -3.044 -2.02 -3.076 -3.076 -3.044 -0.996 -3.044 -3.076 -3.044
201+
-3.108 -3.364 -2.34 -3.364 -3.38 -3.364 -3.876 -3.428 -3.364 -1.316
202+
-3.428 -3.46 -3.364 -3.428 -3.3 -3.428 -3.444 -3.428 -3.428 -3.3
203+
-3.244 -2.988 -3.244 -3.244 -3.244 -3.244 -3.244 0.852 -3.244 -3.244
204+
-3.364 -3.372 -2.348 -3.372 -3.372 -2.348 -2.348 -3.388 -3.372 -1.324
205+
-3.364 -3.364 -3.876 -3.876 -3.364 -3.364 0.732 -3.364 -3.372 -3.364
206+
-3.444 -1.268 -3.316 -3.316 -3.316 -3.316 -3.316 -3.252 -2.292 -3.316
207+
-3.412 -3.284 -3.284 -3.252 -3.412 0.812 0.812 -3.284 -2.26 -2.26
208+
-3.372 -3.436 -3.372 -3.116 -3.364 -3.404 -3.372 -3.372 -3.244 -3.372
209+
-3.26 0.844 -3.252 -3.252 0.844 -3.26 -3.252 -3.26 -3.252 -3.252
210+
1.036 -3.092 -3.06 -1.012 -3.188 -3.572 -3.06 -3.06 -3.06 -3.06
211+
-2.98 -2.212 -3.236 -3.236 -3.748 -3.268 -3.236 -3.3 -3.236 -3.244
212+
-3.068 -3.068 -3.068 -3.068 -3.068 -3.324 1.028 1.028 -3.052 -3.068
213+
-3.236 -3.236 -3.236 -2.98 -3.748 -3.236 -3.3 -3.236 -3.236 -2.98
214+
-3.244 -3.372 -3.884 -3.372 -3.884 -3.372 0.724 -3.372 -3.244 -3.372
215+
-3.06 -3.044 -2.02 -3.172 -3.044 -3.3 -3.556 -3.3 -3.044 -3.044
216+
-3.332 -3.268 -3.268 -3.268 -3.268 -3.268 -3.268 -3.276 -3.396 -3.268
217+
-3.316 -3.316 -3.828 -3.316 -3.316 -3.316 -3.316 -3.316 0.78 -3.316
218+
-3.268 -3.396 -3.396 -3.396 -3.364 -3.396 -3.412 -3.46 -1.348 -3.46
219+
-3.236 -3.364 -3.236 -3.748 -2.98 -3.236 -3.236 -3.748 -3.236 -3.236
220+
-3.284 -3.292 -3.284 -3.292 -1.244 -3.26 -3.276 -3.292 0.804 -3.292
221+
-3.3 -3.812 -3.3 -3.3 -3.3 -3.3 -3.3 0.796 -3.332 -3.428
222+
-3.5 -3.5 -2.988 -2.988 -3.244 -2.988 -2.988 -2.988 -2.988 -3.02
223+
-3.38 -3.252 -3.252 -3.252 -3.252 -1.204 -3.284 -3.252 -2.996 -3.284
224+
-3.348 -3.316 -3.348 -3.348 -3.348 -3.86 -1.3 -1.3 -3.316 -3.348
225+
-3.836 -3.812 -3.316 -1.78 -3.572 -3.828 -3.764 -3.316 -3.316 -2.804
226+
-3.188 -3.188 -3.444 0.652 -3.38 -2.42 -3.188 -3.316 -2.42 -3.316
227+
-3.316 -3.444 -3.316 -3.324 0.78 -3.3 -2.292 -3.3 -1.268 -3.252
228+
-3.292 0.804 -3.292 -1.244 -1.244 -3.804 -3.276 -3.036 -3.292 -3.292
229+
-3.012 -3.076 -3.028 -3.076 -3.14 -3.524 -2.98 -3.012 -3.012 -1.988
230+
-3.188 -3.124 0.908 0.908 -3.06 -3.188 -3.188 -3.7 -1.14 -3.188
231+
-3.316 -3.324 -3.252 -3.316 -3.316 -3.316 -3.316 -3.316 -3.324 -3.316
232+
-2.164 -3.188 -3.172 -3.444 -3.444 -3.444 -3.196 -3.188 -3.188 -2.164
233+
-1.268 -3.316 -3.316 -3.316 -3.3 -3.316 -1.268 -3.252 -3.316 -3.316
234+
-3.572 -1.012 -3.06 -3.044 -3.06 -3.044 -3.572 -3.316 -3.06 -3.06
235+
-3.428 -3.428 -3.428 -3.3 -3.364 0.668 -3.364 -3.428 -1.38 0.668
236+
-3.348 -3.348 -3.348 -3.348 -3.348 -3.284 -3.284 -3.348 -3.316 -3.284
237+
-1.22 -1.284 -1.22 -1.188 -1.348 -1.22 -1.188 -1.22 -1.188 -1.22
238+
-3.012 -3.012 -3.02 -3.012 -3.028 -3.012 -3.012 -3.012 -3.524 -3.012
239+
-3.316 -3.348 -3.348 -3.284 -3.348 -3.332 -3.348 -3.476 -3.092 -2.324
240+
-3.076 -3.076 -1.028 -3.084 -3.076 1.02 -2.052 -3.076 -3.076 -3.076
241+
-3.028 -3.028 -2.996 -3.028 -3.028 -3.54 -2.996 -2.004 -3.028 -0.98
242+
-3.444 -3.316 -3.316 -2.292 -3.316 -3.828 0.78 -2.292 -3.316 -3.348
243+
-3.188 -3.188 -3.188 -3.124 -3.188 -2.164 -3.196 -3.188 -3.22 -3.188
244+
-3.076 -3.044 -3.044 -3.044 -3.172 -3.06 -3.044 -3.044 -3.3 -3.556
245+
-3.044 -3.3 -3.3 -3.3 -3.3 -3.3 -3.3 -3.428 -3.3 0.796
246+
-3.188 -3.124 -3.7 -3.188 -2.164 -3.188 -3.7 -1.14 -3.188 -3.444
247+
-3.764 -3.38 -3.236 -3.252 -3.252 -3.316 -2.996 -3.252 -3.38 -3.236
248+
-3.308 -3.3 -3.3 -3.3 -3.3 -3.3 -3.308 -3.3 -3.3 -1.252
249+
-3.18 0.924 0.924 -3.428 -3.172 -3.172 -3.108 -2.148 -2.148 -3.172
250+
-3.252 -2.356 -3.38 -3.892 -3.252 0.716 -3.38 -3.38 -3.38 0.716
251+
-3.188 -3.38 -3.444 -3.956 -3.444 -3.452 -3.428 -3.444 -3.316 0.652
252+
-3.068 -3.068 -3.068 -3.068 1.028 -3.052 -3.068 -3.004 -3.068 -3.068
253+
-3.1 -1.02 -3.068 -3.068 -3.068 -2.044 -3.052 -3.068 -3.068 -3.58
254+
-2.98 -2.98 -3.492 -2.996 -1.956 -3.492 -2.98 -3.108 1.116 -3.044
255+
-3.236 -3.236 -2.212 -3.364 -3.364 -3.236 -3.748 -3.236 -3.748 -3.236
256+
-2.18 -2.18 -3.204 -2.18 -3.172 -3.204 -3.204 -3.204 -2.18 -1.156
257+
-3.324 -3.316 -3.316 -1.268 -3.444 -3.324 -3.828 -3.316 -3.316 -3.316
258+
-3.06 -3.06 -2.996 -3.06 -3.06 -3.092 -3.044 -3.092 -2.036 -3.092
259+
-3.044 0.796 -3.236 -3.3 -3.3 -1.252 -3.308 0.796 -3.3 -3.3
260+
-3.3 -3.3 -3.3 -3.044 -3.044 -3.3 -3.3 -3.3 -3.3 -3.308
261+
-3.236 -3.3 -3.244 -3.236 -3.236 -3.236 -3.252 -3.748 -3.236 -3.3
262+
-2.004 -3.028 -2.996 -3.028 -3.028 -3.028 -2.996 -3.028 -3.028 -3.028
263+
-3.028 -3.036 -2.996 1.068 -0.98 -3.028 -3.028 -2.996 -3.012 -3.156
264+
0.844 -2.228 -3.252 -3.316 -3.26 -3.764 -2.228 -3.252 -3.316 -3.252
265+
-3.092 -3.092 -3.092 -3.092 -3.092 -3.092 1.004 -3.348 -3.076 -2.068
266+
-3.444 -3.444 -3.444 -3.428 -3.444 -3.38 -3.444 -3.444 -3.452 -1.396
267+
-3.396 -3.46 -2.372 0.7 -3.396 -3.396 -3.412 -3.412 -3.268 -3.14
268+
-3.428 -3.364 -3.364 -2.34 -3.396 -3.364 -1.316 -3.364 -3.364 -3.364
269+
-3.044 -3.044 1.052 -3.3 -3.044 -3.044 -3.044 -3.044 -0.996 -3.044
270+
-3.332 -3.092 -3.348 -3.348 -3.348 -2.324 -3.316 -3.316 -3.348 -1.3
271+
-3.06 -2.996 -3.06 -3.572 -3.06 -3.06 -3.188 -2.036 -3.06 -3.188
272+
-2.988 0.852 -1.196 -3.756 -3.244 -3.244 -3.244 -3.244 -2.22 -2.988]
273+
"""
274+
```
275+
276+
## 可视化结果分布
277+
278+
```python
279+
import matplotlib.pyplot as plt
280+
plt.hist(_genPop, bins=30, color='skyblue', edgecolor='black')
281+
plt.xlabel('Value X')
282+
plt.ylabel('Frequency')
283+
plt.title('Population Distribution')
284+
plt.grid(True)
285+
plt.show()
286+
```
287+
288+
### 分布图
289+
290+
291+
292+
### 目标函数图像

0 commit comments

Comments
 (0)