Skip to content

Instantly share code, notes, and snippets.

View samirsaci's full-sized avatar

Samir Saci samirsaci

View GitHub Profile
@samirsaci
samirsaci / final_results.py
Last active April 12, 2022 14:27
Results Bakery
# 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)))
@samirsaci
samirsaci / constraints.py
Last active May 6, 2021 07:29
Bakery Optimization
# 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
@samirsaci
samirsaci / distance_matrix.csv
Created May 6, 2021 17:15
Last Mile Delivery
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
@samirsaci
samirsaci / initialize.py
Last active May 6, 2021 17:44
Last Mile Delivery
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()
@samirsaci
samirsaci / functions.py
Created May 6, 2021 17:46
Last Mile Delivery
# 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):
@samirsaci
samirsaci / mode.py
Created May 6, 2021 17:48
Last Mile Delivery
# 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)
@samirsaci
samirsaci / solution.py
Created May 6, 2021 17:50
Last Mile Delivery
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)
@samirsaci
samirsaci / nutritions_facts.csv
Last active May 8, 2021 14:12
Optimize Diet
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
@samirsaci
samirsaci / costs.csv
Created May 8, 2021 14:14
Optimize Diet
Ingredients Costs
Chicken 0.095
Beef 0.15
Mutton 0.10
Rice 0.002
Wheat bran 0.005
Corn 0.012
Peanuts 0.013
@samirsaci
samirsaci / parameters.py
Last active May 8, 2021 16:35
Optimize Diet
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