⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deterministic.cpp

📁 Simulated Annealing for both Stochastic models and Deterministic models
💻 CPP
字号:
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <cmath>
#include <fstream>
#include <stdio.h>
#include <time.h>
#define PI  3.1415926/180
#define PAI  3.1415926


using namespace std;


double ObjectFunction( double x, double y )
{

            double z = 0.0;

             //z=sin(PI*x)*sin(PI*x)+cos(PI*y)*cos(PI*y); //F1 [-5,5]
            // z = sin(PI*x*y)+ x*x +y*y;//F2
             z = sin(PI*4*x)*y+cos(PI*4*y)*x; //F3
			// z= (x*sin(x/PAI)+y*cos(y/PAI))*PAI*0.3/360; //F4 [0,82]
			//z = PAI* x * 0.5 * cos(((y*y)/(PAI*PAI)+ 2*(x*x)/(PAI*PAI)))/(1+(y*y)/(PAI*PAI)+ 2*(x*x)/(PAI*PAI)) ;//F5  [0,45]
            
            return z;
}	


	  
double randf() 
{ 
            return (double)(rand()/(double)RAND_MAX); 
}  



void main()

{       
	    int      InLoop = 0; 

		int      OutLoop = 0;

		double   TemperaturePause;

        int      MarkovLength;               // iteration

        double   Temperature;               // the initial temperature

        double   LastTemperature = 0.0;

	    double   Keep;


        double   DecayScale = 0.95;               // decay parameter
			  
	    double   StepFactor;               //strategy for neighborhood


 
             

        const double XMAX = 5;                          // define the space of X and Y

        const double YMAX = 5;  


        double   PreX,NextX;                // prior and next value of x 

        double   PreY,NextY;                // prior and next value of y 
			 
        double   PreBestX,PreBestY;           // the previous optimun value       

        double   BestX,BestY;               // the optimun value

        int   AcceptPoints = 0.0;         // #.of points during the Metropolis



        double randf() ;
        double ObjectFunction( double x, double y );
	
              
		// pick the initial point randomly, initialization

        srand((unsigned int)time((time_t *)NULL));


              PreX = XMAX * randf();

              PreY = YMAX * randf();

              PreBestX = BestX = PreX;

              PreBestY = BestY = PreY;


         // outside loop, change the temperature 

			   cout<<"----------------------Just Run [Research]---------------------"<<endl;
			   
			   cout<<"The Initial Temperature ="; cin>>Temperature; 
			   
			   cout<<"The Inside Loop ="; cin>>MarkovLength;  
			  
			   cout<<"The Pause-Temperature ="; cin>>TemperaturePause;  
                            
               cout<<"StepFactor="; cin>>StepFactor; cout<<endl<<endl;
			   
			   
			   ofstream SaveFile1("01.txt",ios_base::out);

			   SaveFile1<<"The Initial Temperature ="<<Temperature<<";   The Pause-Temperature ="<<TemperaturePause<<";   StepFactor ="<<StepFactor<<endl;
               SaveFile1<<"DecayScale = 0.95; The Inside Loop ="<<MarkovLength<<endl<<endl; 

			   while ( Temperature > TemperaturePause)
			   {
                   
                   // inside loop, at the current temperature

                   for (int i=0;i<MarkovLength;i++)

                   {

                       // neighborhood

                       do

                       {

                            NextX = PreX - StepFactor + randf() * 2 * StepFactor; // TEST here for XMAX

                            NextY = PreY - StepFactor + randf() * 2 * StepFactor;

                       }
                        while ( !(NextX >= -XMAX  && NextX <= XMAX && NextY >= -YMAX && NextY <= YMAX) ); //bound
                     
                  

                       if( ObjectFunction(PreX,PreY) - ObjectFunction(NextX,NextY) > 0 )

                       {

      
						    PreX=NextX;

                            PreY=NextY;

                            AcceptPoints++;

							//SaveFile1<<ObjectFunction (PreX,PreY)<<endl;  
							

                       }


                        else

					{ 

                            double change = -1 * ( ObjectFunction(NextX,NextY) - ObjectFunction(PreX,PreY) ) / Temperature ;

                            Keep = randf();

							if( exp(change) > Keep ) // exp > random value 0~1

                            {    
	
								 PreX=NextX;

                                 PreY=NextY;

                                 AcceptPoints++; //accept 
						
							//	SaveFile1<<ObjectFunction (PreX,PreY)<<endl;  
						
                           }

                      }
             
                        SaveFile1<<ObjectFunction (PreX,PreY)<<endl;  

						
						if (ObjectFunction(BestX,BestY) > ObjectFunction(NextX,NextY))  // check the current best value, compared to the PreBest X,Y

					{   // keep the original BEST value

                            PreBestX = BestX;

                            PreBestY = BestY;

                            // the new BEST value 

                            BestX = NextX;

                            BestY = NextY;
                       }
                  
				  
				  InLoop++;
				   }

				  
				   LastTemperature = Temperature;
                   Temperature = Temperature * DecayScale;
		
                   OutLoop++;
              
			  }

			      
			  SaveFile1<<"-------------------"<<endl;

              SaveFile1<<BestX<<"       "<<BestY<<"       "<<ObjectFunction (BestX,BestY)<<"                 "<<endl;
			
			  SaveFile1.close();
	
                  
			      cout<<"-------------------------Final Result-----------------------"<<endl;
                  cout<<"The Best value of X is  "<<BestX<<endl;
				  cout<<"The Best value of Y is  "<<BestY<<endl;
				  cout<<"The Best value of Object Function Z is  "<<ObjectFunction (BestX,BestY)<<endl;
				  cout<<"The current Temperature is  "<< LastTemperature<<endl;
				  cout<<"The number of the accept points are  "<< AcceptPoints<<endl<<endl<<endl;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -