##——————————————————————————施工中————————————————————————————##

import random

import math

import numpy

#于2020.10.16日写,本程序为老师布置的遗传算法的练习#design by zqh#联系方式QQ962903415,博客园-https://www.cnblogs.com/zqh962903415

#——————————————参数部分————————————————#

mainui='''

design by zqh

遗传算法求解函数在实数域的最小值

选择函数的类型

1.一元函数

2.二元函数

3.更改种群规模和迭代世代数

种群规模默认15,世代数100

'''

firstui='''

design by zqh

请输入初代自变量的区间(请输入正数或者负数)

本区间选的离最优值越接近

得到最优值的速度越快越准确

'''

secondui='''

___请输入公式___

注意:自变量用i表示

注意:乘号用*表示,加号用+表示,减号用-表示

注意:sin函数用math.sin()表示,cos函数用math.cos()表示

注意:e用exp()表示

注意:ln(10)用math.log(10,math.e)表示

'''

secondui2='''

___请输入公式___

自变量用x和y表示

注意:乘号用*表示,加号用+表示,减号用-表示

注意:sin函数用math.sin()表示,cos函数用math.cos()表示

注意:e用math.exp()表示

注意:ln(10)用math.log(10,math.e)表示

'''

populationsize=14 #种群尺寸

popgeneration=100 #程序执行的世代数

basedig=[] #存放每代的最优良的那一个单体,用于制图

fitvalue=[] #存放每代的最优良的单体的适应度值,用于制图

#——————————————(产生种群)的模块————————————————#

def createPop(popsize):

ret=[]

for i in range(0,popsize):

num=round(random.uniform(int(sectionleft), int(sectionright)),3)

ret.append(num)

return ret

def createPop2(popsize):

ret=[]

for i in range(0,popsize):

num=round(random.uniform(int(xsectionleft), int(xsectionright)),3)

ret.append(num)

return ret

def createPop3(popsize):

ret=[]

for i in range(0,popsize):

num=round(random.uniform(int(ysectionleft), int(ysectionright)),3)

ret.append(num)

return ret

#——————————————交叉的模块————————————————#

def crossOver(population):

ret=[]

for i in range(0,len(population)):

target=0

x1=round(random.choice(population),3)

# while target!=1:

x2=round(random.choice(population),3)

# if x2!=x1:

# target=1

# if x2==x1:

# continue

ret.append(round((x1+x2)/2,3))

# print('随机选两个%s和%s,求和除以2后%s' %(x1,x2,round((x1+x2)/2,3)))

return ret

#——————————————变异的模块————————————————#

def mutation(population):

newpop=[]

expect=0

for i in population:

expect=expect+i

expect=round(expect/len(population),3)

# print('求得数学期望',expect)

for i1 in population:

vari=random.uniform(expect-3,expect+3)

# print('从(miu-3sigma),(miu+3sigma)取任意自变量',vari)

switch=random.choice(['a','b'])

if switch=='a':

nor_distru = (1 / pow(2 * math.pi, 1 / 2)) * math.exp(-1*pow((vari-expect),2)/2)

if switch=='b':

nor_distru =-1* (1 / pow(2 * math.pi, 1 / 2)) * math.exp(-1 * pow((vari - expect), 2) / 2)

# print('自变量带入高斯分布函数得到扰动',nor_distru)

count=i1+nor_distru

newpop.append(round(count,3))

# print('高斯扰动后的(变异)新种群',newpop)

return newpop

#——————————————自写的排序公式————————————————#

# def paixu(poplist):

# ret=[]

# while(poplist !=[]):

# minkey=poplist[0]

# for i in poplist:

# if minkey>i:

# minkey=i

# if minkey

# minkey=minkey

# ret.append(minkey)

# position=poplist.index(minkey)

# del poplist[position]

# return ret

#——————————————选择函数的公式————————————————#

def fx(poplist,gongshi):

ret = []

for i in poplist:

res = eval(gongshi)

ret.append(round(res,5))

return ret

def fx2(poplist,poplist2,gongshi):

xkey = []

ykey=[]

final=[]

ret=[]

for x in poplist:

for y in poplist2:

res = eval(gongshi)

xkey.append(x)

ykey.append(y)

final.append(round(res,5))

ret.append(xkey)

ret.append(ykey)

ret.append(final)

return ret

#——————————————适应度选择函数————————————————#

def popChoice(population):

dict1={}

ret1=[]

ret=[]

popfit=[]

unionpop=list(set(a).union(set(population)))

# print('新老种群并集',unionpop)

popfit=fx(unionpop,gongshi)

