人工智能遗传算法c++实现
人工智能遗传算法c++实现
#include <iostream.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#define PI 3.1415926
using namespace std;
class Software{ //软件类及其属性,即价格和运行时间
public :
float price; //价格
float time ; //时间
float adapt; //适应度范围0~1
};
double AverageRandom(double min,double max) { //产生在min与max间的随机数
int minInteger = (int)(min*10000);
int maxInteger = (int)(max*10000);
int randInteger = rand()*rand();
int diffInteger = maxInteger - minInteger;
int resultInteger = randInteger % diffInteger + minInteger;
return resultInteger/10000.0;
}
double Normal(double x,double miu,double sigma) { //概率密度函数
return 1.0/(sqrt(2*PI)*sigma) * exp(-1*(x-miu)*(x-miu)/(2*sigma*sigma));
}
double NormalRandom(double miu, double sigma,double min,double max){ //产生正态分布随机数
double x;
double dScope;
double y;
do {
x = AverageRandom(min,max);
y = Normal(x, miu, sigma);
dScope = AverageRandom(0, Normal(miu,miu,sigma));
}while( dScope > y);
return x;
}
void Init(int type, int n , vector<vector<Software> > &soft){//初始化,产生software样本组
Software temp_s;
vector<Software> temp_v;
for(int i = 0;i<n;i++){
cout<<"class "<<i+1<<endl;
for(int j=0;j<type;j++){
temp_s.price=(int)NormalRandom(0, 0.2,1,10000);
temp_s.time= (int)NormalRandom(0, 0.2,1,10);
temp_s.adapt=2/((temp_s.price/1000)+temp_s.time);
temp_v.push_back(temp_s);
cout<<j+1<<"("<<temp_s.price<<","<<temp_s.time<<","<<temp_s.adapt<<")"<<" ";
}
cout<<endl;
soft.push_back(temp_v);
temp_v.clear();
}
}
float Select(vector<vector<Software> > &soft){//选择
vector<vector<Software> > temp_soft(soft);
float share=0;
float one_share=0;
soft.clear();
for(int num=0;num<temp_soft.size();num++){
for(int type=0;type<temp_soft[num].size();type++){
share+=temp_soft[num][type].adapt;
}
}
for(num=0;num<temp_soft.size();num++){
for(int type=0;type<temp_soft[num].size();type++){
one_share+=temp_soft[num][type].adapt;
}
if(one_share>share/(2*temp_soft.size()))//如果小于两倍适应度平均值就淘汰
soft.push_back(temp_soft[num]);
one_share=0;
}
return share;
}
void Cross(int type,vector<vector<Software> > &soft){ //交叉
int dot=0;
float odds=0;
vector<Software>::iterator iter1,iter2;
for(int num=0;num<soft.size()-1;num++){
odds=rand()%100/100.0;
if(odds>0.6&&odds<0.95){//杂交率设置为0.6~0.95
dot=NormalRandom(0, 0.2,1,type);
iter1=soft[num].begin() + dot-1;
iter2=soft[num+1].begin()+dot-1;
while(iter1!=soft[num].end()){
swap(*iter1,*iter2);
iter1++;
iter2++;
}
}
}
}
void Mutation(int type,vector<vector<Software> > &soft){//变异
float odds=0;
int dot=0;
vector<Softwa
你可能喜欢
- 遗传算法应用实例
- 遗传算法解决TSP问题
- 智能优化算法
- 模拟退火算法
- 图像匹配算法研究
- 人工智能实验报告
- 遗传算法matlab代码
- 算法入门
- MATLAB.遗传算法和粒子群算法程序设计及实例应用11页
- 遗传算法及其应用实例5页
- 遗传算法在数据挖掘中的应用实例分析2页
- 遗传算法在数据挖掘中的应用实例分析3页
- Matlab遗传算法工具箱函数及应用实例2页
- matlab基本遗传算法应用实例2页
- 遗传算法解决TSP问题32页
- 遗传算法解决TSP问题的Matlab程序4页
- 遗传算法解决TSP问题,C++版(带注释)29页
- 遗传算法解决TSP问题的Matlab程序4页
- 遗传算法解决TSP问题8页
- 基于Matlab的遗传算法解决TSP问题的报告12页
- 智能优化算法的认识5页
- 智能优化算法的部分精华笔试试题10页
- 群智能理论及粒子群优化算法115页
- 智能优化算法_数学建模_王成章_人工神经网络_201166页
- 智能优化算法2页
- 智能优化算法29页
- 模拟退火算法10页
- Ch2 模拟退火算法17页
- 基于模拟退火算法的工件位置标定5页
- 单纯形-模拟退火算法3页
- 模拟退火算法机理研究6页
- 模拟退火算法的并行化策略研究2页
- 匹配-图像匹配中特征提取算法研究1页
- 遥感图像匹配算法比较研究-论文67页
- 一种用于图像匹配的演化算法研究4页
- 图像特征点提取及匹配算法研究论文49页
- 图像模板匹配快速算法研究63页
- 基于图像校正与灰度相关性的立体匹配算法研究4页
- AI人工智能实验报告7页
- 中南大学人工智能实验报告12页
- 人工智能实验报告(熟悉专家系统开发工具)4页
- 人工智能实验报告(装错信封问题)4页
- 人工智能实验报告(梵塔问题)3页
- 人工智能实验报告(Fibonacci序列)2页


