(*Exercise 1*)
(*Phase I*)
A={{1/2,1/4,1,0,0,0,4},{1,3,0,-1,1,0,36},{1,1,0,0,0,1,10}};
targetAux={0,0,0,0,-1,-1,0};

tab1=tableau[A,targetAux,{3,5,6}]
thetas[tab1,1]

tab2=tableau[A,targetAux,{1,5,6}]
thetas[tab2,2]

tab3=tableau[A,targetAux,{3,5,2}]

(*End of Phase I. Optimal solution to aux LP found; original LP is not feasible.*)

(*Since there are only 2 variables, we may plot the constraints to check their intersection is empty*)
constr1=RegionPlot[1/2x1+1/4x2≤4,{x1,-30,30},{x2,-30,30},PlotStyle->{Red,Opacity[.5]},BoundaryStyle->Red];
constr2=RegionPlot[x1+3x2≥36,{x1,-30,30},{x2,-30,30},PlotStyle->{Green,Opacity[.5]},BoundaryStyle->Green];
constr3=Plot[10-x1,{x1,-30,30},PlotStyle->{Blue,Thick}];

Show[constr1,constr2,constr3]
Show[constr1,constr2,constr3,PlotRange->{{0,30},{0,30}}] (*In the first quadrant x1≥0, x2≥0 
*)

(*Exercise 2*)
(*Phase I*)
A={{5,-1,-1,0,0,1,0,0,5},{2,1,0,-1,0,0,1,0,3},{-1/2,1,0,0,-1,0,0,1,4}};
targetAux={0,0,0,0,0,-1,-1,-1,0};

tab1=tableau[A,targetAux,{6,7,8}]
thetas[tab1,1]

tab2=tableau[A,targetAux,{1,7,8}]
thetas[tab2,2]

tab3=tableau[A,targetAux,{1,2,8}]
thetas[tab3,4]

tab4=tableau[A,targetAux,{1,2,4}]

(*End of Phase I. Optimal solution to aux LP found; original LP is feasible. No basic artificial variables.*)

(*Phase II*)
(*Remove columns of artificial variables and replace target function*)
A=tab4[[1]][[2;;4,Append[Range[2,6],10]]];
target=-{1,1,0,0,0,0};

tab1=tableau[A,target,{1,2,4}]
thetas[tab1,3]

(*LP is unbounded. Minimum is -7 but maximum is +Infinity. *)

Minimize[x1+x2,5x1-x2≥5&&2x1+x2≥3&&1/2x1-x2≤-4,{x1,x2}]
Maximize[x1+x2,5x1-x2≥5&&2x1+x2≥3&&1/2x1-x2≤-4,{x1,x2}]

(*Since there are only 2 variables, we may plot the constraints to check their intersection is unbounded*)

constr1=RegionPlot[5x1-x2≥5,{x1,-30,30},{x2,-30,30},PlotStyle->{Red,Opacity[.5]},BoundaryStyle->Red];
constr2=RegionPlot[2x1+x2≥3,{x1,-30,30},{x2,-30,30},PlotStyle->{Green,Opacity[.5]},BoundaryStyle->Green];
constr3=RegionPlot[1/2x1-x2≤-4,{x1,-30,30},{x2,-30,30},PlotStyle->{Blue,Opacity[.5]},BoundaryStyle->Blue];

Show[constr1,constr2,constr3]
Show[constr1,constr2,constr3,PlotRange->{{0,30},{0,30}}]

(*Feasible region is clearly unbounded, and target function x1+x2 assumes arbitrarily large values.*)

(*Exercise 3*)
(*Phase I*)
A={{1,-1,0,0,2,0,1,0,0,0},{-2,1,0,0,-2,0,0,1,0,0},{1,0,1,0,1,-1,0,0,1,3},{0,1,1,1,2,1,0,0,0,4}};
targetAux={0,0,0,0,0,0,-1,-1,-1,0};

(*x7,x8,x9 are artificial variables*)

tab1=tableau[A,targetAux,{7,8,9,4}]
thetas[tab1,3]

tab2=tableau[A,targetAux,{7,8,3,4}]

(*End of Phase I. Terminates with two basic artificial variables: x7 and x8 *)

(*To begin Phase II, drop nonbasic artificial variables (x9) and variables with negative entry in the target row (x1) *)

A=tab2[[1]][[2;;5,{3,4,5,6,7,8,9,11}]];
target={-10,0,0,-7,-14,0,0,0};

(*Phase II*)
(*WARNING: since x1 was removd, variables have shifted in index;
in what follows, old x1 is gone, old x2 is now x1, old x3 is now x2 etc. *)
tab1=tableau[A,target,{2,3,6,7}]
thetas[tab1,1]

tab2=tableau[A,target,{2,3,6,1}]
thetas[tab2,4]

tab3=tableau[A,target,{2,4,6,1}]

(*End of Phase II, optimal solution has been found.*)

Maximize[40x1+10x2+7x5+14x6,x1-x2+2x5==0&&-2x1+x2-2x5==0&&x1+x3+x5-x6==3&&x2+x3+x4+2x5+x6==4&&x1≥0&&x2≥0&&x3≥0&&x4≥0&&x5≥0&&x6≥0,{x1,x2,x3,x4,x5,x6}]