Torres de Hanoi

Paula Andrea
01 de Septiembre del 2008
Hola a todos!

He tenido problemas con el siguiente codigo, no se como hacerlo funcionar, me gustaria que me ayudaran con esto. Muchas gracias.

#define A_TO_B 0x01
#define A_TO_C 0x02
#define B_TO_A 0x10
#define B_TO_C 0x12
#define C_TO_A 0x20
#define C_TO_B 0x21

#define MAX_OPERATIONS 6

char moves[MAX_OPERATIONS]={A_TO_B,A_TO_C,B_TO_A,B_TO_C,C_TO_A,C_TO_B};
/*
Para cada torre Torre A, torre B, Torre C....
*/

typedef struct{
char peg[3];
int count;
}peg_t;

peg_t pegs[3];

#define NUM_OPERATIONS 12
typedef struct{
int op_count;
unsigned char plan[NUM_OPERATIONS+1];
double fitness;
}solution_t;

#define POPULATION_SIZE 200
solution_t solutions[2][POPULATION_SIZE];


main(){

RANDINIT();

initialize_population(cur);
compute_population_fitness(cur);

while((avg<(0.999 * max))&&(max<75)){
cur=perform_ga(cur);
compute_population_fitness(cur);
if(((generation++)%500)==0){
printf("%6d: %g %g %gn", generation,min,avg,max);
}
}
}




int perform_ga(int cur_pop)
{
int i,j,new_pop;
int parent_1,parent_2;
int crossover;
new_pop = (cur_pop==0)? 1 : 0;
for(i=0;i<POPULATION_SIZE;i+=2){
par[new_pop][i].op_count=solutions[cur_pop][parent_1].op_count;
ent_1=select_parent(cur_pop);
parent_2=select_parent(cur_pop);
if(RANDOM()<CROSSOVER_PROB){
crossover=RANDMAX(MIN(solutions[cur_pop][parent_1].op_count,solutions[cur_pop][parent_2].op_count));
}
else
{
crossover=NUM_OPERATIONS;
}

for(j=0;j<NUM_OPERATIONS;j++){
if(j<crossover){
solutions[new_pop][i].plan[j]=MUTATE(solutions[cur_pop][parent_1].plan[j]);
solutions[new_pop][i+1].plan[j]=MUTATE(solutions[cur_pop][parent_2].plan[j]);
}
else
{
solutions[new_pop][i].plan[j]=MUTATE(solutions[cur_pop][parent_2].plan[j]);
solutions[new_pop][i+1].plan[j]=MUTATE(solutions[cur_pop][parent_1].plan[j]);
}
}
solutions[new_pop][i].op_count=solutions[cur_pop][parent_1].op_count;
solutions[new_pop][i+1].op_count=solutions[cur_pop][parent_2].op_count;
}
return new_pop;
}



int select_parent(int cur_pop){
int i=RANDMAX(POPULATION_SIZE);
int count=POPULATION_SIZE;
double select=0.0;
while(count--){
select=solutions[cur_pop][i].fitness;
if(RANDOM()<(select/sum))
return i;
if(++i>=POPULATION_SIZE)
i=0;
}
return(RANDMAX(POPULATION_SIZE));
}

double compute_fitness(int cur_pop, int member, int trace) {
int i, from,to,disc=3;
int illegal_moves=0;
int move;
double fitness;
/*Despiste???*/
for(i=0;i<3;i++){
pegs[0].peg[i]=disc--;
pegs[1].peg[i]=0;
pegs[2].peg[i]=0;
}
/*Fin despiste???*/
pegs[0].count=3;
pegs[1].count=0;
pegs[2].count=0;

for(i=0;i<solutions[cur_pop][member].op_count;i++){
move=moves[solutions[cur_pop][member].plan[i]];
from=(move>>4)& 0xf;
if(pegs[from].count==0){
illegal_moves++;
}
else
{
to=move & 0xf;
if((pegs[to].count==0)||(pegs[from].peg[pegs[from].count-1]<pegs[to].peg[pegs[to].count-1])){
pegs[from].count--;
pegs[to].peg[pegs[to].count]=pegs[from].peg[pegs[from].count];
pegs[from].peg[pegs[from].count]=0;
pegs[to].count++;
}
else
{
illegal_moves++;
}
}
}
fitness=(double)(pegs[2].count*25)-(double)illegal_moves;
if(fitness<0)
fitness=0;
return fitness;
}