/* * File: Evolved_Dipole.cpp * Author: Jordan Potter * * Created on July 14, 2017, 11:07 AM */ #include #include #include #include #include #include using namespace std; const int NVAR=2; //number of variables (r and l) const int NPOP=5; //size of population double rand_normal(double mean, double stddev) {//Box muller method to generate normal numbers static double n2 = 0.0; static int n2_cached = 0; if (!n2_cached) { double x, y, r; do { x = 2.0*rand()/RAND_MAX - 1; y = 2.0*rand()/RAND_MAX - 1; r = x*x + y*y; } while (r == 0.0 || r > 1.0); { double d = sqrt(-2.0*log(r)/r); double n1 = x*d; n2 = y*d; double result = n1*stddev + mean; n2_cached = 1; return result; } } else { n2_cached = 0; return n2*stddev + mean; } } void GenerateSample(double new_val[][NVAR], double mean_val[], double std_dev[],int n){ double u; for(int i=0;i= 50) //If there are 50 errors something is going wrong, so stop { cout << "Error: Sample Generation Failed" << endl; return; } } } void Simulation(double x[][NVAR], double y[NVAR], double mean[], double deviation[], double best[]){ //Write to CSV file. Might need to slightly adjust for generations after #1... //For now, this will only write the last generation to file ofstream handshake; handshake.open("handshake.csv"); handshake << "C++ ESTRA -- Jordan Potter" << "\n"; handshake << "Mean Vector:" << "\n"; //Write the Current Mean Values to handshake.csv for(int i=0;i=8) //read in the generation { istringstream stream(csvContent[i]); for(int j=0;j=2) { y[i-2] = atof(strToDbl2[i].c_str()); } } handshook.close(); } int Mutate(double x[][NVAR],double out[], double best[], double mean[]){ //check to see if the output from x[i][] is larger than the output from x[m]. //i.e. output[i]>mean_output int count=0; for(int i=0;ibest[0]) { best[0]=out[i]; for(int j=0;jp then d=d/q //if not then d=d*q double P = (double) numSuccess/NPOP; if(P >= p) { for(int i=0;i