Azzera filtri
Azzera filtri

How can I loop this endless if conditions

1 visualizzazione (ultimi 30 giorni)
Richard Wolvers
Richard Wolvers il 5 Giu 2017
Commentato: Rena Berman il 6 Giu 2017
Hello everyone,
I have a project where I have to build a matlab simulation, where several different items are repaired in a repairshop. Now several items arrive per time period for each item. There is only one repairman and he repairs the items in order of priority. However the repairman can not repair every item, as he is limited by his time. And if there are no items to repair for a certain item then he should go onto the next. So I am trying to build a simulation for this to see the stock of each item in each time period.
What I want my script to do is for every t, to add the arrived items per time period (time period is a column on matrix x) and subtract the items that have been repaired. However which item has been repaired and how much is determined by a priority matrix and some if conditions.
My problem starts at % Stock for condition 2 and 3. If you read my script (script below) from that point you may notice that condition 2 and 3 are conditions that go on untill it is fullfilled. It is possible to write this down manually for a small amount of n, but if n=5 or more it is too much work, then you would have to write down a lot of if conditions after each other. What I want from the condition 3 is that it needs to repair a certain item, but if there are no items for item sort #1 then it should repair items for item sort #2. And if item sort #2 has 0 then it should go to item sort #3 etc. What I want from condition 2 is when it has repaired the items for item sort #1, it should repair some items from item sort #2 and if it still has time it should go on to item sort #3. etc. I am having trouble making loops for this, how can I do this? As the amount of if conditions should be based on my n (Which is the amount of items sorts) Thanks in advance,
Richard
n = 5; % Amount of different items in the system
t = 100; % Runtime
r = [6 12 10 5 14]'; The amount of items that can be repaired per time period for each item
a = [3 8 2 8 3]'; The amount of broken item that arrive for a time period
p = [5 5 5 5 5]'; Priority numbers for each item
% This creates a matrix of random numbers based on the amount of broken items that arrive per time period
at = repmat(a,1,t);
rat = rand(n,t);
xat = at .* -log(rat);
yat = round (xat);
% This creates a matrix of random numbers based on the amount of items that can be repaired per time period
rt = repmat(r,1,t);
rrt = rand(n,t);
xrt = rt .* -log(rrt);
yrt = round (xrt);
% Priority ranking
pr = h .* r; % Priority rule
prr = repmat(pr,1,t);
pr_sorted = sort(prr);
[~, rnk] = ismember(prr,pr_sorted); %This ranks the values in the prr matrix
A = zeros(n,t);
for k=1:n
A(k,1)= find (rnk(:,1) == n+1-k); This puts the place of the highest value in the first row and the place of the second highest value in the second row.
end
% Stock of items
x = zeros (n,t);
x(1:n,1) = yat(1:n,1); % The first column is the amount of broken items in stock, based on the amount of items arrived in that time period
x(1:n,2)=x(1:n,1)+yat(1:n,2); %The second column is the amount of broken items in column 1 + the amount of items arrived.
%Below are conditions to subtract amount of broken items, because some items are repaired. The decision to repair certain items is based on the priority ranking matrix A.
% Condition 1: So A(1,1) gives us the place, which is considered to be the first item that have to be repaired.
if x(A(1,1),1)>=yrt(A(1,1),1)
x(A(1,1),2)= x(A(1,1),1)-yrt(A(1,1),1)+yat(A(1,1),2); %Now it is all good and it should go to the next column
%condition 2: So A(1,1) gives us the place, which is considered to be the first item that have to be repaired, but the repairman can repair more items than there are broken items. So then he is going to repair the items in row x(A(2,1),1) with his spare time.
elseif 0<x(A(1,1),1)<yrt(A(1,1),1)
x(A(1,1),2)= yat(A(1,1),2);
x(A(2,1),2)= x(A(2,1),1)-round(yrt(A(2,1),1)*(1-(x(A(1,1),1)/yrt(A(1,1),1))))+yat(A(2,1),2);
% And if 0<x(A(2,1),1)<round(yrt(A(2,1),1)*(1-(x(A(1,1),1)/yrt(A(1,1),1))))
% Then it should: x(A(3,1),2)= x(A(3,1),1)-round(yrt(A(3,1),1)*(1-(x(A(1,1),1)/(yrt(A(1,1),1)+(yrt(A(2,1),1))))+yat(A(3,1),2);
% Etc...
%condition 3 So A(1,1) gives us the place, which is considered to be the first item that have to be repaired, but there are no items to be repaired so it should go to A(2,1), which is the second sort of item that should be repaired
elseif x(A(1,1),1) == 0 %and x(A(2,1),1) ~=0 and x(A(2,1),1)>=yrt(A(2,1),1)
x(A(2,1),2)= x(A(2,1),1)-yrt(A(2,1),1)+yat(A(2,1),2);
% If x(A(2,1),1) = 0 then it should check if x(A(3,1)~=0 and x(A(3,1),1)>=yrt(A(3,1),1
% And do the same x(A(3,1),2)= x(A(3,1),1)-yrt(A(3,1),1)+yat(A(3,1),2);
% It should do this untill it gets to a value where x(A(1:n,1) ~=0
% And if x(n,1) = 0 then it should do nothing
% And if this value is0<x(A(...,1),2)<yrt(A(...,1),2); then it
% should do the same as line 2
end
  6 Commenti
Guillaume
Guillaume il 5 Giu 2017
Editing your question away is considered extremely rude as you've effectively completely wasted my time and everybody's else that looked at your question.
Rena Berman
Rena Berman il 6 Giu 2017
(Answers Dev) Restored edit

Accedi per commentare.

Risposte (1)

Guillaume
Guillaume il 5 Giu 2017
There's a lot I don't understand about your code (why is A an n x t matrix when you only use the first column?, why is x an n x t matrix if you only use the first two columns? etc.). So, I'm going to answer generally.
If I understood correctly you're trying to repair as many items as possible in a fixed time, according to their priority. A for loop where you iterate over each item by priority, repair as many as you can and recalculate the remaining time is probably the easiest way to do it:
itemcounts = [3 0 3 4 10]; %number of items to repaur in each category
priorities = [4 3 1 2 5]; %for example, lowest number is highest priorities
repairtimes = [3 5 20 10 5]; %time to repair an item of each category
availabletime = 58;
[~, order] = sort(priorities);
itemsrepaired = zeros(size(order));
for iorder = 1:numel(order)
canrepair = min(floor(availabletime / repairtimes(order(iorder))), itemcounts(order(iorder))); %how many items can be repaired
itemsrepaired(order(iorder)) = canrepair;
availabletime = availabletime - canrepair * repairtimes(order(iorder)); %calculate remaining time
end %and move on to next item category
fprintf('\nresults:\n');
fprintf('repaired %d of item %d, this took %d\n', [itemsrepaired; 1:numel(itemsrepaired); itemsrepaired .* repairtimes]);
fprintf('\ntotal time taken: %d\n', sum(itemsrepaired .* repairtimes));
  1 Commento
Richard Wolvers
Richard Wolvers il 5 Giu 2017
Thanks for your answer In the future i will also try other priorities, which could change the priority per time period, that's why I already made a matrix for it. I want to use all the columns eventually, but I try to build this simulation step by step, that's why i am only using the first two columns.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by