-
/* 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;
}
-
-
-
-
-
-
-
-
-
上一篇:常用药用辅料-20190319公布
下一篇:牛津上海版英语五年级上册期末试卷