Added timer for bests solutions
This commit is contained in:
12
Makefile
12
Makefile
@@ -1,11 +1,15 @@
|
|||||||
SHELL := /bin/bash
|
SHELL := /bin/bash
|
||||||
CFLAGS := -w -O3 -pg
|
#CFLAGS := -w -O3 -pg -DDEBUG -DDEBUG_BEST
|
||||||
#CFLAGS := -w -g -pg -DDEBUG
|
CFLAGS := -w -O3 -DDEBUG -DDEBUG_BEST
|
||||||
|
# specific DEBUG flags:
|
||||||
|
# timer: -DDEBUG_TIMER
|
||||||
|
# best control: -DDEBUG_BEST
|
||||||
|
|
||||||
#CFLAGS := -w -g -pg -DDEBUG
|
#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"
|
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
||||||
export PATH := .:$(PATH)
|
export PATH := .:$(PATH)
|
||||||
|
|
||||||
TARGETS=lceb tree oper stack eval best
|
TARGETS=lceb tree oper stack eval best timer
|
||||||
INCLUDES=lceb.h
|
INCLUDES=lceb.h
|
||||||
OBJS=$(TARGETS:=.o)
|
OBJS=$(TARGETS:=.o)
|
||||||
|
|
||||||
@@ -20,6 +24,8 @@ tree: tree.c
|
|||||||
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
||||||
oper: oper.c
|
oper: oper.c
|
||||||
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
$(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
|
lceb.o tree.o oper.o eval.o stack.o best.o: lceb.h
|
||||||
tree oper: lceb.h
|
tree oper: lceb.h
|
||||||
|
34
best.c
34
best.c
@@ -13,19 +13,19 @@ static int bestops=MAXINT;
|
|||||||
|
|
||||||
static BEST bests[1024*10]; /* TODO: should be dynamic */
|
static BEST bests[1024*10]; /* TODO: should be dynamic */
|
||||||
static int nbests=0;
|
static int nbests=0;
|
||||||
static int sigint=0;
|
int sigint_received=0;
|
||||||
|
|
||||||
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
|
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
|
||||||
|
|
||||||
void stopall()
|
void stopall()
|
||||||
{
|
{
|
||||||
printf("SIGINT RECEIVED: aborting eval\n");
|
printf("SIGINT RECEIVED: aborting eval\n");
|
||||||
sigint=1;
|
sigint_received=1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int stopped()
|
int stopped()
|
||||||
{
|
{
|
||||||
return sigint;
|
return sigint_received;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_target(n)
|
void set_target(n)
|
||||||
@@ -45,17 +45,10 @@ int check_best(res, nops, node, values, ops)
|
|||||||
int diff, found=0, i=0;
|
int diff, found=0, i=0;
|
||||||
|
|
||||||
diff=DIFF(target, res);
|
diff=DIFF(target, res);
|
||||||
# ifdef DEBUG1
|
# ifdef DEBUG_BEST1
|
||||||
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
|
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
|
||||||
# endif
|
# endif
|
||||||
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
|
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;
|
//best=res;
|
||||||
// clear old bests
|
// clear old bests
|
||||||
for (i=0; i<nbests; ++i)
|
for (i=0; i<nbests; ++i)
|
||||||
@@ -65,20 +58,29 @@ int check_best(res, nops, node, values, ops)
|
|||||||
nbests=0;
|
nbests=0;
|
||||||
found=1;
|
found=1;
|
||||||
//return 1;
|
//return 1;
|
||||||
} else if (diff == bestdiff && nops == bestops) {
|
}/* else if (diff == bestdiff && nops == bestops) {
|
||||||
# ifdef DEBUG1
|
# ifdef DEBUG_BEST
|
||||||
printf("NEW BEST SOLUTION (%d): res=%d diff=%d nops=%d\n", nbests+1, res, diff, nops);
|
printf("NEW BEST SOLUTION (%d): res=%d diff=%d nops=%d\n", nbests+1, res, diff, nops);
|
||||||
print_node(node, TREE_TOP, 0, 0);
|
print_node(node, TREE_TOP, 0, 0);
|
||||||
# endif
|
# endif
|
||||||
found=2;
|
found=2;
|
||||||
}
|
}*/
|
||||||
if (found==1) {
|
if (found==1) {
|
||||||
|
set_timer(&(bests[nbests].timer));
|
||||||
bests[nbests].res=res;
|
bests[nbests].res=res;
|
||||||
bests[nbests].diff=diff;
|
bests[nbests].diff=diff;
|
||||||
bests[nbests].nops=nops;
|
bests[nbests].nops=nops;
|
||||||
bests[nbests].oper=ops;
|
bests[nbests].oper=ops;
|
||||||
bests[nbests].root=dup_node(node);
|
bests[nbests].root=dup_node(node);
|
||||||
bests[nbests].values=values;
|
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++;
|
nbests++;
|
||||||
return diff;
|
return diff;
|
||||||
}
|
}
|
||||||
@@ -114,10 +116,10 @@ void print_best(node, values, pops, depth)
|
|||||||
void print_bests()
|
void print_bests()
|
||||||
{
|
{
|
||||||
int i;
|
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) {
|
for (i=0; i<nbests; ++i) {
|
||||||
//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);
|
printf("%.5f secs: %5d = ", get_timer(bests[i].timer), bests[i].res);
|
||||||
print_node(bests[i].root, TREE_TOP, 0, 4);
|
print_node(bests[i].root, TREE_TOP, 0, 4);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
//printf("%3d: %d = ", i, bests[i].res);
|
//printf("%3d: %d = ", i, bests[i].res);
|
||||||
|
18
eval.c
18
eval.c
@@ -2,6 +2,9 @@
|
|||||||
#include "lceb.h"
|
#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)
|
int eval_node(node, depth, pvals, pops, ncalcs)
|
||||||
NODE *node;
|
NODE *node;
|
||||||
int depth;
|
int depth;
|
||||||
@@ -11,8 +14,8 @@ int eval_node(node, depth, pvals, pops, ncalcs)
|
|||||||
{
|
{
|
||||||
static int *vals, *val_zero;
|
static int *vals, *val_zero;
|
||||||
static char *ops, *ops_zero;
|
static char *ops, *ops_zero;
|
||||||
static int totcalc;
|
|
||||||
static *node_zero;
|
static *node_zero;
|
||||||
|
static int totcalc;
|
||||||
int val1, val2, op, res=-1, i, lcalcs, rcalcs;
|
int val1, val2, op, res=-1, i, lcalcs, rcalcs;
|
||||||
|
|
||||||
if (depth == 0) {
|
if (depth == 0) {
|
||||||
@@ -34,12 +37,14 @@ int eval_node(node, depth, pvals, pops, ncalcs)
|
|||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
if (node->type == TREE_LEAF) {
|
if (node->type == TREE_LEAF) {
|
||||||
|
leaves_calc++;
|
||||||
node->val=*vals;
|
node->val=*vals;
|
||||||
res=*vals;
|
res=*vals;
|
||||||
vals++;
|
vals++;
|
||||||
*ncalcs=0;
|
*ncalcs=0;
|
||||||
//printf("leaf=%d\n", res);
|
//printf("leaf=%d\n", res);
|
||||||
} else {
|
} else {
|
||||||
|
nodes_calc++;
|
||||||
op=*ops;
|
op=*ops;
|
||||||
node->op=*ops;
|
node->op=*ops;
|
||||||
ops++;
|
ops++;
|
||||||
@@ -80,7 +85,7 @@ int eval_node(node, depth, pvals, pops, ncalcs)
|
|||||||
if (!check_best(res, *ncalcs, node, val_zero, ops_zero))
|
if (!check_best(res, *ncalcs, node, val_zero, ops_zero))
|
||||||
res=-1;
|
res=-1;
|
||||||
}
|
}
|
||||||
if (stopped()) {
|
if (sigint_received) {
|
||||||
print_bests();
|
print_bests();
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@@ -91,3 +96,12 @@ int eval_node(node, depth, pvals, pops, ncalcs)
|
|||||||
# endif
|
# endif
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int get_totnodes()
|
||||||
|
{
|
||||||
|
return nodes_calc;
|
||||||
|
}
|
||||||
|
int get_totleaves()
|
||||||
|
{
|
||||||
|
return leaves_calc;
|
||||||
|
}
|
||||||
|
11
lceb.c
11
lceb.c
@@ -12,6 +12,7 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <locale.h>
|
||||||
#include "lceb.h"
|
#include "lceb.h"
|
||||||
|
|
||||||
int main(ac, av)
|
int main(ac, av)
|
||||||
@@ -20,19 +21,20 @@ int main(ac, av)
|
|||||||
{
|
{
|
||||||
unsigned target;
|
unsigned target;
|
||||||
STACK inputstack, *stack;
|
STACK inputstack, *stack;
|
||||||
int i, j, k, stacksize, val, res;
|
int i, j, k, stacksize, val;
|
||||||
//char *ops="+-*/", *opscomb;
|
|
||||||
//int len_ops=strlen(ops), ncombs, nops;
|
|
||||||
int ncombs, nstacks, ntrees, nops;
|
int ncombs, nstacks, ntrees, nops;
|
||||||
TREE *tree;
|
TREE *tree;
|
||||||
int intarray[1024];
|
int intarray[1024];
|
||||||
int eval;
|
int eval;
|
||||||
char *comb;
|
char *comb;
|
||||||
|
struct timespec end;
|
||||||
|
|
||||||
|
setlocale(LC_ALL, ""); /* to use "%'d" in printf */
|
||||||
if (ac < 4 || ac > 2+MAXINPUT) {
|
if (ac < 4 || ac > 2+MAXINPUT) {
|
||||||
fprintf(stderr, "usage: %s target n1 n2 [...n%d]\n", *av, MAXINPUT);
|
fprintf(stderr, "usage: %s target n1 n2 [...n%d]\n", *av, MAXINPUT);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
start_timer();
|
||||||
target=atoi(av[1]);
|
target=atoi(av[1]);
|
||||||
stacksize=2*(ac-2)-1;
|
stacksize=2*(ac-2)-1;
|
||||||
nops=ac-2-1;
|
nops=ac-2-1;
|
||||||
@@ -110,5 +112,8 @@ int main(ac, av)
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
print_bests();
|
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);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
10
lceb.h
10
lceb.h
@@ -1,3 +1,4 @@
|
|||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define MAXINPUT 6 /* max numbers as input */
|
#define MAXINPUT 6 /* max numbers as input */
|
||||||
#define ALLOCSIZE 1024 /* # of elements to alloc when needed */
|
#define ALLOCSIZE 1024 /* # of elements to alloc when needed */
|
||||||
@@ -55,6 +56,7 @@ typedef struct best {
|
|||||||
char *oper;
|
char *oper;
|
||||||
NODE *root;
|
NODE *root;
|
||||||
int *values;
|
int *values;
|
||||||
|
struct timespec timer;
|
||||||
} BEST;
|
} BEST;
|
||||||
|
|
||||||
/* tree.c */
|
/* tree.c */
|
||||||
@@ -103,6 +105,8 @@ extern char *nth_comb(int n);
|
|||||||
//extern int eval_stack(STACK *stack);
|
//extern int eval_stack(STACK *stack);
|
||||||
/* tree version */
|
/* tree version */
|
||||||
extern int eval_node(NODE *node, int depth, int *pvals, char *pops, int *ncalcs);
|
extern int eval_node(NODE *node, int depth, int *pvals, char *pops, int *ncalcs);
|
||||||
|
extern int get_totnodes();
|
||||||
|
extern int get_totleaves();
|
||||||
|
|
||||||
/* best.c */
|
/* best.c */
|
||||||
extern int stopped();
|
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 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_best(NODE *node, int *values, char *pops, int depth);
|
||||||
extern void print_bests();
|
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
62
timer.c
Normal 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
|
Reference in New Issue
Block a user