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

View File

@@ -1,6 +1,7 @@
SHELL := /bin/bash
#CFLAGS := -w -O3
CFLAGS := -w -g -pg -DDEBUG
CFLAGS := -w -O3 -pg
#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"
export PATH := .:$(PATH)
@@ -13,13 +14,15 @@ OBJS=$(TARGETS:=.o)
all: $(TARGETS)
lceb: $(OBJS)
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $^
tree: tree.c lceb.h
tree: tree.c
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
oper: oper.c lceb.h
oper: oper.c
$(CC) $(CFLAGS) -DSTANDALONE -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
ex2: ex2-c
@$(TIME) ex2-c < $(INPUT)

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');
}
}

30
eval.c
View File

@@ -2,30 +2,33 @@
#include "lceb.h"
int eval_node(node, depth, pvals, pops)
int eval_node(node, depth, pvals, pops, ncalcs)
NODE *node;
int depth;
int *pvals;
char *pops;
int *ncalcs;
{
static int *vals, *val_zero;
static char *ops, *ops_zero;
static int ncalc;
static int totcalc;
static *node_zero;
int val1, val2, op, res=-1, i;
int val1, val2, op, res=-1, i, lcalcs, rcalcs;
if (depth == 0) {
val_zero=vals=pvals;
ops_zero=ops=pops;
node_zero=node;
ncalc=0;
totcalc=0;
}
# ifdef DEBUG1
for (i=0; i<=depth; ++i)
printf(" ");
printf("eval : depth=%d : ", depth);
printf("eval : depth=%d : ncalcs=%d", depth, *ncalcs);
if (node->type == TREE_NODE) {
printf("node(%c)\n", *ops);
print_node(node, TREE_TOP, 0, 0);
} else {
printf("val(%d)\n", *vals);
}
@@ -34,16 +37,20 @@ int eval_node(node, depth, pvals, pops)
node->val=*vals;
res=*vals;
vals++;
*ncalcs=0;
//printf("leaf=%d\n", res);
} else {
op=*ops;
node->op=*ops;
ops++;
ncalc++;
totcalc++;
//printf("NEW node(%s)\n", ops);
val1=eval_node(node->left, depth+1, pvals, ops);
val1=eval_node(node->left, depth+1, pvals, ops, &lcalcs);
//printf("val1=%d ", val1);
if (val1 <= 0)
return -1;
val2=eval_node(node->right, depth+1, pvals, ops);
val2=eval_node(node->right, depth+1, pvals, ops, &rcalcs);
//printf("val2=%d\n", val2);
if (val2 <= 0)
return -1;
switch (op) {
@@ -67,9 +74,12 @@ int eval_node(node, depth, pvals, pops)
exit(1);
break;
}
*ncalcs=lcalcs+rcalcs+1;
}
if (res > 0) {
if (!check_best(res, *ncalcs, node, val_zero, ops_zero))
res=-1;
}
if (res > 0)
check_best(res, ncalc, node_zero, val_zero, ops_zero);
# ifdef DEBUG1
for (i=0; i<=depth; ++i)
printf(" ");

31
lceb.c
View File

