Python人工智能遗传算法示例解析

人工智能8—遗传算法实验 目录 一、遗传算法的原理 二、遗传算法的算法流程 三、代码实现及分析 1、城市个数改变,随机生成城市的坐标 2、种群个数改变 3、交叉概率改变 4、变异概率改变 四、代码附录 一、遗传算法的原理 遗传算法(GA)是一类借鉴生物界的进化规律(适者生存,优胜劣汰遗传机制)演化而来的随机化搜索方法,是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自... 阅读详情


在这里插入图片描述

一、实验目的

熟悉和掌握遗传算法的原理、流程和编码策略,并利用遗传求解函数优化问题,理解求解流程并测试主要参数对结果的影响。

二、实验原理

遗传算法的基本思想正是基于模仿生物界遗传学的遗传过程。它把问题的参数用基因代表,把问题的解用染色体代表(在计算机里用二进制码表示),从而得到一个由具有不同染色体的个体组成的群体。这个群体在问题特定的环境里生存竞争,适者有最好的机会生存和个体组成的群体。后代随机化地继承了父代的最好特征,并也在生存环境的控制支配下继续这一过程,群体的染色体都将逐渐适应环境,不断进化,最后收敛到一族最适应环境的类似个体,即得到问题最优的解。

三、实验条件

Python3,Anaconda3,PyCharm

四、实验内容

import matplotlib.pyplot as plt
import random
import math
#计算函数
def f(args):
    return f2(args)
def f1(args):
    return (3 - (math.sin(2\*args\[0\]))\*\*2 - (math.sin(2\*args\[1\]))\*\*2)
def f2(args):
    x = 1
    for i in range(len(args)):
        z = 0
        for j in range(5):
            z += (j+1) \* math.cos(((j+1)+1)\*args\[i\]+(j+1))
        x \*= z
    return x
#适应函数
def s(x):
    return s2(x)
def s1(x):
    return math.exp(-abs(x-1))
def s2(x):
    return math.exp(-abs(x+187))
# 计算2进制序列代表的数值
'''
解码并计算值
group 染色体
chrom\_length 染色体长度
max\_value, min\_value 上下限
div 分界点
'''
def b2d(b, chrom\_length, max\_value, min\_value, div):
    rwno = \[\]
    #因为染色体里面有多个变量,所以需要div来分割
    for i in range(len(div)):
        if i == 0:
            star = 0
            end = div\[i\]
        else:
            star = div\[i-1\] + 1
 
AI算力网络通信优化:基于遗传算法的智能路由选择实战 本文深入探讨AI算力网络中通信优化的关键技术,重点介绍基于遗传算法的智能路由选择方法。我们将从网络通信面临的挑战出发,详细解析遗传算法的工作原理,并通过Python实现一个完整的智能路由优化系统。文章包含实际测试数据和性能对比,帮助读者理解如何在实际算力网络中应用这一技术,实现20%-40%的通信效率提升。现代AI算力网络如同一个繁忙的交通系统,数据包就像川流不息的车辆。随着模型规模扩大(如GPT-3的1750亿参数),节点间通信开销已占总训练时间的30%-70%。带宽竞争:多个任务同时传输导致网络拥塞。 阅读详情

相关推荐

遗传算法图解_遗传算法图解指南

遗传算法图解In the previous post, We discussed what Artificial Intelligence (AI) in general. It would be less informative if we only know the information from the outside. So in this post, I will go into a ...

weixin_26632369的博客 4112

Python实现遗传算法求解n-queens问题(1)

Python实现遗传算法求解n-queens问题(1)

qq_43680030的博客 985

遗传算法在AI中的应用:10个经典案例解析

本文旨在用通俗易懂的语言,让读者理解遗传算法的核心逻辑,并通过10个真实案例看到它在AI中的具体应用场景。无论是想入门AI算法的学生,还是想优化业务问题的工程师,都能从中找到启发。本文将按“原理→案例”的逻辑展开:先通过“大自然的选秀节目”故事讲清遗传算法的选择、交叉、变异;再用10个经典案例(含代码示例)展示它在AI中的具体应用;最后总结未来趋势和思考题。组合优化(如路径规划、任务调度):解空间巨大,无法暴力枚举;参数调优(如神经网络、投资组合):多参数、非线性关系;设计问题。

AIGC应用创新大全的博客 1489

探索AI人工智能中遗传算法的进化奥秘

本文旨在用通俗易懂的方式解析遗传算法(Genetic Algorithm)的工作原理,揭示其与生物进化论的内在联系,并通过实际案例展示该算法在人工智能领域的应用价值。染色体(Chromosome):由基因组成的解决方案编码适应度函数(Fitness Function):评估方案优劣的评分标准交叉(Crossover):两个父代染色体交换基因片段变异(Mutation):随机改变染色体中的基因基因编码:将问题转化为可进化形式适者生存:通过评分机制筛选优秀方案遗传操作:交叉变异产生更优后代。

AI 领航者的博客 949

生物启发算法助力AI人工智能领域发展

本文旨在揭示生物进化机制与人工智能的深度联系,重点解析遗传算法、蚁群优化等典型生物启发算法的工作原理及其在AI领域的创新应用。涵盖从基础概念到产业实践的完整知识体系。表型空间:算法解的可能表现形式集合,如同生物的不同性状适应度函数:评估解决方案优劣的标准,类似自然选择压力基因编码:将问题参数转换为可操作的遗传信息表示核心概念回顾遗传算法模拟自然选择,通过基因操作实现解空间的智能搜索蚁群优化利用信息素正反馈机制,擅长解决组合优化问题粒子群算法基于群体经验共享,在连续空间优化中表现卓越。

