Solved

comparing min(row,forumula)

Posted on 2010-11-10
15
476 Views
Last Modified: 2013-11-25
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
Comment
Question by:Trying_to_Learn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 4
15 Comments
 

Author Comment

by:Trying_to_Learn
ID: 34108701
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
 

Author Comment

by:Trying_to_Learn
ID: 34108750
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
 

Author Comment

by:Trying_to_Learn
ID: 34109033
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Trying_to_Learn
ID: 34109064
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
 
LVL 2

Expert Comment

by:Eyal-f
ID: 34109673
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
 

Author Comment

by:Trying_to_Learn
ID: 34110658
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
 
LVL 2

Accepted Solution

by:
Eyal-f earned 250 total points
ID: 34110759
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
 

Author Comment

by:Trying_to_Learn
ID: 34110864
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
 

Author Comment

by:Trying_to_Learn
ID: 34110902
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
 

Author Comment

by:Trying_to_Learn
ID: 34110924
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
 
LVL 2

Expert Comment

by:Eyal-f
ID: 34110957
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
 

Author Comment

by:Trying_to_Learn
ID: 34111359
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
 
LVL 2

Expert Comment

by:Eyal-f
ID: 34111508
I'll be offline for a few days - if you follow up, I'll answer then if no-one else doe.
0
 

Assisted Solution

by:Trying_to_Learn
Trying_to_Learn earned 0 total points
ID: 34115075
Resolved by myself!
0
 

Author Closing Comment

by:Trying_to_Learn
ID: 34143495
Solved by myself
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calculating T-SCORE inside Excel. 3 368
Calculating Percentile Value inside Excel. 2 99
Programatically extract date from website 8 89
Most Consistent Performer 4 39
How to Win a Jar of Candy Corn: A Scientific Approach! I love mathematics. If you love mathematics also, you may enjoy this tip on how to use math to win your own jar of candy corn and to impress your friends. As I said, I love math, but I gu…
This article provides a brief introduction to tissue engineering, the process by which organs can be grown artificially. It covers the problems with organ transplants, the tissue engineering process, and the current successes and problems of the tec…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question