//Intermediate Crossover, Predictor Predictor Functions::intermediateCrossover(Predictor &pred1, Predictor &pred2){ scalingfactor = (float)rand()/RAND_MAX * (OFFSPRINGFACTOR*2 + 1) - OFFSPRINGFACTOR; float a = pred1.getA() + scalingfactor*(pred2.getA() - pred1.getA()); scalingfactor = (float)rand()/RAND_MAX * (OFFSPRINGFACTOR*2 + 1) - OFFSPRINGFACTOR; float b = pred1.getB() + scalingfactor*(pred2.getB() - pred1.getB()); scalingfactor = (float)rand()/RAND_MAX * (OFFSPRINGFACTOR*2 + 1) - OFFSPRINGFACTOR; float c = pred1.getC() + scalingfactor*(pred2.getC() - pred1.getC()); scalingfactor = (float)rand()/RAND_MAX * (OFFSPRINGFACTOR*2 + 1) - OFFSPRINGFACTOR; float d = pred1.getD() + scalingfactor*(pred2.getD() - pred1.getD()); return Predictor(a,b,c,d); }
//Real Valued Mutation, Predictor Predictor Functions::realValuedMutation(Predictor &predictor){ float s[2] = {-1, 1}; float u[2] = {0, 1}; float si = s[(int)rand()%2]; float ri = MUTATIONRANGE*10; float ai = pow(2,-u[(int)rand()%2]*MUTATIONPRECISION ); float a = predictor.getA() + (si * ri * ai); float b = predictor.getB() + (si * ri * ai); float c = predictor.getC() + (si * ri * ai); float d = predictor.getD() + (si * ri * ai); return Predictor(a,b,c,d); }
//Polynomial Function Fitness void Functions::fitnessPoly(Predictor &predictor){ float a = 1; float b = 2; float c = 3; float d = 4; float fitness = 0; for(int i = 0; i < SAMPLESIZE; i++){ float x = rand()%XRANGE - (XRANGE/2); //Choosing random x's as a sample float y1 = a*pow(x,3) + b*pow(x,2) + c*x + d; float y2 = predictor.getA()*pow(x,3) + predictor.getB()*pow(x,2) + predictor.getC()*x + predictor.getD(); fitness += pow(y2-y1,2); } fitness /= SAMPLESIZE; predictor.setFitness(fitness); }