0

I am finding weights of a cognitive map which is similar to a feedforward neural network, difference being that there is no self connection (hence diagonals = 0 ). The weight matrix is 3*3 and each particle represents the weights that are needed to be found(optimized). In my application, I need to restrict the values of the weights in the range of [-1,1]. So, when PSO updates its Velocity and Position equations (excerpt below) I selected those indices that are above 1 and -1 and thresholded them. I checked the values of Velocity and Position. First 6 rows and 6 columns of Velocity are

-3.29818766857477e-10   1.06365098218884e-09    1.72718584823501e-08    8.52030002125361e-08    4.24567055691019e-09    1.42682312020373e-10
-0.527487299879998  0.0141963443263498  2   -2  0.0804033989937564  0.0294235685161277
-0.460426149819118  -0.00962472296493386    -0.231141442703638  -0.161996004370743  -0.00140062376098630    -0.0183361675902131
0.490979470234505   -0.150287862753787  -0.453325515475146  1.48710788599919    -2  -0.0168498207544201
-1.49184829189134   0.718322120314944   0.191912248648141   1.67798502924430    -0.811846486653242  -0.00645584872854835
0.0236930729558028  0.725697240982270   -0.571776066383284  -0.481114642909396  -0.623411799726210  0.00226958877218209

And Position values for first 6 rows and 6 columns =

-7.98664279308835   -3.90744804351231   -6.92609001695919   -6.38057220301763   6.34627589189950    0.515032878765257
-8.23428663786642   -3.91888491694193   -5.06678793435555   -7.66405701509194   6.18465937874726    0.527740403314418
-8.18128160012481   -3.91458365620100   -6.65571517234057   -6.64026727804025   6.46684166489122    0.509478265627388
-7.82297889594148   -3.73466474001669   -6.83434245810552   -5.84907863164857   6.76798656860034    0.497959367071159
-8.65135591544159   -3.40466585557339   -7.11566232657911   -6.09503181101550   5.95791723669973    0.511429259659808
-7.93848660180123   -3.22292763621706   -6.41517926678420   -7.12188582637202   6.18562296678481    0.514711315467883

The values of Positions and hence weights and also Velocity are beyond the range that I mentioned. What should be done so that Position values and hence weight matrix is in the range [-1,1]? Where am I going wrong? Please help.

noP = 10;
Dim = 9;
Velocity=zeros(noP,Dim); %Velocity vector
Position=zeros(noP,Dim); %Position vector
weight_min_range = -1;
weight_max_range = 1;
velocity_clamping_factor = 2;

 Vmax=weight_max_range*velocity_clamping_factor;
 Vmin=-Vmax;


%////////Cognitive component///////// 
pBestScore=zeros(noP);
pBest=zeros(noP,Dim);

pBestScore2=zeros(noP);



%Initialization
for i=1:size(Position,1) % For each Particle
    for j=1:size(Position,2) % For each dimension
           Position(i,j)=rand();


           Velocity(i,j)=rand();

    end
end
for PSOITER = 1:IterMax
 %INSIDE PSO LOOP : DETAILS SKIPPED
    .........
............


%Fitness evaluation by each particle for all training set

for i=1:size(pos,1)
        for j=1:size(pos,2)   

 Velocity(i,j)=inertia*vel(i,j)+c1*rand()*(pBest(i,j)-Position(i,j))+c2*rand()*(gBest(j)-Position(i,j));


    Vind1 = Velocity<Vmin;
    Vind2 = Velocity>Vmax;

    Velocity(Vind1) = Vmin;
    Velocity(Vind2) = Vmax;

    Position(i,j)= Position(i,j)+ Velocity(i,j);
end
end

After all the calculations and PSO routine is over I get the following Weight Matrix instead of weight matrix values being in the rage of -1 to 1.

    WeightMatrix =

                   0   -6.9261    6.3463
   -7.9866         0    0.5150
   -3.9074   -6.3806         0
SKM
  • 959
  • 2
  • 19
  • 45

1 Answers1

0

Your Velocity values all appear to be within [-1, 1].

Your code does not contain the thresholding of Position.

Also, you are thresholding all the values of Velocity on each iteration i,j while only updating Velocity(i,j).

If you wanted to threshold both Velocity and Position and avoid using a loop you can replace your entire fitness evaluation loop with:

G = repmat(gBest, Dim, 1);
Velocity = inertia*vel + c1*rand(size(Velocity))*(pBest - Position) + ... 
           c2*rand(size(Velocity))*(G - Position);
Velocity(Velocity < Vmin) = Vmin;
Velocity(Velocity > Vmax) = Vmax;
Position = Position + Velocity;
Position(Position < Pmin) = Pmin;
Position(Position > Pmax) = Pmax;

Alternatively, you could allow Position and Velocity to evolve without thresholding and normalize the weight matrix at the end of the process:

w_max = max(W(:));
w_min = min(W(:));
W_norm = -1.0 + (W - w_min)*2.0/(w_max - w_min);

This will map your weight matrix W into [-1 1] like so:

[0.11444 -0.85202 1.00000;
-1.00000  0.11444 0.18631;
-0.43079 -0.77590 0.11444]

An alternative method which will preserve the scaling of positive and negative values relative to zero is:

w_max = max(W(:));
w_min = min(W(:));
w_abs_max = max([abs(w_max), abs(w_min)]);
W_norm = -1.0 + (W - w_min)/w_abs_max;

Which will map your weight matrix into [-1 1] in the following way:

[0.00000 -0.86722 0.79462;
-1.00000  0.00000 0.06448;
-0.48924 -0.79891 0.00000]

Notice how this mapping retains the zero values and the ratios between the magnitudes of the weights.

MattG
  • 1,416
  • 2
  • 13
  • 31
  • Thank you for your reply but I don't think I understand what you suggested. Even if I want to do inside the I,j loops and I thresholded position as well as velocity, now I am getting weights whose values are only -1 and 1 with no intermediate values in between. I took Pmin = -1 and Pmax = 1. Is the way I did thresholding by storing the indices first and then updating incorrect?Also, why are the result weights now only -1 and 1 if I take Pmin=-1 & Pmax=1?Thank you for your effort and time, shall be grateful – SKM Apr 12 '14 at 17:57
  • Hi SKM, I'm not exactly sure how your algorithm works, but here's a suggestion: why not let the position and velocity evolve without thresholding, and then at the end normalize the weights so that the maximum and minimum values are within [-1, 1]? I don't really know enough about the algorithm to comment much more. I'll add an edit to my answer that shows you how to normalize the weights after updating them. – MattG Apr 12 '14 at 18:14
  • I will certainly try out the above but before that I want to ask that during calculation of fitness I am multiplying input features with weights + Bias. Then I calculate the squared error between this output and the target (targets are [100;010;110]). So, should I be multiplying with the normalized weights in this step or the ones without normalization? Second, what is the significance of retaining zero values in the 2nd way of normalization? Plz pardon my trivial questions. – SKM Apr 12 '14 at 19:28
  • I applied both your methods after the Position is calculated since the position values are multiplied with input features during fitness calculation. I am getting all weights as -1 and nothing in between!! I am at a loss, what should I do?Please help – SKM Apr 13 '14 at 02:42