@@ -19,7 +19,7 @@ int main(ac, av)
char **av;
{
unsigned target;
STACK *stack;
STACK inputstack, *stack;
int i, j, k, stacksize, val, res;
//char *ops="+-*/", *opscomb;
//int len_ops=strlen(ops), ncombs, nops;
@@ -37,24 +37,22 @@ int main(ac, av)
stacksize=2*(ac-2)-1;
nops=ac-2-1;
printf("A\n");
gen_combinations(nops);
printf("A\n");
//ncombs=ncombinations(len_ops, nops);
ncombs=n_combs();
printf("A\n");
print_combs();
printf("target=%d\nstacksize=%d\nops_comb=%d\n", target, stacksize, ncombs);
set_target(target);
//printf("len_ops=%d\nnops=%d\nops_comb=%d\n", len_ops, nops, ncombs);
stack=new_stack(stacksize, "Main Stack", 1);
//stack=new_stack(stacksize, "Main Stack", 1);
strcpy(inputstack.name, "initial");
for (i=2; i<ac; ++i) {
val=atoi(av[i]);
push_stack(stack, val);
push_stack(&inputstack, val);
}
//print_stack(stack, 1);
gen_stacks(stack); // printf("sorting stack...\n");
gen_stacks(&inputstack); // printf("sorting stack...\n");
print_stacks();
//mergesort_stack(stack->stack, 0, stack->last-1);
//print_stack(stack, 1);
@@ -72,24 +70,33 @@ int main(ac, av)
nstacks=n_stacks();
ntrees=n_trees();
printf("nstacks=%d\nncombs=%d\nntrees=%d\n", nstacks, ncombs, ntrees);
//for (k=0; k<nstacks; ++k) {
// stack=nth_stack(k);
// printf("%%%%%%%%%%%%%%%%%%\n");
// print_stack(stack, 0);
// printf("%%%%%%%%%%%%%%%%%%\n");
//}
for (i=0; i<ntrees; ++i) {
tree=nth_tree(i);
for (j=0; j<ncombs; ++j) {
comb=nth_comb(j);
for (k=0; k<nstacks; ++k) {
int ncalcs=0;
stack=nth_stack(k);
eval=eval_node(tree->head, 0, stack->stack, comb);
if (eval > 0) {
//printf("%%%%%%%%%%%%%%%%%%\n");
//print_stack(stack, 0);
//printf("%%%%%%%%%%%%%%%%%%\n");
eval=eval_node(tree->head, 0, stack->stack, comb, &ncalcs);
#ifdef DEBUG1
if (eval > 0) {
printf("============================== %d, %d, %d\n", i, j, k);
print_tree(tree, 0);
print_comb(j);
print_stack(stack, 0);
printf("eval=%d\n", eval);
#endif
printf("eval=%d - calcs=%d\n", eval, ncalcs);
}
#endif
}
}
//opscomb=combination(ops, nops, i);

8
lceb.h
View File

@@ -17,7 +17,7 @@ typedef struct stack {
int size;
int last;
struct stack *next;
int *stack;
int stack[MAXINPUT+1];
} STACK;
#define TREE_UNDEF (-1) /* should not happen */
@@ -64,6 +64,7 @@ extern void print_node(NODE *node, char side, int depth, int details);
extern void print_tree(TREE *tree, int details);
extern void print_trees(int details);
extern TREE *new_tree(char *name);
extern NODE *dup_node(NODE *src);
extern NODE *build_tree(int *desc, int size);
extern void gen_tree(int *seq, int n, int nb1, int nb0);
extern TREE *nth_tree(int n);
@@ -73,6 +74,7 @@ extern int n_trees();
extern void print_stack(STACK *stack, int details);
extern void print_stacks();
extern int keep_stack(STACK *stack);
//extern STACK *new_stack(int size, char *name, int keep);
extern STACK *new_stack(int size, char *name, int keep);
extern int *push_stack(STACK *stack, int val);
extern int *pop_stack(STACK *stack);
@@ -100,10 +102,10 @@ extern char *nth_comb(int n);
//extern int eval_cell(STACKELT *pos);
//extern int eval_stack(STACK *stack);
/* tree version */
extern int eval_node(NODE *node, int depth, int *pvals, char *pops);
extern int eval_node(NODE *node, int depth, int *pvals, char *pops, int *ncalcs);
/* best.c */
extern void set_garget (int n);
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();

109
stack.c
View File

@@ -4,7 +4,10 @@
#include "lceb.h"
static STACK *stacks=NULL;
static int nstacks=NULL;
static STACK *allstacks=NULL;
static int nstacks=0;
static int totalstacks=0;
static int laststack=0;
void print_stack(stack, details)
STACK *stack;
@@ -27,13 +30,17 @@ void print_stack(stack, details)
}
}
print_stacks()
void print_stacks()
{
STACK *stack=stacks;
printf("nstacks=%d\n", nstacks);
while (stack) {
print_stack(stack, 1);
stack=stack->next;
int i;
//STACK *stack=stacks;
printf("nstacks=%d laststack=%d totalstacks=%d\n", nstacks, laststack, totalstacks);
//while (stack) {
for (i=0; i<laststack; ++i) {
printf("stack %d=", i);
//stack=stack->next;
print_stack(nth_stack(i), 1);
//stack=stack->next;
}
}
@@ -43,6 +50,7 @@ int keep_stack(stack)
stack->next=stacks;
stacks=stack;
nstacks++;
return 1;
}
STACK *new_stack(size, name, keep)
@@ -54,27 +62,39 @@ STACK *new_stack(size, name, keep)
int *pelt;
int i;
stack=malloc(sizeof (STACK));
stack->stack=NULL;
stack->size=0;
if (!allstacks) {
totalstacks=ALLOCSIZE;
printf("new_stack %d: allocating %d stacks\n", laststack, totalstacks);
allstacks=malloc(totalstacks*sizeof (STACK));
}
if (laststack==totalstacks) {
totalstacks+=ALLOCSIZE;
printf("new_stack %d: resizing stacks array to %d\n", laststack, totalstacks);
allstacks=realloc(stacks, totalstacks*sizeof (STACK));
}
stack=allstacks+laststack;
printf("new_stack %d/%d: address=%p\n", laststack, totalstacks, stack);
laststack++;
//malloc(sizeof (STACK));
//stack->stack=NULL;
//stack->size=0;
stack->last=0;
//stack->eval=0;
strncpy(stack->name, name? name: "No name", sizeof(stack->name));
if (size) {
pelt=malloc(size*sizeof(int));
stack->size=size;
stack->stack=pelt;
if (keep)
keep_stack(stack);
for (i=0; i<size; ++i) {
//(pelt+i)->nop=0;
//(pelt+i)->curop=0;
//(pelt+i)->op[0]=End;
pelt[i]=-1;
//(pelt+i)->next=i+1;
//(pelt+i+1)->prev=i;
}
//(pelt+i-1)->next=-1;
//if (size) {
//pelt=malloc(size*sizeof(int));
stack->size=MAXINPUT;
//stack->stack=pelt;
if (keep)
keep_stack(stack);
for (i=0; i<MAXINPUT; ++i) {
//(pelt+i)->nop=0;
//(pelt+i)->curop=0;
//(pelt+i)->op[0]=End;
stack->stack[i]=-1;
//(pelt+i)->next=i+1;
//(pelt+i+1)->prev=i;
}
return stack;
}
@@ -174,20 +194,35 @@ int gen_stacks(stack)
char name[80];
int last=stack->last;
int n=1;
STACK *new=stack;
STACK *new;
int exists=1;
printf("sorting stack...\n");
//printf("before sort: ");
//print_stack(stack, 0);
mergesort_stack(stack->stack, 0, last-1);
//printf("after sort: ");
//print_stack(stack, 0);
// push initial stack
//printf("++++++++++++++++ Adding main stack... ");
new=dup_stack(stack, "Main stack");
//keep_stack(new);
//print_stacks();
//print_stack(stack, 1);
while (exists) {
sprintf(name, "Stack copy %d", n);
new=dup_stack(new, name);
exists=permute_stack(new->stack, new->last);
//new=dup_stack(new, name);
exists=permute_stack(stack->stack, stack->last);
//printf("Permute : ");
//print_stack(stack, 0);
if (exists) {
//print_stack(new, 1);
keep_stack(new);
printf("++++++++++++++++ Adding stack... ");
new=dup_stack(stack, name);
// print_stack(new, 0);
//keep_stack(new);
}
// printf("---------------------- Stack... ");
// print_stacks();
n++;
}
}
@@ -224,15 +259,15 @@ void mergesort_stack(array, left, right)
STACK *nth_stack(n)
int n;
{
int i;
STACK *stack=stacks;
for (i=0; i<n && stack; ++i) {
stack=stack->next;
}
return stack;
//int i;
//STACK *stack=stacks;
//for (i=0; i<n && stack; ++i) {
// stack=stack->next;
//}
return allstacks+n;
}
int n_stacks()
{
return nstacks;
return laststack;
}

