关键词不能为空

当前您在: 主页 > 英语 >

文化算法源代码

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-09 23:18
tags:

-

2021年2月9日发(作者:杜尔凯姆)



/* Funciones para el manejo de n



eros aleatorios, de genetic.c





Sin modificar, salvo randomize para recibir la semilla desde





la invocaci



. */



int jrand;


double oldrand[55];


double rndx2;


int rndcalcflag;


void warmup_random(float);


float rndreal(float, float);


int rnd(int, int);


float randomperc(void);


double randomnormaldeviate(void);


void randomizef(double);


double noise(double,double);


void initrandomnormaldeviate(void);


int flip(float);


void advance_random(void);



/* Flip a biased coin - true if heads */



int flip(float prob)



{






if(randomperc() <= prob)



return(1);






else



return(0);



}



/* Fetch a single random number between 0.0 and 1.0 - Subtractive Method */


/* See Knuth, D. (1969), v. 2 for details */


/* name changed from random() to avoid library conflicts on some machines*/



float randomperc()



{






jrand++;







if(jrand >= 55)






{



jrand = 1;



advance_random();






}







return((float) oldrand[jrand]);


}



/* Create next batch of 55 random numbers */



void advance_random()



{






int j1;






double new_random;







for(j1 = 0; j1 < 24; j1++)






{



new_random = oldrand[j1] - oldrand[j1+31];



if(new_random < 0.0) new_random = new_random + 1.0;



oldrand[j1] = new_random;






}







for(j1 = 24; j1 < 55; j1++)






{



new_random = oldrand [j1] - oldrand [j1-24];



if(new_random < 0.0) new_random = new_random + 1.0;



oldrand[j1] = new_random;






}



}



/* Initialize random numbers batch */



void randomizef(double semilla)



{






int j1;







for(j1=0; j1<=54; j1++)




oldrand[j1] = 0.0;







jrand=0;







warmup_random(semilla);


}



/* Get random off and running */



void warmup_random(float random_seed)



{






int j1, ii;






double new_random, prev_random;







oldrand[54] = random_seed;






new_random = 0.000000001;






prev_random = random_seed;







for(j1 = 1 j1 <= 54; j1++)







{



ii = (21*j1)%54;



oldrand[ii] = new_random;



new_random = prev_random-new_random;



if(new_random<0.0) new_random = new_random + 1.0;



prev_random = oldrand[ii];






}







advance_random();






advance_random();






advance_random();







jrand = 0;



}



/* Pick a random integer between low and high */



int rnd(int low, int high)



{






int i;







if(low >= high)



i = low;







else







{



i = (randomperc() * (high - low + 1)) + low;



if(i > high) i = high;






}







return(i);


}



/* real random number between specified limits */



float rndreal(float lo ,float hi)



{






return((randomperc() * (hi - lo)) + lo);


}



/* normal noise with specified mean & std dev: mu & sigma */



double noise(double mu,double sigma)



{






return((randomnormaldeviate()*sigma) + mu);


}



/* 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);






}



}



/* initialization routine for randomnormaldeviate */



void initrandomnormaldeviate()



{






rndcalcflag = 1;


}



/* Write in this file your function and constraints */



/* Modify the following parameters */


#define V


ARIABLES 13


/* Total number of constraints */


#define NUMCONSTR 9


/* Number of equiality constraints (must be the last) */


#define NUMEQCONSTR 0


/* Number of tests for each node expansion










PRUEBAS_ARBOL = V


ARIABLES or more */


#define PRUEBAS_ARBOL 13


/* Maximum depth of the tree */


#define PROFUNDIDAD_MAX 5


/* n for the 2^n-tree (must be less or equal than the





number of variables, but we suggest not more than 4 */


#define TREEDIMS 3


/*







TREENODES = 2^TREEDIMS */


#define TREENODES 8



struct individuo {



float variable[V


ARIABLES];



float aptitud;



float g[NUMCONSTR];



float viol;



char factible;



struct celda *celda;



unsigned char victorias;


};



struct celda {



char clase;



char profundidad;



char d[TREEDIMS];



int factibles, noFactibles;



float lnodo[V


ARIABLES], unodo[V


ARIABLES];



struct celda *hijo, *padre;


};



