# Can you help me debug this code?

6 views (last 30 days)
Lou Simon on 1 Sep 2023
Answered: lazymatlab on 1 Sep 2023
% Define parameter values
Cost = [3 4; 5 6; 2 5];
ProdCost = [1 2; 4 3; 2 3];
TransCost = [8 10 5; 12 7 9; 11 9 15];
HoldCost = [3 2 4];
RawReq = [1200 1500; 800 1300; 1500 2000];
ProdCap = [1500 2000; 2000 1800; 1800 2500];
WareCap = [1000; 1500; 1000];
WareDemand = [500 600 900; 600 800 1400; 1100 1100 1200];
OrderCost = [10000 10000; 8000 8500; 6500 5000];
% Create optimization model
model = optimproblem('ObjectiveSense', 'minimize');
% Define decision variables
x = optimvar('x', 2, 3, 'LowerBound', 0, 'Type', 'integer');
y = optimvar('y', 3, 2, 'LowerBound', 0, 'Type', 'integer');
z = optimvar('z', 3, 3, 'LowerBound', 0, 'Type', 'integer');
q = optimvar('q', 3, 3, 'LowerBound', 0, 'Type', 'integer');
O = optimvar('O', 2, 3, 'LowerBound', 0, 'UpperBound', 1, 'Type', 'integer');
% Define objective function
obj = sum(sum(Cost .* x)) + sum(sum(ProdCost .* y)) + sum(sum(TransCost .* z)) + ...
sum(HoldCost .* (sum(y, 2) / 2)) + sum(sum(OrderCost .* O));
% Add objective function to the model
model.Objective = obj;
% Define constraints
cons1 = sum(sum(z .* RawReq)) <= x;
cons2 = sum(z, 2) <= ProdCap;
cons3 = sum(z, 1) <= WareCap';
cons4 = sum(q, 2) == WareDemand;
cons5 = y - z == 0;
% Add constraints to the model
model.Constraints.cons1 = cons1;
model.Constraints.cons2 = cons2;
model.Constraints.cons3 = cons3;
model.Constraints.cons4 = cons4;
model.Constraints.cons5 = cons5;
% Solve the optimization problem
[solution, fval, exitflag, output] = solve(model);
% Display results
disp("Optimal Solution:");
disp("Total Cost: " + fval);
disp("Raw Material Purchase Plan:");
disp(solution.x);
% Display more results as needed
% ...
% Sensitivity/Scenario Analysis (Nested Function)
function sensitivityAnalysis()
fprintf("Sensitivity Analysis:\n");
% Varying transportation costs by +/- 10%
transport_cost_variation = [0.9 1.1]; % Vary costs by 10%
for factor = transport_cost_variation
TransCost_variation = TransCost * factor;
% Update the objective function with the new transportation costs
obj_variation = sum(sum(Cost .* x)) + ...
sum(sum(ProdCost .* y)) + ...
sum(sum(TransCost_variation .* z)) + ...
sum(HoldCost .* (sum(y, 2) / 2)) + ...
sum(sum(OrderCost .* O));
model.Objective = obj_variation;
% Solve the optimization problem
[~, fval_variation] = solve(model);
fprintf("Transportation Cost Variation Factor: %.2f\n", factor);
fprintf("Total Cost: %.2f\n", fval_variation);
% Display more results or analysis as needed
end
end
% Monte Carlo Simulation (Nested Function)
function monteCarloSimulation()
fprintf("Monte Carlo Simulation:\n");
% Simulate parameter variations for a single scenario
num_simulations = 1000;
simulation_costs = zeros(num_simulations, 1);
for i = 1:num_simulations
% Generate random variations for transportation costs
random_cost_variation = 0.9 + 0.2 * rand(); % Random variation between 0.9 and 1.1
TransCost_variation = TransCost * random_cost_variation;
% Update the objective function with the new transportation costs
obj_variation = sum(sum(Cost .* x)) + ...
sum(sum(ProdCost .* y)) + ...
sum(sum(TransCost_variation .* z)) + ...
sum(HoldCost .* (sum(y, 2) / 2)) + ...
sum(sum(OrderCost .* O));
model.Objective = obj_variation;
% Solve the optimization problem
[~, fval_variation] = solve(model);
% Store total cost of simulation
simulation_costs(i) = fval_variation;
end
% Calculate mean and standard deviation of simulation costs
mean_cost = mean(simulation_costs);
std_deviation = std(simulation_costs);
fprintf("Mean Total Cost: %.2f\n", mean_cost);
fprintf("Standard Deviation: %.2f\n", std_deviation);
% Display more insights or analysis as needed
end
% Call sensitivity analysis and Monte Carlo simulation functions
sensitivityAnalysis();
Function definitions in a script must appear at the end of the file.
Move all statements after the "monteCarloSimulation" function definition to before the first local function definition.
monteCarloSimulation();
% ...
end

Dinesh on 1 Sep 2023
Hey Lou Simon.
Is there a specific problem that you are facing while trying to debug this file?
If you generally want to know how to debug a file or if you are new to debugging code in MATLAB, the following link can help you:

lazymatlab on 1 Sep 2023
1. If you intended to make sensitivityAnalysis and monteCarloSimulation to be nested functions, the overall code should be also a function. This is what 'nested' means. Just adding something like below at line 1 will do:
function function_name
2. There are a lot of size mismatches in your code. The operator .* is for element-wise multiplication which implies that two operands are at the same size. If you are trying to matrix multiplication, use * instead of .*.

### Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!