---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
 Linear programming
 
# An example
#
# Let U be the monetary unit in use. An industry produces two products, A and B, and
# uses two machines, P and Q. For each unit of A, 1 hour of P and 3 hours of Q are
# required, for each B unit two hours of P and 2 hours of Q are required. A can not be
# produced in more than 18 pieces per week; the P machine can not work for more than 40
# hours a week, while B for no more than 60. We know that each unit of A is sold at
# 16000 U and every B unit at 10000 U. We assume that every quantity produced is sold.
# Determine what is the most convenient production combination.
# Resolution schema:
#        A    B   available hours    x = quantity of A
#    P   1    2        40            y = quantity of B
#    Q   3    2        60            z = objective function
# Mathematical model:
#   when  z = 16000*x + 10000*y  is maximum?
#   constraints  x+2*y <= 40, 3*x+2*y <= 60, 0 <= x <= 18, 0 <= y
#
P = function(x,y) (x+2*y <= 40) & (3*x+2*y <= 60) & (0 <= x) & (x <= 18) & (0 <= y)
HF = 3; BF = 3
Plane(-2,22, -2,22)
Diseq2(P,0, "grey")  # Where P > 0, ie P = 1
                   
# Let's see the limitations:
f1 = function(x,y) x+2*y -40
f2 = function(x,y) 3*x+2*y - 60
f3 = function(x,y) x
f4 = function(x,y) x - 18
f5 = function(x,y) y
CURVE(f1,"brown"); CURVE(f2,"seagreen"); CURVE(f3,"orange")
CURVE(f4,"blue"); CURVE(f5,"magenta")
#
F = function(x,y) 16000*x + 10000*y - M
M=0; CUR(F,"red")
M=2e5; CUR(F,"red")
M=3e5; CUR(F,"red")
# I understand that the solution is the intersection between f2 and f4
 
                   
# f2; 3*x+2*y - 60 = 0 -> y = (60-3*x)/2
f=function(x) (60-3*x)/2; f(18)
# 3      The solution is   x=18, y=3
POINT(18,f(18),"black")
M = 16000*18+10000*3; M
# 318000   maximum
CUR(F,"red"); POINT(18,f(18),"black")
#
# The same problem with the simplex method
# 
#   max z = 16000*x + 10000*y
#    x+2*y <= 40
#  3*x+2*y <= 60
#    x+0*y <= 18
#    x+0*y >= 0
#    0*x+y >= 0

library(boot)     # I load the library containing  "simplex":
# The constraints:
A11 = c(1,2); b11 = 40     # constraints <=
A12 = c(3,2); b12 = 60
A13 = c(1,0); b13 = 18
A21 = c(1,0); b21 = 0      # constraints >=
A22 = c(0,1); b22 = 0
# The problem to be maximized:
a1 = c(16000, 10000)
simplex(A1=rbind(A11,A12,A13), A2=rbind(A21,A22), a=a1,
b1=c(b11,b12,b13), b2=c(b21,b22), maxi=TRUE)
Maximization Problem with Objective Function Coefficients
   x1    x2 
16000 10000 
Optimal solution has the following values
x1 x2
18  3
The optimal value of the objective  function is 318000
# a1: the coefficients of the objective function
# A1: the matrix having the constraints coefficients <= for rows
# b1: the known terms
# A2: the matrix having the constraints coefficients => for rows
# b2: the known terms
# maxi: TRUE/FALSE if it is a max/min problem
# rbind: it is used to build the matrices row by row
#
# For more information type:   ??"linear programming"
#
#
# With WolframAlpha:
# maximize 16000x+10000y in x+2y<=40 && 3x+2y<=60 && 0<=x<=18 && 0<=y
# or:
# maximize[z,{ z=16000x+10000y && x+2y<=40 && 3x+2y<=60 && 0<=x<=18 && 0<=y},{x,y,z}]
 
# I obtain:
 
 
 
# I can represent constraints with:
# plot(x+2y<=40 && 3x+2y<=60 && 0<=x<=18 && 0<=y)
 
                  
# For a minimization problem we use the minimize command.
#
# More about the subject in Wikipedia and in http://www.WolframAlpha.com (write
# linear programming, optimization, convex optimization theory, simplex method).

Other examples of use