# -*- coding: cp936 -*-
import numpy as np
import matplotlib.pyplot as plt
import math
import random
# 采用DE/best/1/bin的差分进化策略
# 测试函数
def object_function(x):
f = 0
for c in range(0, len(x)):
f = f+(-x[c]*math.sin(math.sqrt(abs(x[c]))))
return f
# 种群初始化
def initialization(zq):
np_list1 = []
for a in range(0, zq):
x_list = []
for j in range(0, len_x):
x_list.append(value_down_range + random.random() * (value_up_range - value_down_range))
np_list1.append(x_list)
return np_list1
# 列表相减
def subtraction(a_list, b_list):
a = len(a_list)
new_list = []
for c in range(0, a):
new_list.append(a_list[c] - b_list[c])
return new_list
# 列表相加
def addition(a_list, b_list):
a = len(a_list)
new_list = []
for c in range(0, a):
new_list.append(a_list[c] + b_list[c])
return new_list
# 列表的数乘
def multiplier(a, b_list):
b = len(b_list)
new_list = []
for c in range(0, b):
new_list.append(a * b_list[c])
return new_list
# 变异
def mutation(np_list1):
v_list1 = []
for gen in range(1, generation+1):
d = math.exp(1-generation/(generation+1-gen))
f = F0*math.pow(2, d)
for c in range(0, NP):
r1 = random.randint(0, NP - 1)
while r1 == c:
r1 = random.randint(0, NP - 1)
r2 = random.randint(0, NP - 1)
while r2 == r1 | r2 == c:
r2 = random.randint(0, NP - 1)
r3 = random.randint(0, NP - 1)
while r3 == r2 | r3 == r1 | r3 == c:
r3 = random.randint(0, NP - 1)
v_list1.append(addition(np_list1[r1], multiplier(f, subtraction(np_list1[r2], np_list1[r3]))))
return v_list1
# 交叉
def crossover(np_list1, v_list1):
u_list1 = []
for c in range(0, NP):
vv_list = []
for j in range(0, len_x):
if (random.random() <= CR) | (j == random.randint(0, len_x - 1)):
if (v_list[c][j] < value_down_range) | (v_list[c][j] > value_up_range):
v_list[c][j] = random.random() * (value_up_range - value_down_range) + value_down_range
vv_list.append(v_list1[c][j])
else:
if (np_list[c][j] < value_down_range) | (np_list[c][j] > value_up_range):
np_list[c][j] = random.random() * (value_up_range - value_down_range) + value_down_range
vv_list.append(np_list1[c][j])
u_list1.append(vv_list)
return u_list1
# 选择
def selection(u_list1, np_list1):
for c in range(0, NP):
if object_function(u_list1[c]) <= object_function(np_list1[c]):
np_list1[c] = u_list1[c]
else:
np_list1[c] = np_list1[c]
return np_list1
# 主函数
# 参数
NP = 150
F0 = 0.4
CR = 0.5*(1+random.uniform(0, 1))
generation = 5000
len_x = 30
value_up_range = 500
value_down_range = -500
np_list = initialization(NP)
min_x = []
min_f = []
xx = []
xx2 = []
for i in range(0, NP):
xx.append(object_function(np_list[i]))
min_f.append(min(xx))
min_x.append(np_list[xx.index(min(xx))])
for m in range(1, generation+1):
v_list = mutation(np_list)
u_list = crossover(np_list, v_list)
np_list = selection(u_list, np_list)
xx1 = []
for n in range(0, NP):
xx1.append(object_function(np_list[n]))
min_f.append(min(xx1))
min_x.append(np_list[xx1.index(min(xx1))])
min_ff1 = min(min_f)
xx2.append(min_ff1)
print('迭代次数:%d 当前最小值:%f' % (m, min_ff1))
# 输出
min_ff = min(min_f)
min_xx = min_x[min_f.index(min_ff)]
print('最小值点:%s' % min_xx)
print('测试函数的最小值:%f' % min_ff)
# 画图
x_label = np.arange(0, generation, 1)
y_label = xx2
plt.rcParams['axes.unicode_minus'] = False
plt.plot(x_label, y_label, color='blue')
plt.xlabel('迭代次数')
plt.ylabel('log10(f-fmin)')
plt.savefig(r'S:\pycharm ONE\test4\20.png')
plt.show()
没有合适的资源?快使用搜索试试~ 我知道了~
python--代码实现差分进化算法(DE)并对测试函数进行测试及其边界处理.rar
共1个文件
py:1个
需积分: 5 0 下载量 37 浏览量
2023-12-04
20:03:25
上传
评论
收藏 1KB RAR 举报
温馨提示
python--代码实现差分进化算法(DE)并对测试函数进行测试及其边界处理
资源推荐
资源详情
资源评论
收起资源包目录
python--代码实现差分进化算法(DE)并对测试函数进行测试及其边界处理.rar (1个子文件)
testfunc4.py 4KB
共 1 条
- 1
资源评论
温柔-的-女汉子
- 粉丝: 629
- 资源: 3933
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于 Java的班级管理系统课程设计
- 深入探索Suno AI:教程、元标签与案例分析.pdf
- 超市会员积分管理系统主要用于实现了企业管理数据统计等
- 基于 Java的班级管理系统
- MyBatis 动态 SQL:灵活而强大的查询构建器.pdf
- com.accordion.prettyo.apk
- 毕业设计:基于SSM的mysql-ssm软件bug管理系统(源码 + 数据库 + 说明文档)
- MTSQL8.0.35windows(64bit)-mysql-installer-community-8.0.35.0
- 人工智能引领音乐创作新时代之Suno AI
- Public-bicycle-usage-forecast-master.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功