Vectorization of block consecutive matrixes in a matrix (M = [N(1);N(2)....;N(n)])

1 view (last 30 days)
Hello,
I want to vectorize the following code, but I do not know if this is possible. I have a list with solutions called 'pop'. A solution is a 1x48 vector called 'x'. This 'x' contains indexes to another matrix. Based on this matrix, a fitness value is calculated (f(x)).
At the moment my code looks like the following:
for i = 1:size(pop,1)
x = pop(i,:);
Tt = N(x(:),3:11)'; %Always a [9x48 matrix]
f1 = something using Tt(:,1:6)
f2 = something using Tt(:,1:12)
etc.
Nf = [f1,f2,f3,f4,f5,f6,f7,f8];
f = somefunction(Nf);
end
It is possible to translate the list pop to a matrix [Tt(1);Tt(2);...;Tt(end)] where every 9 rows defines a solution, however due to the calculation of f1,f2 etc, a for loop is still required I think.
Because this is the fitness function of a GA, the speed will greatly improve if it uses vectorization. Is it possible to adapt this code so that it can use vectorization or is it impossible in this case?
With kind regards,
Paul
  6 Comments
Stephen23
Stephen23 on 31 Jul 2015
Edited: Stephen23 on 31 Jul 2015
As far as I can tell your original question is about vectorization of a for loop, which although in the question above it starts with for i = 1:size(pop,1) in the code it seems to correlate to %for a = 1:size(thisPopulation,1). Note that in your sample code this line (i.e. the loop itself) is commented-out and that you do not define thisPopulation. Nor do you use the loop variable a, which is only referred to on one line: % A_penalty(a) = penalty; (also commented out).
You state that I "dont need to make any adjustments", in which case the code certainly runs without error and without this for loop. If you do not need the loop then what is your question about? If you do want this loop (or the vectorized equivalent) then how is thisPopulation defined? Or was the loop never actually working?
In any case some observations about your code:
  • you only refer to a once inside the loop, so everything before that point can be moved out of the for-loop.
  • stop reassigning variables unnecessarily: it just wastes memory and slows things down for no reason. Just stick with one copy of each variable.
  • Instead of defining lots of separate variables and then concatenating them together just define these values directly as a row/column of a matrix.
  • The loop for i=1:Horizon can probably be vectorized quite easily.
Here is the version that I started to write:
Paul
Paul on 31 Jul 2015
Hey Stephen,
You probably used the old m.file that I attached, however the current m-file is slightly different, because it uses a matrix instead of a vector as input ( function [ V_fit ] = Fitness_sol_expl( pop ) instead of function [ scores ] = Fitness_sol_expl( x ) ). The 3 files newly attached should work properly together.
The question is indeed about vectorizing the for i = 1:size(pop,1) loop ( in the m-file the a = 1:size(pop,1) loop).
The matlab GA toolbox has 2 options for a fitness function, i.e:
1. serial, in which it runs the fitness function in serial for each candidate solution 'x' (where x = pop(a,:), thus the for-loop is performed by the GA toolbox somewhere else). This results in a fitnessfunction that looks like function [value] = Fitness_sol_expl(single_solution such as x) resulting in a single fitness value.
2. Vectorized, in which the fitness of the entire population (called pop ), is calculated in a vectorized way. This results in a fitness function as function [V_fit] = Fitness_sol_expl( pop ), where V_fit is a vector containing the fitness of the entire population (a size(pop,1)x1 vector).
In the new m-file called looped_Fitness_sol the for-loop is operational when the matrix pop is loaded into the workspace (by typing looped_Fitness_sol(pop)). I would like to vectorize the loop a = 1:size(pop,1)
I hope this clears things up, thanks for your help.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!