how to find the values of variables which give minimum value of a function after iterations

1 view (last 30 days)
I'm running this code. after getting the minimum value from loop. I'm asking for the corresponding matrices from cells on which the minimum value is achieved..but If I check those matrices manually, the answer is not correct..for example..this code gives minimum value of (2) and if I check corresponding matrices manually, the answer is 6.. how can I get the correct set of variables in output? please help me in solving this problem.If the same method is applied from single arrays or integers, the result is correct but not in this case when the variables are cells containing matrices.
tic;
no_of_machines=7;
no_of_cells=3;
No_of_Parts = 6;
P1=[1 0 0 1 0 1 1;1 1 0 0 1 0 1];
P2=[0 1 1 1 0 0 1;1 0 1 0 1 1 0];
P3=[1 0 0 1 1 0 0;0 1 1 0 0 0 1];
P4=[1 0 0 0 1 0 1;0 1 0 1 0 1 0];
P5=[1 1 0 0 0 1 0;1 1 0 0 1 0 1];
P6=[0 1 0 0 0 1 1;1 1 0 1 0 1 0];
P = [P1;P2;P3;P4;P5;P6];
z = [size(P1,1) size(P2,1) size(P3,1) size(P4,1) size(P5,1) size(P6,1)];
c = [0 cumsum(z(1:end-1))];
a = allcomb(1:z(1),1:z(2),1:z(3),1:z(4),1:z(5),1:z(6));
n = size(a,1);
all_comb_of_routes = cell(1,n);
for k=1:n
all_comb_of_routes{k} = P(c+a(k,:),:);
end
CELL = zeros(no_of_cells^no_of_machines,no_of_machines);
t = 0;
for k = 0:(no_of_cells^no_of_machines)-1
s = dec2base(k,no_of_cells,no_of_machines);
if length(unique(s))==no_of_cells
t = t+1;
CELL(t,:) = s-'0'+1;
end
end
CELL = CELL(1:t,:);
combination_array=num2cell(CELL,2);
[r1,c1]=size(combination_array);
for l=1:r1
R=1:numel(combination_array{l});
Z = zeros(R(end),max(combination_array{l}));
Z(sub2ind(size(Z),R,combination_array{l})) = 1;
allCells_array{l}=Z;
end
[r2,c2]=size(all_comb_of_routes);
count=0;
for m=1:c2
for n=1:numel(allCells_array)
movement=(all_comb_of_routes{m})*allCells_array{n};
movement=movement>0;
total_movement=sum((sum(movement,2))-1);
count=count+1;
total_all(count)=total_movement';
end
end
minValue=min(total_all)
[all_comb_of_routes(m) combination_array(n) allCells_array(n) minValue]
toc;
the code uses allcomb function whose m.file is attached below allcomb

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!