#include<iostream>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<fstream>
using namespace std;
double minf(double x1,double x2){
return 4*x1*x1-2.1*pow(x1,4)+0.33*pow(x1,6)+x1*x2-4*x2*x2+4*pow(x2,4); //目标函数 [-50,50]
}
class Ant{
public:
Ant(){
x=0;
y=0;
f=0;
ms=0;
}
Ant(double a,double b){
x=a+0.5;
y=b+0.5;
}
double getX(){
return x;
}
double getY(){
return y;
}
double getF(){
return minf(x,y);
}
double getMS(){
return ms;
}
void setX(double a){
x=a;
}
void setY(double b){
y=b;
}
void setMS(double mnf,double dert){
ms=0.8*mnf+0.2*exp(-dert);
}
double prb[100]; //去往其他蚂蚁的概率
double nm[100]; //上述概率的分子
private:
double x;
double y;
double f;
double ms;
};
int MAX=99;
int MAX2=10;
double Q0=0.2; //全局随机搜索常数
double B=0.8; //信息素遗忘因子
double r=0.5; //向较优解靠近时坐标的比例系数
int main(){
ofstream fout;
fout.open("output.txt");
srand((unsigned)time(NULL));
//100,间距为10,共100只
int dstns=10;
int dsttl=100;
int numAnt=dsttl/dstns;
int numAllAnt=numAnt*numAnt;
Ant cd[100];
for(int i=0;i<numAnt;i++){
for(int j=0;j<numAnt;j++){
cd[j*numAnt+i].setX(-50+dstns*i-numAnt/2);
cd[j*numAnt+i].setY(-50+dstns*j-numAnt/2);
}
}
for(int cnt=0;cnt<numAllAnt;cnt++){
// cout<<cd[cnt].getX()<<" "<<cd[cnt].getY()<<endl;
// cout<<cd[cnt].getF()<<endl;
cd[cnt].setMS(0,cd[cnt].getF());
// cout<<cd[cnt].getMS()<<" ";
}
int times=50;
do{
//局部搜索
for(int c=0;c<numAllAnt;c++){
double xlcl=cd[c].getX()+dstns*0.1*(rand()%MAX2+1);
double ylcl=cd[c].getY()+dstns*0.1*(rand()%MAX2+1);
double fnew=minf(xlcl,ylcl);
if(fnew<cd[c].getF()){
cd[c].setX(xlcl);
cd[c].setY(ylcl);
}
double msTemp=cd[c].getMS();
cd[c].setMS(msTemp,cd[c].getF());
// cout<<cd[c].getX()<<" "<<cd[c].getY()<<endl;
// cout<<cd[c].getF()<<" "<<cd[c].getMS()<<endl;
}
//全局搜索
double avrF=0;
double sumF=0;
for(int i=0;i<numAllAnt;i++){
sumF+=cd[i].getF();
double sumDeno=0; //概率分母定义
for(int s=0;s<numAllAnt;s++){
if(cd[i].getMS()-cd[s].getMS()>0){ //自己信息素多,保留原地不动
cd[i].nm[s]=0;
continue;
}
cd[i].nm[s]=cd[s].getMS();
sumDeno+=cd[i].nm[s];
}
// cout<<sumDeno<<endl;
for(int m=0;m<numAllAnt;m++){
if(sumDeno==0){
cd[i].prb[m]=0;
continue;
}
cd[i].prb[m]=cd[i].nm[m]/sumDeno; //向更优方向选择的概率
// cout<<cd[i].prb[m]<<" ";
}
// cout<<endl;
}
for(int w=0;w<numAllAnt;w++){
// cout<<cd[0].prb[w]<<" "<<cd[1].prb[w]<<endl;;
}
avrF=sumF/100;
fout<<avrF<<endl;
for(int j=0;j<100;j++){
if(0.01*(rand()%MAX+1)<=Q0&&cd[j].getF()>avrF){ //全局随机搜索
cd[j].setX(rand()%dsttl-dsttl/2+0.01*(rand()%MAX+1)); //-5到5的随机数,两位小数
cd[j].setX(rand()%dsttl-dsttl/2+0.01*(rand()%MAX+1));
// cout<<j<<"~~"<<cd[j].getX()<<"~~~"<<cd[j].getY()<<endl;
double msTemp1=cd[j].getMS();
cd[j].setMS(msTemp1,cd[j].getF());
}
else{
double sumX=0;
double sumY=0;
for(int t=0;t<numAllAnt;t++){
sumX+=cd[j].prb[t]*cd[t].getX();
sumY+=cd[j].prb[t]*cd[t].getY();
}
// cout<<cd[j].getX()<<"~~"<<cd[j].getY()<<"~~"<<cd[j].getF()<<endl;
// cout<<sumX<<" "<<sumY<<" "<<minf(sumX,sumY)<<endl;
if(sumX!=0||sumY!=0){
double dump1,dump2;
dump1=cd[j].getX();
dump2=cd[j].getY();
cd[j].setX(sumX*r+(1-r)*dump1);
cd[j].setY(sumY*r+(1-r)*dump2);
}
// cout<<cd[j].getX()<<"~~"<<cd[j].getY()<<"~~"<<cd[j].getF()<<endl;
double msTemp2=cd[j].getMS();
cd[j].setMS(msTemp2,cd[j].getF());
}
}
double minRe;
for(int s=0;s<numAllAnt;s++){
if(minRe>cd[s].getF()){
minRe=cd[s].getF();
}
//fout<<cd[s].getX()<<" "<<cd[s].getY()<<" "<<endl;
// cout<<cd[s].getX()<<" "<<cd[s].getY()<<" ";
// cout<<cd[s].getF()<<" ";
}
// cout<<minRe<<endl;
times--;
//fout<<endl<<endl;
}while(times>0);
system("pause");
return 0;
}