Unknown error in running code and applying IF condition in loop
2 次查看(过去 30 天)
显示 更早的评论
I am running this code but after running I get the error Undefined variable cell_worker_sum. and I cannot check the values of variables in the last loop.. Is there any error in applying IF condition? _I want to apply the loop on those matrices of all_comb_of_workers in which sum of each column is 1 or greater than one.
no_of_machines=7;
no_of_cells=3;
No_of_Parts = 6;
Make_topart_power=[12; 7; 4; 6; 12; 6; 8];
move_cost=[12;12;12;12;12;12];
demand=[600 550 500 520 620 500];
labor_cost=[1.04; 1.2; 1.44; 1.6];
OP1=[10 0 0 10 0 20 15;10 13 0 0 12 0 15];
OP2=[0 10 10 15 0 0 20;11 0 13 0 17 10 0];
OP3=[10 0 0 12 11 0 0;0 11 18 0 0 0 17];
OP4=[10 0 0 0 19 0 14;0 14 0 17 0 10 0];
OP5=[18 13 0 0 0 15 0;10 12 0 0 10 0 11];
OP6=[0 10 0 0 0 10 15;12 11 0 13 0 15 0];
setup_cost=[20; 30; 25; 40; 45; 30];
hold_cost=[4; 3; 5; 3; 4; 5];
batch_size=(demand'.*setup_cost.*2./hold_cost).^0.5;
no_of_batches=demand'./batch_size;
M1=[1 0 0 0;0 0 1 0];
M2=[0 0 1 0;0 0 0 1];
M3=[0 1 0 0;0 0 0 1];
M4=[1 0 0 0;0 0 1 0];
M5=[0 1 0 0;0 0 0 1];
M6=[0 0 1 0;0 0 0 1];
M7=[0 1 0 0;0 0 0 1];
E= [OP1;OP2;OP3;OP4;OP5;OP6];
Ez = [size(OP1,1) size(OP2,1) size(OP3,1) size(OP4,1) size(OP5,1) size(OP6,1)];
Ec = [0 cumsum(Ez(1:end-1))];
Ea = allcomb(1:Ez(1),1:Ez(2),1:Ez(3),1:Ez(4),1:Ez(5),1:Ez(6));
En = size(Ea,1);
all_comb_of_OP_routes = cell(1,En);
for i=1:En
all_comb_of_OP_routes{i} =E(Ec+Ea(i,:),:);
end
CELL = zeros(no_of_cells^no_of_machines,no_of_machines);
t = 0;
for k = 0:(no_of_cells^no_of_machines)-1 %k=(2187-1=2186)
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);
for c=1:numel(combination_array )
R=1:numel(combination_array{c});
Z = zeros(R(end),max(combination_array{c}));
Z(sub2ind(size(Z),R,combination_array{c})) = 1;
allCells_array{c}=Z; % machine cell matrix array of all combinations
end
for i=1:numel(all_comb_of_OP_routes)
energy_consumption=(all_comb_of_OP_routes{i}*Make_topart_power).*demand';
total_energy_cost(i)=(sum(energy_consumption))*0.01;
for c=1:numel(allCells_array)
movement=(all_comb_of_OP_routes{i})*allCells_array{c};
movement=movement>0;
total_movement_cost=sum(bsxfun(@times,bsxfun(@times,sum(movement,2)-1,no_of_batches),move_cost));
total_all(i,c)=total_movement_cost;
end
end
[minValue,index]= min(total_all(:));
[rowmin,colmin]=find(total_all==minValue);
for v=1:numel(rowmin)
for w=1:numel(colmin)
X{v}=all_comb_of_OP_routes{rowmin(v)};
Y{w}=allCells_array{colmin(w)};
end
end
ind=true(1,numel(X));
for ii=1:numel(X)-1
for jj=ii+1:numel(X)
if isequal(X{ii},X{jj})
ind(jj)=false;
end
end
end
XX=X(ind);
celltimes=@(XX) sum(bsxfun(@times,XX,demand'),1);
total_timereq_for_all_machines=cellfun(celltimes,XX,'UniformOutput',false);
for n=1:numel(allCells_array)
for v=1:numel(XX)
Time_of_machine_inCells{n,v} = bsxfun(@times, allCells_array{n}, total_timereq_for_all_machines{v}');
end
end
M= [M1;M2;M3;M4;M5;M6;M7];
zz = [size(M1,1) size(M2,1) size(M3,1) size(M4,1) size(M5,1) size(M6,1) size(M7,1)];
cc = [0 cumsum(zz(1:end-1))];
aa = allcomb(1:zz(1),1:zz(2),1:zz(3),1:zz(4),1:zz(5),1:zz(6),1:zz(7));
nn = size(aa,1);
all_comb_of_workers = cell(1,nn);
for j=1:nn
all_comb_of_workers{j} = M(cc+aa(j,:),:);
end
for k=1:numel(Time_of_machine_inCells)
for j=1:numel(all_comb_of_workers)
worker_matrix=all_comb_of_workers{j};
columnSums=sum(worker_matrix,1);
if all(columnSums>1)
all_comb_of_workers{j}=worker_matrix;
cell_worker=Time_of_machine_inCells{k}'*all_comb_of_workers{j};
cell_worker_sum(k,j)=sum(bsxfun(@times,sum(cell_worker)',labor_cost));
end
end
end
[minvalue,ind]=min(cell_worker_sum(:));
[row,col]=find(cell_worker_sum==minvalue);
toc;
0 个评论
回答(1 个)
the cyclist
2017-1-23
编辑:the cyclist
2017-1-23
I suggest you use the debugging capability. Specifically, set a breakpoint at line 100, and see if the values of columnSums are what you expect.
Making a guess, though ...
Did you mean to use
if any(columnSums>1)
rather than
if all(columnSums>1)
?
5 个评论
the cyclist
2017-1-23
I don't know, and your language is not clear enough for me to be sure. Maybe you could give several examples of values of columnsSums, and whether they should trigger the if statement or not.
Again, I suggest you use the debugger, and check for yourself.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!