Problems with loops! (vectorization maybe)

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.

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 8 月 6 日

0 投票

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 件のコメント

Dionisio Agourakis
Dionisio Agourakis 2011 年 8 月 7 日
great...but how can I create such matrix? I mean...it will be made of all permutations of [M , S , H , D , K] which means all the permutations of [1:8 , 1:6 , 1:3 , 1:5 , 1:6]..right?
Fangjun Jiang
Fangjun Jiang 2011 年 8 月 8 日
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

サインインしてコメントする。

カテゴリ

質問済み:

2011 年 8 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by