Added timer for bests solutions

This commit is contained in:
2021-01-23 19:20:21 +01:00
parent 1b43433420
commit e1c734cb63
6 changed files with 123 additions and 24 deletions

View File

@@ -1,11 +1,15 @@
SHELL := /bin/bash
CFLAGS := -w -O3 -pg
#CFLAGS := -w -g -pg -DDEBUG
#CFLAGS := -w -O3 -pg -DDEBUG -DDEBUG_BEST
CFLAGS := -w -O3 -DDEBUG -DDEBUG_BEST
# specific DEBUG flags:
# timer: -DDEBUG_TIMER
# best control: -DDEBUG_BEST
#CFLAGS := -w -g -pg -DDEBUG
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH)
TARGETS=lceb tree oper stack eval best
TARGETS=lceb tree oper stack eval best timer
INCLUDES=lceb.h
OBJS=$(TARGETS:=.o)
@@ -20,6 +24,8 @@ tree: tree.c
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
oper: oper.c
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
timer: timer.c
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
lceb.o tree.o oper.o eval.o stack.o best.o: lceb.h
tree oper: lceb.h

34
best.c
View File

@@ -13,19 +13,19 @@ static int bestops=MAXINT;
static BEST bests[1024*10]; /* TODO: should be dynamic */
static int nbests=0;
static int sigint=0;
int sigint_received=0;
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
void stopall()
{
printf("SIGINT RECEIVED: aborting eval\n");
sigint=1;
sigint_received=1;
}
int stopped()
{
return sigint;
return sigint_received;
}
void set_target(n)
@@ -45,17 +45,10 @@ int check_best(res, nops, node, values, ops)
int diff, found=0, i=0;
diff=DIFF(target, res);
# ifdef DEBUG1
# ifdef DEBUG_BEST1
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("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)
@@ -65,20 +58,29 @@ int check_best(res, nops, node, values, ops)
nbests=0;
found=1;
//return 1;
} else if (diff == bestdiff && nops == bestops) {
# ifdef DEBUG1
}/* else if (diff == bestdiff && nops == bestops) {
# ifdef DEBUG_BEST
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=2;
}
}*/
if (found==1) {
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;
# ifdef DEBUG_BEST
// printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
printf("%.5f: diff=%d nops=%d %d=", get_timer(bests[nbests].timer),
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
nbests++;
return diff;
}
@@ -114,10 +116,10 @@ void print_best(node, values, pops, depth)
void print_bests()
{
int i;
printf("BESTS: diff=%d solutions=%d nops=%d\n", bestdiff, nbests, bestops);
printf("BEST SOLUTION: diff=%d ops=%d\n", bestdiff, bestops);
for (i=0; i<nbests; ++i) {
//print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
printf("%3d: %d = ", i, bests[i].res);
printf("%.5f secs: %5d = ", get_timer(bests[i].timer), bests[i].res);
print_node(bests[i].root, TREE_TOP, 0, 4);
putchar('\n');
//printf("%3d: %d = ", i, bests[i].res);

18
eval.c
View File

@@ -2,6 +2,9 @@
#include "lceb.h"
static int nodes_calc; /* total nodes evaluated */
static int leaves_calc; /* total leaves evaluated */
int eval_node(node, depth, pvals, pops, ncalcs)
NODE *node;
int depth;
@@ -11,8 +14,8 @@ int eval_node(node, depth, pvals, pops, ncalcs)
{
static int *vals, *val_zero;
static char *ops, *ops_zero;
static int totcalc;
static *node_zero;
static int totcalc;
int val1, val2, op, res=-1, i, lcalcs, rcalcs;
if (depth == 0) {
@@ -34,12 +37,14 @@ int eval_node(node, depth, pvals, pops, ncalcs)
}
# endif
if (node->type == TREE_LEAF) {
leaves_calc++;
node->val=*vals;
res=*vals;
vals++;
*ncalcs=0;
//printf("leaf=%d\n", res);
} else {
nodes_calc++;
op=*ops;
node->op=*ops;
ops++;
@@ -80,7 +85,7 @@ int eval_node(node, depth, pvals, pops, ncalcs)
if (!check_best(res, *ncalcs, node, val_zero, ops_zero))
res=-1;
}
if (stopped()) {
if (sigint_received) {
print_bests();
exit(1);
}
@@ -91,3 +96,12 @@ int eval_node(node, depth, pvals, pops, ncalcs)
# endif
return res;
}
int get_totnodes()
{
return nodes_calc;
}
int get_totleaves()
{
return leaves_calc;
}

11
lceb.c
View File

@@ -12,6 +12,7 @@
#include <stdarg.h>
#include <malloc.h>
#include <string.h>
#include <locale.h>
#include "lceb.h"
int main(ac, av)
@@ -20,19 +21,20 @@ int main(ac, av)
{
unsigned target;
STACK inputstack, *stack;
int i, j, k, stacksize, val, res;
//char *ops="+-*/", *opscomb;
//int len_ops=strlen(ops), ncombs, nops;
int i, j, k, stacksize, val;
int ncombs, nstacks, ntrees, nops;
TREE *tree;
int intarray[1024];
int eval;
char *comb;
struct timespec end;
setlocale(LC_ALL, ""); /* to use "%'d" in printf */
if (ac < 4 || ac > 2+MAXINPUT) {
fprintf(stderr, "usage: %s target n1 n2 [...n%d]\n", *av, MAXINPUT);
exit(1);
}
start_timer();
target=atoi(av[1]);
stacksize=2*(ac-2)-1;
nops=ac-2-1;
@@ -110,5 +112,8 @@ int main(ac, av)
}
printf("\n");
print_bests();
set_timer(&end);
printf("Total time elapsed: %.5f secs, nodes/leaves evaluated:%'d/%'d\n",
get_timer(end), get_totnodes(), get_totleaves());
exit(0);
}

10
lceb.h
View File

@@ -1,3 +1,4 @@
#include <time.h>
#define MAXINPUT 6 /* max numbers as input */
#define ALLOCSIZE 1024 /* # of elements to alloc when needed */
@@ -55,6 +56,7 @@ typedef struct best {
char *oper;
NODE *root;
int *values;
struct timespec timer;
} BEST;
/* tree.c */
@@ -103,6 +105,8 @@ extern char *nth_comb(int n);
//extern int eval_stack(STACK *stack);
/* tree version */
extern int eval_node(NODE *node, int depth, int *pvals, char *pops, int *ncalcs);
extern int get_totnodes();
extern int get_totleaves();
/* best.c */
extern int stopped();
@@ -110,3 +114,9 @@ extern void set_target (int n);
extern int check_best(int res, int nops, NODE *node, int *values, char *ops);
extern void print_best(NODE *node, int *values, char *pops, int depth);
extern void print_bests();
/* timer.c */
extern int sigint_received;
extern void start_timer();
extern void set_timer(struct timespec *timer);
extern double get_timer(struct timespec timer);

62
timer.c Normal file
View File

@@ -0,0 +1,62 @@
#include <stdio.h>
#include <time.h>
#define NANOSEC 1000000000.0
#define MILLISEC 1000
static struct timespec start;
void start_timer()
{
printf("timer started...\n");
clock_gettime(CLOCK_MONOTONIC, &start);
# ifdef DEBUG_TIMER
printf("sec=%d nsec=%d\n", start.tv_sec, start.tv_nsec);
# endif
}
void set_timer(timer)
struct timespec *timer;
{
clock_gettime(CLOCK_MONOTONIC, timer);
# ifdef DEBUG_TIMER
printf("timer: sec=%d nsec=%d orig: sec=%d nsec=%d\n",
timer->tv_sec, timer->tv_nsec, start.tv_sec, start.tv_nsec);
# endif
}
double get_timer(timer)
struct timespec timer;
{
double diff;
diff=(timer.tv_sec - start.tv_sec) + (timer.tv_nsec - start.tv_nsec) / NANOSEC;
# ifdef DEBUG_TIMER
printf("timer sec: %.5f\n", diff);
# endif
return diff;
}
#ifdef STANDALONE
main(ac, av)
int ac;
char **av;
{
int delay;
struct timespec end, tsdelay;
if (ac != 2) {
fprintf(stderr, "usage: %s msecs\n", *av);
exit (1);
}
delay=atoi(*(av+1));
start_timer();
tsdelay.tv_sec=delay/MILLISEC;
tsdelay.tv_nsec=delay%MILLISEC;
printf("delay %d=%d:%d\n", delay, tsdelay.tv_sec, tsdelay.tv_nsec);
nanosleep(&tsdelay, NULL);
set_timer(&end);
printf("Time elapsed: %.5f seconds\n", get_timer(end));
return 0;
}
#endif