This file contains hidden or 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
| # Names | |
| dict_name = dict(zip(["A", "B", "C", "D", "E", "F"], | |
| ["Lemon Cake", "Sandwich", "Chocolate Cake", "Croissant", "Chocolate Eclair", "Panini"])) | |
| # Solve Model | |
| model.solve() | |
| for v in model.variables(): | |
| print(dict_name[v.name], "=", int(v.varValue)) | |
| print("Profit reached: {:,} euros".format(sum(var_dict[i].varValue * profit[i] for i in items))) |
This file contains hidden or 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
| # Define Objective Function | |
| model += lpSum([profit[i] * var_dict[i] for i in items]) | |
| # 4. Define Constraints | |
| # Bakers | |
| model += lpSum([var_dict[i]*baker[i] for i in items]) <= 24*60 | |
| # Oven | |
| model += lpSum([var_dict[i]*oven[i] for i in items]) <= 48*60 | |
| # Assistant | |
| model += lpSum([var_dict[i]*assist[i] for i in items]) <= 4*60 |
This file contains hidden or 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
| col0 | col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 | col10 | col11 | col12 | col13 | col14 | col15 | col16 | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| row0 | 0 | 548 | 776 | 696 | 582 | 274 | 502 | 194 | 308 | 194 | 536 | 502 | 388 | 354 | 468 | 776 | 662 | |
| row1 | 548 | 0 | 684 | 308 | 194 | 502 | 730 | 354 | 696 | 742 | 1084 | 594 | 480 | 674 | 1016 | 868 | 1210 | |
| row2 | 776 | 684 | 0 | 992 | 878 | 502 | 274 | 810 | 468 | 742 | 400 | 1278 | 1164 | 1130 | 788 | 1552 | 754 | |
| row3 | 696 | 308 | 992 | 0 | 114 | 650 | 878 | 502 | 844 | 890 | 1232 | 514 | 628 | 822 | 1164 | 560 | 1358 | |
| row4 | 582 | 194 | 878 | 114 | 0 | 536 | 764 | 388 | 730 | 776 | 1118 | 400 | 514 | 708 | 1050 | 674 | 1244 | |
| row5 | 274 | 502 | 502 | 650 | 536 | 0 | 228 | 308 | 194 | 240 | 582 | 776 | 662 | 628 | 514 | 1050 | 708 | |
| row6 | 502 | 730 | 274 | 878 | 764 | 228 | 0 | 536 | 194 | 468 | 354 | 1004 | 890 | 856 | 514 | 1278 | 480 | |
| row7 | 194 | 354 | 810 | 502 | 388 | 308 | 536 | 0 | 342 | 388 | 730 | 468 | 354 | 320 | 662 | 742 | 856 | |
| row8 | 308 | 696 | 468 | 844 | 730 | 194 | 194 | 342 | 0 | 274 | 388 | 810 | 696 | 662 | 320 | 1084 | 514 |
This file contains hidden or 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
| from ortools.constraint_solver import routing_enums_pb2 | |
| from ortools.constraint_solver import pywrapcp | |
| import pandas as pd | |
| import numpy as np | |
| # Import Distance Matrix | |
| df_distance = pd.read_excel('df_distance_matrix.xlsx', index_col=0) | |
| # Transform to Numpy Array | |
| distance_matrix = df_distance.to_numpy() |
This file contains hidden or 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
| # Calculate Distance between two nodes | |
| def distance_callback(from_index, to_index): | |
| """Returns the distance between the two nodes.""" | |
| # Convert from routing variable Index to distance matrix NodeIndex. | |
| from_node = manager.IndexToNode(from_index) | |
| to_node = manager.IndexToNode(to_index) | |
| return data['distance_matrix'][from_node][to_node] | |
| # Get the order quantity of each node (location) | |
| def demand_callback(from_index): |
This file contains hidden or 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
| # Create the routing index manager. | |
| manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']), | |
| data['num_vehicles'], data['depot']) | |
| # Create Routing Model | |
| routing = pywrapcp.RoutingModel(manager) | |
| # Create and register a transit callback. | |
| transit_callback_index = routing.RegisterTransitCallback(distance_callback) |
This file contains hidden or 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
| if solution: | |
| total_distance = 0 | |
| total_load = 0 | |
| for vehicle_id in range(data['num_vehicles']): | |
| index = routing.Start(vehicle_id) | |
| plan_output = 'Route for driver {}:\n'.format(vehicle_id) | |
| route_distance = 0 | |
| route_load = 0 | |
| while not routing.IsEnd(index): | |
| node_index = manager.IndexToNode(index) |
This file contains hidden or 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
| Ingredients | Protein | Fat | Fibre | Salt | Sugar | |
|---|---|---|---|---|---|---|
| Chicken | 0.1000 | 0.0800 | 0.0010 | 0.0020 | 0.0000 | |
| Beef | 0.2000 | 0.1000 | 0.0050 | 0.0050 | 0.0000 | |
| Mutton | 0.1500 | 0.1100 | 0.0030 | 0.0070 | 0.0000 | |
| Rice | 0.0000 | 0.0100 | 0.1000 | 0.0020 | 0.0000 | |
| Wheat bran | 0.0400 | 0.0100 | 0.1500 | 0.0080 | 0.0000 | |
| Corn | 0.0329 | 0.0128 | 0.0280 | 0.0000 | 0.045 | |
| Peanuts | 0.2580 | 0.4920 | 0.0850 | 0.0010 | 0.047 |
This file contains hidden or 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
| Ingredients | Costs | |
|---|---|---|
| Chicken | 0.095 | |
| Beef | 0.15 | |
| Mutton | 0.10 | |
| Rice | 0.002 | |
| Wheat bran | 0.005 | |
| Corn | 0.012 | |
| Peanuts | 0.013 |
This file contains hidden or 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 pandas as pd | |
| from pulp import * | |
| # Import Nutrition Facts | |
| nutrition = pd.read_excel('Nutrition Facts.xlsx', index_col = 0) | |
| # Import Costs | |
| costs = pd.read_excel('Costs.xlsx') | |
| dict_costs = dict(zip(costs['Ingredients'], costs['Costs'])) | |
| # Variables |