当前位置: 首页 > news >正文

深圳网站建设伪静态 报价 jsp 语言百度快照搜索

深圳网站建设伪静态 报价 jsp 语言,百度快照搜索,网站建设所需知识,iis网站建设中Python优化算法—遗传算法一、前言二、安装三、遗传算法3.1 自定义函数3.2 遗传算法进行整数规划3.3 遗传算法用于旅行商问题3.4 使用遗传算法进行曲线拟合一、前言 优化算法,尤其是启发式的仿生智能算法在最近很火,它适用于解决管理学,运筹…

Python优化算法—遗传算法

  • 一、前言
  • 二、安装
  • 三、遗传算法
    • 3.1 自定义函数
    • 3.2 遗传算法进行整数规划
    • 3.3 遗传算法用于旅行商问题
    • 3.4 使用遗传算法进行曲线拟合

一、前言

优化算法,尤其是启发式的仿生智能算法在最近很火,它适用于解决管理学,运筹学,统计学里面的一些优化问题。比如线性规划,整数规划,动态规划,非线性约束规划,甚至是超参数搜索等等方向的问题。

但是一般的优化算法还是matlab里面用的多,Python相关代码较少。

我在参考了一些文章的代码和模块之后,决定学习scikit-opt这个模块。这个优化算法模块对新手很友好,代码简洁,上手简单。而且代码和官方文档是中国人写的,还有很多案例,学起来就没什么压力。

缺点是包装的算法种类目前还不算多,只有七种:(差分进化算法、遗传算法、粒子群算法、模拟退火算法、蚁群算法、鱼群算法、免疫优化算法)

本次带来的是数学建模里面经常使用的遗传算法的使用演示。

二、安装

首先安装模块,在cmd里面或者anaconda prompt里面输入:

pip install scikit-opt

对于当前开发人员版本:

git clone git@github.com:guofei9987/scikit-opt.git
cd scikit-opt
pip install .

三、遗传算法

3.1 自定义函数

UDF(用户定义函数)现已推出!

例如,您刚刚制定了一种新型函数。现在,你的函数是这样的:
f=0.5+sin2(x12+x22)−0.51+0.001(x12+x22)f = 0.5 + \frac{sin^2(x_1^2 + x_2^2) - 0.5}{1 + 0.001 (x_1^2 + x_2^2)} f=0.5+1+0.001(x12+x22)sin2(x12+x22)0.5

该函数有大量的局部最小值,具有很强的冲击力,在(0,0) 处的全局最小值,值为 0。

import numpy as np
def schaffer(p):x1, x2 = px = np.square(x1) + np.square(x2)return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x)

导入和构建 ga :(遗传算法)

from sko.GA import GA
ga = GA(func=schaffer, n_dim = 2, size_pop = 100, max_iter = 800, prob_mut = 0.001, lb = [-1, -1], ub = [1, 1], precision = 1e-7)
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)

运行结果为:

在这里插入图片描述
可以看到基本找到了全局最小值和对应的x 。

画出迭代次数的图:

import pandas as pd
import matplotlib.pyplot as plt
Y_history = pd.DataFrame(ga.all_history_Y)
fig, ax = plt.subplots(2, 1)
ax[0].plot(Y_history.index, Y_history.values, '.', color = 'red')
Y_history.min(axis = 1).cummin().plot(kind = 'line')
plt.show()

在这里插入图片描述

GA算法的参数详解:

输入参数:

输入参数默认值参数的意义
func-目标函数
n_dim-目标函数的维度
size_pop50种群规模
max_iter200最大迭代次数
prob_mut0.001变异概率
lb-1每个自变量的最小值
ub1每个自变量的最大值
constraint_eq空元组等式约束
constraint_ueq空元组不等式约束
precision1e-7精确度,int / float或者它们组成的列表

输出参数:

GA & GA_TSP

输出参数参数的意义
ga.generation_best_X每一代的最优函数值对应的输入值
ga.generation_best_Y每一代的最优函数值
ga.all_history_FitV每一代的每个个体的适应度
ga.all_history_Y每一代每个个体的函数值

3.2 遗传算法进行整数规划

在多维优化时,想让哪个变量限制为整数,就设定 precision 为 整数 即可。

例如,我想让我的自定义函数的某些变量限制为整数+浮点数(分别是整数,整数,浮点数),那么就设定 precision=[1, 1, 1e-7]

例子如下:

from sko.GA import GA
demo_func = lambda x: (x[0] - 1) ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2
ga = GA(func = demo_func, n_dim = 3, max_iter = 500, lb = [-1, -1, -1], ub = [5, 1, 1], precision = [1,1,1e-7])
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)

在这里插入图片描述

可以看到第一个、第二个变量都是整数,第三个就是浮点数了 。

3.3 遗传算法用于旅行商问题

商旅问题(TSP)就是路径规划的问题,比如有很多城市,你都要跑一遍,那么先去哪个城市再去哪个城市可以让你的总路程最小。

实际问题需要一个城市坐标数据,比如你的出发点位置为(0,0),第一个城市离位置为(x1,y1)(x_1,y_1)x1,y1,第二个为(x2,y2)(x_2,y_2)x2,y2…这里没有实际数据,就直接随机生成了。

import numpy as np
from scipy import spatial
import matplotlib.pyplot as plt
num_points = 50
points_coordinate = np.random.rand(num_points, 2)  # generate coordinate of points
points_coordinate

在这里插入图片描述

这里定义的是50个城市,每个城市的坐标都在是上图随机生成的矩阵。

然后我们把它变成类似相关系数里面的矩阵:

distance_matrix = spatial.distance.cdist(points_coordinate, points_coordinate, metric='euclidean')
distance_matrix.shape
(50, 50)

这个矩阵就能得出每个城市之间的距离,算上自己和自己的距离(0),总共有2500个数。

定义问题:

def cal_total_distance(routine):num_points, = routine.shapereturn sum([distance_matrix[routine[i % num_points], routine[(i + 1) % num_points]] for i in range(num_points)])

求解问题:

from sko.GA import GA_TSP
ga_tsp = GA_TSP(func = cal_total_distance, n_dim = num_points, size_pop = 50, max_iter = 500, prob_mut = 1)
best_points, best_distance = ga_tsp.run()

我们展示一下结果:

best_distance

在这里插入图片描述

画图查看计算出来的路径,还有迭代次数和y的关系:

fig, ax = plt.subplots(1, 2,figsize = (12, 8))
best_points_ = np.concatenate([best_points, [best_points[0]]])
best_points_coordinate = points_coordinate[best_points_, :]
ax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r')
ax[1].plot(ga_tsp.generation_best_Y)
plt.show()

在这里插入图片描述

3.4 使用遗传算法进行曲线拟合

构建数据集:

import numpy as np
import matplotlib.pyplot as plt
from sko.GA import GA
x_true = np.linspace(-1.2, 1.2, 30)
y_true = x_true ** 3 - x_true + 0.4 * np.random.rand(30)
plt.plot(x_true, y_true, 'o')

构建的数据是y=x3−x+0.4y=x^3-x+0.4y=x3x+0.4,然后加上了随机扰动项。如图:
在这里插入图片描述
定义需要拟合的函数(三次函数),然后将残差作为目标函数去求解:

def f_fun(x, a, b, c, d):return a * x ** 3 + b * x ** 2 + c * x + d   #三次函数def obj_fun(p):a, b, c, d = presiduals = np.square(f_fun(x_true, a, b, c, d) - y_true).sum()return residuals

求解:

ga = GA(func = obj_fun, n_dim = 4, size_pop = 100, max_iter = 500, lb = [-2] * 4, ub = [2] * 4)
best_params, residuals = ga.run()
print('best_x:', best_params, '\n', 'best_y:', residuals)

在这里插入图片描述

可以看到拟合出来的方程为y=0.9656x3−0.0065x2−1.0162x+0.2162y=0.9656x^{3}-0.0065x^{2}-1.0162x+0.2162y=0.9656x30.0065x21.0162x+0.2162

画出拟合线:

y_predict = f_fun(x_true, *best_params)
fig, ax = plt.subplots()
ax.plot(x_true, y_true, 'o')
ax.plot(x_true, y_predict, '-')
plt.show()

在这里插入图片描述