struct creencias {



float


l[VARIABLES],


u[V


ARIABLES],


L[V


ARIABLES],


U[V


ARIABLES],


lp[V


ARIABLES], up[V


ARIABLES];



struct celda *raiz;


};



void limites(float[], float[]);


void evalua(struct individuo *, float, float, int);



/* Give here the lower and upper bounds for the variables */


void limites(float l[], float u[]) {



int i;



for (i = 0; i < 9; i++) {




l[i] = 0.0;




u[i] = 1.0;



}



for (i = 9; i < 12; i++) {




l[i] = 0.0;




u[i] = 100.0;



}



l[12] = 0.0;



u[12] = 1.0;


}



/* Evaluaci




de la aptitud y de las restricciones.





Funci




que cambia con el problema */


void evalua(struct individuo *ind, float deltai, float deltaf, int Gmax) {



int i;



float s1 = 0.0, s2 = 0.0, s3 = 0.0;



float delta = 0.001; /* Para las restricciones de igualdad */





/* Evaluate the objective function, and store its value in ind->aptitud.






Evaluate the constraints and store 1 in ind->factible if the point is feasible,






or 0 if it is infeasible.






Use the values stored in ind->variable[i]. */





for (i = 0; i < 4; i++) {




s1 += ind->variable[i];




s2 += ind->variable[i]*ind->variable[i];



}



for (i = 4; i < 13; i++) {




s3 += ind->variable[i];



}




ind->aptitud = 5*s1 - 5*s2 - s3;




ind->g[0] = 2*ind->variable[0] + 2*ind->variable[1] + ind->variable[9] + ind->variable[10] -


10;



ind->g[1] = 2*ind->variable[0] + 2*ind->variable[2] + ind->variable[9] + ind->variable[11] -


10;



ind->g[2] = 2*ind->variable[1] + 2*ind->variable[2] + ind->variable[10] + ind->variable[11]


- 10;



ind->g[3] = -8*ind->variable[0] + ind->variable[9];



ind->g[4] = -8*ind->variable[1] + ind->variable[10];



ind->g[5] = -8*ind->variable[2] + ind->variable[11];



ind->g[6] = -2*ind->variable[3] - ind->variable[4] + ind->variable[9];



ind->g[7] = -2*ind->variable[5] - ind->variable[6] + ind->variable[10];



ind->g[8] = -2*ind->variable[7] - ind->variable[8] + ind->variable[11];




ind->factible = 1;



for (i = 0; i < NUMCONSTR; i++) {




if (i < NUMCONSTR - NUMEQCONSTR) {





if (ind->g[i] > 0) {






ind->factible = 0;






break;





}




}




else {





if (fabs(ind->g[i]) - delta > 0) {






ind->factible = 0;






break;





}




}



}


}








/* Cultural algorithm for constrained optimization */



#include


#include


#include


#include


#include


#include


#include


/* Give the file name with the problem */


#include



#define TRUSSPROBL 1



/* Give the following 2 parameters. Do not modify anything else in this file */


/* Population size */


#define TAMPOBL 20


/* How many individuals will take the acceptance function at each generation */


#define TOP 2



/* Clases de celdas de creencias */


#define SEMIFACTIBLE 1


#define FACTIBLE 2


#define DESCONOCIDA 3


#define NO_FACTIBLE 4



void violacionInic(float *);


void violacion(struct individuo *, float *);


void creenciasInic(struct creencias *);


void poblacionInic(struct individuo *, struct creencias *);


void actualizaCreencias(struct individuo *, struct creencias *, int);


void expande(struct celda *, struct individuo *, struct creencias *);


void aceptar(struct individuo *);


int compAptitud(const void *, const void *);


void generarHijos(struct individuo *, struct creencias *);


void mueve(int, int, float, struct individuo *, struct creencias *);


struct celda *cercana(int, struct celda *);


struct celda *busca(int, struct celda *);


void selecciona(struct individuo *);


int compVictorias(const void *, const void *);


void nuevoInd(struct individuo *);


void extremos(int *, int *, struct individuo *);



float media;


/* pacific standard & daylight savings */


char *tzstr =


