home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ainet / t3runtim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-11  |  12.5 KB  |  353 lines

  1. /* ----------------------------------------------------------------------- *
  2.  *                                                                         *
  3.  *    (C) Copyright 1996 by:  aiNet                                        *
  4.  *                                        Trubarjeva 42                                *
  5.  *                            SI-3000 Celje                                *
  6.  *                                        Europe, Slovenia                             *
  7.  *     All Rights Reserved                                                 *
  8.  *                                                                         *
  9.  *     Subject: C code for single vector prediction.                       *
  10.  *     File:    T3RUNTIM - The XOR problem with low level model creation   *
  11.  *                                                                         *
  12.  * ----------------------------------------------------------------------- */
  13.  
  14. /*--------------------------------------------------------------------------
  15.     Here it will be shown how we can colve the XOR problem using
  16.     aiNet C functions
  17.  
  18.     The XOR problem:
  19.     ================
  20.         Number of model vectors: 4
  21.              Number of variables: 3
  22.      Number of input variables: 3
  23.          Any discrete variables: NONE
  24.  
  25.         Model vectors:  Inp,Inp,Out
  26.                   row 1:  1,  1,  0
  27.                   row 2:  1,  0,  1
  28.                   row 3:  0,  1,  1
  29.                   row 4:  0,  0,  0
  30.  
  31.     Test vectors (vectors which will be used in prediction) together with
  32.     penalty coefficient and penalty method.
  33.  
  34.          Prediction vectors:  Inp  Inp  Out
  35.                           prd 1:  0.9  0.1  ??
  36.                           prd 2:  0.1  0.9  ??
  37.                           prd 3:  0.2  0.2  ??
  38.                           prd 4:  0.7  0.7  ??
  39.  
  40.          Penalty coeffcient: 0.5
  41.          Penalty methods: DYNAMIC
  42.  
  43.     NOTE: Selected penalty coefficients are in no case optimal.
  44.             They were selected randomly, to perform a few tests.
  45.             The test results were compared with the results calculated by
  46.             the main aiNet 1.14 application.
  47.  
  48.     --------------------------------------------------------------------------
  49.     Results (rounded at fourth decimal):
  50.     --------------------------------------------------------------------------
  51.  
  52.          Penalty cefficient: 0.5
  53.          Penalty method:     DYNAMIC
  54.                                                   (RESULT)
  55.          Prediction vectors:  Inp  Inp  (  Out )
  56.                           prd 1:  0.9  0.1  (0.6948)
  57.                           prd 2:  0.1  0.9  (0.6948)
  58.                           prd 3:  0.2  0.2  (0.3321)
  59.                           prd 4:  0.7  0.7  (0.3869)
  60.  
  61.  ---------------------------------------------------------------------------*/
  62.  
  63. /*
  64.  * This file assumes that ainetxx.dll will be loaded at run time,
  65.  * which is default option and no flags need to be defined.
  66.  * ainetxx.lib must NOT be included in the linking process.
  67.  */
  68.  
  69. #include "ainetdll.h"
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72.  
  73. /*
  74.  * Path and the filename of dll which will be loaded.
  75.  */
  76.  
  77. #if defined(__WIN32__)
  78. const char ainetDll[] = "ainet32.dll";
  79. #else
  80. const char ainetDll[] = "ainet16.dll";
  81. #endif
  82. /*
  83.  * Pointers to ainet dll functions. They are made global - all functions
  84.  * can use them.
  85.  */
  86.  
  87. t_aiRegistration              aiRegistration;
  88. t_aiGetVersion                aiGetVersion;
  89. t_aiCreateModel               aiCreateModel;
  90. t_aiCreateModelFromCSVFile    aiCreateModelFromCSVFile;
  91. t_aiDeleteModel               aiDeleteModel;
  92. t_aiNormalize                 aiNormalize;
  93. t_aiDenormalize               aiDenormalize;
  94. t_aiPrediction                aiPrediction;
  95. t_aiGetNumberOfVariables      aiGetNumberOfVariables;
  96. t_aiGetNumberOfModelVectors   aiGetNumberOfModelVectors;
  97. t_aiGetNumberOfInputVariables aiGetNumberOfInputVariables;
  98. t_aiSetDiscreteFlag           aiSetDiscreteFlag;
  99. t_aiGetDiscreteFlag           aiGetDiscreteFlag;
  100. t_aiSetVariable               aiSetVariable;
  101. t_aiGetVariable               aiGetVariable;
  102. t_aiGetVariableVB             aiGetVariableVB;
  103. t_aiGetCSVFileModelSize       aiGetCSVFileModelSize;
  104. // New in version 1.24
  105. t_aiSetCapacity                    aiSetCapacity;
  106. t_aiGetCapacity                    aiGetCapacity;
  107. t_aiGetFreeEntries                aiGetFreeEntries;
  108. t_aiInsertModelVector            aiInsertModelVector;
  109. t_aiOverwriteModelVector        aiOverwriteModelVector;
  110. t_aiAppendModelVector            aiAppendModelVector;
  111. t_aiDeleteModelVector            aiDeleteModelVector;
  112. t_aiPredictionEx                    aiPredictionEx;
  113. t_aiExcludeModelVector            aiExcludeModelVector;
  114. t_aiExcludeModelVectorRange    aiExcludeModelVectorRange;
  115. t_aiIsModelVectorExcluded        aiIsModelVectorExcluded;
  116. t_aiSaveCSVFile                    aiSaveCSVFile;
  117.  
  118.  
  119. /*
  120.  *  ainet32.dll module variable.
  121.  */
  122.  
  123. HINSTANCE hLib;
  124.  
  125. /*
  126.  *  The load_aiNetLibrary() function is implemented below.
  127.  *  This function will load ainet32.dll and define pointers to
  128.  *  ainet functions.
  129.  */
  130.  
  131. int load_aiNetLibrary(void);
  132.  
  133. /*
  134.  *
  135.  */
  136.  
  137. void main()
  138. {
  139.     /*
  140.      * Here we present how to create a model in a "low level" way.
  141.      * We recommend you to avoid this model creation type. Use rather
  142.      * aiCreateModel functions instead. Please also note that model must
  143.      * not be deleted using aiDeleteModel function.
  144.      */
  145.  
  146.     int i;
  147.     int version;
  148.  
  149.     float* data[4];                /* Model data    */
  150.     float row1[3] = { 1,1,0 }; /* model vectors */
  151.     float row2[3] = { 1,0,1 };
  152.     float row3[3] = { 0,1,1 };
  153.     float row4[3] = { 0,0,0 };
  154.     int   disc[3] = {0,0,0};    /* Discrete flags */
  155.     float n1[3];                    /* Normalization  */
  156.     float n2[3];                    /* buffers        */
  157.    unsigned char flag[4];     /* NEW 1.24: additional information for prediction */
  158.  
  159.     aiModel model = { 0, /* data     */
  160.                             4, /* nMV      */
  161.                             3, /* nVar     */
  162.                             2, /* ni       */
  163.                             0, /* discrete */
  164.                             0, /* n1       */
  165.                             0  /* n2       */
  166.     };
  167.  
  168.     float predict[4][3] = { { 0.9,0.1, 999 },   /* vectors to be predicted */
  169.                                     { 0.1,0.9, 999 },
  170.                                     { 0.2,0.2, 999 },
  171.                                     { 0.7,0.7, 999 } };
  172.  
  173.     /*
  174.      * Setup the model
  175.      */
  176.  
  177.     data[0] = row1;
  178.     data[1] = row2;
  179.     data[2] = row3;
  180.     data[3] = row4;
  181.     model.data = data;
  182.     model.discrete = disc;
  183.     model.n1 = n1;
  184.     model.n2 = n2;
  185.    model.flag = flag;
  186.  
  187.    /*
  188.     * Load DLL
  189.     */
  190.    if( !load_aiNetLibrary() ) {
  191.         exit(EXIT_FAILURE);
  192.     }
  193.  
  194.     /*
  195.      * Title
  196.      */
  197.  
  198.     version = aiGetVersion();
  199.     printf( "\naiNetDLL version %i.%i! (C) Copyright by aiNet, 1996",
  200.               version/100, version%100 );
  201.     printf( "\n---------------------------------------------------\n" );
  202.  
  203.     /*
  204.      * Register DLL
  205.      */
  206.  
  207.     aiRegistration( "Your registration name", "Your code" );
  208.  
  209.     /*
  210.      * Output the model
  211.      */
  212.  
  213.     printf( "\n             Model name: aiNet DLL test 3 (Low level creation)");
  214.     printf( "\nNumber of model vectors: %i", aiGetNumberOfModelVectors(&model));
  215.     printf( "\n    Number of variables: %i", aiGetNumberOfVariables(&model));
  216.     printf( "\n         Variable names: A,   B,   A xor B" );
  217.     printf( "\n          Discrete flag: %i,   %i,   %i",
  218.               aiGetDiscreteFlag(&model,1),
  219.               aiGetDiscreteFlag(&model,2),
  220.               aiGetDiscreteFlag(&model,3) );
  221.     for( i=1; i<=aiGetNumberOfModelVectors(&model); i++ ) {
  222.         printf( "\n\t\t\t %3.1lf, %3.1lf, %3.1lf",
  223.                   aiGetVariable(&model, i,1),
  224.                   aiGetVariable(&model, i,2),
  225.                   aiGetVariable(&model, i,3) );
  226.      }
  227.  
  228.     /*
  229.      * Normalize the model
  230.      */
  231.  
  232.     aiNormalize( &model, NORMALIZE_STATISTICAL );
  233.  
  234.     /*
  235.      * Prediction: Pen. coefficient = 0.50, Pen. method = STATIC
  236.      * This test has dynamic penalty coefficient 0.50
  237.      */
  238.  
  239.     printf( "\n\n  Penalty coefficient: 0.50" );
  240.     printf(   "\n       Penalty method: DYNAMIC" );
  241.     printf(   "\n\t A(inp), B(inp), A xor B(out)" );
  242.     for ( i=0; i<4; i++ ) {
  243.         aiPrediction( &model, predict[i], 0.50, PENALTY_DYNAMIC );
  244.         printf( "\n\t%7.4f, %7.4f, %7.4f",
  245.                   predict[i][0],predict[i][1],predict[i][2] );
  246.     }
  247.  
  248.     /*
  249.      * Denormalize the model (in this case it is not necessary)
  250.      */
  251.  
  252.     aiDenormalize(&model);
  253.  
  254.     /*
  255.      * We must not call the aiDeleteModel function here since the model
  256.      * was not allocated dynamicaly using the aiCreateModel function.
  257.      */
  258.  
  259.    FreeLibrary(hLib);
  260.  
  261.     printf( "\n\nEnd." );
  262.     exit(EXIT_SUCCESS);
  263. }
  264.  
  265. int load_aiNetLibrary()
  266. {
  267.    /*
  268.     * Load the Dynamic Link Library AINET32.DLL
  269.     */
  270.  
  271.    hLib = LoadLibrary(ainetDll);
  272.    if((unsigned)hLib<=HINSTANCE_ERROR){
  273.       char bfr[40];
  274.       wsprintf(bfr, "Failure loading library: %s", ainetDll);
  275.       MessageBox(NULL, bfr, "Error", MB_OK|MB_APPLMODAL);
  276.       return 0;
  277.    }
  278.  
  279.    /*
  280.     * Get all the entry points for the functions in ainet32.dll
  281.     */
  282.  
  283.     aiRegistration              = (t_aiRegistration)              GetProcAddress(hLib, "aiRegistration");
  284.    aiGetVersion                = (t_aiGetVersion)                GetProcAddress(hLib, "aiGetVersion");
  285.    aiCreateModel               = (t_aiCreateModel)               GetProcAddress(hLib, "aiCreateModel");
  286.    aiCreateModelFromCSVFile    = (t_aiCreateModelFromCSVFile)    GetProcAddress(hLib, "aiCreateModelFromCSVFile");
  287.    aiDeleteModel               = (t_aiDeleteModel)               GetProcAddress(hLib, "aiDeleteModel");
  288.    aiNormalize                 = (t_aiNormalize)                 GetProcAddress(hLib, "aiNormalize");
  289.    aiDenormalize               = (t_aiDenormalize)               GetProcAddress(hLib, "aiDenormalize");
  290.    aiPrediction                = (t_aiPrediction)                GetProcAddress(hLib, "aiPrediction");
  291.    aiGetNumberOfVariables      = (t_aiGetNumberOfVariables)      GetProcAddress(hLib, "aiGetNumberOfVariables");
  292.    aiGetNumberOfModelVectors   = (t_aiGetNumberOfModelVectors)   GetProcAddress(hLib, "aiGetNumberOfModelVectors");
  293.    aiGetNumberOfInputVariables = (t_aiGetNumberOfInputVariables) GetProcAddress(hLib, "aiGetNumberOfInputVariables");
  294.    aiSetDiscreteFlag           = (t_aiSetDiscreteFlag)           GetProcAddress(hLib, "aiSetDiscreteFlag");
  295.    aiGetDiscreteFlag           = (t_aiGetDiscreteFlag)           GetProcAddress(hLib, "aiGetDiscreteFlag");
  296.    aiSetVariable               = (t_aiSetVariable)               GetProcAddress(hLib, "aiSetVariable");
  297.    aiGetVariable               = (t_aiGetVariable)               GetProcAddress(hLib, "aiGetVariable");
  298.    aiGetVariableVB             = (t_aiGetVariableVB)             GetProcAddress(hLib, "aiGetVariableVB");
  299.    aiGetCSVFileModelSize       = (t_aiGetCSVFileModelSize)       GetProcAddress(hLib, "aiGetCSVFileModelSize");
  300.    aiSetCapacity               = (t_aiSetCapacity)               GetProcAddress(hLib, "aiSetCapacity");
  301.    aiGetCapacity               = (t_aiGetCapacity)               GetProcAddress(hLib, "aiGetCapacity");
  302.    aiGetFreeEntries            = (t_aiGetFreeEntries)            GetProcAddress(hLib, "aiGetFreeEntries");
  303.    aiInsertModelVector         = (t_aiInsertModelVector)         GetProcAddress(hLib, "aiInsertModelVector");
  304.    aiOverwriteModelVector      = (t_aiOverwriteModelVector)      GetProcAddress(hLib, "aiOverwriteModelVector");
  305.    aiAppendModelVector         = (t_aiAppendModelVector)         GetProcAddress(hLib, "aiAppendModelVector");
  306.    aiDeleteModelVector         = (t_aiDeleteModelVector)         GetProcAddress(hLib, "aiDeleteModelVector");
  307.    aiPredictionEx              = (t_aiPredictionEx)              GetProcAddress(hLib, "aiPredictionEx");
  308.    aiExcludeModelVector        = (t_aiExcludeModelVector)        GetProcAddress(hLib, "aiExcludeModelVector");
  309.    aiExcludeModelVectorRange   = (t_aiExcludeModelVectorRange)   GetProcAddress(hLib, "aiExcludeModelVectorRange");
  310.    aiIsModelVectorExcluded     = (t_aiIsModelVectorExcluded)     GetProcAddress(hLib, "aiIsModelVectorExcluded");
  311.    aiSaveCSVFile               = (t_aiSaveCSVFile)               GetProcAddress(hLib, "aiSaveCSVFile");
  312.  
  313.    /*
  314.     * GetProcAddress returns null on failure
  315.     */
  316.    if( aiRegistration == NULL
  317.        || aiGetVersion == NULL
  318.        || aiCreateModel == NULL
  319.        || aiCreateModelFromCSVFile == NULL
  320.        || aiDeleteModel == NULL
  321.        || aiNormalize == NULL
  322.        || aiDenormalize == NULL
  323.        || aiPrediction == NULL
  324.        || aiGetNumberOfVariables == NULL
  325.        || aiGetNumberOfModelVectors == NULL
  326.        || aiGetNumberOfInputVariables == NULL
  327.        || aiSetDiscreteFlag == NULL
  328.        || aiGetDiscreteFlag == NULL
  329.        || aiSetVariable == NULL
  330.        || aiGetVariable == NULL
  331.        || aiGetVariableVB == NULL
  332.        || aiGetCSVFileModelSize == NULL
  333.          || aiSetCapacity == NULL
  334.        || aiGetCapacity == NULL
  335.        || aiGetFreeEntries == NULL
  336.        || aiInsertModelVector == NULL
  337.        || aiOverwriteModelVector == NULL
  338.        || aiAppendModelVector == NULL
  339.        || aiDeleteModelVector == NULL
  340.        || aiPredictionEx == NULL
  341.        || aiExcludeModelVector == NULL
  342.        || aiExcludeModelVectorRange == NULL
  343.        || aiIsModelVectorExcluded == NULL
  344.        || aiSaveCSVFile == NULL ) {
  345.         MessageBox(NULL, "Failure locating procedures.", "Error",
  346.             MB_OK|MB_APPLMODAL);
  347.       return 0;
  348.     }
  349.    return 1;
  350. }
  351.  
  352. /* THE END */
  353.