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
#include
//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)
{
C++ Neural Networks and Fuzzy Logic:Preface
Source File for Hopfield Network for Traveling Salesperson Problem
347
citnbr = k;
int i,j;
cout<<”\n”;
for(i=0;i
{
dist[i][i]=0;
for(j=i+1;j
{
cout<<”\ntype distance (integer) from city “<<
i<<” to city “<
cin>>dist[i][j];
}
cout<<”\n”;
}
for(i=0;i {
for(j=0;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
{
for(j=0;j
{
cout<
}
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
{
for(j=0;j
{
tnrn[i][j].getnrn(i,j);
C++ Neural Networks and Fuzzy Logic:Preface
Source File for Hopfield Network for Traveling Salesperson Problem
348
}
}
//find weight matrix
for(i=0;i
{
for(j=0;j
{
p = ((j == citnbr−1) ? (0) : (j+1));
q = ((j == 0) ? (citnbr−1) : (j−1));
t1 = j + i*citnbr;
for(k=0;k
{
for(l=0;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
{
for(j=0;j
{
if(j%k == 0)
{
cout<<”\n”;
}
cout<
}
cout<<”\n”;
}
}
//present input to network
void network::asgninpt(float *ip)
{
int i,j,k,l,t1,t2;
for(i=0;i
{
for(j =0;j
{
C++ Neural Networks and Fuzzy Logic:Preface
Source File for Hopfield Network for Traveling Salesperson Problem
349
acts[i][j] = 0.0;
}
}
//find initial activations
for(i=0;i
{
for(j =0;j
{
t1 = j + i*citnbr;
for(k=0;k
{
for(l=0;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
{
r4 = 0.0;
p = ((i == citnbr−1) ? (0) : (i+1));
q = ((i == 0) ? (citnbr−1) : (i−1));
for(j=0;j
{
r1 = citouts[i] − outs[i][j];
r2 = ordouts[i] − outs[i][j];
for(k=0;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;
C++ Neural Networks and Fuzzy Logic:Preface
Source File for Hopfield Network for Traveling Salesperson Problem
350
totout = 0.0;
for(i=0;i
{
citouts[i] = 0.0;
for(j=0;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
{
ordouts[j] = 0.0;
for(i=0;i
{
ordouts[j] += outs[i][j];
}
}
}
//find tour
void network::findtour()
{
int i,j,k,tag[MXSIZ][MXSIZ];
float tmp;
for (i=0;i
{
for(j=0;j
{
tag[i][j] = 0;
}
}
for (i=0;i
{
tmp = −10.0;
for(j=0;j
{
for(k=0;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 “<
<<” tour order “<
for(k=0;k
{
tag[i][k] = 1;
tag[k][j] = 1;
}
}
C++ Neural Networks and Fuzzy Logic:Preface
Source File for Hopfield Network for Traveling Salesperson Problem
351
}
}
}
//print outputs
void network::prouts()
{
int i,j;
cout<<”\nthe outputs\n”;
for(i=0;i
{
for(j=0;j
{
cout<
}
cout<<”\n”;
}
}
//calculate total distance for tour
void network::calcdist()
{
int i, k, l;
distnce = 0.0;
for(i=0;i
{
k = tourorder[i];
l = ((i == citnbr−1 ) ? (tourorder[0]):(tourorder[i+1]));
distnce += dist[k][l];
}
cout<<”\n distance of tour is : “<
}
// print tour
void network::prtour()
{
int i;
cout<<”\n the tour :\n”;
for(i=0;i
{
cout<
cout<<”\n”;
}
}
//print activations
void network::practs()
{
int i,j;
cout<<”\n the activations:\n”;
for(i=0;i
{
for(j=0;j
{
cout<
}
C++ Neural Networks and Fuzzy Logic:Preface
Source File for Hopfield Network for Traveling Salesperson Problem
352
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” <
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
{
if(i%n == 0)cout<<”\n”;
input_vector[i] =(u + (float)(randomnum(100)/100.0))/20.0;
cout<
}
C++ Neural Networks and Fuzzy Logic:Preface
Source File for Hopfield Network for Traveling Salesperson Problem
353
//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
Copyright ©
IDG Books Worldwide, Inc.
C++ Neural Networks and Fuzzy Logic:Preface
Source File for Hopfield Network for Traveling Salesperson Problem
354
Do'stlaringiz bilan baham: |