Example

A company has two products with two demand functions (a list of products could also be added). Each product has a set of hours it takes to create per liter (liters cold be kilos, tons, etc.). Product 1 takes 30 hours of work for each liter. Product 2 takes 20 hours of work for each liter. The company has a maximum of 2700 hours of work for each period (periods can be seen as: annually, monthly, weekly, daily, etc.). The company has machine hours to consider. This could be seen as the rental rate (\(r\)). There are 850 machine hours that can be use. A number of other constraints could be added.

For product 1 each one liter yield produces 20 million in profit. For product 2 each liter yields 60 million in profit for each liter. The company already has an out front demand of 95 liters for both goods. Find the maximization point where good 1 and good 2 must be produced to maximize total profits.

Product 1 can be seen as \(x_1\) and product 2 as \(x_2\). In this case the Objective function is equal to \(max_\pi = 20x_1 + 60x_2\); where we are trying to maximize profits given the constraints of labor hours and rental rate. The 20 and 60 came from how much each good cost to consumers per liter. These number could also been seen as: how much it cost to produce the goods, how much of a gain we get from each of the goods, or how much utility we get from each of the goods produced.

Constraints
There are three main constraints in the problem above. The first one working hours (WH). Each good takes a certain hours of work to produce. For good 1 it takes 30 and for good 2 it takes 20. The hours of work has a constrain of less or equal to 2700 hours.
1. The linear constraint function is WH: \(30x_1 + 20x_2 \leq 2700\).
2. Constraint number two is the machine hours (MH). the function for is \(5x_1 + 10x_2 \leq 850\).
3. PM = \(x_1 + x_2 \geq 95\). This constraint is how much the company has to produce because of the out front demand. This could be seen as expected items sold.
4. \(x_1 \geq 0, x_2 \geq 0\). This last part is just saying that \(x_1\) & \(x_2\) cannot be 0.

How the model would look like
\(max_\pi = 20x_1 + 60x_2\). The Objective function is to maximize profit.
st.
WH: \(30x_1 + 20x_2 \leq 2700\). Constraint 1. Number of working hours.
MH: \(5x_1 + 10x_2 \leq 850\). Constraint 2. Number of machine hours.
PM: \(x_1 + x_2 \geq 95\). Constraint 3. How many good have to be produce.

Here is the r code.

library(lpSolve)
library(lpSolveAPI)
# First defind parameters.
objc.fun <- c(20, 60) # The objecive function could be a list of values.
constr = matrix(c(30, 20, 5, 10, 1, 1), ncol = 2, byrow = T,) # This is a matrix of the constraints WH, MH, PM. There would be a hole list of constraints as well. 
constr.dir = c("<=", "<=", ">=") # this has to equal the order of the matrix. This are the signs of the costraint. 
rhs = c(2700, 850, 95) # This is everything on the right hand side of the equal sign.
# Basically we are transforming the function given, set equal to 0 and find the minima or maxima of the function. 
# Solving for max
sol = lp(direction = "max", # You are stating what you are doing. "max", "min", etc. 
         objective.in = objc.fun, # This is where the objective function goes.
         const.mat = constr, #this is where you add the number of constraints as a metrix form. 
         const.dir = constr.dir, # this is where you add direction of the constraint. 
         const.rhs = rhs, # This is where you add everything on the right hand side of the inequality. 
         compute.sens = T)

To show results

This how much the company should produce to maximize profits. 20 of good \(x_1\) and 75 of good \(x_2\).

sol$solution
## [1] 20 75

Cobb-Douglas Utility

A tipical microeconomic problem is to find the optimal level of consumptions of two goods. Set to some restrictions like prices, budget constraint, and preferences.

The process involves creating: 1. the budger line.
2. Drawing the indifference curve.
3. Finding wich indiference curve is tangent line where it meets the budget line.

Example
There is a budget line of $45 that must be spend.
Pizza cost 3 dollars and is \(x\).
Soda cost 1.50 and is \(y\).
The utility function is:
\(u(x,y) = x^{2}0.25y\)

library(Ryacas) # This is to be able to run complex calculus and algebra problems.
## 
## Attaching package: 'Ryacas'
## The following objects are masked from 'package:base':
## 
##     %*%, diag, diag<-, lower.tri, upper.tri
library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.2
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()   masks stats::filter()
## ✖ dplyr::lag()      masks stats::lag()
## ✖ purrr::simplify() masks Ryacas::simplify()
# Variables
x = sym("x")
y = sym("y")
U = sym("U")

Creating a budget line.

This would be the budget line \(y = -2b + 30\).

Any point on this line is the budget constraint.

Create & Plot the Utility indifference curve \(u(x,y) = x^{2}0.25y\)

library(Ryacas)
utility.u = function(x, y, U) x^2*(0.25 * y)
# Test with adding values for x and y. 5 and 5
utility.u(5,5) # This wold tell you his number of utils at each combination of pizza and soda. 
## [1] 31.25
# We then cahnge the equation to solve for y
utility.solved = function(x,y,U) U / (0.25 * x^2)
eval(utility.solved, list(x=5, U = 10))
## function(x,y,U) U / (0.25 * x^2)

Function
\(U = x^{\alpha}y^{1-\alpha}\). \({\alpha}\) is a parameter given. K and L are unkown.
The Budget constraint is: \(x + py = w\). \(w\) is the income.