Problems with loops! (vectorization maybe)
Show older comments
Hi everybody, I hope you guys can help me!
I'm trying to write an optimization routine based on Simulated Annealing to solve an assignment problem (school timetabling), but the program performs very poorly in terms of convergence performance.
I am very convinced that there is a much faster way to write the constraints of the problem. I did it using lots of for-loops, I'm wondering if someone could help me eliminating those!
Here's what it looks like:
There is a 5-dimension master matrix: A(m,s,h,d,k)
m = subject (1 , 2 , 3 , ...)
s = classroom (1 , 2 , 3 , ...)
h = class time (1 , 2 , 3 , ...)
d = days in the week (1 , 2 , 3 , ...)
k = grades (1 , 2 , 3 , ...)
So, for instance: A(1,3,2,1,1) = 1 --> Subject 1 (Math) IS assigned to be taught in classroom 3 (room 283 - 2nd floor), in the 2 class time (9-11 am) of the day 1 (monday), to the 1 grade (High School first year).
If A(1,3,2,1,1) = 0, then the subject would be NOT assigned.
The challenge now is to face the hard constraints, here is an example:
%First Constraint: A classroom can't be assigned to more than one subject, at a given class time in a given day
%for all days of the week
for d=1:D
%for every class time
for h=1:H
%for every classroom
for s=1:S
%check if there is more than one subject assingned
if sum(sum(A(:,s,h,d,:))) > 1
I = I + 10; %Increase in Penalty function(which is the one being minimized)
n1 = n1 + 1; %Count how many times this constraint has been violated
end
end
end
end
I hope I've made myself clear!
Thank you all.
Answers (1)
Fangjun Jiang
on 6 Aug 2011
You don't really have a 5-dimensional data. I think it should be nx5 matrix list of all the arrangements.
The function you are looking for is unique(A,'rows'). You'll get better and faster answer if you give an example data.
PenaltyPoints=size(unique(A(:,1:4),'rows'),1)
2 Comments
Dionisio Agourakis
on 7 Aug 2011
Fangjun Jiang
on 8 Aug 2011
If there is no restriction at all, that is right. That is not a lot combination at all, right. 8*6*3*5*6=4320
Categories
Find more on Solver Outputs and Iterative Display 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!