124
tree.c
View File

@@ -60,26 +60,75 @@ void print_node(node, side, depth, details)
{
int i;
if (details) {
/* left padding */
for (i=0; i<=depth; ++i)
printf(" ");
printf("%c: type:%s ", side, node->type==TREE_NODE? "node": "leaf");
if (details) {
printf("op=%c val=%d eval=%d", node->op, node->val, node->eval);
}
putchar('\n');
} else {
if (node->type==TREE_NODE) {
printf("* ");
} else {
printf("x ");
}
if (!node)
return;
switch (details) {
case 1:
if (node->type==TREE_NODE) {
printf("%c ", node->op);
} else {
printf("%d ", node->val);
}
print_node(node->left, TREE_LEFT, depth+1, details);
print_node(node->right, TREE_RIGHT, depth+1, details);
break;
case 0:
print_node(node->left, TREE_LEFT, depth+1, details);
print_node(node->right, TREE_RIGHT, depth+1, details);
if (node->type==TREE_NODE) {
printf("%c ", node->op);
} else {
printf("%d ", node->val);
}
break;
case 3:
if (node->type==TREE_NODE) {
if (!depth) {
printf("(%c", node->op);
//printf("(op");
} else {
//printf(" (op");
printf(" (%c", node->op);
}
} else {
printf(" %d", node->val);
}
print_node(node->left, TREE_LEFT, depth+1, details);
print_node(node->right, TREE_RIGHT, depth+1, details);
if (node->type==TREE_NODE) {
printf(")");
}
break;
case 4:
if (node->type==TREE_NODE) {
printf("(");
}
print_node(node->left, TREE_LEFT, depth+1, details);
if (node->type==TREE_NODE) {
printf(" %c ", node->op);
} else {
printf("%d", node->val);
}
print_node(node->right, TREE_RIGHT, depth+1, details);
if (node->type==TREE_NODE) {
printf(")");
}
break;
case 2:
/* left padding */
for (i=0; i<=depth; ++i)
printf(" ");
printf("%c: type:%s ", side, node->type==TREE_NODE? "node": "leaf");
if (details) {
printf("op=%c val=%d eval=%d", node->op, node->val, node->eval);
}
putchar('\n');
print_node(node->left, TREE_LEFT, depth+1, details);
print_node(node->right, TREE_RIGHT, depth+1, details);
break;;
}
if (node->left)
print_node(node->left, TREE_LEFT, depth+1, details);
if (node->right)
print_node(node->right, TREE_RIGHT, depth+1, details);
}
void print_tree(tree, details)
@@ -87,16 +136,18 @@ void print_tree(tree, details)
int details;
{
int i;
if (details) {
printf("Tree [%s] nodes:%d leaves:%d depth:%d\n",
tree->name, tree->nodes, tree->leaves, tree->depth);
print_node(tree->head, TREE_TOP, 0, 1);
} else {
printf("Tree [%s] : ", tree->name);
print_node(tree->head, TREE_TOP, 0, 0);
// for (i=0; )
printf("\n");
switch (details) {
case 2:
print_node(tree->head, TREE_TOP, 0, details);
break;
case 0:
case 1:
case 3:
case 4:
//printf("Tree [%s] : ", tree->name);
print_node(tree->head, TREE_TOP, 0, details);
printf("\n");
break;
}
}
@@ -201,10 +252,12 @@ void gen_tree(seq, n, nb1, nb0)
if((nb1 + nb0) == 2*n) { /* end */
seq[2*n] = 0;
ntrees++;
# ifdef DEBUG1
printf("tree %d desc=", ntrees);
for (i=0; i<=2*n; ++i)
printf("%d", seq[i]);
putchar('\n');
# endif
sprintf(name, "Tree %d", ntrees);
tree=new_tree(name);
tree->head=build_tree(seq, 2*n+1);
@@ -243,11 +296,20 @@ main(ac, av)
int ac;
char **av;
{
int n;
int n, details=0;
int array[1024];
if (ac<2 || ac>3) {
fprintf(stderr, "usage: %s nodes [type]\n", *av);
fprintf(stderr, "type can be 0 (RPN, default, 1 (polish), 2 (details), 3 (lisp notation), 4 (parenthesed notation)\n");
exit (1);
}
if (ac==3) {
details=atoi(av[2]);
}
n=atoi(av[1]);
gen_tree(array, n, 0, 0);
print_trees(1);
print_trees(details);
exit(0);
}
#endif