1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
/* The population is what i need to transfer but only the best_prog from that population and other index everytime my function is called.*/
void initialise_population()
{
struct prog *progp = population;
int i, j, depth, full, attempts;
char *end_tree;
for (i = 2; i <= MAX_INIT_DEPTH; i++)
{
for (j = 0; j < PARTITION_SIZE; j++)
{ depth = i; full = j%2; attempts=0;
do
{
end_tree = create_tree(progp->code, depth, full);
*end_tree = '\0';
progp->proglen = strlen(progp->code);
attempts++;
if (attempts >= 5 && depth < MAX_INIT_DEPTH)
{ attempts = 0; depth++;
}
} while (duplicate(progp));
progp++;
}
}
for (i=0; i < POPULATION_SIZE && !correct; i++)
test_fitness(i);
}
// Enum tokens that convert to number to populate an array which would be just a string of numbers..
enum tokens {DUMMY, LEFT, RIGHT, MOVE, IF_FOOD_AHEAD, PROGN2, PROGN3};
struct token_entry
{
char *name;
int arity_sub1;
// function
};
struct token_entry token_table [] =
{ {"# ", -1}, // String end. Arity must be -1 for sub-string search
{"LEFT ", -1},
{"RIGHT ", -1},
{"MOVE ", -1},
{"IF_FOOD_AHEAD ", 1},
{"PROGN2 ", 1},
{"PROGN3 ", 2},
//{"GHOST_NODE ", -1}
};
//The arity is for comparing not really for what I need to figure out.
struct prog
{
char code[MAX_PROG_SIZE+1]; // allow for string terminator
int proglen;
int fitness;
};
struct prog population [POPULATION_SIZE];
int best_prog;
int smallest_proglen;
int best_fitness;
//Testing fitness is to determine the best prog for its generation, I used similar technique of memcpy to try and get the values into my function, the pellets are to do with a matrix map that has no real value to me.
void test_fitness(int index)
{
// Evaluate fitness of the indexed individual
struct prog *progp = &population[index];
int count = progp->proglen;
// Initialise the trail matrix
memcpy(matrix, matrix_data, MATRIX_SIZE*MATRIX_SIZE);
pellets = NUM_PELLETS;
if (trace)
memcpy(trace_matrix, matrix_data, MATRIX_SIZE*MATRIX_SIZE);
// Position the ant
row = 0; col = 0; heading = EAST; steps = 0;
// Repeatedly evaluate tree until all pellets eaten or we time out
do
{ nodep = progp->code;
evaltree();
} while (pellets > 0 && steps < MAX_STEPS);
correct = (pellets == 0);
progp->fitness = pellets;
if (pellets < best_fitness ||
(pellets == best_fitness && count < smallest_proglen))
{ best_prog = index;
best_fitness = pellets;
smallest_proglen = count;
}
}
|