Last active
December 25, 2019 07:26
-
-
Save syedmisbah/f207435ee140986234fcc319f16bbdd4 to your computer and use it in GitHub Desktop.
Embed code for Intro to Optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Import ortools solver | |
from ortools.linear_solver import pywraplp | |
#Creating a solver object | |
solver_lin = pywraplp.Solver('Swiggy Food Solver',pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) | |
#Step 1 - Create Decision variables | |
# x[0] for number of biryanis | |
# x[1] for number of Bowls | |
# x[2] for number of Wraps | |
#0 and 1000 define the upper and lower bound for the values x[0],x[1] and x[2] can take | |
x = [solver_lin.NumVar(0, 1000,'x[%i]' % i) for i in range(3)] | |
#Step 2 - Add constraints | |
solver_lin.Add(2*x[0] + x[1] + x[2] <= 1500) | |
solver_lin.Add(x[0] + 3*x[1] + 2*x[2] <= 3000) | |
solver_lin.Add(x[0] + 2*x[1] + 3*x[2] <= 4000) | |
#Step 3 - Define objective function | |
#Total population variable | |
max_items_total = solver_lin.NumVar(0,3000,'max_items_total') | |
#Since x[0], x[1] and x[2] already have constraints on them(1000 max), | |
# whatever upperbound we define on max_items_total doesn't matter - it's just syntactical sugar | |
solver_lin.Add(max_items_total == x[0] + x[1] + x[2]) | |
#Step 4 - Set objevtive function and solve | |
solver_lin.Maximize(max_items_total) | |
solver_lin.Solve() #Should return 0 if run successfully | |
#Print results | |
print("Total number of items we can cook is", round(max_items_total.SolutionValue()), "\n") | |
print("No of Biryanis we can cook is", round(x[0].solution_value())) | |
print("No of Bowls we can cook is", round(x[1].solution_value())) | |
print("No of wraps we can cook is", round(x[2].solution_value())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment