Linear Programming
Introduction:
linear programming is a method of optimizing operations with some constraints. The main objective of linear programming is to maximize or minimize the numerical value. It consists of Linear functions which are subjected to the constraints in the form of linear equations or in the form of inequalities.
Today want to minimize a cost function i.e., c₁x₁ + c₂x₂ + … + cₙxₙ. ere, each x₋ is a variable and it is associated with some cost c. A constraint that’s represented as a sum of variables that is either less than or equal to a value (a₁x₁ + a₂x₂ + … + aₙxₙ<=b) or precisely equal to this value (a₁x₁ + a₂x₂ + … + aₙxₙ=b).In this case, x₋ is a variable, and a₋ is some resource associated with it, and b is how many resources we can dedicate to this problem
This is an Artificial Intelligence course conducted in City Universit by Nuruzzaman Faruqui. This is the best course in Bangladesh. Because Sir explained all the necessary things to us well. The problem we are dealing with in the lab. First, sir gives us a very good idea of the problem by using many examples. After that sir explains about the solutions like how we can solve the problem, how they work. For that, we understand everything very easily.
Problem:
The problem is :
- Two machines, X₁ and X₂.
- X₁ costs $50/hour to run, X₂ costs $80/hour to run. The goal is to minimize cost.
- This can be formalized as a cost function: 50x₁ + 80x₂.
- X₁ requires 5 units of labor per hour.
- X₂ requires 2 units of labor per hour
- Total of 20 units of labor to spend.
- This can be formalized as a constraint: 5x₁ + 2x₂ ≤ 20.
- X₁ produces 10 units of output per hour.
- X₂ produces 12 units of output per hour.
- The company needs 90 units of output.
- This is another constraint.
- Literally, it can be rewritten as 10x₁ + 12x₂ ≥ 90
- However, constraints need to be of the form (a₁x₁ + a₂x₂ + … + aₙxₙ = b)
- Therefore, we multiply by (-1) to get to an equivalent equation of the desired form: (-10x₁) + (-12x₂) ≤ -90.
import scipy.optimize
# Objective Function :50x_1 + 80x_2
# Constraint 1: 5x_1 + 2x_2 <=20
# Constraint 1: -10x_1 + -12x_2 <=-90
result =scipy.optimize.linprog(
[50,80], # Cost function: 50x_1 +80x_2
A_ub=[
[5,2],
[-10,-12]
], # Coefficients for inequalities
b_ub=[20,-90] # Constraints for inequalities: 20 and -90
)
if result.success:
print(f"x1: {round(result.x[0],2)} hours")
print(f"x2: {round(result.x[1], 2)}Hours")
else:
print("No Solution")
Result :
If everything is fine then we will get this type of result.
No comments