Question: long run time problem
1
gravatar for yealmor
2.2 years ago by
yealmor10
yealmor10 wrote:

Hi everyone!
I wrote an algorithm (based on Monte - Carlo algorithm - for who is familiar with).
The problem is that it runs days..
And I don't know if it supposed to run days or it has an error in it....

the algorithm get function and find the global minimum by go in random steps. the main call (in loops- that change the parameters) to the function with function that need to minimizing maximum of 10 million cycle,

Here is my algorithm, in c++:

#include <cmath>
#include <iostream>
#include <vector>
#include <ctime>
#include <fstream> 
using namespace std;


float addition (vector<float> v)
    //the function of the optimizition.
{
    float f=0;
    for(int i=0;i<v.size();i++)
        f+=v[i]*v[i];
    return f;
    /*
    for(int i=0;i<v.size();i++)
    sum(-x(i)新in(sqrt(abs(x(i)))))
    */
}



float* MonteCarlo (ofstream &fi,vector<float> x,float (*function)(vector<float>),float LO,float HI,float maxnChange=1000,float maxIter=10000,float rangeVar=5,float maxTmp=100,float chantmp=0.5,float minTmp=0,float numrisetmp=10)

    //input:function and vector of variables, and the minimum and maximum varbiels.
    //output:the value f in the minimum,
{
    float* re=new float[20];
    float f,min,fnext,littChan;
    float t;
    int nChange,numverbchange,iran;
    int numAccep=0;//number of times the next step acceptence .
    float j=0;
    float numt=0;
    vector<float> xMin;


    for(int i=0; i < x.size();i++) //started the first value of the prametrs of the function.
    {
        x[i]= LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));
    }
    f=function(x);
    min=f;
    fi<<"\n \n \n \n minimums: \n";
    fi<<min<<endl;
    xMin=x;
    numverbchange=rand()%(x.size()-1)+1;
    for(int r=0;r<numverbchange;r++)
    {
        iran=rand()%(x.size());
        littChan=(-rangeVar) + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(2*rangeVar)));
        while(( x[iran]+littChan<LO||x[iran]+littChan>HI) && littChan!=0)
            littChan=(-rangeVar) + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(2*rangeVar)));
        x[iran]+=littChan;
    }
    fnext=function(x);


    for(t=maxTmp;t>minTmp;t-=chantmp)
    {
        nChange=0;
        while(nChange<maxnChange && j<maxIter)
        {
            if(fnext<f)
            {
                numAccep++;
                f=fnext;
                if(min>f)
                {
                    min=f;
                    fi<<min<<endl;
                    xMin=x;
                    nChange=0;  //counter the number of the change in min to know exit from the while.
                }
                else
                    nChange++;
            }
            else
            {
                nChange++;
                if(exp(f-fnext/t)>((double) rand() / (RAND_MAX)))
                    f=fnext;
            }
            /*  for(int i=0; i < x.size();i++)
            x[i]= LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));*/
            numverbchange=rand()%(x.size()-1)+1;
            for(int r=0;r<numverbchange;r++)   //the change of the varbiels for new value to the function.
            {
                iran=rand()%(x.size());
                littChan=(-rangeVar) + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(2*rangeVar)));
                while( x[iran]+littChan<LO||x[iran]+littChan>HI)   //the new step need to be in the range
                    littChan=(-rangeVar) + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(2*rangeVar)));
                x[iran]+=littChan;
            }
            fnext=function(x);
            j++;
        }
        if (numt!=numrisetmp && t<=0.5)
        {
            numt++;
            t=maxTmp;
        }
    }

    //cout<<min<<endl<<numAccep/(float)j<<endl;
    //for(int i=0; i < x.size();i++)
    //  cout<<xMin[i]<<"  ";
    //cout<<endl;
    re[0]=min;
    re[1]=numAccep/(float)j;
    for(int b=0;b<10;b++)
        re[b+2]=xMin[b];
    //re[13]= maxnChange;
    //re[14]=maxTmp;
    //re[15]=chantmp;
    //re[16]=maxIter;
    //re[17]=rangeVar;
    //re[18]=j;
    //re[19]=numrisetmp;
    return re;

}




int main(){
    //vector<float> vec;
    //for (int i=0;i<10;i++)
    //  vec.push_back(0);
    //MonteCarlo(vec,addition,-5.12,5.12,1000,100000000,4);
    //return 0;

    ofstream fi("yaelmin.txt");
    if ( !fi) {
    cout << "File could not be opened.\n";
    } 
    ofstream fil("yaelres.txt");
    if ( !fil ) {
        cout << "File could not be opened.\n";
    } 

    float* ans=new float[20];
    float meanans[20];
    float minis[100];
    vector<float> vec;
    for (int i=0;i<10;i++)
        vec.push_back(0);
    float maxtmp,changecor;
    float stan=0;
    int k;
    for (int i=0;i<20;i++)
        meanans[i]=0;
    float j=0.5;
    for(float j=0.25;j<=0.5;j+=0.25)
    {
        for(k=0;k<3;k++)
        {
            if(k<3)
                maxtmp=100;
            else
                maxtmp=200;
            if(k==0 ||k==3)
                changecor=0.5;
            else if(k==1 ||k==4)
                changecor=2;
            else 
                changecor=0.1;
            for(int i=0;i<100;i++)
            {
                //ans=MonteCarlo(fi,vec,addition,-5.12,5.12,10,1000,changecor,maxtmp,j);
                ans=MonteCarlo(fi,vec,addition,-5.12,5.12,10000000,1000000000000,changecor,maxtmp,j);
                for(int j=0;j<20;j++)
                    meanans[j]+=ans[j];
                minis[i]=ans[0];
            }
            for(int j=0;j<20;j++)
                meanans[j]/=100;
            for(int j=0;j<100;j++)
                stan+=((minis[j]-meanans[0])*(minis[j]-meanans[0]));
            stan/=99;
            stan=sqrt(stan);
            /*
            ofstream fil("yaelmin.txt");
            if ( !fil ) {
            cout << "File could not be opened.\n";
            } */
            fil<<"\n   k="<<k<<'\n'; 
            fil<<"min       acceptance                      cordinatot    \n";
            for(int j=0;j<13;j++)
                fil<<meanans[j]<<"   ";
            fil<<"\n \n \n ";
            //fil.close();
        }
    }
    fil.close();
    fi.close();
    return 0;


}

If someone can help me solve this problem I really appreciate it.
Thanks!!

c++ algorithm monte-carlo • 649 views
ADD COMMENTlink written 2.2 years ago by yealmor10
1
gravatar for Pierre Lindenbaum
2.2 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum122k wrote:

not

float addition (vector<float> v)

but

float addition (vector<float>& v)
ADD COMMENTlink written 2.2 years ago by Pierre Lindenbaum122k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1254 users visited in the last hour