AI云原生与云计算技术学院 798

遗传算法Python实战项目

在工程、科学、经济和管理等领域,优化问题普遍存在于我们的日常生活中。优化问题本质上是一种决策制定过程,旨在从所有可能的选项中找到最佳或最满意的解决方案。它可以是求解最大或最小值的问题,如资源分配、调度问题、设计工程问题等。这类问题通常包含大量的变量和复杂的约束条件,给传统优化方法带来挑战。优化问题的重要性在于,它直接关系到资源的利用效率、成本的控制、效益的最大化等方面。随着技术的发展和需求的增加,问题的规模和复杂度不断提高,更需要高效的优化算法来求解这些问题。

weixin_36303807的博客 1126

智能计算遗传算法实验报告

详细描述了使用遗传算法完成的实验报告,包含关键代码,以及结果截图

计算机专业人工智能遗传算法实验报告

这是完整的遗传算法的实验报告 有实验程序 实验内容 实验结果

人工智能遗传算法实验报告.pdf

人工智能遗传算法实验报告.pdf

新手入门:N-CMAPSS数据集解析与实战指南

本文详细解析了N-CMAPSS数据集的结构与应用,为新手提供航空发动机故障预测的实战指南。通过Python代码示例展示数据加载、探索和特征工程技巧,帮助读者快速掌握剩余寿命(RUL)预测的关键步骤,并分享常见问题解决方案与进阶学习路径。

weixin_29056701的博客 381

保姆级教程:手把手教你从NASA Earthdata下载ASTER L2地表温度数据(附shp文件拖拽技巧)

本文提供了一份详细的NASA Earthdata平台ASTER L2地表温度数据下载教程,涵盖账号注册、数据筛选、shp文件拖拽技巧及质量检查等关键步骤。特别适合遥感领域初学者,帮助快速掌握高效获取ASTER L2T数据的方法,适用于城市热岛效应和农业监测等研究。

weixin_30736301的博客 475

通信工程学习:什么是ASK振幅键控、FSK频移键控、PSK相移键控

ASK、FSK和PSK是三种基础数字调制技术。ASK通过改变载波振幅传输信息,实现简单但抗干扰弱;FSK利用频率变化传递信号,抗干扰较强但频谱利用率一般;PSK通过相位变化编码数据,抗干扰和频谱效率最优但同步要求高。三种技术各具特点,分别适用于不同通信场景:ASK用于低速光纤通信,FSK常见于无线广播,PSK则广泛应用于Wi-Fi、蓝牙等现代通信系统。

limengshi138392的博客 8741

等保: Postgresql配置ssl链接

等保: postgresql配置ssl链接完整方案和避坑指南

weixin_43557605的博客 4383

Jetson Nano 【1】入手开箱、烧系统

类似于半开箱哈哈 怀着小期待的心情,屁颠屁颠跑到村口领快递,拿到了这个香喷喷的Jetson Nano 开发板 开箱: 由于太激动了忘记拍点啥,于是在最后留了张全家福 板子的结构: 我觉得这个有必要记录一下,于是去官方文档拿了张图来:官方文档传送 1=SD card 的卡槽,位于第二层板子的背部,也就是大风扇屁股位置 2=40针扩展接头(还没开用) 3=USB的供电口,根据大佬测试(某哩...

椰子奶糖的博客 4198

Oracle EBS FA 创建成批增加执行时间长且报错

摘要 Oracle EBS系统在执行"创建成批增"请求时出现长时间运行并报错(ORA-00603和ORA-03114)。

陈显平的专栏-Oracle EBS 176

2026年VIVO嵌入式岗位高频面试题(含参考答案)

摘要:VIVO嵌入式岗位考核涵盖智能手机、IoT设备、芯片适配等方向,重点考察硬件底层交互、RTOS/Linux开发、C/C++功底与问题排查能力。题目按优先级排序,分硬件架构、RTOS、C/C++、数据结构、Linux实践、行业认知6大模块,共40道题。硬件部分涉及中断、ARM架构、加速技术、BSP开发等;RTOS部分区分硬/软实时系统;C/C++部分侧重内存管理、位操作;Linux部分考察常用命令;行业部分关注鸿蒙等系统理念。题目设计贴合实际工作场景,全面评估嵌入式开发能力。

Open 54

授时中心被攻击?Windows 11 电脑时间不准?手把手教您如何更换国内稳定高速的时间服务器(时间服务器整理)

近期授时中心被攻击,时间同步重要性凸显。Windows 11 默认服务器在国内常遇网络问题。本文教你轻松修改时间服务器,选择更稳定、快速的国内优质服务器,彻底解决电脑时间不准的烦恼,让你的系统时间永远精准如一!🚀

XiaoqiangClub的博客 1096

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

上一篇: 详解Python可视化神器Plotly,用简单代码,绘制出更好的图表。
下一篇: 深入浅出的理解Python类的使用;通过类的多个技术点,构建更加灵活和可维护的程序。
一秋的编程笔记
博客等级 码龄4年 1589粉丝 319原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值