# print('将并集求适应度函数值',popfit)

for xp in range(0,len(unionpop)):

dict1.update({popfit[xp]:unionpop[xp]})

print(dict1)

popfit.sort()

unionpop =popfit

# print('将其从小到大排列',unionpop)

for i1 in range(len(population)):

ret1.append(unionpop[i1])

# print('只取前面popsize个',ret1)

for xp1 in ret1:

ret.append(dict1[xp1])

print('新种群',ret)

print('本代最优单体为%s,它的适应度为%s' %(ret[0],ret1[0]))

print('#------结果——————#')

print('最小值处的自变量为%s,取得fx的最小值为%s' % (ret[0],ret1[0]))

print('#------结果——————#')

fitvalue.append(ret1[0])

basedig.append(ret[0])

return ret

def popChoice2(population,population2):

dict1={}

xpop=[]

ypop=[]

ret1=[]

ret=[]

popfit=[]

unionpop=list(set(a).union(set(population)))

# print('新老种群并集',unionpop)

unionpop2=list(set(a1).union(set(population2)))

# print('新老种群并集',unionpop2)

popfit=fx2(unionpop,unionpop2,gongshi)

# print('将并集求适应度函数值',popfit[2])

for xp in range(0, len(popfit[2])):

dict1.update({popfit[2][xp]: [popfit[0][xp],popfit[1][xp]]})

print(dict1)

popfit=popfit[2]

popfit.sort()

unionpop =popfit

# print('将其适应度函数值从小到大排列',unionpop)

for i1 in range(len(population)):

ret1.append(unionpop[i1])

# print('只取前面popsize个',ret1)

for xp1 in ret1:

ret.append(dict1[xp1])

xpop.append(dict1[xp1][0])

ypop.append(dict1[xp1][1])

print('新种群',ret)

print('本代最优单体为%s,它的适应度为%s' %(ret[0],ret1[0]))

print('#------结果——————#')

print('最小值处的自变量为%s,取得fx的最小值为%s' % (ret[0],ret1[0]))

print('#------结果——————#')

ret.clear()

ret.append(xpop)

ret.append(ypop)

return ret

#——————————————函数主体————————————————#

while 1:

print(mainui)

xuhao=input('请输入操作序号并按下回车')

try:

if xuhao=='1':

print(firstui)

sectionleft=input('初代区间左端>>>>')

sectionright=input('初代区间右端>>>>')

print(secondui)

gongshi=input('>>>>')

start=createPop(populationsize)

print('初始种群', start)

a = start

for i in range(0,popgeneration):

b=crossOver(a)

print('交叉后的新种群',b)

c=mutation(b)

print('变异后的新种群',c)

d=popChoice(c)

a=d

if xuhao=='2':

print(firstui)

xsectionleft=input('x初代区间左端>>>>')

xsectionright=input('x初代区间右端>>>>')

ysectionleft=input('y初代区间左端>>>>')

ysectionright=input('y初代区间右端>>>>')

print(secondui2)

gongshi=input('>>>>')

start=createPop2(populationsize)

print('初始种群', start)

a = start

start1=createPop3(populationsize)

print('初始种群', start1)

a1= start1

for i in range(0,popgeneration):

c=mutation(a)

c1=mutation(a1)

print('变异后的新种群',c)

print('变异后的新种群',c1)

d=popChoice2(c,c1)

a=d[0]

a1=d[1]

if xuhao == '3':

tp1 = input('请输入种群尺寸') # 种群尺寸

tp2 = input('请输入世代数') # 程序执行的世代数

if tp1.isdigit()==False or tp2.isdigit()==False:

print('种群尺寸和世代数只能输入数字')

else:

populationsize =int(tp1) # 种群尺寸

popgeneration =int(tp2) # 程序执行的世代数

except Exception as wrong:

print('操作有误,程序重启')

# plt.plot(basedig,fitvalue)

# plt.ylabel('fitness value') #为y轴加注释

# plt.xlabel('best one') #为x轴加注释

# plt.show()

#于2020.10.16日写,本程序为老师布置的遗传算法的练习

# #design by zqh#联系方式QQ962903415,博客园-https://www.cnblogs.com/zqh962903415

