Info

This question is closed. Reopen it to edit or answer.

'While' function

2 views (last 30 days)
lucas myrby
lucas myrby on 20 Oct 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
My code lookes like this, its a problem where the placement of a factory has to be optimized for the transportation and demand for products, m is factories and n is costumers. When i run the code it nerver ends. What am i missing?
m = 3;
n = 5;
s = [ 239 225 184 ] ;
d = [ 92 82 83 69 74 ] ;
f = [ 589 766 886 ];
c = [ 14 5 6 24 6
9 22 26 5 21
16 11 23 28 24 ];
e=0.01;
tic
x = zeros(size(c));
y = zeros(size(f));
ss=s;
dd=d;
cc=c;
ff=f;
wow = 20;
cost = 0;
while sum(dd)>0
[Factory_price,i] = min(ff(ff>0));
factory_kap = ss(i);
row = cc(i,:);
[factory_rk,j] = min(row(row>0));
cost = factory_rk+cost;
demand = dd(j);
while factory_kap > 0 && demand > 0
factory_kap = factory_kap-1;
demand = demand-1;
cost = factory_rk+cost;
if demand == 0
dd(j) = 0;
cc(i,j) = 0;
elseif factory_kap == 0
ss(i) = 0;
ff(i) = 0;
end
end
wow=wow-1;
end
cost;
toc
  1 Comment
lucas myrby
lucas myrby on 20 Oct 2020
m = 3;
n = 5;
s = [ 239 225 184 ] ;
d = [ 92 82 83 69 74 ] ;
f = [ 589 766 886 ];
c = [ 14 5 6 24 6
9 22 26 5 21
16 11 23 28 24 ];
tic
x = zeros(size(c));
y = zeros(size(f));
ss=s;
dd=d;
cc=c;
ff=f;
e=1;
while sum(dd)>0
[startfactory, place]= min(ff);
[mintransport, place_effectiv]=min(cc(place,:));
DD=dd(place_effectiv);
send=DD;
for I=1:dd(place_effectiv)
DD=DD-1;
if ss(place) <=0
break
end
ss(place)=ss(place)-1;
end
x(place,place_effectiv)=skickad;
if ss(place) >0
cc(:,place_effectiv)=999999;
dd(place_effectiv)=0;
elseif ss(place)==0 && DD==0
ff(place)=999999;
dd(place_effectiv)=0;
cc(place,:)=999999;
ss(place)=0;
elseif ss(place)==0
ff(place)=999999;
dd(place_effectiv)=DD;
cc(place,:)=99999;
ss(place)=0;
end
end
for K=1:numel(s)
dif=s(K)-ss(K);
if dif >0
y(K)=1;
else
y(K)=0;
end
end
cost=sum(sum(c.*x)) + e*sum(f.*y);
fprintf('Totalkostnad: %.2f\n', cost);
str0=sprintf('%d ',y);
toc
Uppdate but now i cant run at all :(

Answers (1)

Andy
Andy on 20 Oct 2020
If this is your complete code it could be that i and j are not set or changed anywhere.

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by