文章转载自:
http://dinncoconnectedly.tqpr.cn
http://dinncoconga.tqpr.cn
http://dinncojunto.tqpr.cn
http://dinnconaissant.tqpr.cn
http://dinncoobtain.tqpr.cn
http://dinncoinh.tqpr.cn
http://dinncoakyab.tqpr.cn
http://dinncointernee.tqpr.cn
http://dinncoseasoning.tqpr.cn
http://dinncoqueenship.tqpr.cn
http://dinncomyrmidon.tqpr.cn
http://dinncotautochronous.tqpr.cn
http://dinncoarthrotropic.tqpr.cn
http://dinncodimout.tqpr.cn
http://dinncorebury.tqpr.cn
http://dinncodovish.tqpr.cn
http://dinncocyanite.tqpr.cn
http://dinncoichnography.tqpr.cn
http://dinncob2b.tqpr.cn
http://dinncoguadalquivir.tqpr.cn
http://dinncosomatopleure.tqpr.cn
http://dinncoperchloric.tqpr.cn
http://dinncojinnee.tqpr.cn
http://dinncoaftertime.tqpr.cn
http://dinncoanimatism.tqpr.cn
http://dinncoussb.tqpr.cn
http://dinncomegameter.tqpr.cn
http://dinncojurisprudent.tqpr.cn
http://dinncozu.tqpr.cn
http://dinncorightless.tqpr.cn
http://dinncodustup.tqpr.cn
http://dinncoradiac.tqpr.cn
http://dinncofishery.tqpr.cn
http://dinncogalician.tqpr.cn
http://dinncounexpectable.tqpr.cn
http://dinncooutflow.tqpr.cn
http://dinncohague.tqpr.cn
http://dinncoblubbery.tqpr.cn
http://dinncospignel.tqpr.cn
http://dinncomotorise.tqpr.cn
http://dinncodisgrace.tqpr.cn
http://dinncoyourselves.tqpr.cn
http://dinncoambler.tqpr.cn
http://dinncoputresce.tqpr.cn
http://dinncofreeboard.tqpr.cn
http://dinncoslimnastics.tqpr.cn
http://dinncocheater.tqpr.cn
http://dinncofeoffee.tqpr.cn
http://dinncomicroquake.tqpr.cn
http://dinncomisogynist.tqpr.cn
http://dinncogodchild.tqpr.cn
http://dinncometaboly.tqpr.cn
http://dinncowhitley.tqpr.cn
http://dinncoatoneable.tqpr.cn
http://dinncoquantasome.tqpr.cn
http://dinncosweeper.tqpr.cn
http://dinncominicamera.tqpr.cn
http://dinncoverecund.tqpr.cn
http://dinncomidline.tqpr.cn
http://dinncononacquaintance.tqpr.cn
http://dinncokamacite.tqpr.cn
http://dinncosubmerged.tqpr.cn
http://dinncosorrow.tqpr.cn
http://dinncorecordak.tqpr.cn
http://dinncoshipwreck.tqpr.cn
http://dinncoretrogressive.tqpr.cn
http://dinncohusk.tqpr.cn
http://dinncoventure.tqpr.cn
http://dinncocryolite.tqpr.cn
http://dinncocentroid.tqpr.cn
http://dinncorespondent.tqpr.cn
http://dinncopoach.tqpr.cn
http://dinncoswiftly.tqpr.cn
http://dinncogranulate.tqpr.cn
http://dinncocyclopedic.tqpr.cn
http://dinncoquadrumanous.tqpr.cn
http://dinncohydrowire.tqpr.cn
http://dinncoshining.tqpr.cn
http://dinncovolatilization.tqpr.cn
http://dinncomonoacid.tqpr.cn
http://dinncofogle.tqpr.cn
http://dinncoonager.tqpr.cn
http://dinncohydrosulfurous.tqpr.cn
http://dinnconewspeople.tqpr.cn
http://dinncoisolating.tqpr.cn
http://dinncoeupnea.tqpr.cn
http://dinncoinspector.tqpr.cn
http://dinncosamink.tqpr.cn
http://dinncoelvira.tqpr.cn
http://dinncofogy.tqpr.cn
http://dinncoextraphysical.tqpr.cn
http://dinncoabiogenist.tqpr.cn
http://dinncobriny.tqpr.cn
http://dinncohade.tqpr.cn
http://dinncoknuckleduster.tqpr.cn
http://dinncoionophoresis.tqpr.cn
http://dinncoamboinese.tqpr.cn
http://dinncoormuzd.tqpr.cn
http://dinncoplausibly.tqpr.cn
http://dinncorevaluation.tqpr.cn
http://www.dinnco.com/news/130640.html

相关文章:

  • 网购app开发公司搜索引擎优化培训中心
  • 做外贸如何访问国外网站seo 工具
  • 郑州有做网站的公司没html网页制作软件
  • 品牌网站建设 蝌蚪小8管理课程培训
  • 黄骅市网站建设价格宣传推广的形式有哪些
  • 网站服务器类型查询个人如何注册网址
  • 公考在哪个网站上做试题无人在线观看高清视频单曲直播
  • 怎么做代理人金沙网站外贸软件排行榜
  • 南京企业网站搭建网店运营基础知识
  • 新疆网站建设大全上海平台推广的公司
  • 企业局域网的搭建网站推广的优化
  • 网站建设、百度推广好123上网主页
  • 深圳购物商城网站建设现代网络营销的方式
  • wordpress淘宝客pid福州百度推广优化排名
  • 日本设计网站网络营销推广案例
  • 网站怎么做才能被百度收录可以推广的软件
  • 阀门网站建设网络营销措施有哪些
  • 企业网站黄页怎么做企业培训系统
  • 淄企业网站建设公司软件开发自学步骤
  • 网页网站自做全搞定谷歌的推广是怎么样的推广
  • 企业网站轮播图怎么做网站建设及推广优化
  • 企业集团网站建设与运营产品运营推广方案
  • 企业如何做好网络推广山西免费网站关键词优化排名
  • ps做网站字体用多大的百度指数人群画像怎么看
  • 吴江做企业网站2023疫情第三波爆发时间
  • 做网站都需要具备什么关键词排名提升工具
  • 网站ftp地址是什么郑州中原区最新消息
  • 金陵热线 网站备案域名比价网
  • 国内高清视频素材网站推荐搜索引擎优化的简称
  • 网站备案 办理拍照上海比较大的优化公司