遗传算法求二元函数极值怎么编码_遗传算法求解一元函数二元函数最值相关推荐

  1. 遗传算法求二元函数极值怎么编码_遗传算法求解二元函数极值源码

    网上看到了一个比较不错的讲解遗传算法的帖子,链接如下 http://blog.csdn.net/b2b160/article/details/4680853 但是却没有贴源代码,正好最近闲来无事,就尝 ...

  2. 遗传算法求二元函数极值怎么编码_使用遗传算法求二元函数的最小值

    二元函数为y=x1^2+x2^2,x∈[-5,5] NIND=121; %初始种群的个数(Number of individuals) NVAR=2; %一个染色体(个体)有多少基因 PRECI=20 ...

  3. 遗传算法求二元函数极值怎么编码_用遗传算法求复杂函数的极值点

    确定遗传 在元素个体,遗传得到的个体和变异个体中选取最好的30个个体(对应的函数值最大的30个个体)作为下一次迭代的父样本. from random import randint from numpy ...

  4. correl函数相关系数大小意义_在EXCEL中用“CORREL”函数求 两列的相关系数为什么是等于1?...

    展开全部 因为正负号表示的e5a48de588b662616964757a686964616f31333365646235是正相关还是负相关,数值表示的是相关程度. 一.相关表和相关图可反映两个变量之 ...

  5. python函数模块关键代码_从零开始学Python(六):函数,模块和类的使用

    从这章开始,我们不在使用sublime文本编译器进行编码了,主要是我们每次运行的使用还得去命令行执行pyhon ***.py命令查看结果,操作比较麻烦.推荐大家使用Pycharm(具体安装使用教程大家 ...

  6. python 函数作用于矩阵_图解NumPy:常用函数的内在机制

    原标题:图解NumPy:常用函数的内在机制 来源: 机器之心 支持大量多维数组和矩阵运算的 NumPy 软件库是许多机器学习开发者和研究者的必备工具,本文将通过直观易懂的图示解析常用的 NumPy 功 ...

  7. python中高阶函数和装饰器_三.Python高阶函数和装饰器

    1高阶函数 1.1 数学概念回顾下数学知识: y=f(x) 这是最开始接触的普通函数 y=g(f(x)) 这个就是我们接触到的高阶函数 在数学和计算机科学中,高阶函数至少应当是满足下面一个条件的函数: ...

  8. mysql 聚合函数内比较运算符_关于常用 MYSQL 聚合函数,其他函数 ,类型转换,运算符 总结...

    /* 关于MYSQL 聚合函数,其他函数 ,类型转换,运算符 总结,*/ -- 1 运算符优先级  /* 12.1.1. 操作符优先级 以下列表显示了操作符优先级的由低到高的顺序.排列在同一行的操作符 ...

  9. python函数的使用方法_百度资讯搜索_python函数的使用方法

    金生水起程序猿 2020年11月22日 11:23函数语法格式及调用参数:默认值.元组和字典可变参数的使用全局变量和局部变量作用域,局部变量如何升级为全局变量函数是可重复使用的,实现单一功能的代码块. ...

最新文章

  1. 马斯克:今年占全球发射质量65%,星舰5月或首次轨道试飞
  2. 数据科学家所需的大脑训练
  3. memcache分布式 [一致性hash算法] 的php实现
  4. 批处理之判断文件是否存在
  5. 【Linux网络】Linux Socket编程 TCP协议
  6. 正整数 n 所有可能的和式的组合
  7. 一用户使用LTC以168万美元的价格购入收藏界“圣杯“卡片
  8. SQLCE数据库的几点研究
  9. linux 防ddos攻击软件,linux系统下免费防DDOS CC攻击脚本,有效减轻服务器压力 【转】...
  10. 【OpenCV学习笔记】【错误处理】计算机缺失MSVCP120D.dll
  11. gzdeflate函数_php 压缩函数gzencode gzdeflate gzcompress
  12. Android毕业实践报告,毕业实习报告范文
  13. 【网易云信】直播推流SDK特性、系统架构、使用说明
  14. Java 接入 cachecloud 入门
  15. Redis 与 MySQL 数据一致性问题
  16. 解决:打开阿里云图片显示下载,而不是直接预览的问题
  17. 数据库在软件开发中的作用是什么?
  18. C++核心编程(中)
  19. 你和语言模型,谁的填空能力更强?
  20. mysql语句批量更新数据库_sql 更新批量数据库语句

热门文章

  1. 数据库ORA-03113排查
  2. HDU 1814 Peaceful Commission
  3. 在Ubuntu 7.04上安装Xfce 4.4.1
  4. 【python自动化办公01】word操作-新建文档
  5. 偏差、方差、贝叶斯误差
  6. postgresql介绍,安装,启动
  7. 【收藏】严重影响IDEA启动速度的插件
  8. cordova降低版本10.0.0为8.1.2
  9. helm uninstall命令的使用:卸载Release
  10. Collections.toArray方法使用的坑