This repository has been archived on 2021-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
NC/mp6/project_6_Maggioni_Claudio.tex
2020-12-19 23:27:43 +01:00

418 lines
16 KiB
TeX

% 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}
\usepackage{hyperref}
\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: Gianmarco De Vita (Exercise 2)}{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.2cm,y=0.2cm,
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) = (24, 8)$, with $z = 104$, as this
is a minimization problem.
\subsection{Problem 2 as linear programming problem}
The following is the linear programming formulation of problem 2:
\begin{equation*}
\begin{aligned}
\text{max} &\; (85 - 25) x + (110 - 40) y \\
\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 < 175 - \frac{5}{8}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$, $y = 265 - x$, and $y = 175 - \frac58 x$.
Here is a plot of the feasability region:
\begin{center}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
\begin{axis}[
x=0.015cm,y=0.015cm,
axis lines=middle,
xmin=-10,ymin=-10,xmax=300,ymax=300]
\fill[line width=2pt,color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612]
(265,0) -- (240,25) -- (0,175) -- (0,0) -- cycle;
\draw [line width=2pt] (0,175)-- (280,0);
\draw [line width=2pt] (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), (265, 0), (0, 175), (240,25) \}$.
\[ m(x, y) = 60x + 70y \]
\[\begin{aligned}
m(0,0) = 0 \\
m(0,175) = 12250 \\
m(240,25) = 16150 \\
m(265,0) = 15900 \\
\end{aligned}\]
We then conclude the tailor should produce 240 trousers of the first type and
25 trousers of the second type, for
a revenue of 16,150.-- Fr.
\section{Implementation of the Simplex Method [35 points]}
The implementation of the simplex method can be found in the
\texttt{Project\_6\_Claudio\_Maggioni/} folder.
\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.
\subsection{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.}
From the problem description we can determine that this problem has 16 variables
$x_{i,j}$, where $1 \leq i,j \leq 4 \; i,j \in N$. Each variable represents a
weight in tonnes, with $i$ determining the type of cargo used and $j$ the
compartment used. Therefore the variables of this problem to optimize represent
how much cargo to put where and in which quantity.
From this initial discussion we can easily derive the objective function to
maximize (as the objective function will output revenue, and we want to be
greedy as most Swiss people):
\[\text{max}\; z =
\text{grandsum}\left[\left(\begin{bmatrix}135\\200\\410\\520\end{bmatrix}\cdot\begin{bmatrix}1&1.1&1.2&1.3
\end{bmatrix}\right) \odot \begin{bmatrix}
x_{1,1}&x_{1,2}&x_{1,3}&x_{1,4}\\
x_{2,1}&x_{2,2}&x_{2,3}&x_{2,4}\\
x_{3,1}&x_{3,2}&x_{3,3}&x_{3,4}\\
x_{4,1}&x_{4,2}&x_{4,3}&x_{4,4}\\\end{bmatrix}\right]\]
where $\odot$ is an element-wise or Hadamard product and $\text{grandsum}(A)$
simply sums all elements of $A$. The column vector represents the value per
ton of each good while the row vector represents the revenue multipliers
associated with each cargo hold.
We can move to cargo hold constraints on weight, which check if the cargo holds are
overloaded:
\[\begin{aligned}
\sum_{i = 1}^4 x_{i,1} \leq 18 & \hspace{1cm} & \sum_{i = 1}^4 x_{i,2} \leq 32
& \hspace{1cm} &
\sum_{i = 1}^4 x_{i,3} \leq 25 & \hspace{1cm} & \sum_{i = 1}^4 x_{i,4} \leq 17
\end{aligned}\]
Then we consider compartment constraints on space, which check if the cargos
will fit:
\[\begin{aligned}
320 x_{1,1} + 510 x_{2,1} + 630 x_{3,1} + 125 x_{4,1} \leq &\; 11930 \\
320 x_{1,2} + 510 x_{2,2} + 630 x_{3,2} + 125 x_{4,2} \leq &\; 22552 \\
320 x_{1,3} + 510 x_{2,3} + 630 x_{3,3} + 125 x_{4,3} \leq &\; 11209 \\
320 x_{1,4} + 510 x_{2,4} + 630 x_{3,4} + 125 x_{4,4} \leq &\; 5870 \\
\end{aligned}\]
Then we have a final set of constraints to check if we are loading more cargo
than we own:
\[\begin{aligned}
\sum_{i=1}^4 x_{1,i} \leq 16 & \hspace{1cm} &
\sum_{i=1}^4 x_{1,i} \leq 32 & \hspace{1cm} &
\sum_{i=1}^4 x_{1,i} \leq 40 & \hspace{1cm} &
\sum_{i=1}^4 x_{1,i} \leq 28
\end{aligned}\]
Therefore, we have a final minimization problem in standard form with 16
variables and 12 feasability constraints.
\subsection{Create a script \emph{exercise3.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?)}
The file that solves this problem using our MATLAB's implementation of the
simplex method can be found in the \emph{exercise3.m} file.
The optimal solution according to the simplex method is the following:
\[\begin{bmatrix}
x_{1,1}&x_{1,2}&x_{1,3}&x_{1,4}\\
x_{2,1}&x_{2,2}&x_{2,3}&x_{2,4}\\
x_{3,1}&x_{3,2}&x_{3,3}&x_{3,4}\\
x_{4,1}&x_{4,2}&x_{4,3}&x_{4,4}\\\end{bmatrix} =
\begin{bmatrix}0&0&0&0\\18&6&0&0\\0&26&14&0\\0&0&11&17\end{bmatrix}\]
The revenue for this load is of 41890.- Fr.
Figure~\ref{fig:hist} shows a graphical representation of this solution.
The solution seems quite intuitive: the simplex method seems to have found quite
a greedy solution, placing the most valuable cargo in the ``best'' cargo hold,
proceeding with less valuable cargos and ``worse'' cargo holds once either the
filling constraints of the hold are reached or we run out of our most valuable
cargo left.
It is notable that weight limits are reached before space limits, but in
hindsight it is a quite likely scenario for a cargo plane. This problem has
quite realistic data, as the total weight limit (82 tonnes) matches almost
exactly the designed load capacity of a cargo variant Boeing 747
(81.6 tonnes\footnote{Source: Wikipedia at
\url{https://en.wikipedia.org/wiki/Boeing_747\#Background}}).
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[ybar interval,
ymax=40,
ymin=0,
minor y tick num = 3,
xticklabels={{$S_1$},{$S_2$},{$S_3$},{$S_4$}}]
\addplot coordinates { (1,0) (2,0) (3,0) (4,0) (5,0) };
\addplot coordinates { (1,18) (2,6) (3,0) (4,0) (5,0) };
\addplot coordinates { (1,0) (2,26) (3,14) (4,0) (5,0) };
\addplot coordinates { (1,0) (2,0) (3,11) (4,17) (5,0) };
\legend {$C_1$, $C_2$, $C_3$, $C_4$}
\end{axis}
\end{tikzpicture}
\caption{Solution to the Cargo Airplane as an histogram of tonnes loaded per
cargo hold per cargo type. Y-axis is the amount tonnes loaded, X-axis is the cargo hold
where the cargo should be loaded, bar color is the cargo type loaded.}\label{fig:hist}
\end{figure}
\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*}
\subsection{Create a script \emph{exercise4.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)}
The MATLAB implementation for this problem can be found under file
\emph{exercise4.m} in the resources folder (i.e.
\texttt{Project\_6\_Claudio\_Maggioni/}).
The simplex method implementation for this problem fails with an error in the
auxiliary problem initialization. The error message is ``The original LP problem does not admit a feasible
solution''. This error message appears when the simplex method does not
converge in the expected maximum number of iterations. The large number of
iterations (793 before forced termination) is caused by an ``oscillation'' or
vicious cycle of swap operations between matrix $B$ and matrix $D$. This
oscillation is cause by the fact that entering and departing variable indexes do
not change, and stay fixed at values 6 and 4 from the second iteration onwards.
This causes an unnecessary continuous oscillation between two solutions that are
equally not optimal ($z = 28$) according to the auxiliary problem definition,
terminating the auxiliary algorithm abnormally after too many iterations.
\subsection{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.}
In figure~\ref{fig:strange} lies a graphical representation of this linear
programming problem.
The anomaly in the feasability region of this problem is that its perimeter has
triangular shape, and it is made up only of one constraint other than the
trivial positivity constraints at the axes (i.e. the constraints different from
$4x +2y \leq 8$ do not make a difference in the feasability region).
\begin{figure}[h]
\centering
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
\begin{axis}[
x=1cm,y=1cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-0.5,
ymin=-0.5,
xmax=6,
ymax=6]
\clip(-4.052471790978185,-1.8483084480511438) rectangle (6.897018211059206,6.075664579739015);
\fill[line width=2pt,color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612] (0,4) -- (0,0) -- (2,0) -- cycle;
\draw [line width=2pt,domain=0:6] plot(\x,{(-12--4*\x)/-3});
\draw [line width=2pt,domain=0:6] plot(\x,{(-8--4*\x)/-2});
\draw [line width=2pt,domain=0:6] plot(\x,{(--8-4*\x)/1});
\draw [line width=2pt] (0,-1.8483084480511438) -- (0,6.075664579739015);
\draw [line width=2pt,domain=0:6] plot(\x,{(-0-0*\x)/1});
\draw [line width=2pt,color=zzttqq] (0,4)-- (0,0);
\draw [line width=2pt,color=zzttqq] (0,0)-- (2,0);
\draw [line width=2pt,color=zzttqq] (2,0)-- (0,4);
\end{axis}
\end{tikzpicture}
\caption{Feasability region plot of maximization problem for exercise 4. Note
the fact that the region perimeter is a triangle.}\label{fig:strange}
\end{figure}
\end{document}