working version: displays only bests results so far.

This commit is contained in:
2021-01-23 14:33:55 +01:00
parent 9f14b5e076
commit 0931d679fa
7 changed files with 246 additions and 111 deletions

42
best.c
View File

@@ -10,7 +10,7 @@ static int target=0;
static int bestdiff=MAXINT;
static int bestops=MAXINT;
static BEST bests[1024]; /* TODO: should be dynamic */
static BEST bests[1024*10]; /* TODO: should be dynamic */
static int nbests=0;
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
@@ -28,39 +28,47 @@ int check_best(res, nops, node, values, ops)
int *values;
char *ops;
{
int diff, found=0;
int diff, found=0, i=0;
diff=DIFF(target, res);
# ifdef DEBUG
# ifdef DEBUG1
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
# endif
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
# ifdef DEBUG
printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
// printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
printf("diff=%d nops=%d %d=", diff, nops, res);
print_node(node, TREE_TOP, 0, 4);
putchar('\n');
//printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
# endif
//best=res;
// clear old bests
for (i=0; i<nbests; ++i)
free_node(bests[i].root);
bestdiff=diff;
bestops=nops;
nbests=0;
found=1;
//return 1;
} else if (diff == bestdiff && nops == bestops) {
# ifdef DEBUG
# ifdef DEBUG1
printf("NEW BEST SOLUTION (%d): res=%d diff=%d nops=%d\n", nbests+1, res, diff, nops);
print_node(node, TREE_TOP, 0, 0);
# endif
found=1;
found=2;
}
if (found) {
if (found==1) {
bests[nbests].res=res;
bests[nbests].diff=diff;
bests[nbests].nops=nops;
bests[nbests].oper=ops;
bests[nbests].root=node;
bests[nbests].root=dup_node(node);
bests[nbests].values=values;
nbests++;
return 1;
return diff;
}
return 0;
return -1;
}
void print_best(node, values, pops, depth)
@@ -94,7 +102,15 @@ void print_bests()
int i;
printf("BESTS: diff=%d solutions=%d nops=%d\n", bestdiff, nbests, bestops);
for (i=0; i<nbests; ++i) {
printf("res = %d : ", bests[i].res);
print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
//print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
printf("%3d: %d = ", i, bests[i].res);
print_node(bests[i].root, TREE_TOP, 0, 0);
putchar('\n');
//printf("%3d: %d = ", i, bests[i].res);
//print_node(bests[i].root, TREE_TOP, 0, 1);
//putchar('\n');
//printf("%3d: %d = ", i, bests[i].res);
//print_node(bests[i].root, TREE_TOP, 0, 4);
//putchar('\n');
}
}