void rnd_mutate_ind (individual *ind) { int pos,pos1,pos2,tmp, node_aux_pos, core_aux_pos; if (randomperc() <= opt_params.pmut){ if(randomperc() <= 0.5 || opt_params.nw->free_vcores == ind->length){ pos1 = rnd(0, ind->length - 1); pos2 = rnd(0, ind->length - 1); tmp = ind->bit_string[pos1]; ind->bit_string[pos1] = ind->bit_string[pos2]; ind->bit_string[pos2] = tmp; } else { pos2 = rnd(0, ind->length - 1); pos = rnd(0, opt_params.nw->total_vcores - 1); node_aux_pos = pos / opt_params.nw->cores_node; core_aux_pos = pos % opt_params.nw->cores_node; while(opt_params.nw->n[node_aux_pos].vcores[core_aux_pos] != -1 || contains(pos, ind->length, ind->bit_string) != -1){ pos = rnd(0, opt_params.nw->total_vcores - 1); node_aux_pos = pos / opt_params.nw->cores_node; core_aux_pos = pos % opt_params.nw->cores_node; } ind->bit_string[pos2] = pos; } } ind->f1 = eval_f1(ind); ind->f2 = eval_f2(ind); ind->valid = check_constrainsts(ind); }
/* random normal deviate after ACM algorithm 267 / Box-Muller Method */ double randomnormaldeviate() { double t, rndx1; if (rndcalcflag) { rndx1 = sqrt(-2.0 * log((double) randomperc())); t = 6.2831853072 * (double) randomperc(); rndx2 = sin(t); rndcalcflag = 0; return (rndx1 * cos(t)); } else { rndcalcflag = 1; return (rndx2); } }
void realcross (individual *parent1, individual *parent2, individual *child1, individual *child2) { int i; double rand; double y1, y2, yl, yu; double c1, c2; double alpha, beta, betaq; for (i=0; i<nreal; i++) { if (randomperc()<=0.5 ) { child1->xreal[i] = parent2->xreal[i]; child2->xreal[i] = parent1->xreal[i]; } else { child1->xreal[i] = parent1->xreal[i]; child2->xreal[i] = parent2->xreal[i]; } } #ifdef TEST for (i=0; i<nreal; i++) { printf("%d\t%d\t%d\t%d\n", child1->xreal[i], parent1->xreal[i], child2->xreal[i], parent2->xreal[i]); } scanf("%d",&i); #endif return; }
/* Function to cross two individuals */ void crossover (individual *parent1, individual *parent2, individual *child1, individual *child2) { int choice,i; if (nreal!=0) { if (randomperc() <= pcross_real){ nrealcross++; choice = rndint(0,2); switch(choice){ case 0: realcross (parent1, parent2, child1, child2); break; case 1: k_cross (parent1, parent2, child1, child2); break; case 2: fitness_cross (parent1, parent2, child1, child2); break; } } else { for (i=0; i<nreal; i++) { child1->xreal[i] = parent1->xreal[i]; child2->xreal[i] = parent2->xreal[i]; } } } if (nbin!=0) { bincross (parent1, parent2, child1, child2); } return; }
/* Flip a biased coin - true if heads */ int flip(float prob) { if (randomperc() <= prob) return (1); else return (0); }
/* Routine for binary tournament */ individual* tournament (NSGA2Type *nsga2Params, individual *ind1, individual *ind2) { int flag; flag = check_dominance (nsga2Params, ind1, ind2); if (flag==1) { return (ind1); } if (flag==-1) { return (ind2); } if (ind1->crowd_dist > ind2->crowd_dist) { return(ind1); } if (ind2->crowd_dist > ind1->crowd_dist) { return(ind2); } if ((randomperc()) <= 0.5) { return(ind1); } else { return(ind2); } }
int CCGDE3::flip(float prob){ if(randomperc() <= prob){ return(1); }else{ return(0); } }
/* Routine for binary mutation of an individual */ void bin_mutate_ind (individual *ind) { int j, k; double prob; for (j=0; j<nbin; j++) { for (k=0; k<nbits[j]; k++) { prob = randomperc(); if (prob <=pmut_bin) { if (ind->gene[j][k] == 0) { ind->gene[j][k] = 1; } else { ind->gene[j][k] = 0; } nbinmut+=1; } } } return; }
/* Function to initialize an individual randomly */ void initialize_ind (individual *ind) { int j, k; if (nreal!=0) { for (j=0; j<nreal; j++) { ind->xreal[j] = rndint (min_realvar[j], (max_realvar[j])); } } if (nbin!=0) { for (j=0; j<nbin; j++) { for (k=0; k<nbits[j]; k++) { if (randomperc() <= 0.5) { ind->gene[j][k] = 0; } else { ind->gene[j][k] = 1; } } } } return; }
int flip(float prob) // Tira una moneda, retorna True si es cara { if(randomperc() <= prob) return(1); else return(0); }//End flip
/* Fetch a single random integer between low and high including the bounds */ int CRand::rnd(int low, int high) { int res; if (low >= high) res = low; else { res = low + (int)(randomperc()*(high-low+1)); if (res > high) res = high; } return (res); }
void fitness_cross (individual *parent1, individual *parent2, individual *child1, individual *child2) { int i; double rand; double y1, y2, yl, yu; double c1, c2; double p,q; for (i=0; i<nreal; i++) { p = normalized_power[parent1->xreal[i]] / (normalized_power[parent1->xreal[i]] + normalized_power[parent2->xreal[i]]); q = normalized_frequency_coeff[parent1->xreal[i]] / (normalized_frequency_coeff[parent1->xreal[i]] + normalized_frequency_coeff[parent2->xreal[i]]); if (randomperc()<= p ) { child1->xreal[i] = parent1->xreal[i]; } else { child1->xreal[i] = parent2->xreal[i]; } if (randomperc()<= q ) { child2->xreal[i] = parent2->xreal[i]; } else { child2->xreal[i] = parent1->xreal[i]; } } #ifdef TEST for (i=0; i<nreal; i++) { printf("%d\t%d\t%d\t%d\n", child1->xreal[i], parent1->xreal[i], child2->xreal[i], parent2->xreal[i]); } scanf("%d",&i); #endif return; }
/* Routine for real polynomial mutation of an individual */ void real_mutate_ind (individual *ind) { int j; double rnd, delta1, delta2, mut_pow, deltaq; double y, yl, yu, val, xy; for (j=0; j<nreal; j++) { if (randomperc() <= pmut_real) { y = ind->xreal[j]; yl = min_realvar[j]; yu = max_realvar[j]; delta1 = (y-yl)/(yu-yl); delta2 = (yu-y)/(yu-yl); rnd = randomperc(); mut_pow = 1.0/(eta_m+1.0); if (rnd <= 0.5) { xy = 1.0-delta1; val = 2.0*rnd+(1.0-2.0*rnd)*(pow(xy,(eta_m+1.0))); deltaq = pow(val,mut_pow) - 1.0; } else { xy = 1.0-delta2; val = 2.0*(1.0-rnd)+2.0*(rnd-0.5)*(pow(xy,(eta_m+1.0))); deltaq = 1.0 - (pow(val,mut_pow)); } y = y + deltaq*(yu-yl); if (y<yl) y = yl; if (y>yu) y = yu; ind->xreal[j] = y; nrealmut+=1; } } return; }
int rnd(int low,int high) // Entrega un entero aleatorio entre low y high { int i; if(low >= high) i = low; else { i = (randomperc() * (high - low + 1)) + low; if(i > high) i = high; if(i < low) i = low; }//End else return(i); }//End rnd
individual_tPtr discreteReco(unsigned long lowerParentIndex, unsigned long upperParentIndex, unsigned long lowerIndex, unsigned long upperIndex, param_tPtr eps, population_tPtr pop, int gen, individual_tPtr offspring) { unsigned long S, T = lowerParentIndex; unsigned long i; if(lowerParentIndex > upperParentIndex) return(offspring); S = rnd(lowerParentIndex, upperParentIndex); if(LDS_REC == RecombinationScheme) T = rnd(lowerParentIndex, upperParentIndex); for(i = lowerIndex; i <= upperIndex; i++) { if(GDS_REC == RecombinationScheme) T = rnd(lowerParentIndex, upperParentIndex); if(gen != D) inAssignComponent(gen, i, ((randomperc() <= 0.5) ? inGetComponent(gen, i, poGetIndividual(S, pop)) : inGetComponent(gen, i, poGetIndividual(T, pop))), 0, offspring); else inAssignComponent(gen, i, 0.0, ((randomperc() <= 0.5) ? inGetDComponent(i, poGetIndividual(S, pop)) : inGetDComponent(i, poGetIndividual(T, pop))), offspring); } return(offspring); }
/* Pick a random integer between low and high */ int rnd(int low, int high) { int i; float randomperc(); if (low >= high) i = low; else { i = (randomperc() * (high - low + 1)) + low; if (i > high) i = high; } return (i); }
/* Routine for two point binary crossover */ void bincross (individual *parent1, individual *parent2, individual *child1, individual *child2) { int i, j; double rand; int temp, site1, site2; for (i=0; i<nbin; i++) { rand = randomperc(); if (rand <= pcross_bin) { nbincross++; site1 = rnd(0,nbits[i]-1); site2 = rnd(0,nbits[i]-1); if (site1 > site2) { temp = site1; site1 = site2; site2 = temp; } for (j=0; j<site1; j++) { child1->gene[i][j] = parent1->gene[i][j]; child2->gene[i][j] = parent2->gene[i][j]; } for (j=site1; j<site2; j++) { child1->gene[i][j] = parent2->gene[i][j]; child2->gene[i][j] = parent1->gene[i][j]; } for (j=site2; j<nbits[i]; j++) { child1->gene[i][j] = parent1->gene[i][j]; child2->gene[i][j] = parent2->gene[i][j]; } } else { for (j=0; j<nbits[i]; j++) { child1->gene[i][j] = parent1->gene[i][j]; child2->gene[i][j] = parent2->gene[i][j]; } } } return; }
int CCGDE3::rnd(int low, int high) { int res; if (low >= high) { res = low; } else { res = low + (randomperc()*(high-low+1)); if (res > high) { res = high; } } return (res); }
void OmniOptOptimizer::initialize_random_pop (OmniOptOptimizer::population *pop) { int i; for (i=0; i<popsize; i++) { int j, k; double rand; if (nreal!=0) { for (j=0; j<nreal; j++) { pop->ind[i].xreal[j] = rndreal (min_realvar[j], max_realvar[j]); } } if (nbin!=0) { for (i=0; i<popsize; i++) { for (j=0; j<nbin; j++) { pop->ind[i].xbin[j] = rndreal (min_binvar[j], max_binvar[j]); int l = floor(pop->ind[i].xbin[j]); int h = ceil(pop->ind[i].xbin[j]); if (h>max_binvar[j]) { h=max_binvar[j]; } else if (l < min_binvar[j]) { l= min_binvar[j]; } if (l!=h) { if (randomperc() < 0.5) { pop->ind[i].xbin[j] = l; } else { pop->ind[i].xbin[j] = h; } } else { pop->ind[i].xbin[j] = l; } } } } } return; }
/* real random number between specified limits */ float rndreal(float lo, float hi) { return ((randomperc() * (hi - lo)) + lo); }
/* Fetch a single random real number between low and high including the bounds */ double CRand::rndreal(double low, double high) { return (low + (high-low)*randomperc()); }
void topoaware_mutate_ind (individual *ind){ int core, dist, pos1, pos2, i, tmp, found, d, node_aux_pos, core_aux_pos; double rnddist; if (randomperc() <= opt_params.pmut){ if(randomperc() <= 0.5 || opt_params.nw->free_vcores == ind->length){ pos1 = rnd(0, ind->length - 1); pos2 = rnd(0, ind->length - 1); while(pos1==pos2){ pos2 = rnd(0, ind->length - 1); } tmp = ind->bit_string[pos1]; ind->bit_string[pos1] = ind->bit_string[pos2]; ind->bit_string[pos2] = tmp; } else{ pos2 = rnd(0, ind->length - 1); core = ind->bit_string[pos2]; rnddist = randomperc(); if(rnddist <= 0.5){ dist=2; } else if(rnddist <= 0.8){ dist=4; } else{ dist=6; } found=0; while(found==0){ for(i = 0;i < opt_params.nw->total_vcores;i++){ d = distance_network_cores(opt_params.nw, i, core); if(d == 0){ continue; } node_aux_pos = i / opt_params.nw->cores_node; core_aux_pos = i % opt_params.nw->cores_node; if(opt_params.nw->n[node_aux_pos].vcores[core_aux_pos] == -1 && d == dist && contains(i, ind->length, ind->bit_string) == -1){ found=1; break; } } pos2 = rnd(0, ind->length - 1); core = ind->bit_string[pos2]; rnddist = randomperc(); if(rnddist <= 0.5){ dist=2; } else if(rnddist <= 0.8){ dist=4; } else{ dist=6; } } ind->bit_string[pos2] = i; } } ind->f1 = eval_f1(ind); ind->f2 = eval_f2(ind); ind->valid = check_constrainsts(ind); }
void OmniOptOptimizer::initialize_latin_pop (OmniOptOptimizer::population *pop) { int i, j, k; int *a; int temp; int rand; double random; double *grid, *val; a = (int *)malloc(popsize*sizeof(int)); grid = (double *)malloc(nreal*sizeof(double)); val = (double *)malloc(popsize*sizeof(double)); if (nreal!=0) { for (j=0; j<nreal; j++) { grid[j] = (max_realvar[j]-min_realvar[j])/(double)popsize; } for (i=0; i<popsize; i++) { a[i] = i; } for (j=0; j<nreal; j++) { for (i=0; i<popsize; i++) { rand = rnd (i, popsize-1); temp = a[rand]; a[rand] = a[i]; a[i] = temp; } for (i=0; i<popsize; i++) { val[i] = rndreal(min_realvar[j]+grid[j]*i, min_realvar[j]+grid[j]*(i+1)); if (val[i] < min_realvar[j]) val[i] = min_realvar[j]; if (val[i] > max_realvar[j]) val[i] = max_realvar[j]; pop->ind[a[i]].xreal[j] = val[i]; } } if (nbin!=0) { for (i=0; i<popsize; i++) { for (j=0; j<nbin; j++) { pop->ind[i].xbin[j] = rndreal (min_binvar[j], max_binvar[j]); int l = floor(pop->ind[i].xbin[j]); int h = ceil(pop->ind[i].xbin[j]); if (h>max_binvar[j]) { h=max_binvar[j]; } else if (l < min_binvar[j]) { l= min_binvar[j]; } if (l!=h) { if (randomperc() < 0.5) { pop->ind[i].xbin[j] = l; } else { pop->ind[i].xbin[j] = h; } } else { pop->ind[i].xbin[j] = l; } } } } } free (a); free (grid); free (val); return; }