struct timeb tiempo1, tiempo2;


int diferencia;



main(int argc, char **argv) {



int i, j, indmin, indmax, Gmax;



float semilla;



float gmax[NUMCONSTR];



char cadena[25];



long evs = 0;



FILE *a;



struct creencias espcreencias;



struct individuo pobl[2*TAMPOBL + 3];




if (argc == 4) {




Gmax = atoi(argv[1]);




semilla = atof(argv[2]);




strcpy(cadena, argv[3]);



}



else {




printf(



ero m



imo de generaciones:




scanf(




printf(




scanf(




printf(




scanf(



}





if ( (a = fopen(cadena,




printf(




return (1);



}



fprintf(a,



ero m



imo de generaciones: %dn



fprintf(a,




putenv(tzstr);



tzset();



diferencia = 0;




/* Inicializa aleatorios */



randomizef(semilla);



initrandomnormaldeviate();




/* Inicia los valores gmax */



violacionInic(gmax);




/* Inicia el espacio de creencias */



creenciasInic(&espcreencias);




/* Crea una poblaci




inicial aleatoria */



poblacionInic(pobl, &espcreencias);




/* Se eval




la aptitud de cada individuo */



for (i = 0; i < TAMPOBL; i++) {




evalua(&(pobl[i]), 0, 0, 0);




violacion(&(pobl[i]), gmax);




evs++;



}




/* El ciclo principal, se ejecuta Gmax generaciones */



for (j = 0; j < Gmax; j++) {





/* Actualiza el espacio de creencias,







tanto normativas como de restricciones */




actualizaCreencias(pobl, &espcreencias, j);





/* Se generan los hijos mediante mutaci




influenciada







por el espacio de creencias */




generarHijos(pobl, &espcreencias);




/* Se eval




la aptitud de cada hijo */




for (i = TAMPOBL; i < 2*TAMPOBL; i++) {





evalua(&(pobl[i]), 0, 0, 0);





violacion(&(pobl[i]), gmax);





evs++;




}





/* Se hace el torneo entre padres e hijos */




selecciona(pobl);







/* Impresi




de los datos de la generaci




*/




if (j >= Gmax - 11 || (j+1)%(Gmax/100) == 0) {




extremos(&indmin, &indmax, pobl);




fprintf(a,




%dn




fprintf(a,



ima: %0.7f, aptitud m



ima: %0.7fn


media, pobl[indmax].aptitud, pobl[indmin].aptitud);




fprintf(a,




for (i = 0; i < V


ARIABLES; i++) {





fprintf(a,




}




for (i = 0; i < NUMCONSTR; i++) {





fprintf(a,




}


fprintf(a,




fprintf(a,




if (!pobl[indmin].factible) {





fprintf(a,




}




fprintf(a,




}



}



fclose(a);


//


printf(



printf(



if (!pobl[indmin].factible) {




printf(



}



printf(



return (0);


}



/* Inicia el espacio de creencias, con los intervalos para cada variable. */


void creenciasInic(struct creencias *esp) {



int i;



float l[VARIABLES], u[V


ARIABLES];




limites(l, u);



for (i = 0; i < V


ARIABLES; i++) {




esp->L[i] = esp->U[i] = MAXFLOAT;




esp->l[i] = esp->lp[i] = l[i];




esp->u[i] = esp->up[i] = u[i];



}



esp->raiz = (struct celda *) malloc(sizeof(struct celda));



esp->raiz->padre = NULL;



esp->raiz->hijo = NULL;



esp->raiz->profundidad = PROFUNDIDAD_MAX;


}



void poblacionInic(struct individuo *pobl, struct creencias *esp) {



int i, j;



for (i = 0; i < TAMPOBL; i++) {




/* Para cada variable se elige un n



ero real,







en el rango especificado */




for (j = 0; j < V


ARIABLES; j++) {





pobl[i].variable[j] = rndreal(esp->lp[j], esp->up[j]);




}



}


}



void violacionInic(float gmax[]) {



int i;




for (i = 0; i < NUMCONSTR; i++) {




gmax[i] = 0;



}


}



void violacion(struct individuo *ind, float gmax[]) {



int i;



float v;




if (TRUSSPROBL) {




return;



}




ind->viol = 0;




for (i = 0; i < NUMCONSTR; i++) {




/* Inequiality constraints */




if (i < NUMCONSTR - NUMEQCONSTR) {





v = (ind->g[i] > 0)? ind->g[i]: 0;




}




/* Equality constraints */




else {





v = fabs(ind->g[i]);




}





if (v > gmax[i]) {





gmax[i] = v;




}




ind->viol += v/gmax[i];



}


}



/* Actualiza el espacio de creencias.





La parte normativa se actualiza cada k generaciones */


void actualizaCreencias(struct individuo *pobl, struct creencias *esp, int t) {



int aceptados[TOP + 3];



int i, j, iinf, isup, dim, numHijo, sumando, celda, k = 20;



float sup, inf;



struct celda *nodoAct;




/* Decide si realiza la actualizaci




de la parte normativa */



if (t%k == 0) {




/* Elige a los aceptados para afectar a la parte normativa







del espacio de creencias */




aceptar(pobl);




for (i = 0; i < V


ARIABLES; i++) {





iinf = 0;





inf = pobl[0].variable[i];





isup = 0;





sup = pobl[0].variable[i];





/* Ciclo para buscar los valores m




altos y bajos








de cada una de las variables */





for (j = 1; j < TOP; j++) {






if (pobl[j].variable[i] < inf) {







iinf = j;







inf = pobl[j].variable[i];






}






if (pobl[j].variable[i] > sup) {







isup = j;







sup = pobl[j].variable[i];






}





}






/* Aplicaci




de las reglas de actualizaci










de la parte normativa */


/*




if ( (inf < esp->l[i]) || (pobl[iinf].aptitud < esp->L[i] && pobl[iinf].factible) ) {






if


(


(sup


>


esp->u[i])


||


(pobl[isup].aptitud


<


esp->U[i]


&&


pobl[isup].factible) ) {







if (inf < sup || inf < esp->l[i]) {








esp->l[i] = inf;








esp->L[i] = pobl[iinf].aptitud;







}







if (sup > inf || sup > esp->u[i]) {








esp->u[i] = sup;








esp->U[i] = pobl[isup].aptitud;







}

-


-


-


-


-


-


-


-



本文更新与2021-02-09 23:18,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/624827.html

文化算法源代码的相关文章

  • 爱心与尊严的高中作文题库

    1.关于爱心和尊严的作文八百字 我们不必怀疑富翁的捐助,毕竟普施爱心,善莫大焉,它是一 种美;我们也不必指责苛求受捐者的冷漠的拒绝,因为人总是有尊 严的,这也是一种美。

    小学作文
  • 爱心与尊严高中作文题库

    1.关于爱心和尊严的作文八百字 我们不必怀疑富翁的捐助,毕竟普施爱心,善莫大焉,它是一 种美;我们也不必指责苛求受捐者的冷漠的拒绝,因为人总是有尊 严的,这也是一种美。

    小学作文
  • 爱心与尊重的作文题库

    1.作文关爱与尊重议论文 如果说没有爱就没有教育的话,那么离开了尊重同样也谈不上教育。 因为每一位孩子都渴望得到他人的尊重,尤其是教师的尊重。可是在现实生活中,不时会有

    小学作文
  • 爱心责任100字作文题库

    1.有关爱心,坚持,责任的作文题库各三个 一则150字左右 (要事例) “胜不骄,败不馁”这句话我常听外婆说起。 这句名言的意思是说胜利了抄不骄傲,失败了不气馁。我真正体会到它

    小学作文
  • 爱心责任心的作文题库

    1.有关爱心,坚持,责任的作文题库各三个 一则150字左右 (要事例) “胜不骄,败不馁”这句话我常听外婆说起。 这句名言的意思是说胜利了抄不骄傲,失败了不气馁。我真正体会到它

    小学作文
  • 爱心责任作文题库

    1.有关爱心,坚持,责任的作文题库各三个 一则150字左右 (要事例) “胜不骄,败不馁”这句话我常听外婆说起。 这句名言的意思是说胜利了抄不骄傲,失败了不气馁。我真正体会到它

    小学作文