So I have been working with this Inventory Problem and this is where I am now:

What I want to do is to determine a production schedule that will meet all demands and minimize cost.

I want to store the minimum cost and production quantity for each period and inventory level in a 2 matrices

Here is the code I have so far, I would say I am very close to being done

I am having problems finding the correct minimum cost and the corresponding Product_Quantity.

Basically I compute the cost values, compare it to the cost matrix and when I find the min cost, I record the cost and quantity associated.

Please help ASAP!!

function [f] = Inventory_ma2rafiq(C_Cost)Demand = [1,3,2,4];%C_Cost = [% 20 16 31/2 15 27/2% 16 15 14 12 21/2% 12 10 7 13/2 6% 0 4 5 6 7];Setup_cost = 3;variable_cost = 1;holding_cost = 0.5;Prod_Cap = 5;WareHouse_Cap = 4;Period_Begining = 1;Period_End = 4;Inv_Min = 0;Inv_Max = 4;%Prod_Q_Min = 0;%Prod_Q_Max = 5;n=size(C_Cost,1);Min_Cost=zeros(1,n); %added matrix to keep argmin(Min_Cost)Opt_Prod_Quantity=zeros(1,n); %added matrix to keep argmin(Min_Cost)for Period = Period_End:-1:Period_Begining; % Period Loop for 4 - 1 for Inv_Level = Inv_Min:Inv_Max; % Inventory Level Loop from 4 - 0 for Prod_Quantity = max(0,Demand(Period)-Inv_Level):min(Prod_Cap,WareHouse_Cap-Inv_Level+Demand(Period)); % Loop only with Current Month Demand met constraint and Ending Inventory cannot exceed Capacity Constraint if Period == 4 % Cost for Period 4 if Prod_Quantity == 0 cost = 0; % No Production = 0 Cost else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)); % Holding Cost + ProdCost end end if Period ~= 4 % Cost for Periods other than 4 if Prod_Quantity == 0 cost = C_Cost(Inv_Level+1,Inv_Level+Prod_Quantity-Demand(Period)); % Calculate only cost from Cost Matrix else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)) + C_Cost(Inv_Level+1,Inv_Level+Prod_Quantity-Demand(Period)); % Complete Cost Formula end end %Inv_Level + Prod_Quantity - demand(Period) <=4; % Current Month Demand Constraint %Inv_Level + Prod_Quantity - demand(Period) >=0; % Ending Inventory cannot exceed Capacity Constraint if C_Cost(Period,Inv_Level+1) == cost;% Check to see if this is the minimum cost X = (cost+0.5*(Inv_Level + Prod_Quantity - Demand(Period))); % Store the Minimum cost Min_Cost(1,Inv_Level+1) = (X); % If we are at the minimum cost then store min_cost Opt_Prod_Quantity(1,Inv_Level+1) = (Prod_Quantity); % If we are at the minimum cost then store min_cost associated Prod_Quantity end end endendMin_Cost % Output Min_Cost matrixOpt_Prod_Quantity % Output Production Quantity matrixend

I am getting the 2 matrices now but the values are not correct

Updated code attached...

