minimum value is selected always from last iteration

1 次查看(过去 30 天)
part_machine=[1 0 0 1 0 1 1;
0 1 1 1 0 0 1;
1 0 0 1 1 0 0;
1 0 0 0 1 0 1;
1 1 0 0 0 1 0;
0 1 0 0 0 1 1];
demand=[600;550;620;500;590;600];
numIterations=3;
for k=1:numIterations
rand_cell_assigntomachines=randi([0,1],7,3);
Part_cell=part_machine*rand_cell_assigntomachines;
Part_cell(Part_cell>=1)=1;
no_of_movements=sum(Part_cell,2)-1;
movement_cost=sum(bsxfun(@times,no_of_movements,demand))
end
minval=min(movement_cost)
the result is
movement_cost =
5110
movement_cost =
6920
movement_cost =
6300
minval =
6300
but in 3 iterations, 6300 is not minimum value.. I checked it and the value which is always picked as min is from the last iteration.. why is this so?

采纳的回答

Walter Roberson
Walter Roberson 2016-12-16
Assign to movement_cost(k) instead of overwriting all of movement_cost
  3 个评论
Walter Roberson
Walter Roberson 2016-12-16
Inside the loop,
movement_cost(k)=sum(bsxfun(@times,no_of_movements,demand));
After the loop, stay with your existing
minval=min(movement_cost)
summyia qamar
summyia qamar 2016-12-19
I'm running this code.
part_machine=[1 0 0 1 0 1 1
0 1 1 1 0 0 1
1 0 0 1 1 0 0
1 0 0 0 1 0 1
1 1 0 0 0 1 0
0 1 0 0 0 1 1];
demand=[600;550;620;500;590;600];
numIterations=100;
for k=1:numIterations
rand_cell=randi([0,1],7,3);
if all(sum(rand_cell,2)==1);
Part_cell(k)=part_machine*rand_cell;
end
Part_cell(Part_cell>=1)=1
no_of_movements=sum(Part_cell,2)-1;
movement_cost(k)=sum(bsxfun(@times,no_of_movements,demand));
end
minval=min(movement_cost);
[rand_cell minval(ones(7,1))]
the idea behind this code is * generate rand_cell * select that rand_cell in which sum of rows is 1 * multiply Part_machine matrix with that rand_cell * then apply the other functions on part_machine matrix * then at the end select the minval from all iterations * result is displayed with that rand_cell matrix which has generated the minimum value
now the result I'm getting is like this
ans =
1 0 1 6920
1 0 0 6920
0 1 0 6920
1 1 0 6920
1 1 1 6920
1 1 1 6920
1 0 1 6920
it is not the rand_cell matrix according to condition
all(sum(rand_cell,2)==1)
where is the problem?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by