Genetic Algorithm 遗传算法:
遗传算法受到达尔文自然选择理论的启发,以遗传/变异/择优的策略进行机器学习进程。
核心算法逻辑:
# Copyright:CMPUT 296, University of Alberta
# Pseudocode 伪代码
population = initialize populationSize random points
time = 0
while avg or best (F(population))<threshold and time<maxTime:
time++
MutatePopulation(population)
population += CrossoverPopulation(population)
population = Reduce(population, populationSize)
return best (F(population))
其内容一般包括:
(此处伪代码以2D游戏环境举例,参考地牢游戏设定,包括B=障碍物,P=全体玩家,K=钥匙,M=怪兽,L=门锁)
Fitness Func*(适配函数:检测空间设定有效性,非必须存在)
e.g. 游戏环境下是否有有效路径抵达出口,是否有玩家生成于此,是否符合环境设定等
# Pseudocode 伪代码
fitnessFunc(P, K, L, B, M) {
bool isValid = true;
if (P != null):
return 0;
for (p in P):
if (K or L in p.neighbors) or (L in K.neighbors):
isValid = false;
continue;
Apply A* algorithm to find the possible pathes (from p.position to K) & (from K to L), if fails:
isValid = false;
continue;
if (isValid):
return 1.0;
else return 0;
}
Mutation Func(突变函数:在原有点位的邻点或特定空间范围内移动)
e.g. 由P(x, y, z) 移动至P‘(x+1, y+1, z+1)
# Pseudocode 伪代码
mutationFunc(P) {
for (p in P):
for (neighborCell in neighborCells around p):
if (neighborCell is empty):
C.add(neighborCell);
mutation = random(C, 1);
p.position = mutation;
return P;
}```
CrossOver Func(继承函数/跨域函数:针对某两个或多个点位,使用曼哈顿距离或其他距离计算算法获取两点的中位,并在此位置放置新的Agent)
```python
# Pseudocode 伪代码
crossoverFunc(P) {
for (p1 in P):
for (p2 in P):
newX = min(p1.position.X, p2.position.X) + abs(p1.position.X - p2.position.X);
newY = min(p1.position.Y, p2.position.Y) + abs(p1.position.Y - p2.position.Y);
pNew = new player; // compute coordination & create player in next generations
pNew.position = (newX, newY);
if (pNew not in P):
P.add(pNew);
return P
}
算法优点:
- 可控性强,可以认为设定其中的遗传/突变策略以更好的获取优化;
- 具备贪婪算法(GreedySearch)和随机漫步(RandomWalk)的全部优势;
- 算法效率待补充;