function [f] = Inventory_ma2rafiq(C_Cost)Demand = [1,3,2,4];%C_Cost = [% 20 16 31/2 15 27/2% 16 15 14 12 21/2% 12 10 7 13/2 6% 7 6 5 4 0];Setup_cost = 3;variable_cost = 1;holding_cost = 0.5;Prod_Cap = 5;WareHouse_Cap = 4;Period_Begining = 1;Period_End = 4;Inv_Min = 0;Inv_Max = 4;%Prod_Q_Min = 0;%Prod_Q_Max = 5;n=size(C_Cost,1);Min_Cost=zeros(1,n); %added matrix to keep argmin(Min_Cost)Opt_Prod_Quantity=zeros(1,n); %added matrix to keep argmin(Min_Cost)for Period = Period_End:-1:Period_Begining; % Period Loop for 4 - 1 for Inv_Level = Inv_Min:Inv_Max; % Inventory Level Loop from 0 - 4 for Prod_Quantity = max(0,Demand(Period)-Inv_Level):min(Prod_Cap,WareHouse_Cap-Inv_Level+Demand(Period)); % Loop only with Current Month Demand met constraint and Ending Inventory cannot exceed Capacity Constraint if Period == 4 % Cost for Period 4 if Prod_Quantity == 0 cost = 0; % No Production = 0 Cost else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)); % Holding Cost + ProdCost end end if Period ~= 4 % Cost for Periods other than 4 if Prod_Quantity == 0 cost = Setup_cost + Prod_Quantity*variable_cost; % Calculate only cost from Cost Matrix else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)) + Setup_cost + Prod_Quantity*variable_cost; % Complete Cost Formula end end %Inv_Level + Prod_Quantity - demand(Period) <=4; % Current Month Demand Constraint %Inv_Level + Prod_Quantity - demand(Period) >=0; % Ending Inventory cannot exceed Capacity Constraint if C_Cost(Period,Inv_Level+1) == cost;% Check to see if this is the minimum cost X = (cost+0.5*(Inv_Level + Prod_Quantity - Demand(Period))); % Store the Minimum cost Min_Cost(1,Inv_Level+1) = (X); % If we are at the minimum cost then store min_cost Opt_Prod_Quantity(1,Inv_Level+1) = (Prod_Quantity); % If we are at the minimum cost then store min_cost associated Prod_Quantity end end endendMin_Cost % Output Min_Cost matrixOpt_Prod_Quantity % Output Production Quantity matrixend

Ok I've got a work around for the min cost, but I need the right production quantities.

The Correct Prod_Quantity is [1 5 0 4]

Updated Code Attached

function [f] = Inventory_ma2rafiq(C_Cost)Demand = [1,3,2,4];%C_Cost = [% 20 16 31/2 15 27/2% 16 15 14 12 21/2% 12 10 7 13/2 6% 0 4 5 6 7];Setup_cost = 3;variable_cost = 1;holding_cost = 0.5;Prod_Cap = 5;WareHouse_Cap = 4;Period_Begining = 1;Period_End = 4;Inv_Min = 0;Inv_Max = 4;%Prod_Q_Min = 0;%Prod_Q_Max = 5;n=size(C_Cost,1);%Min_Cost=zeros(1,n); %added matrix to keep argmin(Min_Cost)Min_Cost = 0;Opt_Prod_Quantity=zeros(1,n); %added matrix to keep argmin(Min_Cost)for Period = Period_End:-1:Period_Begining; % Period Loop for 4 - 1 for Inv_Level = Inv_Min:Inv_Max; % Inventory Level Loop from 4 - 0 for Prod_Quantity = max(0,Demand(Period)-Inv_Level):min(Prod_Cap,WareHouse_Cap-Inv_Level+Demand(Period)); % Loop only with Current Month Demand met constraint and Ending Inventory cannot exceed Capacity Constraint if Period == 4 % Cost for Period 4 if Prod_Quantity == 0 cost = 0; % No Production = 0 Cost else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)); % Holding Cost + ProdCost end end if Period ~= 4 % Cost for Periods other than 4 if Prod_Quantity == 0 cost = Setup_cost + Prod_Quantity*variable_cost; % Calculate only cost from Cost Matrix else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)) + Setup_cost + Prod_Quantity*variable_cost; % Complete Cost Formula end end %Inv_Level + Prod_Quantity - demand(Period) <=4; % Current Month Demand Constraint %Inv_Level + Prod_Quantity - demand(Period) >=0; % Ending Inventory cannot exceed Capacity Constraint if C_Cost(Period,Inv_Level+1) == cost;% Check to see if this is the minimum cost X = (cost+0.5*(Inv_Level + Prod_Quantity - Demand(Period))); % Store the Minimum cost Min_Cost = C_Cost(1,1); % If we are at the minimum cost then store min_cost Opt_Prod_Quantity(1,Period) = (Prod_Quantity); % If we are at the minimum cost then store min_cost associated Prod_Quantity end end endendMin_Cost % Output Min_Cost matrixOpt_Prod_Quantity % Output Production Quantity matrixend

