Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

comparing min(row,forumula)

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
    end
end
Min_Cost % Output Min_Cost matrix
Opt_Prod_Quantity % Output Production Quantity matrix
end

Open in new window

0
Trying_to_Learn
Asked:
Trying_to_Learn
  • 11
  • 4
2 Solutions
 
Trying_to_LearnAuthor Commented:
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
    end
end
Min_Cost % Output Min_Cost matrix
Opt_Prod_Quantity % Output Production Quantity matrix
end

Open in new window

0
 
Trying_to_LearnAuthor Commented:
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
    end
end
Min_Cost % Output Min_Cost matrix
Opt_Prod_Quantity % Output Production Quantity matrix
end

Open in new window

0
 
Trying_to_LearnAuthor Commented:
So just to clarify the logic,

I have a min cost matrix, C_Cost (this matrix represents the minimum costs of periods 1-4 (Rows) and with inventory levels 0-4 (Cols)

C_Cost =

   20.0000   16.0000   15.5000   15.0000   13.5000
   16.0000   15.0000   14.0000   12.0000   10.5000
   12.0000   10.0000    7.0000    6.5000      6.0000
    7.0000     6.0000     5.0000    4.0000         0

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
    end
end
Min_Cost % Output Min_Cost matrix
Opt_Prod_Quantity % Output Production Quantity matrix
end

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Trying_to_LearnAuthor Commented:
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
    end
end
Min_Cost % Output Min_Cost matrix
Opt_Prod_Quantity % Output Production Quantity matrix
end

Open in new window

0
 
Eyal-fCommented:
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).

So you can do (p is your output):

p=zeros(length(prodv),1)
p(min(C_cost,[],2)==cost)=prodv(a);

0
 
Trying_to_LearnAuthor Commented:
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
0
 
Eyal-fCommented:
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
    end
end
Min_Cost % Output Min_Cost matrix
Opt_Prod_Quantity % Output Production Quantity matrix
end

Open in new window

0
 
Trying_to_LearnAuthor Commented:
Actually this if min(C_Cost,[],2)==cost; statement is never true so now I am getting a row of 0's in my production quantity :(
0
 
Eyal-fCommented:
The last could be an issue of accuracy; try rounding the values (e.g. floor(a*1e2)/1e2 will round a down to 2 decimal points)
0
 
Trying_to_LearnAuthor Commented:
I think I have some errors in my cost function, I will try working on it and see if I can resolve the issue

0
 
Eyal-fCommented:
I'll be offline for a few days - if you follow up, I'll answer then if no-one else doe.
0
 
Trying_to_LearnAuthor Commented:
Resolved by myself!
0
 
Trying_to_LearnAuthor Commented:
Solved by myself
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now