home *** CD-ROM | disk | FTP | other *** search
/ CICA 1994 September / CICA_Shareware_for_Windows_Walnut_Creek_September_1994.iso / win3 / programr / atre27.exe / ATREE_27 / MULTWIN / MULT.C next >
C/C++ Source or Header  |  1992-08-01  |  7KB  |  225 lines

  1. /*****************************************************************************
  2.  ****                                                                     ****
  3.  **** mult.c                                                              ****
  4.  ****                                                                     ****
  5.  **** atree release 2.7 for Windows                                       ****
  6.  **** Adaptive Logic Network (ALN) simulation program.                    ****
  7.  **** Copyright (C) A. Dwelly, R. Manderscheid, M. Thomas, W.W. Armstrong ****
  8.  ****               1991, 1992                                            ****
  9.  **** License:                                                            ****
  10.  **** A royalty-free license is granted for the use of this software for  ****
  11.  **** NON_COMMERCIAL PURPOSES ONLY. The software may be copied and/or     ****
  12.  **** modified provided this notice appears in its entirety and unchanged ****
  13.  **** in all derived source programs.  Persons modifying the code are     ****
  14.  **** requested to state the date, the changes made and who made them     ****
  15.  **** in the modification history.                                        ****
  16.  ****                                                                     ****
  17.  **** Patent License:                                                     ****
  18.  **** The use of a digital circuit which transmits a signal indicating    ****
  19.  **** heuristic responsibility is protected by U. S. Patent 3,934,231     ****
  20.  **** and others assigned to Dendronic Decisions Limited of Edmonton,     ****
  21.  **** W. W. Armstrong, President.  A royalty-free license is granted      ****
  22.  **** by the company to use this patent for NON_COMMERCIAL PURPOSES to    ****
  23.  **** adapt logic trees using this program and its modifications.         ****
  24.  ****                                                                     ****
  25.  **** Limited Warranty:                                                   ****
  26.  **** This software is provided "as is" without warranty of any kind,     ****
  27.  **** either expressed or implied, including, but not limited to, the     ****
  28.  **** implied warrantees of merchantability and fitness for a particular  ****
  29.  **** purpose.  The entire risk as to the quality and performance of the  ****
  30.  **** program is with the user.  Neither the authors, nor the             ****
  31.  **** University of Alberta, its officers, agents, servants or employees  ****
  32.  **** shall be liable or responsible in any way for any damage to         ****
  33.  **** property or direct personal or consequential injury of any nature   ****
  34.  **** whatsoever that may be suffered or sustained by any licensee, user  ****
  35.  **** or any other party as a consequence of the use or disposition of    ****
  36.  **** this software.                                                      ****
  37.  **** Modification history:                                               ****
  38.  ****                                                                     ****
  39.  **** 90.09.05 Initial implementation, A.Dwelly                           ****
  40.  **** 91.04.15 Port to PC and minor bug fixes, R. Manderscheid            ****
  41.  **** 91.05.20 Windows Port & Windows Extensions, M. Thomas               ****
  42.  **** 91.07.17 atree v2.0 for Windows, M. Thomas                          ****
  43.  **** 92.04.27 atree v2.5 for Windows, M. Thomas                          ****
  44.  **** 92.03.07 Release 2.6, Monroe Thomas                                 ****
  45.  **** 92.01.08 Release 2.7, Monroe Thomas                                 ****
  46.  ****                                                                     ****
  47.  *****************************************************************************/
  48.  
  49. /* multiplexor test */
  50.  
  51. #include <stdio.h>
  52. #include <windows.h>
  53. #include "atree.h"
  54.  
  55. #define CONTROL_BITS    3
  56.  
  57. /* number of leaves should increase with number of control bits */
  58.  
  59. #define LEAVES          1536
  60. #define TEST_SIZE       500
  61. #define TRAIN_SIZE      500
  62. #define VOTERS          1
  63. #define EPOCHS          100
  64. #define VERBOSITY       1
  65.  
  66. #define WIDTH           ((CONTROL_BITS) + (1 << (CONTROL_BITS)))
  67.  
  68. #define Printf(str,fmt1,fmt2) \
  69.             { \
  70.             char Buff[80]; \
  71.             sprintf(Buff, str, fmt1, fmt2); \
  72.             MessageBox(hwnd, Buff, "Multiplexor", MB_OK); \
  73.             }
  74.  
  75.  
  76. BOOL quit_flag;
  77.  
  78. char multiplexor(v)
  79. char *v;
  80. {
  81. int i;
  82. int lead = 0;
  83.  
  84.     for (i = 0; i < CONTROL_BITS; i++)
  85.     {
  86.        lead = (lead << 1) + v[i];
  87.     }
  88.     return(v[lead + CONTROL_BITS]);
  89. }
  90.  
  91. int NEAR PASCAL
  92. main(HWND hwnd, HANDLE hInstance)
  93. {
  94.     LPATREE tree[VOTERS];
  95.     LPBIT_VEC training_set;
  96.     LPBIT_VEC result_set;
  97.     LPBIT_VEC test;
  98.     char vec[WIDTH];
  99.     char unpacked_result[1];
  100.     int correct;
  101.     int voter;
  102.     int i;
  103.     int j;
  104.     HCURSOR hCursor;
  105.  
  106.     /* Initialize */
  107.  
  108.     quit_flag = FALSE;
  109.  
  110.     training_set = (LPBIT_VEC) Malloc(TRAIN_SIZE * sizeof(bit_vec));
  111.     MEMCHECK(training_set);
  112.  
  113.     result_set = (LPBIT_VEC) Malloc(TRAIN_SIZE * sizeof(bit_vec));
  114.     MEMCHECK(result_set);
  115.  
  116.     atree_init(hInstance, hwnd);
  117.  
  118. /* if VOTERS is odd the next loop will generate a unreachable code
  119.    error - use pragma to turn off checking */
  120.  
  121. #pragma warn -rch
  122. #pragma warn -ccc
  123.     if (VOTERS % 2 != 1)
  124.     {
  125.        MessageBox(hwnd, "VOTERS must be odd", "MULTIPLEXOR", MB_OK);
  126.        return(FALSE);
  127.     }
  128. #pragma warn .ccc
  129. #pragma warn .rch
  130.  
  131.     /* Create the training data */
  132.  
  133.     for (i = 0; i < TRAIN_SIZE; i++)
  134.     {
  135.  
  136.         /* allow multitasking during long loop! */
  137.         Windows_Interrupt(1500);
  138.  
  139.         if (quit_flag) exit(0);
  140.  
  141.         for (j = 0; j < WIDTH; j++)
  142.         {
  143.             vec[j] = RANDOM(2);
  144.         }
  145.         training_set[i] = *(bv_pack(vec, WIDTH));
  146.         unpacked_result[0] = multiplexor(vec);
  147.         result_set[i] = *(bv_pack(unpacked_result, 1));
  148.     }
  149.  
  150.     if (quit_flag) exit(0);
  151.  
  152.     /* Create a tree and train it */
  153.  
  154.     for (voter = 0; voter < VOTERS; voter++)
  155.     {
  156.         tree[voter] = atree_create(WIDTH, LEAVES);
  157.         (void) atree_train(tree[voter], training_set, result_set, 0,
  158.                            TRAIN_SIZE, TRAIN_SIZE-1, EPOCHS, VERBOSITY);
  159.     }
  160.  
  161.     if (quit_flag) exit(0);
  162.  
  163.     /* Test the trained tree */
  164.  
  165.     correct = 0;
  166.     for (i = 0; i < TEST_SIZE; i++)
  167.     {
  168.         int weight = 0;
  169.  
  170.         /* allow multitasking during long loop! */
  171.         Windows_Interrupt(1500);
  172.  
  173.         if (quit_flag) exit(0);
  174.  
  175.         for (j = 0; j < WIDTH; j++)
  176.         {
  177.             vec[j] = RANDOM(2);
  178.         }
  179.         test = bv_pack(vec, WIDTH);
  180.  
  181.         for (voter = 0; voter < VOTERS; voter++)
  182.         {
  183.             weight += atree_eval(tree[voter], test);
  184.         }
  185.  
  186.         if (multiplexor(vec) == (weight > VOTERS / 2))
  187.         {
  188.             correct++;
  189.         }
  190.  
  191.         bv_free(test);
  192.     }
  193.  
  194.     if (quit_flag) exit(0);
  195.  
  196.     Printf("%d correct out of %d in final test\n", correct, TEST_SIZE);
  197.  
  198.     /* Discard training set */
  199.  
  200.     hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  201.     ShowCursor(TRUE);
  202.  
  203.     for (i = 0; i < TRAIN_SIZE; i++)
  204.         {
  205.         Free(training_set[i].bv);
  206.         Free(result_set[i].bv);
  207.         }
  208.  
  209.     Free(training_set);
  210.     Free(result_set);
  211.  
  212.     ShowCursor(FALSE);
  213.     SetCursor(hCursor);
  214.  
  215.     /* Discard tree */
  216.  
  217.     for (voter = 0; voter < VOTERS; voter++)
  218.     {
  219.         atree_free(tree[voter]);
  220.     }
  221.  
  222.     atree_quit();
  223.     return(TRUE);
  224. }
  225.