Files
Le-Compte-est-Bon/best.c

155 lines
3.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <values.h>
#include <signal.h>
#include "lceb.h"
static int target=0;
//static int best=MAXINT;
static int bestdiff=MAXINT;
static int bestops=MAXINT;
static BEST bests[1024*10]; /* TODO: should be dynamic */
static int nbests=0;
extern int displaytimer;
int sigint_received=0;
int displayintermediate=0;
int displaytype=0;
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
void stopall()
{
printf("SIGINT RECEIVED: aborting eval\n");
sigint_received=1;
}
int stopped()
{
return sigint_received;
}
void set_target(n)
int n;
{
target=n;
signal(SIGINT, stopall);
}
int check_best(res, nops, node, values, ops)
int res;
int nops;
NODE *node;
int *values;
char *ops;
{
int diff, found=0, i=0;
diff=DIFF(target, res);
# ifdef DEBUG_BEST1
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
# endif
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
//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) {
// if (nbests) {
found=2;
for (i=0; i<nbests; ++i) {
if (compare_nodes(node, bests[i].root, 0)) {
found=0;
break;
}
}
# ifdef DEBUG_BEST
if (found==2) {
printf("new diff solution (%d): res=%d diff=%d nops=%d\n",
nbests+1, res, diff, nops);
print_node(node, TREE_TOP, 0, 0);
} else {
printf("skipping duplicate solution (%d): res=%d diff=%d nops=%d\n",
nbests+1, res, diff, nops);
}
# endif
}
//}
if (found) {
set_timer(&(bests[nbests].timer));
bests[nbests].res=res;
bests[nbests].diff=diff;
bests[nbests].nops=nops;
bests[nbests].oper=ops;
bests[nbests].root=dup_node(node);
bests[nbests].values=values;
// printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
if (displayintermediate) {
if (displaytimer) {
printf("%.5f secs: ", get_timer(bests[nbests].timer));
}
printf("diff=%d nops=%d %d=", diff, nops, res);
print_node(node, TREE_TOP, 0, displaytype);
putchar('\n');
}
//printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
nbests++;
return diff;
}
return -1;
}
void print_best(node, values, pops, depth)
NODE *node;
int *values;
char *pops;
int depth;
{
static int *vals;
static char *ops;
if (depth == 0) {
vals=values;
ops=pops;
}
if (node->type == TREE_LEAF) {
printf("%d ", *vals);
vals++;
} else {
print_best(node->left, vals, ops, depth+1);
print_best(node->right, vals, ops, depth+1);
printf(" %c ", *ops);
ops++;
}
if (depth == 0)
putchar('\n');
}
void print_bests()
{
int i=0;
printf("BEST SOLUTION: res=%d diff=%d ops=%d ", bests[i].res, bestdiff, bestops);
if (displaytimer)
printf("after %.5f secs.", get_timer(bests[i].timer));
putchar('\n');
for (i=0; i<nbests; ++i) {
//print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
//printf("%5d =", bests[i].res);
print_node(bests[i].root, TREE_TOP, 0, displaytype);
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');
}
}