Using my cost formula I compare the computed cost to C_Cost. When the computed cost = min(C_Cost) in that row, I want to record the Production quantity which corresponds to that minimum cost.

I have attached the code I have so far.

I need help with this ASAP!

Thanks in advance

function [f] = Inventory_ma2rafiq(C_Cost)Demand = [1,3,2,4];Setup_cost = 3;variable_cost = 1;holding_cost = 0.5;Prod_Cap = 5;WareHouse_Cap = 4;Period_Begining = 1;Period_End = 4;Inv_Min = 0;Inv_Max = 4;n=size(C_Cost,1);Min_Cost = 0;Opt_Prod_Quantity=zeros(1,n); %added matrix to keep argmin(Min_Cost)for Period = Period_End:-1:Period_Begining; % Period Loop for 4 - 1 for Inv_Level = Inv_Min:Inv_Max; % Inventory Level Loop from 4 - 0 for Prod_Quantity = max(0,Demand(Period)-Inv_Level):min(Prod_Cap,WareHouse_Cap-Inv_Level+Demand(Period)); % Loop only with Current Month Demand met constraint and Ending Inventory cannot exceed Capacity Constraint if Period == 4 % Cost for Period 4 if Prod_Quantity == 0 cost = 0; % No Production = 0 Cost else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)); % Holding Cost + ProdCost end end if Period ~= 4 % Cost for Periods other than 4 if Prod_Quantity == 0 cost = Setup_cost + Prod_Quantity*variable_cost; % Calculate only cost from Cost Matrix else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)) + Setup_cost + Prod_Quantity*variable_cost; % Complete Cost Formula end end %Inv_Level + Prod_Quantity - demand(Period) <=4; % Current Month Demand Constraint %Inv_Level + Prod_Quantity - demand(Period) >=0; % Ending Inventory cannot exceed Capacity Constraint if min(C_Cost(Period,Inv_Level+1)) == cost;% Check to see if this is the minimum cost X = cost; % Store the Minimum cost Min_Cost = C_Cost(1,1); % If we are at the minimum cost then store min_cost Opt_Prod_Quantity(1,Period) = (Prod_Quantity); % If we are at the minimum cost then store min_cost associated Prod_Quantity end end endendMin_Cost % Output Min_Cost matrixOpt_Prod_Quantity % Output Production Quantity matrixend

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

I tried something but I am still not getting the right results...

function [f] = Inventory_ma2rafiq(C_Cost)Demand = [1,3,2,4];Setup_cost = 3;variable_cost = 1;holding_cost = 0.5;Prod_Cap = 5;WareHouse_Cap = 4;Period_Begining = 1;Period_End = 4;Inv_Min = 0;Inv_Max = 4;n=size(C_Cost,1);Min_Cost = 0;Opt_Prod_Quantity=zeros(1,n); %added matrix to keep argmin(Min_Cost)for Period = Period_End:-1:Period_Begining; % Period Loop for 4 - 1 for Inv_Level = Inv_Min:Inv_Max; % Inventory Level Loop from 4 - 0 for Prod_Quantity = max(0,Demand(Period)-Inv_Level):min(Prod_Cap,WareHouse_Cap-Inv_Level+Demand(Period)); % Loop only with Current Month Demand met constraint and Ending Inventory cannot exceed Capacity Constraint if Period == 4 % Cost for Period 4 if Prod_Quantity == 0 cost = 0; % No Production = 0 Cost else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)); % Holding Cost + ProdCost end end if Period ~= 4 % Cost for Periods other than 4 if Prod_Quantity == 0 cost = Setup_cost + Prod_Quantity*variable_cost; % Calculate only cost from Cost Matrix else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)) + Setup_cost + Prod_Quantity*variable_cost; % Complete Cost Formula end end %Inv_Level + Prod_Quantity - demand(Period) <=4; % Current Month Demand Constraint %Inv_Level + Prod_Quantity - demand(Period) >=0; % Ending Inventory cannot exceed Capacity Constraint for i = 1:5; min_value = min(C_Cost(Period,i)); end if min_value == cost;% Check to see if this is the minimum cost %X = cost; % Store the Minimum cost Min_Cost = C_Cost(1,1); % If we are at the minimum cost then store min_cost Opt_Prod_Quantity(1,Period) = (Prod_Quantity); % If we are at the minimum cost then store min_cost associated Prod_Quantity end end endendMin_Cost % Output Min_Cost matrixOpt_Prod_Quantity % Output Production Quantity matrixend

