# Simple implementation and results of genetic algorithm.

2022年11月23日
0 收藏 359 点赞 3,201 浏览 147235 个字

This experiment was done for the final assignment of my Professional English class.

This part has been written in haste, please forgive me.

#include<stdlib.h>
#include<iostream>
#include<time.h>
#include<Windows.h>
#define N 5//种群规模
using namespace std; int roulettewheelselection(double T[]); int main()
{
double C[][];
//double C1[3];//染色体1，以（x1,x2,x3,x4）形式存储
//double C2[3];//染色体2
//double C3[3];//···
//double C4[3];
//double C5[3];
srand((unsigned)time(NULL));//不同时间种子不同
for (int j = ; j < ; j++)
{
for (int i = ; i < ; i++)
{
C[j][i] = rand() / double(RAND_MAX);
//C1[i] = rand() / double(RAND_MAX);
//C2[i] = rand() / double(RAND_MAX);//0到1的随机值
//C3[i] = rand() / double(RAND_MAX);//定义<<0（x1,x2,x3,x4）<<1
//C4[i] = rand() / double(RAND_MAX);
//C5[i] = rand() / double(RAND_MAX);
}
}
cout << "初始化结果为：" << endl; for (int j = ; j < ; j++)
{
for (int i = ; i < ; i++)
{
cout << C[j][i] << endl;
/* cout << C1[i] << endl;
cout << C2[i] << endl;
cout << C3[i] << endl;
cout << C4[i] << endl;
cout << C5[i] << endl;*/
}
}
//评估
double f[];//五个染色体对应的函数值
double E[];//五个染色体对应的适应值
for (int j = ; j < ; j++)
{
f[j] = / (C[j + ][] * C[j + ][] + C[j + ][] * C[j + ][] + C[j + ][] * C[j + ][] + C[j + ][] * C[j + ][] + );
}
//f[0] = 1 / (C1[0] * C1[0] + C1[1] * C1[1] + C1[2] * C1[2] + C1[3] * C1[3] + 1);//染色体1的函数值
//f[1] = 1 / (C2[0] * C2[0] + C2[1] * C2[1] + C2[2] * C2[2] + C2[3] * C2[3] + 1);//染色体2函数值
//f[2] = 1 / (C3[0] * C3[0] + C3[1] * C3[1] + C3[2] * C3[2] + C3[3] * C3[3] + 1);//染色体3的函数值
//f[3] = 1 / (C4[0] * C4[0] + C4[1] * C4[1] + C4[2] * C4[2] + C4[3] * C4[3] + 1);
//f[4] = 1 / (C5[0] * C5[0] + C5[1] * C5[1] + C5[2] * C5[2] + C5[3] * C5[3] + 1);//染色体5的函数值
for (int i = ; i < ; i++)
{
E[i] = / f[i];
} double best = ;
for (int i = ; i < ; i++)
{
if (E[i] > best)//找best适应值最大的染色体
{
best = E[i];
}
}
cout << "best 为 " << i + << "染色体"<<endl;
//选择
/*
double E_sum;//适用值求和,总适应值
for (int i = 0; i < 4; i++)
{
E_sum = E_sum + E[i];
}
*/
/*
double E_ratio[4];//各染色体适应值与总适应值之比
for (int i = 0; i < 4; i++)
{
E_ratio[i] = E[i]/E_sum;
}
*/
//roulettewheelselection(E);
double D[][];
for (int j = ; j < ; j++)//选择五次,C赋给D，形成新种群
{
for (int i = ; i < ; i++)
{
D[roulettewheelselection(E)][i] = C[roulettewheelselection(E)][i];
}
}
//cout << "最终被选择进入种群的染色体是：" << << endl; //交配
double p = 0.88;//交配概率为0.88
srand((unsigned)time(NULL));//不同时间种子不同
int i = ;
double s[];
for(i=;i<;i++)
{
s[i] = rand() / double(RAND_MAX);//0到1的随机值s
int m = ;
if (s[i] <= 0.88)
{
m = m + ;
cout << "染色体" << i + << "参加交配" << endl;
} } system("pause");
return ;
} int roulettewheelselection(double T[])//轮盘赌算法实现（GA算法中）
{
//int N;//染色体数目 //http://www.cnblogs.com/guihailiuli/p/4154416.html
srand((unsigned)time(NULL));//不同时间种子不同
double s = rand() / double(RAND_MAX);//0到1的随机值s double m = ; //cout << "请输入染色体数目N(不能超过10哦）" << endl;
//cin >> N;
//double T[10];//每个染色体体的适应值
double P[];//计算出的概率，决定扇区大小
double t = ;//总适应值，为T[i]的和
//cout << "请输入每个染色体的适应值" << endl;
for (int i = ; i < N; i++)
{
////输入每个染色体的适应值
// cin >> T[i];
t = t + T[i];//求适应值之和
} for (int i = ; i < N; i++)
{
P[i] = T[i] / t;//计算扇区占比
} for (int i = ; i < N; i++)
{
m = m + P[i];
if (m >= s)
{
return i;
break;
}
} }
#include<stdlib.h>
#include<iostream>
#include<time.h>
#include<Windows.h>
using namespace std;
int main()
{
double D[][];
D[][] = 0.0182501
D[][] = 0.286416
D[][] = 0.730888
D[][] = 0.99588 D[][] = 0.524155
D[][] = 0.937254
D[][] = 0.120579
D[][] = 0.25721 D[][] = 0.423475
D[][] = 0.787622
D[][] = 0.832698
D[][] = 0.606769 D[][] = 0.0910672
D[][] = 0.114475
D[][] = 0.427229
D[][] = 0.404187
//交配位为1.2.
//交配
D[][] = 0.0182501
D[][] = 0.286416
D[][] = 0.120579
D[][] = 0.25721 D[][] = 0.524155
D[][] = 0.937254
D[][] = 0.730888
D[][] = 0.99588 D[][] = 0.423475
D[][] = 0.787622
D[][] = 0.427229
D[][] = 0.404187 D[][] = 0.0910672
D[][] = 0.114475
D[][] = 0.832698
D[][] = 0.606769
//染色体2保留
D[][] = 0.870174
D[][] = 0.346599
D[][] = 0.798273
D[][] = 0.762688
//变异
p = 0.1;//变异概率为0.1
srand((unsigned)time(NULL));//不同时间种子不同
double s[];
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
s[i][j] = rand() / double(RAND_MAX);//0到1的随机值s
int m = ;
if (s[i][j] < p)
{
D[i][j]= rand() / double(RAND_MAX);
//m = m + 1;
//cout << "染色体" << i + 1 << "参加交配" << endl;
}
}
}
cout<<"变异后的染色体值为："
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
cout << D[i][j] << endl;
}
}
//重新评价适应值
//评估
double f[];//五个染色体对应的函数值
double E[];//五个染色体对应的适应值
for (int j = ; j < ; j++)
{
f[j] = / (D[j + ][] * D[j + ][] + D[j + ][] * D[j + ][] + D[j + ][] * D[j + ][] + D[j + ][] * D[j + ][] + );
}
for (int i = ; i < ; i++)
{
E[i] = / f[i];
} double best = ;
for (int i = ; i < ; i++)
{
if (E[i] > best)//找best适应值最大的染色体
{
best = E[i];
}
}
cout << "best 为 " << i + << "染色体" << endl; system("pause");
return ;
}

python开发_常用的python模块及安装方法

Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接：http://www.codeforces.com/contest/660/problem/CDes…

zengkefu@server1:/usr/src\$ uname -aLinux server1 4.10.0-19-generic #21…

Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式，并且由于涉及到要把拍到的照片显…

Struts的使用