Tag Archives: fuzzy

Fuzzy Cognitive Maps in C++

Here is some code I wrote to compute FCM. I used this while learning about Fuzzy Systems. Just posted here for storage.

FCM is one of the technologies in field of Soft Computing.

// *************************************************************************
// File:  FCM2.H  ver. 0.03   Date:  September 25, 1993
// By:    Josef Betancourt    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 Beginner's Understandable Matrix Package, 
BUMP.ZIP, by Clopper Almon. It is found in the Borland C++ forum on CIS. 

This version of my FCM implementation allows the setting of more than one
policy variable in the policy vector.  This is accomplished by loading
a state setting i by 2 matrix.  If column one is zero than cell i in the
policy vector is not reset by the state vector, else during the reasoning
process that cell is reset to the value in column two.

 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.

        Example       FCM file       Policy        State
      Africa         AFRICA.TXT     INVEST.TXT    INVEST2.TXT
      Africa         AFRICA.TXT     DIVEST.TXT    DIVEST2.TXT

    To test the africa example,
    Use the syntax:  FCM africa.txt invest.txt invest2.txt 9 .5
    or               FCM africa.txt divest.txt divest2.txt 9 .5

    To test the cocaine example in the book Fuzzy Thinking,
    Use the syntax: FCM cocaine.txt interdic.txt interdi2.txt 11 .5
    ( this example does not result in cocaine supply falling as stated in
    the book.)
#ifndef FCM2_H
#define FCM2_H

// -----------------------
// dependencies
#include <iostream.h>   // for cout
#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
// -----------------------

#define TRUE (1==1)
#define FALSE (!TRUE)

class FCM;  // forward reference.

// Prototypes
void Reason( FCM &fcm);
void Again(void);
void ThresholdMF( Matrix & mat, float thresh = .5);
void ConvertMat( Matrix & matDest, Matrix &matSrc);
void ResetPolicy( Matrix & matDest, Matrix &matSrc);
void ShowCondenseMP( Matrix &Mat);
void DrawBorder(void);

class FCM{  // Fuzzy Cognitive Map
        Matrix *pmatFCM;    // directed graph.
        Matrix *pmatPolicy; // initial policy.
        Matrix *pmatState;  // policies to reset after epoch.
        char *Names[];      // the labels of each node.
        int   nPolicies;     // number of nodes.
        float fThreshold;
        FCM( int n);  // constructor.
        // FCM( FCM other); // copy constructor.
        // ~FCM(); // destructor.
        void  SetSizeI( int size){ nPolicies = size;}
        void  SetThresh( float fthr){ fThreshold = fthr; }
        int   IGetSize( void){ return nPolicies;}
        void  SetFCM( Matrix *fcm){ pmatFCM = fcm;}
        void  SetPolicy( Matrix *Policy){ pmatPolicy = Policy;}
        void  SetState( Matrix *State){ pmatState = State; }
        void  ReadFCM( char *psz);
        float &Cell( Matrix *pmat, int i, int j);
        float GetCell( Matrix *pmat, int i, int j);
        void  ReasonStep( void);
        void  ThresholdMF(void);
        void  ResetPolicy( void );
        void  DisplayPolicy( char *msg);
        Matrix &WhatIsPolicy( void){  return *pmatPolicy ; }
        void  DumpPolicy(void);

// end of file FCM2.H

// *************************************************************************
// File:  FCM2.CPP  ver. 0.03               Date:  September 25, 1993
// By:  Josef Betancourt  tcmits1@cs.com    System:  Borland C++ 4.0
// *************************************************************************
Purpose:  Illustrate the Fuzzy Cognitive Map computations.

// dependencies....

#include "fcm2.h"

// -----------------------------------------
//  constructor.
FCM::FCM( int size){
    SetSizeI(size );
    fThreshold = .5;
// -----------------------------------------
// get address of Cell.
float &FCM::Cell( Matrix *pmat, int i, int j){
    return pmat->operator[](i)[j];
// -----------------------------------------
// get cell contents.
float FCM::GetCell( Matrix *pmat, int i, int j){
    return pmat->operator[](i)[j];
// -----------------------------------------
void FCM::ReasonStep( void){
   // Matrix multiply.  Nice syntax!
    *pmatPolicy = (*pmatPolicy)*(*pmatFCM);
   //DisplayPolicy( "policy after multiply: ");

   //DisplayPolicy( "policy after threshold: ");

   //DisplayPolicy( "policy after reset: ");
// -----------------------------------------
// threshold policy vector.
void FCM::ThresholdMF( void ){
    // apply thresh to policy matrix, default thresh is 0.5.
    for( int i = 1; i < nPolicies + 1; i++){
        if( GetCell( pmatPolicy, 1, i) >= fThreshold ){
            Cell(pmatPolicy, 1, i) = 1.;
            Cell( pmatPolicy, 1, i) = 0;
// -----------------------------------------
// reset policy with state vector.
void FCM::ResetPolicy( void ){
    for( int i = 1; i < nPolicies+1; i++){
        if( GetCell( pmatState, i,1) == 1){
            Cell( pmatPolicy, 1, i) = GetCell(pmatState, i, 2) ;
// -----------------------------------------
// show policy using matrix library display routine.
void FCM::DisplayPolicy( char *message){
// -----------------------------------------
// dump policy as simple number dump.
void FCM::DumpPolicy(void){
    for( int i=1; i < nPolicies+1; i++){
        cout << GetCell( pmatPolicy, 1, i);
// -----------------------
int main(int argc, char * argv[]){
    cout << "\n*** Fuzzy Cognitive Map example.  By Josef Betancourt ***\n";
    if( argc != 6 ){
     cerr << "\nUsage: fcm <FCM file> <policy file> <state file>";
     cerr << "<matrix size> <thresh>\n";
     cerr << "         Where cell and value refer to policy vector.\n";
     exit (-1);
    // convert strings to integers...........
    int iSize = atoi(argv[4]); // matrix order.
    float fThresh = atof( argv[5]); // threshold value.

    FCM theFCM( iSize);

   // create the helper matrices.
    Matrix matFCM(iSize,iSize), matPolicy(1, iSize),
            matVPolicy(1,iSize), matState( iSize, 2);

   // link them into the FCM
    theFCM.SetFCM( &matFCM);
    theFCM.SetPolicy( &matPolicy);
    theFCM.SetState( &matState);
    theFCM.SetThresh( fThresh);

    // populate using files
    matFCM.ReadA( argv[1] );
    matPolicy.ReadA( argv[2] );
    matState.ReadA( argv[3] );

   // and show matrices ..........
    matFCM.Display("This is the FCM matrix:");
    matPolicy.Display("This is the policy matrix: ");
   matState.Display("This is the state matrix: ");

    cout << "test of dump\n" ;

    Again();    // wait for key tap or escape.

   // perform the FCM process using the object.
    Reason(theFCM );

    return FALSE;
}  // end of main.
// -----------------------
void Reason( FCM &fcm ){
    // apply simple FCM computation using matrices.
    int i = 1;  // epoch counter.
    while( 1){
        cout << "Epoch " << i << '\n';
        fcm.DisplayPolicy("New policy vector: ");

      // for ease in seeing limit cycles.
        ShowCondenseMP( fcm.WhatIsPolicy() );

        Again();   // wait for key tap or escape.
} // reason end.
// -----------------------
void ShowCondenseMP( Matrix &Mat){
    // print policy vector in a more visual pattern.
    cout << "\nPattern: ";
    char temp ;
    for( int i=1; i< Mat.columns()+1; i++){
        if( Mat[1][i] > 0 ){
            temp = 0x2;
            if( Mat[1][i] < 0){
                temp = 0x1F;
                temp = 0x1;
        cout.put( temp);
    cout << '\n' ;
// -----------------------
void DrawBorder(void){
    cout << '\n';
    for( int i=0; i<75; i++){
        cout.put( char(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){
// ----------------------------------------------------------------------
/*  computing Eigenvectors is similar to computing a FCM without the
     thresholding.  Here is such an algorithm.

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, M;
     Matrix A(size,size), X(size,1),Y(size,1),Z(size,1);
     A.Display("Here is the B matrix:");
     AllOnes( X);
        cout << "Iteration " << i << "\n";
        Y = A * X;
        Y.Display("Here is the Y matrix:");
        for( j=2; j < Y.rows()+1;j++){
            if( fabs( Y[k][1]) >= fabs( Y[j][1]) ){
            k= j;
        Y = ( 1/ fabs( Y[k][1])) * Y;
        X = Y;
        X.Display("the adjusted vector is:");
        Z = A*X;
        for( M=1; M<size + 1; M++){
            s += X[M][1] * Z[M][1];
            t += X[M][1] * X[M][1];
        e = s/t;
        cout << "Approx eigenvalue is " << e << "\n" ;
// -----------------------
// End of file FCM.CPP *********************************

Off topic
There are a lot of examples of connectionist presentations. I wonder if these could be coupled with the FCM technique to create computable structures. For example, look at the work of Mark Lombardi.


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Predictive Interface using Fuzzy Logic

A Fuzzy Logic Controller Using Fuzzy Feedback

This is based on an old web page. I thought I would store it on this blog just for backup. It was an attempt to construct a predictive interface. Since a users actions could not be completely predicted, it seemed to demand an approximation system. BTW, there is a new field called Soft Computing that this type of investigation is related to.


For background on Fuzzy Logic:

Table of Contents

  • Introduction – Brief narrative of how I came to do this.
  • Background – What is this paper about?
  • The Paper – This is the original version of the paper.
  • Links – For more info


In February 1992, while watching my daughter learn how to use our home computer, I came up with a new product idea. Hopefully I will post more about that would-be product on this site. The following presents the approach I used for its controller.

I could not get over some technical hurdles with the product, so I put it aside until I could get back to it. One thing that I was proud of was learning a little bit of Fuzzy Logic and using it as the controller. I even wrote a graphical simulator in the C++ language; threading was fun stuff. Watching the fuzzy sets behave like analog surfaces or neural EEG waves gave me the idea for the biomimicry aspects.

Lab Notebook entry September 13, 1995
Yesterday while at a UNISYS class at Burlington, Mass., all of a sudden a thought came to me full blown, I should save each resultant Fuzzy set for each object and add to it instead of resetting it at each sample of the user focusing.
Lab Notebook entry September 23, 1995
It works! I added fuzzy memory to the prototype and it works beautifully! No jitter except for the GetZone() function which doesn’t use fuzzy memory.
I think this development will allow me to do away with the focal space creation entirely.I also started writing a research paper called Controller Using Fuzzy Memory. I should call it short-term memory
since the fuzzy rules serves as the long term memories. Thus, there are three “organic” memories, Long-Term, Short-Term, and Reflective, which correspond to Instinct, Learning, and Reflex.

A Year later in 1996, I dusted off my notes and looked at the Fuzzy Logic Controller (FLC) again, and thought maybe someone else can make use of it, or lead to some other ideas. So, I quickly wrote it up and sent it to a journal for review. The paper aludes to biomimicry, I tried to duplicate how animal systems have both a voluntary and reflex arc (but I never showed how the reflex would override the “voluntary” control output. Perhaps by using weights?).

It was a ginormous rejection! And, reading it now (2006), I would have rejected it too! A little knowledge is dreadful. A snippet of what the reviewer wrote: “It’s hardly clear what’s …. The paper continues without much of an organization & jumps from posterior possibilities (hardly the case) to ANN.”

The reviewer suggested the book: An Introduction to Fuzzy Control by Driankov, Hellendoorn, and Reinfrank. I purchased it recently. Very good book. Whats funny is after reading a little bit of this book, my paper’s main innovation is still valid, as far as I can see.

I make no apologies; I was a computer programmer at the time and not trained as a researcher. I learned enough to get the job done. I read a bunch of stuff and just put it all together.

So, once again I dusted off the notes and exported the paper to HTML. I have not attempted to check if the idea is original or still bad. The contents are unmodified. The diagrams were redone since I lost the digital version. Don’t ask me what the diagrams mean, I vaguely remember.

If I had time I would have liked to see if it could compete with a Kalman filter controller. But, that would have meant learning a lot more, like what “Gaussian white noise with covariance matrix” means. Yeah right.

What is this about?

This section is under construction and was meant as a non-technical introduction to Fuzzy Logic Control.

A Fuzzy Controller, the ones I was aware of then, and even most simple controllers, sample control variables, and based on the error calculations, produces control outputs. The sampling is performed on a set time period (or triggered by input events). So the controller samples some values, does some calculations, then outputs a control value.

In a car cruise control system, for example, every few milliseconds the speed is checked and the calculation determines how far the speed is from the desired cruise control speed. The output is increasing or lowering the throttle to change the engine speed. The amount of engine speed increase or decrease level is based on the size of the error.

Of course, actual controllers are more complex then this. The important point in terms of this paper is that each “cycle” forgets the previous cycle’s sampling (in simplest cases). Josef Betancourt, last modified 8/15/2007 – 10:03:51 PM


Inefficiencies in the conventional fuzzy logic controller are discussed, and a model to overcome them is presented. Conventional Fuzzy Controllers produce a Fuzzy Set output for each output control variable and defuzzifies these to produce a crisp control variable. Instead of using the fuzzy set output for defuzzification, the presented model uses Short-Term Memory. This memory is an aggregation of the sets produced at each control cycle. This allows inter-cycle evidence formation which improves the control processing. Defuzzification of this short term memory more fully uses information generated by the inference engine.


Fuzzy Logic, controller, adaptive control, feedback.


Since it seemed to meet all of the requirements, a Fuzzy Logic Controller (FLC) was chosen for use in a new product development. However, the FLC did not perform well. Many attempts to improve the controller did not succeed: alpha cut thresholds were added, the term sets tinkered with, knowledge pools created, and different fuzzy logic operators tested. No doubt there is a combination of FLC parameters and pre and post process filtering that would work; however, such a search seemed daunting.

Finally, by an intuitive leap, a simple modification to the FLC model was found that could make it work. And this solution seemed to be unique and usable in other applications.

Fuzzy Logic Controllers

During a control cycle, a FLC, see figure 1, fuzzifies the output of the controlled process and applies this to a fuzzy logic engine which uses a knowledge base, term sets, and Zadehan logic. An output fuzzy set is then generated and finally defuzzified to produce crisp outputs to supply to the controlled process. During the next control cycle the reinitialized output fuzzy spaces, where the interim fuzzy sets are aggregated, are again reused. The operation of such a controller is well documented in the literature.

Figure 1


There is a basic inefficiency in this FLC model; the information generated by the FLC operation is not fully used. First, defuzzification, though necessary, is a contraction of dimensionality, thus information loss [1]. Second, by reinitialization of the output fuzzy space in preparation for the next control cycle, the generated information is not used to effect subsequent cycles. Even in Adaptive FLCs, the exogenous ad hoc processes, such as an adaptation engine or a neural network, that monitors the FLC operating characteristics and modify the parameters or knowledge base, only affect a change after several processor cycles. This information loss is also partially true in feedback FLCs, since after information is fed back, they also operate in a conventional feedforward manner. Third, the evidence formation is intra-cycle, that is, the contrary effects of rule firings occur only within a control cycle. Contrary evidence from cycle to cycle is not used. For example, if a particular cycle generated the fuzzy output set HIGH THROTTLE with a maximum height of .9, and in the next cycle HIGH THROTTLE now has a maximum height of .2, then that is inter-cycle contrary evidence in that output space.


This information loss and adaptation delay is easily reduced by the use of Short-Term Memory (STM). As shown in figure 2, a memory space is added to the FLC to store fuzzy sets. In a multiple output system each variable would have such a memory space.

Figure 2
Each cycle of the fuzzy logic engine produces an output fuzzy set called the Reflex-Term Memory (RTM), and this is used to update STM. It is the contents of STM that are Defuzzified to produce the controller manipulated variable Om . Optionally, to improve throughput and conserve resources, instead of creating a new RTM in each cycle, the fuzzy memory can be updated directly.

Now, since some information is stored to influence the following cycles, every cycle improves the controller. Furthermore, since Defuzzification is performed on the updated memory and not on the greatly changing reflex fuzzy set, a smoother control surface that rapidly converges to the optimum output control value without overshoot is expected. This smoothing “naturally” resists transient or noisy input.


The method of updating fuzzy memory must be a suitable Aggregation operation that provides the optimum response for the particular control application. Ideally, to allow adaptation this operation should be parameterized, such as a Generalized Mean [2].

In the new product that inspired this FLC model, the aggregation operation used is a simple arithmetic average

Where ri is the i th membership value at the t cycle of the reflex-term fuzzy set and si , of the short-term fuzzy set,. Another operation also tested is an exponential smoothing of the membership values

Where a is the smoothing parameter .

Note that when the stored membership value is zero, the current value is fully used. The rationale here is that if there is no contrary evidence, what remains should be fully used.

These aggregation operators properly accumulate evidence, but most importantly, contrary evidence, rules firing with low value, reduce the fuzzy set surfaces. Interpreting the fuzzy sets as Possibility Distributions, this feedback creates, analogously to Bayesian posterior probabilities, posterior possibilities.

Other aggregation operations are possible. For example, since the output of a FLC is usually defuzzified, and normalization is not required, a simple unbounded sum, which acts as strong positive feedback, may be adequate in certain situations. This type of aggregation accumulates all information and is useful in applications that undergo mode changes, choose among options, or are human interacting.

One important concern with any aggregation method is overflow or saturation of the fuzzy memory space. This may be addressed in many ways. For example, a threshold or normalization can be applied to the fuzzy set memory. Another option is to reset the fuzzy memory when the controlled process undergoes distinct mode changes or operations. Lacking such reset criteria, the FLC system can accomplish this through autoadaptive methods.


This FLC model adds to the many FLC adaptation options available. Such as opportunities for the use of new metrics. For example, the reflex fuzzy set output can be defuzzified to produce an internal control value Oc . Comparing this to the STM control output, Om , provides metrics that can be used to qualify or change the FLC system: the aggregation operation can be changed to either increase or decrease the smoothing effect, or this can be used in run-length memory reset control.

Similarly, comparing the reflex fuzzy set surface with the STM fuzzy set provides other metrics that can adapt the generation of the reflex fuzzy sets by modifying the input sensitivity, term sets, rule weights, etc. Figure two illustrates a possible schematic for such an adaptive FLC model.

Fuzzy memory can also be viewed as a form of Artificial Neural Network field or layer. Thus, the techniques used in ANN can be applied. Furthermore, since Neural networks sum throughputs while Fuzzy systems sum outputs [3], the aggregation of Reflex output in Short-Term Memory brings a FLC closer to the operation of a neural network.


The presented FLC model has the ability to accumulate evidence in each cycle for discrete changes of state in a specific new product under development. This is accomplished by the use of a tripartite memory scheme: Reflex, Short, and Long-Term. The Reflex corresponds to the conventional FLC output fuzzy sets, the Short-Term to the aggregated fuzzy memory space disclosed above, and the Long-Term to the Fuzzy Logic Rule base used in the controller.

The term ‘Reflex-Term Memory’ is an indicator for future use in complex Soft Computing controllers having separate ‘reflex’ control paths that bypass the high-level knowledge base and inference engine ‘intelligence’. There can even be competing parallel control paths, each using different technologies.

Perhaps the presented model may offer the same advantages in other applications areas. For example, since the updating of the STM is a trans-cycle evidence accumulation, it may be useful in a multistage decision system. Each goal or solution variable is allocated a fuzzy memory. The decision system updates these in each step or cycle to ultimately produce a crisp score for each.

Another example is an automotive cruise control with automatic inter-car safety gap system where evidence for a mode change is accumulated until a certain compatibility level is reached, then the fuzzy memory is reset and the process repeats. Cruising along at 195 mph in the future super highway, the controller should use all the evidence, all the time!

Further research is needed to determine the actual differences, performance, applicability, and advantages of this model compared to both conventional and fuzzy controllers.


1. Cox, E., Fuzzy Logic For Business and Industry, Charles River Media, Inc., Rockland, MA., 1995, pg 107.

2. Kosko, B., Neural Networks and Fuzzy Systems, Prentice Hall, Englewood Cliffs, N.J., 1992, pg 29.

3. Klir, G.J., Yuan, B., Fuzzy Sets and Fuzzy Logic: theory and applications, Prentice Hall, Englewood Cliffs, N.J., 1995, pp 89-90.

End of original paper: “An Adaptive Controller Using Fuzzy Memory”, Originally written: 1996-03-30T03:13:00Z words: 1406 characters: 8019


1. Cox, E., Fuzzy Logic For Business and Industry, Charles River Media, Inc., Rockland, MA., 1995, pg 107.

2. Kosko, B., Neural Networks and Fuzzy Systems, Prentice Hall, Englewood Cliffs, N.J., 1992, pg 29.

3. Klir, G.J., Yuan, B., Fuzzy Sets and Fuzzy Logic: theory and applications, Prentice Hall, Englewood Cliffs, N.J., 1995, pp 89-90.

4. Design Of Fuzzy Controllers, Jan Jantzen, http://www.iau.dtu.dk/~jj/pubs/design.pdf, accessed 20070321

5. Prevention of Load Sway by a Fuzzy Controller http://people.clarkson.edu/~esazonov/neural_fuzzy/loadsway/LoadSway.htm

6. An Introduction to Fuzzy Control by Dimiter. Driankov, Hans. Hellendoom, Michael Reinfrank

7. search: feedback fuzzy control system

8. search: fuzzy systems

9. search: fuzzy sets

10. search: fuzzy logic

11. Introduction to Fuzzy Control by Hans P. Geering, September 1998. Accessed August 16, 2007.

12. search: Fuzzy Feedback

13. search: Fuzzy Controller

An Adaptive Controller Using Fuzzy Memory

All rights reserved. No part of this document may be reproduced or transmitted in any form by any means, electronic or mechanical, including photocopy, recording, or any information storage and retrieval system, without permission in writing from Josef Betancourt.

Author Josef Betancourt

Originally written: 1996-03-30T03:13:00Z words: 1406 characters: 8019

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.