| 
    C++ Neural Networks and Fuzzy Logic
     by Valluru B. Rao M&T Books, IDG Books Worldwide, Inc. ISBN: 1558515526 Pub Date: 06/01/95  | 
| Previous | Table of Contents | Next | 
The following listing gives the source code for the C++ program for the Hopfield network for traveling salesperson problem. The user is prompted to input the number of cities and the maximum number of iterations for the operation of the network.
The parameters a, b, c, d declared in the function main correspond to A1, A2, A3, and A4, respectively. These and the parameters tau, lambda, and nprm are all given values in the declaration statements in the function main. If you change these parameter values or change the number of cities in the traveling salesperson problem, the program will compile but may not work as youd like.
Listing 15.2 Source file for the C++ program for the Hopfield network for the traveling salesperson problem
//trvslsmn.cpp V. Rao,  H. Rao
#include trvslsmn.h
#include <stdlib.h>
#include <time.h>
//generate random noise
int randomnum(int maxval)
{
// random number generator
// will return an integer up to maxval
return rand() % maxval;
}
//Kronecker delta function
int krondelt(int i,int j)
       {
       int k;
       k= ((i == j) ? (1):(0));
       return k;
       };
void tsneuron::getnrn(int i,int j)
       {
       cit = i;
       ord = j;
       output = 0.0;
       activation = 0.0;
       };
//distances between cities
void network::getdist(int k)
       {
       citnbr = k;
       int i,j;
       cout<<\n;
for(i=0;i<citnbr;++i)
       {
       dist[i][i]=0;
       for(j=i+1;j<citnbr;++j)
              {
              cout<<\ntype distance (integer) from city <<
              i<< to city <<j<<\n;
              cin>>dist[i][j];
              }
       cout<<\n;
       }
for(i=0;i<citnbr;++i)
       {
       for(j=0;j<i;++j)
              {
              dist[i][j] = dist[j][i];
              }
       }
prdist();
cout<<\n;
}
//print distance matrix
void network::prdist()
       {
       int i,j;
       cout<<\n Distance Matrix\n;
       for(i=0;i<citnbr;++i)
              {
              for(j=0;j<citnbr;++j)
                     {
                     cout<<dist[i][j]<<  ;
                     }
       cout<<\n;
       }
}
//set up network
void network::getnwk(int citynum,float a,float b,float c,float d)
        {
        int i,j,k,l,t1,t2,t3,t4,t5,t6;
        int p,q;
        citnbr = citynum;
        pra = a;
        prb = b;
        prc = c;
        prd = d;
        getdist(citnbr);
        for(i=0;i<citnbr;++i)
                {
                for(j=0;j<citnbr;++j)
                       {
                       tnrn[i][j].getnrn(i,j);
                       }
                }
        //find weight matrix
        for(i=0;i<citnbr;++i)
                 {
                 for(j=0;j<citnbr;++j)
                        {
                        p = ((j == citnbr-1) ? (0) : (j+1));
                        q = ((j == 0) ? (citnbr-1) : (j-1));
                        t1 = j + i*citnbr;
                        for(k=0;k<citnbr;++k)
                                {
                 for(l=0;l<citnbr;++l)
                        {
                        t2 = l + k*citnbr;
                        t3 = krondelt(i,k);
                        t4 = krondelt(j,l);
                        t5 = krondelt(l,p);
                        t6 = krondelt(l,q);
                        mtrx[t1][t2] =
                        -a*t3*(1-t4) -b*t4*(1-t3)
                        -c -d*dist[i][k]*(t5+t6);
            }
                 }
            }
        }
        prmtrx(citnbr);
}
//print weight matrix
void network::prmtrx(int k)
         {
         int i,j,nbrsq;
         nbrsq = k*k;
         cout<<\nWeight Matrix\n;
         for(i=0;i<nbrsq;++i)
                {
                for(j=0;j<nbrsq;++j)
                       {
                       if(j%k == 0)
                              {
                              cout<<\n;
                              }
                       cout<<mtrx[i][j]<<  ;
                       }
                cout<<\n;
                }
         }
//present input to network
void network::asgninpt(float *ip)
       {
       int i,j,k,l,t1,t2;
       for(i=0;i<citnbr;++i)
             {
             for(j =0;j<citnbr;++j)
                    {
                    acts[i][j] = 0.0;
                    }
             }
       //find initial activations
       for(i=0;i<citnbr;++i)
             {
             for(j =0;j<citnbr;++j)
                    {
                    t1 = j + i*citnbr;
                    for(k=0;k<citnbr;++k)
                           {
                           for(l=0;l<citnbr;++l)
                                  {
                                  t2 = l + k*citnbr;
                                  acts[i][j] +=
                                  mtrx[t1][t2]*ip[t1];
                                  }
                           }
                    }
             }
       //print activations
       cout<<\ninitial activations\n;
       practs();
       }
//find activations
void network::getacts(int nprm,float dlt,float tau)
       {
       int i,j,k,p,q;
       float r1, r2, r3, r4,r5;
       r3 = totout - nprm ;
       for(i=0;i<citnbr;++i)
             {
             r4 = 0.0;
             p = ((i == citnbr-1) ? (0) : (i+1));
             q = ((i == 0) ? (citnbr-1) : (i-1));
             for(j=0;j<citnbr;++j)
                    {
                    r1 = citouts[i] - outs[i][j];
                    r2 = ordouts[i] - outs[i][j];
                    for(k=0;k<citnbr;++k)
                           {
                           r4 += dist[i][k] *
                           (outs[k][p] + outs[k][q]);
                           }
                    r5 = dlt*(-acts[i][j]/tau -
                    pra*r1 -prb*r2 -prc*r3 -prd*r4);
                    acts[i][j] += r5;
                    }
             }
       }
