home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR36 / FCM2.ZIP / FCM.CPP < prev    next >
C/C++ Source or Header  |  1993-09-26  |  6KB  |  189 lines

  1. // *************************************************************************
  2. // File:  FCM.CPP                                      Date:  September 25, 1993
  3. // By:  Josef Betancourt  CIS 71351,356     System:  Borland C++ ver. 2.0.
  4. // *************************************************************************
  5. /*
  6. Purpose:  Illustrate the Fuzzy Cognitive Map computations.
  7.  
  8.     This is a very very simple implementation of the FCM example found in:
  9. Neural Networks and Fuzzy Systems. Kosko, B. page 154.
  10. Fuzzy Thinking, the new science of fuzzy logic. Kosko, B. page 222.
  11. Fuzzy Logic. McNeill D., Freiberger, P. page 237.
  12.  
  13. I used a very nice matrix class called BUMP,
  14. Beginner's Understandable Matrix Package.  It is found in the Borland C++
  15. forum on CIS (Go BCPPDOS); the file is BUMP.ZIP.  The author is:
  16.     Clopper Almon
  17.     Compuserve: 73377,1466
  18.  
  19.  -------------------------------------------------------------------------
  20.  Of course, a general purpose interactive graphical system where the FCM
  21.  can be entered graphically by multiple experts and the inference process
  22.  is visually presented would be nice.  It should be a FCM CAD system!
  23.  But are FCMs really useful?  How are they used in control systems?
  24.  
  25. --------------------------------------------------------------------------
  26. USE:  This must be compiled and linked with BUMP.CPP. Large memory model.
  27.     The FCM is in file AFRICA.TXT.
  28.     Investment policy vector is in INVEST.TXT.
  29.     Disinvestment is in DIVEST.TXT
  30. */
  31. // -----------------------
  32. // dependencies
  33. #include <iostream.h>   // for cout
  34. //#include <strstrea.h>   // for istrstream
  35. #include <ctype.h>        // for isdigit()
  36. #include <stdio.h>        // for fgets()
  37. #include <alloc.h>        // for coreleft()
  38. #include <stdlib.h>        // for atof()
  39. #include <conio.h>        // for cprintf()
  40. #include "bump.h"       // for Matrix class
  41. // -----------------------
  42. // Prototypes
  43. void Reason(Matrix const &matFCM, Matrix &matPolicy,
  44.                          int iCell, float fValue, float fThresh);
  45. void Again(void);
  46. void ThresholdMF( Matrix & mat, float thresh = .5);
  47. void ShowCondenseMP( Matrix &Mat);
  48. void DrawBorder(void);
  49. // -----------------------
  50. int main(int argc, char * argv[]){
  51.     cout << "\n*** Fuzzy Cognitive Map example.  By Josef Betancourt ***\n";
  52.     if( argc != 7 ){
  53.      cerr << "\nUsage: fcm <FCM file> <policy file> ";
  54.      cerr << "<matrix size> <cell> <value> <thresh>\n";
  55.      cerr << "         Where cell and value refer to policy vector.\n";
  56.      exit (-1);
  57.     }
  58.     // convert strings to integers...........
  59.     int iSize, iCell;
  60.     float fValue, fThresh;
  61.     iSize = atoi(argv[3]); // matrix order.
  62.     iCell = atoi(argv[4]); // policy cell.
  63.     fValue = atof(argv[5]); // its value.
  64.     fThresh = atof( argv[6]); // threshold value.
  65.  
  66.     Matrix matFCM(iSize,iSize), matPolicy(1,iSize);
  67.  
  68.     // populate and show matrices ..........
  69.     matFCM.ReadA(argv[1]);
  70.     matPolicy.ReadA(argv[2]);
  71.     matFCM.Display("This is the FCM matrix:");
  72.     matPolicy.Display("This is the policy matrix: ");
  73.  
  74.     Again();    // wait for key tap or escape.
  75.  
  76.     Reason(matFCM, matPolicy, iCell, fValue, fThresh);
  77.  
  78. }  // ------- main end.
  79. // -----------------------
  80. void Reason(Matrix const &matFCM, Matrix &matPolicy,
  81.                              int iCell, float fValue, float fThresh){
  82.     // iteratively apply simple FCM computation using matrices.
  83.     int i = 1;  // epoch counter.
  84.     while( 1){
  85.         cout << "Epoch " << i << '\n';
  86.         matPolicy = matPolicy * matFCM;    // Matrix multiply.  Nice syntax!
  87.  
  88.         matPolicy.Display( "Policy vector before thresholding: ");
  89.         ThresholdMF( matPolicy, fThresh);
  90.  
  91.         matPolicy[1][iCell] = fValue; // since its being tested, set it.
  92.  
  93.         matPolicy.Display("New policy vector: ");
  94.  
  95.         ShowCondenseMP( matPolicy);  // for ease in seeing limit cycles.
  96.         DrawBorder();
  97.  
  98.         Again();   // wait for key tap or escape.
  99.         i++;
  100.     }
  101. }
  102. // -----------------------
  103. void ThresholdMF( Matrix & mat, float fthresh ){
  104.     // apply thresh to policy matrix, default thresh is 0.5.
  105.     for( int i = 1; i < mat.columns() + 1; i++){
  106.         if( mat[1][i] >= fthresh){
  107.             mat[1][i] = 1.;
  108.         }else{
  109.             mat[1][i] = 0;
  110.         }
  111.     }
  112. }
  113. // -----------------------
  114. void ShowCondenseMP( Matrix &Mat){
  115.     // print policy vector in a more visual pattern.
  116.     cout << "\nPattern: ";
  117.     int temp ;
  118.     for( int i=1; i< Mat.columns()+1; i++){
  119.         if( Mat[1][i] > 0 ){
  120.             temp = 0x2;
  121.         }else{
  122.             if( Mat[1][i] < 0){
  123.                 temp = 0x1F;
  124.             }else{
  125.                 temp = 0x1;
  126.             }
  127.         }
  128.         cout.put( temp);
  129.     }
  130.     cout << '\n' ;
  131. }
  132. void DrawBorder(void){
  133.     cout << '\n';
  134.     for( int i=0; i<75; i++){
  135.         cout.put( 0xDF );
  136.     }
  137.     cout << '\n';
  138. }
  139. // -----------------------
  140. void Again(void){
  141.     // query user for continuation or end of run.
  142.     cerr << "\n\t\t\t< Tap a key to continue.  ESC to exit. >\n";
  143.     if(getch() == 27){
  144.         exit(1);
  145.     }
  146. }
  147. // ----------------------------------------------------------------------
  148. /*  computing Eigenvectors is similar to computing a FCM without the
  149.      thresholding.  Here is such an algorithim.
  150.  
  151. void EigenTest(int size, char *pszName){
  152.     // algorithim from Computational Linear Algebra with Models.
  153.     // Williams, Gareth.  Allyn and Bacon, Massachusetts. 1978. page 449.
  154.      float e, s, t;
  155.      int i, j, k, l;
  156.      Matrix A(size,size), X(size,1),Y(size,1),Z(size,1);
  157.      A.ReadA(pszName);
  158.      A.Display("Here is the B matrix:");
  159.      AllOnes( X);
  160.      while(1){
  161.         cout << "Iteration " << i << "\n";
  162.         Y = A * X;
  163.         Y.Display("Here is the Y matrix:");
  164.         k=1;
  165.         for( j=2; j < Y.rows()+1;j++){
  166.             if( fabs( Y[k][1]) >= fabs( Y[j][1]) ){
  167.                 continue;
  168.             }
  169.             k= j;
  170.         }
  171.         Y = ( 1/ fabs( Y[k][1])) * Y;
  172.         X = Y;
  173.         X.Display("the adjusted vector is:");
  174.         Z = A*X;
  175.         s=0;
  176.         t=0;
  177.         for( l=1; l<size + 1; l++){
  178.             s += X[l][1] * Z[l][1];
  179.             t += X[l][1] * X[l][1];
  180.         }
  181.         e = s/t;
  182.         cout << "Approx eigenvalue is " << e << "\n" ;
  183.         Again();
  184.     }
  185. }
  186. */
  187. // -----------------------
  188. // End of file FCM.CPP *********************************
  189.