home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
High Voltage Shareware
/
high1.zip
/
high1
/
DIR36
/
FCM2.ZIP
/
FCM.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-26
|
6KB
|
189 lines
// *************************************************************************
// File: FCM.CPP Date: September 25, 1993
// By: Josef Betancourt CIS 71351,356 System: Borland C++ ver. 2.0.
// *************************************************************************
/*
Purpose: Illustrate the Fuzzy Cognitive Map computations.
This is a very very simple implementation of the FCM example found in:
Neural Networks and Fuzzy Systems. Kosko, B. page 154.
Fuzzy Thinking, the new science of fuzzy logic. Kosko, B. page 222.
Fuzzy Logic. McNeill D., Freiberger, P. page 237.
I used a very nice matrix class called BUMP,
Beginner's Understandable Matrix Package. It is found in the Borland C++
forum on CIS (Go BCPPDOS); the file is BUMP.ZIP. The author is:
Clopper Almon
Compuserve: 73377,1466
-------------------------------------------------------------------------
Of course, a general purpose interactive graphical system where the FCM
can be entered graphically by multiple experts and the inference process
is visually presented would be nice. It should be a FCM CAD system!
But are FCMs really useful? How are they used in control systems?
--------------------------------------------------------------------------
USE: This must be compiled and linked with BUMP.CPP. Large memory model.
The FCM is in file AFRICA.TXT.
Investment policy vector is in INVEST.TXT.
Disinvestment is in DIVEST.TXT
*/
// -----------------------
// dependencies
#include <iostream.h> // for cout
//#include <strstrea.h> // for istrstream
#include <ctype.h> // for isdigit()
#include <stdio.h> // for fgets()
#include <alloc.h> // for coreleft()
#include <stdlib.h> // for atof()
#include <conio.h> // for cprintf()
#include "bump.h" // for Matrix class
// -----------------------
// Prototypes
void Reason(Matrix const &matFCM, Matrix &matPolicy,
int iCell, float fValue, float fThresh);
void Again(void);
void ThresholdMF( Matrix & mat, float thresh = .5);
void ShowCondenseMP( Matrix &Mat);
void DrawBorder(void);
// -----------------------
int main(int argc, char * argv[]){
cout << "\n*** Fuzzy Cognitive Map example. By Josef Betancourt ***\n";
if( argc != 7 ){
cerr << "\nUsage: fcm <FCM file> <policy file> ";
cerr << "<matrix size> <cell> <value> <thresh>\n";
cerr << " Where cell and value refer to policy vector.\n";
exit (-1);
}
// convert strings to integers...........
int iSize, iCell;
float fValue, fThresh;
iSize = atoi(argv[3]); // matrix order.
iCell = atoi(argv[4]); // policy cell.
fValue = atof(argv[5]); // its value.
fThresh = atof( argv[6]); // threshold value.
Matrix matFCM(iSize,iSize), matPolicy(1,iSize);
// populate and show matrices ..........
matFCM.ReadA(argv[1]);
matPolicy.ReadA(argv[2]);
matFCM.Display("This is the FCM matrix:");
matPolicy.Display("This is the policy matrix: ");
Again(); // wait for key tap or escape.
Reason(matFCM, matPolicy, iCell, fValue, fThresh);
} // ------- main end.
// -----------------------
void Reason(Matrix const &matFCM, Matrix &matPolicy,
int iCell, float fValue, float fThresh){
// iteratively apply simple FCM computation using matrices.
int i = 1; // epoch counter.
while( 1){
cout << "Epoch " << i << '\n';
matPolicy = matPolicy * matFCM; // Matrix multiply. Nice syntax!
matPolicy.Display( "Policy vector before thresholding: ");
ThresholdMF( matPolicy, fThresh);
matPolicy[1][iCell] = fValue; // since its being tested, set it.
matPolicy.Display("New policy vector: ");
ShowCondenseMP( matPolicy); // for ease in seeing limit cycles.
DrawBorder();
Again(); // wait for key tap or escape.
i++;
}
}
// -----------------------
void ThresholdMF( Matrix & mat, float fthresh ){
// apply thresh to policy matrix, default thresh is 0.5.
for( int i = 1; i < mat.columns() + 1; i++){
if( mat[1][i] >= fthresh){
mat[1][i] = 1.;
}else{
mat[1][i] = 0;
}
}
}
// -----------------------
void ShowCondenseMP( Matrix &Mat){
// print policy vector in a more visual pattern.
cout << "\nPattern: ";
int temp ;
for( int i=1; i< Mat.columns()+1; i++){
if( Mat[1][i] > 0 ){
temp = 0x2;
}else{
if( Mat[1][i] < 0){
temp = 0x1F;
}else{
temp = 0x1;
}
}
cout.put( temp);
}
cout << '\n' ;
}
void DrawBorder(void){
cout << '\n';
for( int i=0; i<75; i++){
cout.put( 0xDF );
}
cout << '\n';
}
// -----------------------
void Again(void){
// query user for continuation or end of run.
cerr << "\n\t\t\t< Tap a key to continue. ESC to exit. >\n";
if(getch() == 27){
exit(1);
}
}
// ----------------------------------------------------------------------
/* computing Eigenvectors is similar to computing a FCM without the
thresholding. Here is such an algorithim.
void EigenTest(int size, char *pszName){
// algorithim from Computational Linear Algebra with Models.
// Williams, Gareth. Allyn and Bacon, Massachusetts. 1978. page 449.
float e, s, t;
int i, j, k, l;
Matrix A(size,size), X(size,1),Y(size,1),Z(size,1);
A.ReadA(pszName);
A.Display("Here is the B matrix:");
AllOnes( X);
while(1){
cout << "Iteration " << i << "\n";
Y = A * X;
Y.Display("Here is the Y matrix:");
k=1;
for( j=2; j < Y.rows()+1;j++){
if( fabs( Y[k][1]) >= fabs( Y[j][1]) ){
continue;
}
k= j;
}
Y = ( 1/ fabs( Y[k][1])) * Y;
X = Y;
X.Display("the adjusted vector is:");
Z = A*X;
s=0;
t=0;
for( l=1; l<size + 1; l++){
s += X[l][1] * Z[l][1];
t += X[l][1] * X[l][1];
}
e = s/t;
cout << "Approx eigenvalue is " << e << "\n" ;
Again();
}
}
*/
// -----------------------
// End of file FCM.CPP *********************************