mp6: Screwy implementation of ex2
This commit is contained in:
parent
34dc22c5b0
commit
cbc8fbfd84
11 changed files with 782 additions and 0 deletions
15
mp6/Makefile
Normal file
15
mp6/Makefile
Normal file
|
@ -0,0 +1,15 @@
|
|||
filename=template
|
||||
|
||||
pdf:
|
||||
pdflatex ${filename}
|
||||
#bibtex ${filename}
|
||||
pdflatex ${filename}
|
||||
pdflatex ${filename}
|
||||
make clean
|
||||
|
||||
read:
|
||||
evince ${filename}.pdf &
|
||||
|
||||
|
||||
clean:
|
||||
rm -f *.out *.log *.bbl *.blg *.aux ${filename}.log ${filename}.ps ${filename}.aux ${filename}.out ${filename}.dvi ${filename}.bbl ${filename}.blg
|
106
mp6/Project_6_Maggioni_Claudio/auxiliary.m
Normal file
106
mp6/Project_6_Maggioni_Claudio/auxiliary.m
Normal file
|
@ -0,0 +1,106 @@
|
|||
function [B,D,c_B,c_D,x_B,x_D,index_B,index_D] = auxiliary (A_aug,c_aug,h,m,n)
|
||||
% The auxiliary problem is always a minimization problem
|
||||
|
||||
% The output will be: B and D (basic and nonbasic matrices), c_B and c_D
|
||||
% (subdivision of the coefficient vector in basic and nonbasic parts), x_B
|
||||
% and x_D (basic and nonbasic variables) and index_B and index_D (to keep
|
||||
% track of the variables indices)
|
||||
|
||||
% Redefine the problem by introducing the artificial variables required by
|
||||
% the auxiliary problem (the objective function has to reach value 0)
|
||||
A_aug = [A_aug, eye(m)];
|
||||
c_aug = [c_aug, zeros(1,m)]; % original objective function coefficients
|
||||
c_aux = [zeros(1,n+m), ones(1,m)]; % auxiliary c of minimization problem
|
||||
index = 1:n+2*m; % index to keep track of the different variables
|
||||
|
||||
% Defining the basic elements and the nonbasic elements
|
||||
B = A_aug(:,(n+m+1):(n+2*m)); % basic variables
|
||||
D = A_aug(:,1:(n+m)); % nonbasic variables
|
||||
c_Baux = c_aux(1,(n+m+1):(n+2*m));
|
||||
c_Daux = c_aux(1,1:(n+m));
|
||||
c_B = c_aug(1,(n+m+1):(n+2*m));
|
||||
c_D = c_aug(1,1:(n+m));
|
||||
x_B = h;
|
||||
x_D = zeros((n+m),1);
|
||||
index_B = index(1,(n+m+1):(n+2*m));
|
||||
index_D = index(1,1:(n+m));
|
||||
|
||||
nIter = 0;
|
||||
z = c_Baux*x_B;
|
||||
itMax = factorial(2*m+n)/(factorial(n+m)*factorial(m));
|
||||
|
||||
% Compute B^{-1}*D and B^{-1}*h
|
||||
BiD = B\D;
|
||||
Bih = B\h;
|
||||
|
||||
% Compute reduced cost coefficients
|
||||
r_D = c_Daux - c_Baux*BiD;
|
||||
|
||||
while(z~=0)
|
||||
|
||||
% Find nonnegative index
|
||||
idxIN = find(r_D == min(r_D));
|
||||
|
||||
% Using Bland's rule to avoid cycling
|
||||
if(size(idxIN,2)>1)
|
||||
idxIN = min(idxIN);
|
||||
end
|
||||
in = D(:,idxIN);
|
||||
c_inaux = c_Daux(1,idxIN);
|
||||
c_in = c_D(1,idxIN);
|
||||
index_in = index_D(1,idxIN);
|
||||
|
||||
% Evaluating the coefficients ratio
|
||||
inRatio = BiD(:,idxIN);
|
||||
ratio = Bih./inRatio;
|
||||
|
||||
% Find the smallest ratio
|
||||
for i = 1:size(ratio,1) % Eliminating negative ratios
|
||||
if(ratio(i,1)<0)
|
||||
ratio(i,1) = Inf;
|
||||
end
|
||||
end
|
||||
idxOUT = find(ratio==min(ratio));
|
||||
% Using Bland's rule to avoid cycling
|
||||
if(size(idxOUT,1)>1)
|
||||
idxOUT = min(idxOUT);
|
||||
end
|
||||
out = B(:,idxOUT);
|
||||
c_outaux = c_Baux(1,idxOUT);
|
||||
c_out = c_B(1,idxOUT);
|
||||
index_out = index_B(1,idxOUT);
|
||||
|
||||
% Update the matrices by exchanging the columns
|
||||
B(:,idxOUT) = in;
|
||||
D(:,idxIN) = out;
|
||||
c_Baux(1,idxOUT) = c_inaux;
|
||||
c_Daux(1,idxIN) = c_outaux;
|
||||
c_B(1,idxOUT) = c_in;
|
||||
c_D(1,idxIN) = c_out;
|
||||
index_B(1,idxOUT) = index_in;
|
||||
index_D(1,idxIN) = index_out;
|
||||
|
||||
% Compute B^{-1}*D and B^{-1}*h
|
||||
BiD = B\D;
|
||||
Bih = B\h;
|
||||
|
||||
% Compute reduced cost coefficients
|
||||
r_D = c_Daux - (c_Baux * BiD);
|
||||
|
||||
% Detect inefficient loop if nIter > total number of basic solutions
|
||||
nIter = nIter + 1;
|
||||
if nIter > itMax
|
||||
error('The original LP problem does not admit a feasible solution.');
|
||||
end
|
||||
|
||||
x_B = Bih - BiD * x_D;
|
||||
z = c_Baux * x_B;
|
||||
end
|
||||
|
||||
check = index_D<(n+m+1);
|
||||
D = D(:,check);
|
||||
index_D = index_D(1,check);
|
||||
c_D = c_D(1,check);
|
||||
x_D = x_D(check,1);
|
||||
|
||||
end
|
18
mp6/Project_6_Maggioni_Claudio/printSol.m
Normal file
18
mp6/Project_6_Maggioni_Claudio/printSol.m
Normal file
|
@ -0,0 +1,18 @@
|
|||
function [x_B,index_B] = printSol (z,x_B,index_B,m,n)
|
||||
% this function prints the solution while distinguishing between original variables and slack variables
|
||||
|
||||
[sorted, index] = sort(index_B);
|
||||
x_B = x_B(index);
|
||||
index_B = sorted;
|
||||
|
||||
fprintf('Variables and optimal solution:\n\n');
|
||||
for i = 1:m
|
||||
if(index_B(i)<n+1)
|
||||
fprintf('x%d = %f\n', sorted(i), x_B(i));
|
||||
else
|
||||
fprintf('s%d = %f\n', sorted(i)-n, x_B(i));
|
||||
end
|
||||
end
|
||||
fprintf('\nOptimal value of z = %d\n\n', z);
|
||||
|
||||
end
|
33
mp6/Project_6_Maggioni_Claudio/simplex.m
Normal file
33
mp6/Project_6_Maggioni_Claudio/simplex.m
Normal file
|
@ -0,0 +1,33 @@
|
|||
function [z,x_B,index_B] = simplex (type,A,h,c,sign)
|
||||
% Simplex method solver for a linear programming problem
|
||||
% Input arguments:
|
||||
% type = 'max' for maximization, 'min' for minimization
|
||||
% A = matrix holding the constraints coefficients
|
||||
% h = coefficients of the constraints inequalities (rhs vector)
|
||||
% c = coefficients of the objective function
|
||||
% sign = vector holding information about the constraints if the system
|
||||
% needs to be standardized (-1: less or equal, 0: equal, 1:vgreater or equal)
|
||||
|
||||
m = size(A,1);
|
||||
n = size(A,2);
|
||||
|
||||
% Compute the maximum number of basic solutions of the original
|
||||
% problem (i.e., the maximum number of iterations necessary to solve the problem)
|
||||
itMax = factorial(2*m+n)/(factorial(n+m)*factorial(m));
|
||||
|
||||
% Writing the problem in standard form
|
||||
[A_aug,h,c_aug] = standardize(type,A,h,c,m,sign);
|
||||
|
||||
% Determining a starting feasible initial basic solution
|
||||
[B,D,c_B,c_D,x_B,x_D,index_B,index_D] = auxiliary(A_aug,c_aug,h,m,n);
|
||||
|
||||
% Solving the linear programming problem with the Simplex method
|
||||
[x_B,c_B,index_B] = simplexSolve(type,B,D,c_B,c_D,h,x_B,x_D,index_B,index_D,itMax);
|
||||
|
||||
% Compute the value of the objective function
|
||||
z = sum(c_B' .* x_B) + sum(c_D' .* x_D);
|
||||
|
||||
% Output of the solution
|
||||
[x_B,index_B] = printSol(z,x_B,index_B,m,n);
|
||||
|
||||
end
|
82
mp6/Project_6_Maggioni_Claudio/simplexSolve.m
Normal file
82
mp6/Project_6_Maggioni_Claudio/simplexSolve.m
Normal file
|
@ -0,0 +1,82 @@
|
|||
function [x_B,c_B,index_B] = simplexSolve(type, B, D, c_B, c_D, h, x_B, ...
|
||||
x_D, index_B, index_D, itMax)
|
||||
% Solving a maximization problem with the simplex method
|
||||
|
||||
% Initialize the number of iterations
|
||||
nIter = 0;
|
||||
|
||||
% Compute B^{-1}*D and B^{-1}*h
|
||||
BiD = B\D;
|
||||
Bih = B\h;
|
||||
|
||||
% Compute the reduced cost coefficients
|
||||
r_D = c_D - (c_B * BiD);
|
||||
|
||||
% the optimality condition is satisfied if all
|
||||
% reduced cost coefficients are positive or negative (depending on the
|
||||
% problem)
|
||||
tol = max(size(r_D));
|
||||
|
||||
% Check the optimality condition, in order to skip the loop if the
|
||||
% solution is already optimal
|
||||
optCheck = typeCond(type, sum(r_D <= 0), sum(r_D >= 0));
|
||||
|
||||
while optCheck ~= tol
|
||||
% Find the index of the entering variable
|
||||
idxIN = find(r_D == typeCond(type, max(r_D), min(r_D)), 1, 'first');
|
||||
|
||||
in = D(:,idxIN);
|
||||
c_in = c_D(1,idxIN);
|
||||
index_in = index_D(1,idxIN);
|
||||
|
||||
% Evaluate the coefficients ratio for the column corresponding to
|
||||
% the entering variable
|
||||
ratio = Bih / BiD;
|
||||
|
||||
% Find the smallest positive ratio
|
||||
idxOUT = find(ratio == min(ratio(ratio > 0)), 1, 'first');
|
||||
|
||||
out = B(:,idxOUT);
|
||||
c_out = c_B(1,idxOUT);
|
||||
index_out = index_B(1,idxOUT);
|
||||
|
||||
% TODO: Update the matrices by exchanging the columns
|
||||
B(:,idxOUT) = in;
|
||||
D(:,idxIN) = out;
|
||||
c_B(1,idxOUT) = c_in;
|
||||
c_D(1,idxIN) = c_out;
|
||||
index_B(1,idxOUT) = index_in;
|
||||
index_D(1,idxIN) = index_out;
|
||||
|
||||
% Compute B^{-1}*D and B^{-1}*h
|
||||
BiD = B\D;
|
||||
Bih = B\h;
|
||||
|
||||
% Compute the reduced cost coefficients
|
||||
r_D = c_D - (c_B * BiD);
|
||||
|
||||
% Check the optimality condition, in order to exit the loop if the
|
||||
% solution is already optimal
|
||||
optCheck = typeCond(type, sum(r_D <= 0), sum(r_D >= 0));
|
||||
|
||||
% Detect inefficient looping if nIter > total number of basic solutions
|
||||
nIter = nIter + 1;
|
||||
if nIter > itMax
|
||||
error('Incorrect loop, more iterations than the number of basic solutions');
|
||||
end
|
||||
|
||||
% Compute the new x_B
|
||||
x_B = Bih - BiD * x_D;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function x = typeCond(type, ifMaximum, ifMinimum)
|
||||
if strcmp(type, 'max')
|
||||
x = ifMaximum;
|
||||
elseif strcmp(type, 'min')
|
||||
x = ifMinimum;
|
||||
else
|
||||
error('Incorrect type specified. Choose either a maximisation (max) or minimisation (min) problem.')
|
||||
end
|
||||
end
|
56
mp6/Project_6_Maggioni_Claudio/standardize.m
Normal file
56
mp6/Project_6_Maggioni_Claudio/standardize.m
Normal file
|
@ -0,0 +1,56 @@
|
|||
function [A_aug,h,c_aug] = standardize(type,A,h,c,m,sign)
|
||||
% Input arguments:
|
||||
% type = 'max' for maximization, 'min' for minimization
|
||||
% A = matrix holding the constraints coefficients
|
||||
% h = coefficients of the constraints inequalities (rhs vector)
|
||||
% c = coefficients of the objective function
|
||||
% m = size(A, 1)
|
||||
% sign = vector holding information about the constraints if the system
|
||||
% needs to be standardized (-1: less or equal, 0: equal, 1:vgreater or equal)
|
||||
% return arguments are:
|
||||
% (1) A_aug = augmented matrix A, containing also the surplus and slack variables
|
||||
% (2) c_aug = augmented coefficients vector c (check compatibility of dimensions with A)
|
||||
% (3) h, right hand side vector (remember to flip the signs when changing the inequalities)
|
||||
|
||||
aug_matrix = eye(m); % matrix corresponding to the slack/surplus variables
|
||||
|
||||
if strcmp(type, 'max')
|
||||
for i = 1:m
|
||||
if sign(i) == 1
|
||||
% Using a surplus instead of a slack variable
|
||||
aug_matrix(i,:) = -aug_matrix(i,:);
|
||||
end
|
||||
end
|
||||
elseif strcmp(type, 'min')
|
||||
for i = 1:m
|
||||
if sign(i) == -1
|
||||
% Using a slack instead of a surplus variable
|
||||
aug_matrix(i,:) = -aug_matrix(i,:);
|
||||
end
|
||||
end
|
||||
else
|
||||
error('Incorrect type specified. Choose either a maximisation (max) or minimisation (min) problem.')
|
||||
end
|
||||
|
||||
% Correction on the sign of h for auxiliary problem (we want to
|
||||
% ensure that h>=0, but we need to flip all the signs)
|
||||
for i = 1:m
|
||||
if h(i) < 0
|
||||
A(i,:) = -A(i,:);
|
||||
h(i,:) = -h(i,:);
|
||||
aug_matrix(i,:) = -aug_matrix(i,:);
|
||||
end
|
||||
end
|
||||
|
||||
c_aug = [c, zeros(1,m)];
|
||||
if strcmp(type,'max')
|
||||
% Extend matrix A by adding the slack variables
|
||||
A_aug = [A (+1 * full(aug_matrix == +1))];
|
||||
elseif strcmp(type,'min')
|
||||
% Extend matrix A by adding the surplus variables
|
||||
A_aug = [A (-1 * full(aug_matrix == -1))];
|
||||
else
|
||||
error('Incorrect type specified. Choose either a maximisation (max) or minimisation (min) problem.')
|
||||
end
|
||||
|
||||
end
|
115
mp6/Project_6_Maggioni_Claudio/testSimplex.m
Normal file
115
mp6/Project_6_Maggioni_Claudio/testSimplex.m
Normal file
|
@ -0,0 +1,115 @@
|
|||
% This script has the purpose of testing your implementation of the Simplex Method. In case there should be any issue with your solution, you will
|
||||
% get an error and the code will stop. If your proposed solution passes all the tests, you can happily move to the next part of the assignment.
|
||||
|
||||
close all;
|
||||
clear;
|
||||
clc;
|
||||
|
||||
runTests = [0 0 1 0 0 0];
|
||||
|
||||
%% Test 1 (example page 86)
|
||||
if runTests(1)
|
||||
type = 'max';
|
||||
A = [2 1; 1 3; 1 1];
|
||||
h = [360;480;220];
|
||||
c = [14,18];
|
||||
sign = [0,0,0];
|
||||
|
||||
[z,x_B,index_B] = simplex (type,A,h,c,sign);
|
||||
|
||||
if(z==3600 && sum(uint32(x_B)-uint32([90;130;50]))==0 && sum(index_B==[1,2,3])==3)
|
||||
fprintf('Test 1 has given the expected results. Proceding with the next one.\n\n');
|
||||
else
|
||||
error('Test 1 has given a wrong result. Interrupting the testing phase.');
|
||||
end
|
||||
pause
|
||||
end
|
||||
|
||||
%% Test 2 (example page 114)
|
||||
if runTests(2)
|
||||
type = 'min';
|
||||
A = [2,1,3; 3,4,2];
|
||||
h = [210;360];
|
||||
c = [9,11,8];
|
||||
sign = [0,0];
|
||||
|
||||
[z,x_B,index_B] = simplex (type,A,h,c,sign);
|
||||
|
||||
if(z==1062 && sum(uint32(x_B)-uint32([96;18]))==0 && sum(index_B==[1,2])==2)
|
||||
fprintf('Test 2 has given the expected results. Proceding with the next one.\n\n');
|
||||
else
|
||||
error('Test 2 has given a wrong result. Interrupting the testing phase.');
|
||||
end
|
||||
pause
|
||||
end
|
||||
|
||||
%% Test 3 (example page 126)
|
||||
if runTests(3)
|
||||
type = 'max';
|
||||
A = [1,2,1; 2,1,0; 1,-2,-1];
|
||||
h = [5;8;1];
|
||||
c = [3,-2,5];
|
||||
sign = [0,1,1];
|
||||
|
||||
[z,x_B,index_B] = simplex (type,A,h,c,sign);
|
||||
|
||||
if(z==17 && sum(uint32(x_B)-uint32([4;1;2]))==0 && sum(index_B==[1,3,6])==3)
|
||||
fprintf('Test 3 has given the expected results. Proceding with the next one.\n\n');
|
||||
else
|
||||
error('Test 3 has given a wrong result. Interrupting the testing phase.');
|
||||
end
|
||||
pause
|
||||
end
|
||||
|
||||
%% Test 4 (TED)
|
||||
if runTests(4)
|
||||
type = 'min';
|
||||
A = [-10,-5,-8; -10,-2,-10; -5,-5,-4; 10,5,8; 10,2,10; 5,5,4];
|
||||
h = [-110,-60,-90,78,43,70]';
|
||||
c = [20000,30000,25000];
|
||||
sign = [0,0,0,0,0,0];
|
||||
|
||||
[z,x_B,index_B] = simplex (type,A,h,c,sign);
|
||||
|
||||
if(z==380000 && sum(uint32(x_B)-uint32([4;10;20;20;12;17]))==0 && sum(index_B==[1,2,4,6,7,8])==6)
|
||||
fprintf('Test 4 has given the expected results. Proceding with the next one.\n\n');
|
||||
else
|
||||
error('Test 4 has given a wrong result. Interrupting the testing phase.');
|
||||
end
|
||||
pause
|
||||
end
|
||||
|
||||
%% Test 5 (LP Exercise 2)
|
||||
if runTests(5)
|
||||
type = 'max';
|
||||
A = [1,1,2; 5,3,1; 2,1,0];
|
||||
h = [1200;2000;600];
|
||||
c = [5,20,28];
|
||||
sign = [0,0,0];
|
||||
|
||||
[z,x_B,index_B] = simplex (type,A,h,c,sign);
|
||||
|
||||
if(z==20160 && sum(uint32(x_B)-uint32([560;320;40]))==0 && sum(index_B==[2,3,6])==3)
|
||||
fprintf('Test 5 has given the expected results. Proceding with the next one.\n\n');
|
||||
else
|
||||
error('Test 5 has given a wrong result. Interrupting the testing phase.');
|
||||
end
|
||||
pause
|
||||
end
|
||||
|
||||
%% Test 6 (Vinitaly page 171)
|
||||
if runTests(6)
|
||||
type = 'max';
|
||||
A = [1,1,1,0,0,0,0,0,0; 0,0,0,1,1,1,0,0,0; 0,0,0,0,0,0,1,1,1; 0,1,0,0,1,0,0,1,0; 0,0,1,0,0,1,0,0,1];
|
||||
h = [4800;4000;2300;2500;4200];
|
||||
c = [2,3,4,4,7,12,4,9,13];
|
||||
sign = [0,0,0,0,0];
|
||||
|
||||
[z,x_B,index_B] = simplex (type,A,h,c,sign);
|
||||
|
||||
if(z==79500 && sum(uint32(x_B)-uint32([4400;400;4000;2100;200]))==0 && sum(index_B==[1,2,6,8,9])==5)
|
||||
fprintf('Test 6 has given the expected results. Congratulations!!!\n\n');
|
||||
else
|
||||
error('Test 6 has given a wrong result. Interrupting the testing phase.');
|
||||
end
|
||||
end
|
96
mp6/assignment.sty
Normal file
96
mp6/assignment.sty
Normal file
|
@ -0,0 +1,96 @@
|
|||
\usepackage{ifthen}
|
||||
\usepackage[utf8]{inputenc}
|
||||
\usepackage{graphics}
|
||||
\usepackage{graphicx}
|
||||
\usepackage{hyperref}
|
||||
\usepackage{amsmath}
|
||||
|
||||
\pagestyle{plain}
|
||||
\voffset -5mm
|
||||
\oddsidemargin 0mm
|
||||
\evensidemargin -11mm
|
||||
\marginparwidth 2cm
|
||||
\marginparsep 0pt
|
||||
\topmargin 0mm
|
||||
\headheight 0pt
|
||||
\headsep 0pt
|
||||
\topskip 0pt
|
||||
\textheight 255mm
|
||||
\textwidth 165mm
|
||||
|
||||
\newcommand{\duedate} {}
|
||||
\newcommand{\setduedate}[1]{%
|
||||
\renewcommand\duedate {Due date:~ #1}}
|
||||
\newcommand\isassignment {false}
|
||||
\newcommand{\setassignment}{\renewcommand\isassignment {true}}
|
||||
\newcommand{\ifassignment}[1]{\ifthenelse{\boolean{\isassignment}}{#1}{}}
|
||||
\newcommand{\ifnotassignment}[1]{\ifthenelse{\boolean{\isassignment}}{}{#1}}
|
||||
|
||||
\newcommand{\assignmentpolicy}{
|
||||
\begin{table}[h]
|
||||
\begin{center}
|
||||
\scalebox{0.8} {%
|
||||
\begin{tabular}{|p{0.02cm}p{16cm}|}
|
||||
\hline
|
||||
&\\
|
||||
\multicolumn{2}{|c|}{\Large\textbf{Numerical Computing 2020 --- Submission Instructions}}\\
|
||||
\multicolumn{2}{|c|}{\large\textbf{(Please, notice that following instructions are mandatory: }}\\
|
||||
\multicolumn{2}{|c|}{\large\textbf{submissions that don't comply with, won't be considered)}}\\
|
||||
&\\
|
||||
\textbullet & Assignments must be submitted to \href{https://www.icorsi.ch/course/view.php?id=10018}{iCorsi} (i.e. in electronic format).\\
|
||||
\textbullet & Provide both executable package and sources (e.g. C/C++ files, Matlab).
|
||||
If you are using libraries, please add them in the file. Sources must be organized in directories called:\\
|
||||
\multicolumn{2}{|c|}{\textit{Project\_number\_lastname\_firstname}}\\
|
||||
& and the file must be called:\\
|
||||
\multicolumn{2}{|c|}{\textit{project\_number\_lastname\_firstname.zip}}\\
|
||||
\multicolumn{2}{|c|}{\textit{project\_number\_lastname\_firstname.pdf}}\\
|
||||
\textbullet & The TAs will grade your project by reviewing your project write-up, and looking at the implementation
|
||||
you attempted, and benchmarking your code's performance.\\
|
||||
|
||||
\textbullet & You are allowed to discuss all questions with anyone you like; however: (i) your submission must list anyone you discussed problems with and (ii) you must write up your submission independently.\\
|
||||
\hline
|
||||
\end{tabular}
|
||||
}
|
||||
\end{center}
|
||||
\end{table}
|
||||
}
|
||||
\newcommand{\punkte}[1]{\hspace{1ex}\emph{\mdseries\hfill(#1~\ifcase#1{Points}\or{Points}\else{Points}\fi)}}
|
||||
|
||||
|
||||
\newcommand\serieheader[6]{
|
||||
\thispagestyle{empty}%
|
||||
\begin{flushleft}
|
||||
\includegraphics[width=0.4\textwidth]{usi_inf}
|
||||
\end{flushleft}
|
||||
\noindent%
|
||||
{\large\ignorespaces{\textbf{#1}}\hspace{\fill}\ignorespaces{ \textbf{#2}}}\\ \\%
|
||||
{\large\ignorespaces #3 \hspace{\fill}\ignorespaces #4}\\
|
||||
\noindent%
|
||||
\bigskip
|
||||
\hrule\par\bigskip\noindent%
|
||||
\bigskip {\ignorespaces {\Large{\textbf{#5}}}
|
||||
\hspace{\fill}\ignorespaces \large \ifthenelse{\boolean{\isassignment}}{\duedate}{#6}}
|
||||
\hrule\par\bigskip\noindent% \linebreak
|
||||
}
|
||||
|
||||
\makeatletter
|
||||
\def\enumerateMod{\ifnum \@enumdepth >3 \@toodeep\else
|
||||
\advance\@enumdepth \@ne
|
||||
\edef\@enumctr{enum\romannumeral\the\@enumdepth}\list
|
||||
{\csname label\@enumctr\endcsname}{\usecounter
|
||||
{\@enumctr}%%%? the following differs from "enumerate"
|
||||
\topsep0pt%
|
||||
\partopsep0pt%
|
||||
\itemsep0pt%
|
||||
\def\makelabel##1{\hss\llap{##1}}}\fi}
|
||||
\let\endenumerateMod =\endlist
|
||||
\makeatother
|
||||
|
||||
|
||||
|
||||
|
||||
\usepackage{textcomp}
|
||||
|
||||
|
||||
|
||||
|
BIN
mp6/project_6_Maggioni_Claudio.pdf
Normal file
BIN
mp6/project_6_Maggioni_Claudio.pdf
Normal file
Binary file not shown.
261
mp6/project_6_Maggioni_Claudio.tex
Normal file
261
mp6/project_6_Maggioni_Claudio.tex
Normal file
|
@ -0,0 +1,261 @@
|
|||
% vim: set ts=2 sw=2 et tw=80:
|
||||
|
||||
\documentclass[unicode,11pt,a4paper,oneside,numbers=endperiod,openany]{scrartcl}
|
||||
\usepackage{pgfplots}
|
||||
\pgfplotsset{compat=1.17}
|
||||
\usepackage{mathrsfs}
|
||||
\usetikzlibrary{arrows}
|
||||
\input{assignment.sty}
|
||||
\definecolor{zzttqq}{rgb}{0.6,0.2,0}
|
||||
\definecolor{uuuuuu}{rgb}{0.26666666666666666,0.26666666666666666,0.26666666666666666}
|
||||
\definecolor{ccqqqq}{rgb}{0.8,0,0}
|
||||
\definecolor{qqqqff}{rgb}{0,0,1}
|
||||
\begin{document}
|
||||
\setassignment
|
||||
\setduedate{Friday, December 18, 2020, 11:59 PM}
|
||||
|
||||
\serieheader{Numerical Computing}{2020}{Student: Claudio Maggioni}{Discussed
|
||||
with: --}{Solution for Project 6}{}
|
||||
\newline
|
||||
|
||||
\assignmentpolicy
|
||||
|
||||
The purpose of this mini-project is to implement the Simplex Method to find the
|
||||
solution of linear programs, involving both the minimisation and the maximisation
|
||||
of the objective function.
|
||||
|
||||
\section{Graphical Solution of Linear Programming Problems [25 points]}
|
||||
Please consider the following two problems:
|
||||
\begin{enumerate}
|
||||
\item[(1)] \begin{equation*}
|
||||
\begin{aligned}
|
||||
& \text{min} & z=4x&+y\\
|
||||
& \text{ s.t.} & x+2y &\leq 40 \\
|
||||
& & x+y &\geq 30\\
|
||||
& & 2x+3y &\geq 72 \\
|
||||
& & x,\,y &\geq0 \\
|
||||
\end{aligned}
|
||||
\end{equation*}
|
||||
\item[(2)] A tailor plans to sell two types of trousers, with production costs
|
||||
of 25 CHF and 40 CHF, respectively. The former type can be sold for 85 CHF,
|
||||
while the latter for 110 CHF. The tailor estimates a total monthly demand
|
||||
of 265 trousers. Find the number of units of each type of trousers that
|
||||
should be produced in order to maximise the net profit of the tailor, if we
|
||||
assume that the he cannot spend more than 7000 CHF in raw materials.
|
||||
\end{enumerate}
|
||||
Start by writing problem (2) as a linear programming problem. Then complete the
|
||||
following tasks:
|
||||
\begin{itemize}
|
||||
\item Solve the system of inequalities.
|
||||
\item Plot the feasible region identified by the constraints.
|
||||
\item Find the optimal solution and the value of the objective function in
|
||||
that point.
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Solving problem 1}
|
||||
|
||||
We first solve the system of inequalities for y:
|
||||
|
||||
\[\begin{cases}
|
||||
y \leq 20 - \frac{1}{2} x \\
|
||||
y \geq 30 - x \\
|
||||
y \geq 24 - \frac{2}{3} x \\
|
||||
x, y \geq 0 \\
|
||||
\end{cases}
|
||||
\]
|
||||
|
||||
Here is the feasability region plot, which was derived geometrically by plotting
|
||||
the inequalities above.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
|
||||
\begin{axis}[
|
||||
x=0.3cm,y=0.3cm,
|
||||
xmin=-1, ymin=-1, xmax=40,ymax=32,
|
||||
axis lines=middle,
|
||||
ymajorgrids=true,xmajorgrids=true]
|
||||
\draw [line width=2pt,color=qqqqff,domain=-11.510887860744262:56.37536226673365] plot(\x,{(--20-0.5*\x)/1});
|
||||
\draw [line width=2pt,color=ccqqqq,domain=-11.510887860744262:56.37536226673365] plot(\x,{(--30-1*\x)/1});
|
||||
\draw [line width=2pt,color=ccqqqq,domain=-11.510887860744262:56.37536226673365] plot(\x,{(--24-0.6666666666666666*\x)/1});
|
||||
\draw [line width=2pt,domain=-11.510887860744262:56.37536226673365] plot(\x,{(-0-0*\x)/1});
|
||||
\draw [line width=2pt] (0,-15.596475658682538) -- (0,33.531731670413244);
|
||||
\draw [line width=2pt,color=zzttqq] (24,8)-- (36,0);
|
||||
\draw [line width=2pt,color=zzttqq] (36,0)-- (40,0);
|
||||
\draw [line width=2pt,color=zzttqq] (40,0)-- (24,8);
|
||||
\fill[line width=2pt,color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612] (24,8) -- (36,0) -- (40,0) -- cycle;
|
||||
\end{axis}
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
Red lines represent feasibility constraints keeping the upper part of the graph,
|
||||
while blue lines keep the low part. The resulting region is the area in brown.
|
||||
|
||||
We therefore identify the verticies $(24,8)$, $(36,0)$ and $(40,0)$ as candidate
|
||||
solutions, and therefore we evaluate the minimization function in these points:
|
||||
|
||||
\[ m(x, y) = 4x + y \]
|
||||
\[\begin{aligned}
|
||||
m(24,8) = 104 \\
|
||||
m(36,0) = 144 \\
|
||||
m(40,0) = 160 \\
|
||||
\end{aligned}\]
|
||||
|
||||
And therefore we have as a solution $(x, y) = (40, 0)$, with $z = 160$.
|
||||
|
||||
\subsection{Problem 2 as linear programming problem}
|
||||
The following is the linear programming formulation of problem 2:
|
||||
|
||||
\begin{equation*}
|
||||
\begin{aligned}
|
||||
\text{max} &\; 85x + 110y \\
|
||||
\text{s.t.} & \begin{cases}
|
||||
25 x + 40 y \leq 7000 \\
|
||||
x + y \leq 265 \\
|
||||
x, y \geq 0 \\
|
||||
\end{cases}
|
||||
\end{aligned}
|
||||
\end{equation*}
|
||||
|
||||
\subsection{Solving problem 2}
|
||||
|
||||
We first solve the system of inequalities for y:
|
||||
|
||||
\[
|
||||
\begin{cases}
|
||||
25 x + 40 y \leq 7000 \\
|
||||
x + y \leq 265 \\
|
||||
x, y \geq 0 \\
|
||||
\end{cases}
|
||||
=
|
||||
\begin{cases}
|
||||
y < 280 - x \\
|
||||
x \leq 265 - x \\
|
||||
x, y \geq 0
|
||||
\end{cases}
|
||||
\]
|
||||
|
||||
From this solution it is clear that the region of satisfactory $x$s and $y$s is
|
||||
bounded by $x = 0$, $y = 0$, and $y = 265 - x$.
|
||||
|
||||
Here is a plot of the feasability region:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
|
||||
\begin{axis}[
|
||||
x=0.03cm,y=0.03cm,
|
||||
axis lines=middle,
|
||||
xmin=-10,ymin=-10,xmax=275,ymax=275]
|
||||
\fill[line width=2pt,color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612]
|
||||
(0,265) -- (0,0) -- (265,0) -- cycle;
|
||||
\draw [line width=2pt,color=zzttqq] (0,265)-- (0,0);
|
||||
\draw [line width=2pt,color=zzttqq] (0,0)-- (265,0);
|
||||
\draw [line width=2pt,color=zzttqq] (265,0)-- (0,265);
|
||||
\end{axis}
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
We then proceed to evaluate the maximization function at all verticies of the
|
||||
feasability region, i.e. $\{ (0, 0), (0, 265), (265, 0) \}$.
|
||||
|
||||
\[ m(x, y) = 85x + 110y \]
|
||||
\[\begin{aligned}
|
||||
m(0,0) = 0 \\
|
||||
m(0,265) = 29150 \\
|
||||
m(265,0) = 22525 \\
|
||||
\end{aligned}\]
|
||||
|
||||
We then conclude the tailor should produce 265 trousers of the second type, for
|
||||
a revenue of 29,150.-- Fr.
|
||||
|
||||
\section{Implementation of the Simplex Method [35 points]}
|
||||
|
||||
In this first part of the assignment, you are required to complete 2 functions
|
||||
which are part of a dummy implementation of the simplex method. Specifically you
|
||||
have to complete the TODOs in:
|
||||
\begin{itemize}
|
||||
\item \emph{standardise.m}, which writes a maximisation or minimisation input
|
||||
problem in standard form;
|
||||
\item \emph{simplexSolve.m}, which solves a maximisation or minimisation
|
||||
problem using the simplex method.
|
||||
\end{itemize}
|
||||
|
||||
You are given also some already-implemented functions to help you in your task:
|
||||
\emph{simplex.m} is a wrapper which calls all the functions necessary to find a
|
||||
solution to the linear program; \emph{auxiliary.m} solves the auxiliary problem
|
||||
to find a feasible starting basic solution of the linear program;
|
||||
\emph{printSol.m} is a function which prints the optimal solution found by the
|
||||
simplex algorithm. Finally, \emph{testSimplex.m} presents a series of 6
|
||||
problems to check if your implementation is correct, before moving to the next
|
||||
part of the assignment. Additional details to aid you in your implementation
|
||||
can be found in the comments inside the code.
|
||||
|
||||
\section{Applications to Real-Life Example: Cargo Aircraft [25 points]}
|
||||
|
||||
In this second part of the assignment, you are required to use the simplex
|
||||
method implementation to solve a real-life problem taken from economics
|
||||
(constrained profit maximisation).
|
||||
|
||||
A cargo aircraft has 4 compartments (indicated simply as $S_1,\dots,S_4$) used
|
||||
to store the goods to be transported. Details about the weight capacity and
|
||||
storage capacity of the different compartments can be inferred from the data
|
||||
reported in the following table:
|
||||
|
||||
\begin{center}
|
||||
\begin{tabular}{||c | c | c ||}
|
||||
\hline
|
||||
Compartment & Weight Capacity ($t$) & Storage Capacity ($m^3$) \\ [0.5ex]
|
||||
\hline\hline
|
||||
$S_1$ & 18 & 11930\\
|
||||
\hline
|
||||
$S_2$ & 32 & 22552\\
|
||||
\hline
|
||||
$S_3$ & 25 & 11209\\
|
||||
\hline
|
||||
$S_4$ & 17 & 5870\\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
|
||||
The following four cargos are available for shipment during the next flight:
|
||||
|
||||
\begin{center}
|
||||
\begin{tabular}{|| c | c | c | c ||}
|
||||
\hline
|
||||
Cargo & Weight ($t$) & Volume ($m^3/t$) & Profit ($\text{CHF}/t$) \\ [0.5ex]
|
||||
\hline\hline
|
||||
$C_1$ & 16 & 320 & 135 \\
|
||||
\hline
|
||||
$C_2$ & 32 & 510 & 200 \\
|
||||
\hline
|
||||
$C_3$ & 40 & 630 & 410 \\
|
||||
\hline
|
||||
$C_4$ & 28 & 125 & 520 \\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
|
||||
Any proportion of the four cargos can be accepted, and the profit obtained for each cargo is increased by $10\%$ if it is put in $S_2$, by $20\%$ if it is put in $S_3$ and by $30\%$ if it is put in $S_4$, due to the better storage conditions. The objective of this problem is to determine which amount of the different cargos will be transported and how to allocate it among the different compartments, while maximising the profit of the owner of the cargo plane. Specifically you have to:
|
||||
\begin{enumerate}
|
||||
\item Formulate the problem above as a linear program: what is the objective function? What are the constraints? Write down all equations, with comments explaining what you are doing.
|
||||
\item Create a script \emph{exercise2.m} which uses the simplex method implemented in the previous exercise to solve the problem. What is the optimal solution? Visualise it graphically and briefly comment the results obtained (are you surprised of this outcome on the basis of your data?).
|
||||
\end{enumerate}
|
||||
|
||||
|
||||
\section{Cycling and Degeneracy [15 points]}
|
||||
|
||||
Consider now the following simple problem:
|
||||
\begin{alignat*}{2}
|
||||
&\text{max}\;\, && z = 3x_1+4x_2,\\
|
||||
&\text{s.t.} && 4x_1+3x_2\leq 12\\
|
||||
& && 4x_1+x_2\leq 8\\
|
||||
& && 4x_1+2x_2\leq 8\\
|
||||
& && x_1, x_2 \geq 0.
|
||||
\end{alignat*}
|
||||
|
||||
\begin{enumerate}
|
||||
\item Create a script \emph{exercise3.m} which uses the simplex method implemented above to solve this problem. Do you achieve convergence within the maximum number of iterations (given by the maximum number of possible basic solutions)? Do you notice any strange behaviour? (\emph{hint:} check, e.g., the indices of the entering and departing variables)
|
||||
\item Look at the number of constraints and at the number of unknowns: what can you notice about the underlying system of equations? Represent them graphically and try to use this information to explain the behaviour of your solver in the previous point.
|
||||
\end{enumerate}
|
||||
|
||||
|
||||
\end{document}
|
BIN
mp6/usi_inf.pdf
Normal file
BIN
mp6/usi_inf.pdf
Normal file
Binary file not shown.
Reference in a new issue