资源简介
Python 粒子群和遗传解决函数最值,模拟退火和蚁群解决TSP。

代码片段和文件信息
import random
import math
import matplotlib.pyplot as plt
import numpy as np
pop_size = 30
chorm_length = 10
Max_value = 2
pc = 0.6
pm = 0.015
pop = []
pop_X = [] # 对应十进制数
pop_fit = []
bestvalue = []
meanvalue = []
Var = []
def encode():
for i in range(pop_size):
pop_i = []
for j in range(chorm_length):
pop_i.append(random.randint(0 1))
pop.append(pop_i)
def decode():
pop_X.clear()
for i in range(pop_size):
value = 0
for j in range(chorm_length):
value += pop[i][j]*(2**(chorm_length - 1 - j))
pop_X.append(value * Max_value / (2**chorm_length - 1))
def cal_fit():
decode()
pop_fit.clear()
fit_sum = 0
value = []
for i in range(pop_size):
f_value = pop_X[i] * math.sin(10 * 3.1415 * pop_X[i]) + 2
if f_value > 0.0:
pop_fit.append(f_value)
value.append(f_value)
else:
pop_fit.append(0.0)
value.append(0.0)
fit_sum += pop_fit[i]
for i in range(pop_size):
pop_fit[i] = pop_fit[i] / fit_sum
for i in range(1 pop_size):
pop_fit[i] += pop_fit[i - 1]
pop_fit[-1] = 1
return value # 返回初始版本的值
def selection():
random_selection = []
for i in range(pop_size):
random_selection.append(random.random())
random_selection.sort()
new = []
selection_id = 0
global pop
for i in range(pop_size):
while selection_id < pop_size and random_selection[selection_id] < pop_fit[i]:
new.append(pop[i])
selection_id += 1
pop = new
def cross():
for i in range(0 pop_size - 1 2):
if random.random() < pc:
point = random.randint(0 chorm_length - 1)
t1 = []
t2 = []
t1.extend(pop[i][0:point])
t1.extend(pop[i + 1][point:])
t2.extend(pop[i + 1][0:point])
t2.extend(pop[i][point:])
pop[i] = t1
pop[i+1] = t2
def abrupt():
for i in range(pop_size):
if random.random() < pm:
point = random.randint(0 chorm_length - 1)
if pop[i][point] == 0:
pop[i][point] = 1
else:
pop[i][point] = 0
encode()
N = 100
for step in range(N):
decode()
eachvalue = cal_fit()
meanvalue.append(sum(eachvalue)/pop_size)
bestvalue.append(max(eachvalue))
Var.append(np.var(eachvalue))
plt.scatter([step + 1] * pop_size eachvalue)
selection()
cross()
abrupt()
print(bestvalue)
plt.plot(range(1 N + 1) bestvalue color=‘c‘ label=‘Best ‘+str(bestvalue[-1]))
plt.plot(range(1 N + 1) meanvalue color=‘b‘ label=‘Mean ‘+str(meanvalue[-1]))
plt.plot(range(1 N + 1) Var color=‘r‘ label=‘Var ‘+str(Var[-1]))
# plt.title(‘X - Y‘)
# plt.xlabel(‘X‘)
# plt.ylabel(‘Y‘)
plt.legend()
plt.show()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-06-03 10:06 Algorithm\
文件 28666 2018-06-02 15:29 Algorithm\GA 100.png
文件 31569 2018-06-02 15:30 Algorithm\GA 1000.png
文件 3025 2018-06-03 10:03 Algorithm\GA.py
文件 2994 2018-06-03 09:21 Algorithm\模拟退火.py
文件 41369 2018-06-03 08:29 Algorithm\粒子群 当前最优 平均 方差.png
文件 28854 2018-06-02 23:33 Algorithm\粒子群.png
文件 3252 2018-06-03 09:48 Algorithm\粒子群.py
文件 92733 2018-06-02 15:33 Algorithm\蚁群 100.png
文件 100638 2018-06-02 15:39 Algorithm\蚁群 1000.png
文件 5528 2018-06-03 09:16 Algorithm\蚁群.py
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论