how to use 'optim.problemdef.OptimizationVariable' as double
Info
This question is closed. Reopen it to edit or answer.
Show older comments
this is the code:
%cost rates
P = 20
H = 0.1 *P
B = 10*P
E = P;
%demand vector
D = [21 37 1 16 8 5 10 18 20 27 21 35 11 44 2 34 21 28 8 10 41 49 16 35 44 45 5 2 9 44 5 22 48 27 35 16 35 42 1 38 50 38 15 40 6 23 46 15 15 7]
%shelf life
L = 4
% Initialize variables
T = length(D); % Number of days
I = zeros(1, T); % Inventory levels
order_qty = zeros(1, T); % Order quantities
Sexpired = zeros(1, T); % Expired quantities
A = zeros(1, T); % age of inventory
C = zeros(1,T); % backorder amount per day
in = zeros(1,T); % leftover inventory per day
max_inventory = 50;
% Initial conditions
I(1,1) = 10;
A(1,1) = 0;
% starting loop
for t = 1:T-1
% calculate leftover inventory
in(t) = max(0,(I(t)-D(t)));
if in(t) == 0
%order_qty(t) = max(0, max_inventory - I(t)); %order upto S
%%%%%%%%%%%%%%% getting error in this line%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
order_qty(t) = X; % assigning value of optimised variable to order_qty for today
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C(t) = abs(I(t)-D(t)); % counting backorders or shortage
A(t+1) = 1; % Reset age to 1 when new inventory arrives
I(t+1) = min(max_inventory, order_qty(t)); % inventory level for tomorow
else
% FIFO implementation
A(t+1) = A(t) + 1; % Increase age for existing items
A(t+1) = min(A(t+1), L); % Cap age to L
% Check for expired items
if A(t+1) >= L % if expired i.e age exceeds shelf life
Sexpired(t) = in(t); % count leftover inventory today as expired and discard
A(t+1) = 1; % Reset age to 1 for the newly ordered items tomorow
I(t+1) = min(max_inventory,order_qty(t)); % tomorow inventory will be only new arrival
end
end
end
% Calculate costs
holding_cost = sum(H .* I);
purchase_cost = sum(P .* order_qty);
expiry_cost = sum(E .* Sexpired);
shortage_cost = sum(B .* C);
% Calculate total cost outside the loop
total_cost = holding_cost + purchase_cost + expiry_cost + shortage_cost;
% Display costs
disp(['Holding Cost: ' num2str(holding_cost)]);
disp(['Purchase Cost: ' num2str(purchase_cost)]);
disp(['Expiry Cost: ' num2str(expiry_cost)]);
disp(['Shortage Cost: ' num2str(shortage_cost)]);
disp(['Total Cost: ' num2str(total_cost)]);
% Display quantity ordered each day
disp('Quantity Ordered Each Day:');
disp(order_qty);
disp(Sexpired);
% Create optimization variables
X3 = optimvar("X","LowerBound",0,"UpperBound",50);
% Set initial starting point for the solver
initialPoint4.X = repmat(25,size(X3));
% Create problem
problem = optimproblem;
% Define problem objective
problem.Objective = sum(H * I + B * C + E * Sexpired + P * order_qty);
% Set nondefault solver options
options = optimoptions("ga","CreationFcn","gacreationlinearfeasible");
% Display problem information
show(problem);
% Clear variables
clearvars X3 initialPoint4 options
3 Comments
Torsten
on 7 Feb 2024
You use the variable X
order_qty(t) = X; % assigning value of optimised variable to order_qty for today
before you define it
X3 = optimvar("X","LowerBound",0,"UpperBound",50);
But this is not the only problem with your code.
Gauri
on 7 Feb 2024
Torsten
on 7 Feb 2024
We need a mathematical description of your optimization problem.
Answers (0)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!