Let's see if I';m understanding the setup correctly:

you have a 4x5 C_cost matrix as you showed above. For each row, you compute a cost value. In addition, for each row, you have a production value.

So you have a 4x5 C_cost matrix, and if you run through the for loops in your next-to-last comment, exluding the "if min(C_Cost(Period,Inv_Level+1)) " blocke, you get a 4-element cost vector and a 4-element vector of production values (prodv).

The C_Cost matrix is what I am supposed to get when I perform my calculations.

It is only there for reference. So I calculate the cost values that are actually there in the cost matrix using the cost formulas in my code. The min of the C_Cost matrix (row) is then compared to the cost that I come up with, and when both of them are the same, then I add the production quantity to the matrix.

I am having problems with finding the correct cost value = min C_Cost part

I'm not understanding why the code snippet I gave doesn't suit, then.

min(C_Cost,[],2) wil give you a vector containing the minimum values for each row of the matrix (i.e. the first vector element is the min of the first row, etc.).
if you create a computed cost vector, min(C_Cost,[],2)==cost will tell you at what rows the minimum for that row is equal to the computed cost corrosponding to that row.

How is this different from what you need?

0

Trying_to_LearnAuthor Commented:

I added a Break in the last if statement, which decreases the Period loop by 1 which is what I want, but the other 2 inner loops are not being incremented properly and I am getting wrong values for the computed cost formula, which is why I am getting wrong production quantities....

0

Trying_to_LearnAuthor Commented:

Eyal-f: The snippet you provided is working fine and I have incorporated it into my code, but the code is not matching the computed cost to the min cost correctly.

Updated code attached

function [f] = Inventory_ma2rafiq(C_Cost)Demand = [1,3,2,4];Setup_cost = 3;variable_cost = 1;holding_cost = 0.5;Prod_Cap = 5;WareHouse_Cap = 4;Period_Begining = 1;Period_End = 4;Inv_Min = 0;Inv_Max = 4;n=size(C_Cost,1);Min_Cost = 0;Opt_Prod_Quantity=zeros(1,n); %added matrix to keep argmin(Min_Cost)for Period = Period_End:-1:Period_Begining; % Period Loop for 4 - 1 for Inv_Level = Inv_Min:Inv_Max; % Inventory Level Loop from 4 - 0 for Prod_Quantity = max(0,Demand(Period)-Inv_Level):min(Prod_Cap,WareHouse_Cap-Inv_Level+Demand(Period)); % Loop only with Current Month Demand met constraint and Ending Inventory cannot exceed Capacity Constraint if Period == 4 % Cost for Period 4 if Prod_Quantity == 0 cost = 0; % No Production = 0 Cost else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)); % Holding Cost + ProdCost end end if Period ~= 4 % Cost for Periods other than 4 if Prod_Quantity == 0 cost = Setup_cost + Prod_Quantity*variable_cost; % Calculate only cost from Cost Matrix else cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)) + Setup_cost + Prod_Quantity*variable_cost; % Complete Cost Formula end end %Inv_Level + Prod_Quantity - demand(Period) <=4; % Current Month Demand Constraint %Inv_Level + Prod_Quantity - demand(Period) >=0; % Ending Inventory cannot exceed Capacity Constraint %for i = 1:5; %min_value = min(C_Cost(Period,i)); %end if min(C_Cost,[],2)==cost;% Check to see if this is the minimum cost %X = cost; % Store the Minimum cost Min_Cost = C_Cost(1,1); % If we are at the minimum cost then store min_cost Opt_Prod_Quantity(1,Period) = (Prod_Quantity); % If we are at the minimum cost then store min_cost associated Prod_Quantity break end end endendMin_Cost % Output Min_Cost matrixOpt_Prod_Quantity % Output Production Quantity matrixend

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Updated code attached...

Open in new window