home *** CD-ROM | disk | FTP | other *** search
- /* A three control bit multiplexor test (11 bits total) */
-
- #include <stdio.h>
- #include <windows.h>
- #include "atree.h"
-
- #define TRAINSETSIZE 500
- #define TREESIZE 1024
- #define WIDTH 11
- #define TESTSIZE 500
-
- #define Printf(str,fmt1,fmt2) \
- { \
- char Buff[80]; \
- sprintf(Buff, str, fmt1, fmt2); \
- MessageBox(hwnd, Buff, "Multiplexor", MB_OK); \
- }
-
- char multiplexor(v)
-
- char *v;
-
- {
- return(v[(int)(((v[0] << 2) | (v[1] << 1) | v[2]) + 3)]);
- }
-
- BOOL NEAR PASCAL main(HWND hwnd)
- {
- int i;
- int j;
- LPBIT_VEC training_set;
- LPBIT_VEC icres;
- LPBIT_VEC test;
- char vec[WIDTH];
- char ui[1];
- int correct = 0;
- LPATREE tree;
- HCURSOR hCursor;
-
- /* Initialise */
-
- training_set = (LPBIT_VEC) Malloc(TRAINSETSIZE * sizeof(bit_vec));
- MEMCHECK(training_set);
-
- icres = (LPBIT_VEC) Malloc(TRAINSETSIZE * sizeof(bit_vec));
- MEMCHECK(icres);
-
- atree_init();
-
- /* Create the test data */
-
- Printf("Creating training data\n",0,0); /*Printf macro requires 2 args*/
-
- for (i = 0; i < TRAINSETSIZE; i++)
- {
- /* allow multitasking during long loop! */
- Windows_Interrupt(2000);
-
- for (j = 0; j < WIDTH; j++)
- {
- vec[j] = RANDOM(2);
- }
- training_set[i] = *(bv_pack(vec,WIDTH));
- ui[0] = multiplexor(vec);
- icres[i] = *(bv_pack(ui,1));
- }
-
- /* Create a tree and train it */
-
- Printf("Training tree\n",0,0);
-
- tree = atree_create(WIDTH,TREESIZE);
- (void) atree_train(tree,training_set,icres,0,TRAINSETSIZE,
- TRAINSETSIZE-1,100,1);
-
- /* Test the trained tree */
-
- Printf("Testing the tree\n",0,0);
-
- for (i = 0; i < TESTSIZE; i++)
- {
- /* allow multitasking during long loop! */
- Windows_Interrupt(2000);
-
- for (j = 0; j < WIDTH; j++)
- {
- vec[j] = RANDOM(2);
- }
- test = bv_pack(vec,WIDTH);
- if (atree_eval(tree,test) == multiplexor(vec))
- {
- correct++;
- }
- bv_free(test);
- }
-
- Printf("%d correct out of %d in final test\n",correct,TESTSIZE);
-
- /* Discard training set */
-
- Printf("Please wait, discarding training set",0,0);
-
- hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
- ShowCursor(TRUE);
-
- for (i = 0; i < TRAINSETSIZE; i++)
- {
- Free(training_set[i].bv);
- Free(icres[i].bv);
- }
-
- Free(training_set);
- Free(icres);
-
- ShowCursor(FALSE);
- SetCursor(hCursor);
-
- /* Discard tree */
- atree_free(tree);
-
- return TRUE;
- }
-