本文主要是介绍neat-python优化pygame中的自动驾驶小游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用遗传拓扑神经网络优化pygame中的自动驾驶小车游戏
在小车实验中,fitness = ∑ 小 车 行 驶 的 距 离 50 \displaystyle \sum^{}_{}{\frac{小车行驶的距离}{50}} ∑50小车行驶的距离,所以阈值是人为设置一个数,刚开始也不知道fitness最大阈值是多少,所以又添加了一个和异或实验一样可视化的程序,来观察best fitness的值是多少,并显示5个输入和2个输出之间的网络关系,在测试时,fitness最好时为34472.0,所以我们先设置fitness_threshold=30000,这样就有个终止条件了。但实验并未终止,那是因为小车并未撞毁,而且小车实验随机性,此时的fitness没有超过30000,所以实验没有终止,所以我们要重新设置终止参数,添加一个score得分,代码如下:
# 如果不给适当的奖励,就会出现小车原地打转的现象,甚至往回开的现象
remain_cars = 0
score = 0
for i, car in enumerate(cars):if car.get_alive():remain_cars += 1car.update(map)genomes[i][1].fitness += car.get_reward()score += car.get_reward()# 检查,汽车撞毁了,那游戏结束,30000/50=600
if remain_cars == 0 or score > 600:break
得出实验结果:
E:\360Anaconda\envs\tf2.0\python.exe "F:/代码Python/neat神经网络代码/NEAT-Python Car代码/修改的neat小车代码/PyCar修改后的代码.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html****** Running generation 0 ****** Population's average fitness: 4.25000 stdev: 4.50871
Best fitness: 23.40000 - size: (2, 10) - species 1 - id 17
Average adjusted fitness: 0.149
Mean genetic distance 1.144, standard deviation 0.340
Population of 30 members in 1 species:ID age size fitness adj fit stag==== === ==== ======= ======= ====1 0 30 23.4 0.149 0
Total extinctions: 0
Generation time: 1.647 sec****** Running generation 1 ****** Population's average fitness: 113.32000 stdev: 576.79149
Best fitness: 3219.30000 - size: (3, 11) - species 1 - id 31
Average adjusted fitness: 0.035
Mean genetic distance 1.289, standard deviation 0.291
Population of 30 members in 1 species:ID age size fitness adj fit stag==== === ==== ======= ======= ====1 1 30 3219.3 0.035 0
Total extinctions: 0
Generation time: 4.352 sec (3.000 average)****** Running generation 2 ****** Population's average fitness: 1927.28000 stdev: 7861.24063
Best fitness: 43659.00000 - size: (4, 12) - species 1 - id 80Best individual in generation 2 meets fitness threshold - complexity: (4, 12)Best genome:
Key: 80
Fitness: 43659.0
Nodes:0 DefaultNodeGene(key=0, bias=0.5386344117399804, response=1.0, activation=tanh, aggregation=sum)1 DefaultNodeGene(key=1, bias=-0.4649807152173959, response=1.0, activation=tanh, aggregation=sum)2 DefaultNodeGene(key=2, bias=-0.17145923289756496, response=1.0, activation=tanh, aggregation=sum)11 DefaultNodeGene(key=11, bias=0.33565302693397886, response=1.0, activation=tanh, aggregation=sum)
Connections:DefaultConnectionGene(key=(-5, 0), weight=-0.18069934178325028, enabled=True)DefaultConnectionGene(key=(-5, 1), weight=-0.6604964144503376, enabled=True)DefaultConnectionGene(key=(-4, 0), weight=-0.04597748367672962, enabled=True)DefaultConnectionGene(key=(-4, 1), weight=-1.803638751747535, enabled=True)DefaultConnectionGene(key=(-3, 0), weight=0.2598987084044912, enabled=False)DefaultConnectionGene(key=(-3, 1), weight=-0.17009144575558932, enabled=True)DefaultConnectionGene(key=(-3, 2), weight=0.7431377506706864, enabled=True)DefaultConnectionGene(key=(-2, 0), weight=-0.9161891473119879, enabled=True)DefaultConnectionGene(key=(-2, 1), weight=-0.4118068605888451, enabled=True)DefaultConnectionGene(key=(-1, 0), weight=0.3615420784002601, enabled=False)DefaultConnectionGene(key=(-1, 1), weight=1.428554041911235, enabled=True)DefaultConnectionGene(key=(-1, 11), weight=0.659116105137537, enabled=True)DefaultConnectionGene(key=(2, 0), weight=-2.1109902993904184, enabled=True)DefaultConnectionGene(key=(11, 0), weight=-0.4920288668587077, enabled=True)Process finished with exit code 0
连接图为:
在net中,如果是实线,表示为Enable,若为虚线,则为Disable;红线表示权重weight<=0,绿色表示weight>0,线的粗细和大小有关。
fitness图为:
可以看出经过2代就进化出了最优网络,小车也顺利的跑了好几圈后达到score,大于600,也就是fitness大于30000,实验就终止了,然后打印出这些图,实验不终止,结果无法输出。值得注意的是generations必须是整数,比如,如上图,在generations=1.75时,best fitness就已经大于30000了,但实验没有停止,它必须在下一个generations=2(整数),且fitness大于30000才终止。下面的另一次实验可以看出,如图
generations=2时,fitness是小于30000的,generations=3且fitness大于30000,实验终止,并输出结果。
所以代码和实验结果可以去我的码云中看
https://gitee.com/rengarwang/neat-python-self-drivering-car
有用请点个赞!!
本站所有文章均为原创,欢迎转载,请注明文章出处:https://blog.csdn.net/weixin_45092662。百度和各类采集站皆不可信,搜索请谨慎鉴别。技术类文章一般都有时效性,本人习惯不定期对自己的博文进行修正和更新,因此请访问出处以查看本文的最新版本。
这篇关于neat-python优化pygame中的自动驾驶小游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!