Page Nav

HIDE

Grid

GRID_STYLE

Pages

Linear Programming

 Introduction To optimize the solution of a problem linear programming comes in very handy. It can give you the maximum optimal solution u...

 Introduction

To optimize the solution of a problem linear programming comes in very handy. It can give you the maximum optimal solution under some given constraints.  Now, let’s say you want to minimize a cost function i.e., c₁x₁ + c₂x₂ + … + cx. Here, x is a variable, and c_ is similar to co-efficient. In linear programming a constraint for sum of a variable can be shown as follows:

(ax + ax + + ax b) or (ax + ax + + ax = b).

Here, x_ is a variable with a1_co-efficient and b is resources.

This report is a part of Artificial Intelligence course Lab-work under supervision of Nuruzzaman Faruqui, Lecturer of City University, Bangladesh. This course offers students various up-to-date AI topics. Students get to explore the real applicable approaches through AI. From this course, the student acquires better knowledge of the functionality of AI and how AI is making our daily life easier. This is the best Artificial Intelligence course in Bangladesh

Problem statement

Consider the following example:

  1. Two machines, X₁ and X₂.
  2. X₁ costs $50/hour to run, X₂ costs $80/hour to run. The goal is to minimize cost.
  3. This can be formalized as a cost function: 50x₁ + 80x₂.
  4. X₁ requires 5 units of labor per hour.
  5. X₂ requires 2 units of labor per hour.
  6. Total of 20 units of labor to spend.
  7. This can be formalized as a constraint: 5x + 2x 20.
  8. X₁ produces 10 units of output per hour.
  9. X₂ produces 12 units of output per hour.
  10. The company needs 90 units of output.
  11. This is another constraint.
  12. Literally, it can be rewritten as 10x + 12x 90.
  13. However, constraints need to be of the form (ax + ax + + ax b) or (ax + ax + + ax = b).

Therefore, we multiply by (-1) to get to an equivalent equation of the desired form: (-10x) + (-12x) -90

Let’s code it on python:

import scipy.optimize


# Objective Function: 50x_1 + 80x_2
# Constraint 1: 5x_1 + 2x_2 <= 20
# Constraint 2: -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

After executing the above code we get the following output:

 

Conclusion

An optimizing algorithm for linear programming requires background knowledge in geometry and linear algebra that we don’t want to assume. Instead, we can use algorithms that already exist, such as Simplex and Interior-Point

Computer Science is all about mathematics. To get a better grasp on linear programming you should definitely have a good working knowledge in geometry and linear algebra. But for the sake of understanding, we have used already existing methods in the code such as Simplex and Interior-Point.

No comments