📄 main.cpp
字号:
#include <iostream>
#include <math.h>
#include <fstream>
#include <iomanip>
#include "group.h"
void main()
{
using namespace std;
int i,countnumber=0,ave1=0,ave2=0;
int generation_times;
double average=0,difference=0,sum_ave1=0,sum_ave2=0,sum_times=0,result1=0,result2=0;
Genericgroup group;
ofstream out1,out2;
out1.open("output.txt");
if (out1.fail())
{
cout<<"cannot open the file."<<endl;
exit(1);
}
out2.open("final.txt");
if (out2.fail())
{
cout<<"cannot open the file."<<endl;
exit(1);
}
out2.setf(ios::showpoint);
out2.setf(ios::fixed);
out2.precision(6);
out2<<"in order to avoid the local extreme value, we run the algorithm 100 times"<<endl;
out2<<"the average of the population is"<<endl;
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(6);
cout<<"in order to avoid the local extreme value, we run the algorithm 100 times"<<endl;
cout<<"please wait for the result...... "<<endl;
generation_times=(int)group.get_end_generation();
for(i=0;i<100;i++)
{
group.get_rand_initial();
do
{
group.binary_to_decimal();
group.get_fitness();
group.get_choice_result();
group.get_cross_result();
group.get_mutation_result();
group.binary_to_decimal();
difference=group.get_average()-average;
average=group.get_average();
countnumber=countnumber+1;
}
while(fabs(difference)>0.00005);
out1<<"the number of the step times:"<<countnumber<<endl;
group.order();
group.output(out1);
out1<<endl;
if (i%10==9)
{
out2<<setw(10)<<group.get_average()<<endl;
}
else
{
out2<<setw(10)<<group.get_average();
}
if (group.get_average()<8)
{
sum_ave1=sum_ave1+group.get_average();
ave1++;
}
else
{
sum_ave2=sum_ave2+group.get_average();
ave2++;
}
sum_times=sum_times+countnumber;
}
result1=sum_ave1/ave1;
if (ave2>0)
result2=sum_ave2/ave2;
else
result2=0;
cout<<"all the compute date result is saved in \"output.txt\" "<<endl;
cout<<"the final result is saved in \"final.txt\" "<<endl<<endl;
if (result1<7&&result2>8)
{
out2<<"there two max value point "<<endl;
out2<<"one is "<<result1<<endl;
out2<<"the value of the function is"<<result1+5*sin(5*result1)+7*cos(2*result1)<<endl;
out2<<"the other is "<<result2<<endl;
out2<<"the value of the function is"<<result2+5*sin(5*result2)+7*cos(2*result2)<<endl;
out2<<"the average of step times is "<<sum_times/100<<endl;
cout<<"there two similar max value point "<<endl;
cout<<"one is "<<result1<<endl;
cout<<"the value of the function is"<<result1+5*sin(5*result1)+7*cos(2*result1)<<endl;
cout<<"the other is "<<result2<<endl;
cout<<"the value of the function is"<<result2+5*sin(5*result2)+7*cos(2*result2)<<endl;
cout<<"the average of step times is "<<sum_times/100<<endl;
}
else
{
out2<<"there one max value point "<<endl;
out2<<"the max is "<<result1<<endl;
out2<<"the value of the function is"<<result1+5*sin(5*result1)+7*cos(2*result1)<<endl;
out2<<"the average of evolution generation is "<<sum_times/100<<endl;
cout<<"there one max value point "<<endl;
cout<<"the max is "<<result1<<endl;
cout<<"the value of the function is"<<result1+5*sin(5*result1)+7*cos(2*result1)<<endl;
cout<<"the average of evolution generation is "<<sum_times/100<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -