Linear programming is a method used to optimize a linear objective function, subject to constraints represented by linear equations or inequalities. Here's an example of a linear programming problem that can be solved using R and Python:
Example Problem 01
A company produces two products, C and D, using two resources, X and Y. The company wants to determine the production levels of C and D that will minimize the cost, subject to the following constraints:
The availability of resource X is limited to 50 units.
The availability of resource Y is limited to 30 units.
The production of C requires 2 units of X and 1 unit of Y per unit produced.
The production of D requires 3 units of X and 2 units of Y per unit produced.
The cost of producing one unit of C is $3 and the cost of producing one unit of D is $5.
Objective function: Minimize Z = 3C + 5D
Constraints:
2C + 3D <= 50 (availability of resource X)
C + 2D <= 30 (availability of resource Y)
C >= 0, D >= 0 (non-negativity)
Example Problem 02
A company produces two products, A and B, using two resources, X and Y. The company wants to determine the production levels of A and B that will maximize profits, subject to the following constraints:
The availability of resource X is limited to 100 units.
The availability of resource Y is limited to 80 units.
The production of A requires 2 units of X and 1 unit of Y per unit produced.
The production of B requires 3 units of X and 2 units of Y per unit produced.
The profit from selling one unit of A is $5 and the profit from selling one unit of B is $8.
Objective function: Maximize Z = 5A + 8B
Constraints:
2A + 3B <= 100 (availability of resource X)
A + 2B <= 80 (availability of resource Y)
A >= 0, B >= 0 (non-negativity)
The solution in R:
library(lpSolve)
# Define the objective function
obj_fun <- c(5, 8)
# Define the constraint matrix
con_mat <- rbind(c(2, 3), c(1, 2))
# Define the right-hand side of the constraints
rhs <- c(100, 80)
# Define the direction of the constraints (less than or equal to)
dir <- c("<=", "<=")
# Solve the linear programming problem
solution <- lp("max", obj_fun, con_mat, dir, rhs)
# Print the solution
solution$solution
Solution in Python:
from scipy.optimize import linprog
# Define the objective function
obj_fun = [-5, -8]
# Define the constraint matrix
con_mat = [[2, 3], [1, 2]]
# Define the right-hand side of the constraints
rhs = [100, 80]
# Define the direction of the constraints (less than or equal to)
dir = ['<=', '<=']
# Solve the linear programming problem
solution = linprog(c=obj_fun, A_ub=con_mat, b_ub=rhs, bounds=(0, None), method='simplex',
options={"disp": True})
# Print the solution
print(solution)
Both R and Python will give the solution of the LP problem and it will be like:
A = 20, B = 30
The company should produce 20 units of product A and 30 units of product B to maximize profits.
Please keep in mind that the above code is an example and should not be used for solving real-world problems without proper validation and testing.
Here are some
examples of linear programming problems that can be solved using Python and the
libraries scipy.optimize, cvxpy, PuLP, or Gurobi:
Example 03
Maximize 3x +
4y Subject to
2x + y <= 6
x + 2y <= 4
x >= 0 y >= 0
PYTHON CODES
from
scipy.optimize import linprog
c = [-3, -4]
A = [[2, 1],
[1, 2]]
b = [6, 4]
x0_bounds = (0,
None)
x1_bounds = (0,
None)
res =
linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='simplex')
print(res)
Example 04
Minimize x + y
Subject to
2x + y >= 1
x - y <= 2
x >= 0 y >= 0
PYTHON CODES
import cvxpy as
cp
x =
cp.Variable()
y =
cp.Variable()
constraints =
[2*x + y >= 1, x - y <= 2, x >= 0, y >= 0]
obj =
cp.Minimize(x + y)
prob =
cp.Problem(obj, constraints)
prob.solve()
print("Optimal
Value: ", prob.value)
print("Optimal
Variable Values: x=", x.value, "y=", y.value)
Example 05
Maximize 2x +
3y
Subject to
x + y <= 4
2x + y <= 6
x >= 0
y >= 0
PYTHON CODES
from pulp
import *
x =
LpVariable('x', 0)
y =
LpVariable('y', 0)
prob =
LpProblem('problem', LpMaximize)
prob += 2*x +
3*y
prob += x + y
<= 4
prob += 2*x + y
<= 6
status =
prob.solve()
print(LpStatus[status])
print(value(x),
value(y), value(prob.objective))
All the above examples are just for demonstration purposes and will vary depending on the specific problem you are trying to solve. These libraries are powerful and flexible, and can be used to solve a wide range of linear programming problems.
Example 06
![]() |
R CODES FOR LP |
In this case, the farmer should plant 40 acres of crop A and 60 acres of crop B to maximize the total profit.
No comments:
Post a Comment