//find outputs and totals for rows and columns
void network::getouts(float la)
       {
       float b1,b2,b3,b4;
       int i,j;
       totout = 0.0;
       for(i=0;i<citnbr;++i)
             {
             citouts[i] = 0.0;
             for(j=0;j<citnbr;++j)
             {
             b1 = la*acts[i][j];
             b4 = b1/500.0;
             b2 = exp(b4);
             b3 = exp(-b4);
             outs[i][j] = (1.0+(b2-b3)/(b2+b3))/2.0;
             citouts[i] += outs[i][j];};
             totout += citouts[i];
             }
       for(j=0;j<citnbr;++j)
             {
             ordouts[j]  = 0.0;
             for(i=0;i<citnbr;++i)
                    {
                    ordouts[j] += outs[i][j];
                    }
             }
       }
//find tour
void network::findtour()
       {
       int i,j,k,tag[MXSIZ][MXSIZ];
       float tmp;
       for (i=0;i<citnbr;++i)
              {
              for(j=0;j<citnbr;++j)
                     {
                     tag[i][j] = 0;
                     }
              }
              for (i=0;i<citnbr;++i)
                     {
                     tmp = -10.0;
                     for(j=0;j<citnbr;++j)
                           {
                           for(k=0;k<citnbr;++k)
                                 {
                                 if((outs[i][k] >=tmp)&&
                                 (tag[i][k] ==0))
                                        tmp = outs[i][k];
                                 }
                           if((outs[i][j] ==tmp)&&
                           (tag[i][j]==0))
                                 {
                                 tourcity[i] =j;
                                 tourorder[j] = i;
                                 cout<<\ntourcity <<i
                                 << tour order <<j<<\n;
                                 for(k=0;k<citnbr;++k)
                                        {
                                        tag[i][k] = 1;
                                        tag[k][j] = 1;
                                        }
                                 }
                           }
                     }
              }
//print outputs
void network::prouts()
       {
       int i,j;
       cout<<\nthe outputs\n;
       for(i=0;i<citnbr;++i)
              {
              for(j=0;j<citnbr;++j)
                     {
                     cout<<outs[i][j]<<  ;
                     }
              cout<<\n;
              }
       }
//calculate total distance for tour
void network::calcdist()
       {
       int i, k, l;
       distnce = 0.0;
       for(i=0;i<citnbr;++i)
              {
              k = tourorder[i];
              l = ((i == citnbr-1 ) ? (tourorder[0]):(tourorder[i+1]));
              distnce += dist[k][l];
              }
       cout<<\n distance of tour is : <<distnce<<\n;
       }
// print tour
void network::prtour()
       {
       int i;
       cout<<\n the tour :\n;
       for(i=0;i<citnbr;++i)
              {
              cout<<tourorder[i]<<  ;
              cout<<\n;
              }
       }
//print activations
void network::practs()
       {
       int i,j;
       cout<<\n the activations:\n;
       for(i=0;i<citnbr;++i)
              {
              for(j=0;j<citnbr;++j)
                     {
                     cout<<acts[i][j]<<  ;
                     }
              cout<<\n;
              }
       }
//iterate specified number of times
void network::iterate(int nit,int nprm,float dlt,float tau,float la)
       {
       int k;
       for(k=1;k<=nit;++k)
              {
              getacts(nprm,dlt,tau);
              getouts(la);
              }
       cout<<\n <<nit<< iterations completed\n;
       practs();
       cout<<\n;
       prouts();
       cout<<\n;
       }
void main()
{
//numit = #iterations; n = #cities; u=intial input; nprm - parameter n
//dt = delta t;
// -
// parameters to be tuned are here:
       int u=1;
       int nprm=10;
       float a=40.0;
       float b=40.0;
       float c=30.0;
       float d=60.0;
       float dt=0.01;
       float tau=1.0;
       float lambda=3.0;
//-
       int i,n2;
       int numit=100;
       int n=4;
       float input_vector[MXSIZ*MXSIZ];
       srand ((unsigned)time(NULL));
       cout<<\nPlease type number of cities, number of iterations\n;
       cin>>n>>numit;
       cout<<\n;
       if (n>MXSIZ)
              {
              cout << choose a smaller n value\n;
              exit(1);
              }
       n2 = n*n;
       for(i=0;i<n2;++i)
              {
              if(i%n == 0)cout<<\n;
              input_vector[i] =(u + (float)(randomnum(100)/100.0))/20.0;
              cout<<input_vector[i]<< ;
              }
//create network and operate
       network *tntwk = new network;
       if (tntwk==0)
              {
              cout << not enough memory\n;
              exit(1);
              }
       tntwk->getnwk(n,a,b,c,d);
       tntwk->asgninpt(input_vector);
       tntwk->getouts(lambda);
       tntwk->prouts();
       tntwk->iterate(numit,nprm,dt,tau,lambda);
       tntwk->findtour();
       tntwk->prtour();
       tntwk->calcdist();
       cout<<\n;
}
| Previous | Table of Contents | Next |