jeudi 16 juillet 2015

Segmentation fault when using uniform real distribution in C++11

I've been working in an Artificial Bee Colony implementation and for this I need to generate random values in some phases. I've implemented almost half of the code; when I need to generate some random values I get a segmentation fault. I've checked many times for my indexes used in for cycles but everything is fine. I've tried commenting the part where I calculate the last random values and then I do not get any errors. The last 3 lines of code are the ones producing the problem.

Here's the code:

void ABC_Algorithm(string filename,double (*function ) (int, vector<double> )){

ifstream myfile(filename);
int dim;
int colonySize;
int cmax;
double val;
int seed;
vector<double> ub;
vector<double> lb;
int i;
if(myfile.is_open()){
    myfile >> dim;
    myfile >> colonySize;
    myfile >> cmax;
    for(i=0;i<dim;i++){
        myfile >> val;
        lb.push_back(val);
    }
    for(i=0;i<dim;i++){
        myfile >> val;
        ub.push_back(val);
    }
    myfile >> seed;
}
myfile.close();
//##Algorithm Starts##

Food foodSource(colonySize/2);
//Initialize Food Source
foodSource.initializeFood(seed,dim,lb,ub); // <<<- Random seed initiated here
//Initialize their fitness
for(i=0;i<foodSource.size;i++){
    foodSource.Solutions[i].fitness=function(dim,foodSource.Solutions[i].X);
}
//foodSource.displaySolutions();
int employedBees = colonySize/2;
int onlookerBees = colonySize/2;

vector<double> newPositions;
int j,k;
double phi;
//Employed bees phase
for(i=0;i<employedBees;i++){
    j=randint(0,dim-1);
    phi=-1+randreal()*2;
    while(k==i) k=randint(0,colonySize/2);
    newPositions=foodSource.Solutions[i].X;
    newPositions[j]+=phi*(foodSource.Solutions[i].X[j]-foodSource.Solutions[k].X[j]);
    ABCSolution newSol(newPositions);
    newSol.checkBounds(lb,ub); // CHECK IF IT CAN BE CHANGED FOR ONLY 1 IF INSTEAD OF dim IF CALLS
    newSol.fitness=function(dim,newPositions);
    //cout << "New Sol fitness : " << newSol.fitness<< endl;
    if (newSol.fitness<foodSource.Solutions[i].fitness) {
        foodSource.Solutions[i]=newSol;
    }
    else foodSource.Solutions[i].trial++;
}
//foodSource.displaySolutions();
//Assign probabilities to each solution.
double fitness_sum;
for(i=0;i<foodSource.size;i++){
    fitness_sum+=foodSource.Solutions[i].fitness;
}
for(i=0;i<foodSource.size;i++){
    foodSource.Solutions[i].p=foodSource.Solutions[i].fitness/fitness_sum;
}
//foodSource.displaySolutions();
//Onlooker bees phase
// ERROR STARTS HERE ##############################################
default_random_engine generator;
uniform_real_distribution<double> distribution(0.0,1.0);
double anothername=distribution(generator);

For compiling I use g++ -std=c++11 -Wall ABCManager.h -o ABCManager

Any ideas on how to fix this? I've tried rebooting my PC but that didn't work either. This started to happen suddenly, I didn't edit anything else tho.




Aucun commentaire:

Enregistrer un commentaire