Skip to content

Instantly share code, notes, and snippets.

View samirsaci's full-sized avatar

Samir Saci samirsaci

View GitHub Profile
@samirsaci
samirsaci / optimal.py
Created April 15, 2021 13:14
Optimal
# Display the result if Optimal solution is found
if status == cp_model.OPTIMAL:
# Create one list of assigned tasks per machine.
assigned_jobs = collections.defaultdict(list)
for job_id, job in enumerate(jobs_data):
for task_id, task in enumerate(job):
machine = task[0]
assigned_jobs[machine].append(
assigned_task_type(start=solver.Value(
all_tasks[job_id, task_id].start),
@samirsaci
samirsaci / init_model.py
Last active April 15, 2021 13:52
Init model
import collections
# Import Python wrapper for or-tools CP-SAT solver.
from ortools.sat.python import cp_model
# Create the model.
model = cp_model.CpModel()
# Machine id
dict_machine = {0:'Machine 1', 1:'Machine 2', 2:'Machine 3'}
@samirsaci
samirsaci / pivot.py
Last active April 26, 2021 10:44
Pivot Table Sales
import pandas as pd
import numpy as np
# Import Dataframe
df = pd.read_csv('sales_data.csv', sep=";")
print("{:,} transactions in your raw data".format(len(df)))
# Format Date
print("Start Processing ...")
df['date'] = pd.to_datetime(df['date'])
@samirsaci
samirsaci / start_model.py
Last active April 23, 2021 10:02
Initiliaze Container Loading Problem with parameters
import pandas as pd
import numpy as np
from rectpack import newPacker
import rectpack.packer as packer
import matplotlib.pyplot as plt
# Initialize Model Parameters
#-- Pallet Dimensions: 80 x 120 cm
bx = 5 # Buffer x
# Function Solver
def solver(n_812, n_1012, bins):
# Pallets to load
rectangles = [pal_812 for i in range(n_812)] + [pal_1012 for i in range(n_1012)]
# Build the Packer
pack = newPacker()
# Add the rectangles to packing queue
for r in rectangles:
@samirsaci
samirsaci / plot_result.py
Created April 23, 2021 14:55
Plot solution rectpack
def plot_solution(all_rects, pal_812, pal_1012):
# Plot
plt.figure(figsize=(10,10))
# Loop all rect
for rect in all_rects:
b, x, y, w, h, rid = rect
x1, x2, x3, x4, x5 = x, x+w, x+w, x, x
y1, y2, y3, y4, y5 = y, y, y+h, y+h,y
# Pallet type
@samirsaci
samirsaci / run.py
Created April 27, 2021 14:59
Accounting Automation
import pandas as pd
# Dictionnary of filenames
list_files = ['DC-JAN-2017.xlsx', 'DC-FEB-2017.xlsx', 'DC-MAR-2017.xlsx','DC-APR-2017.xlsx', 'DC-MAY-2017.xlsx', 'DC-JUN-2017.xlsx',
'DC-JUL-2017.xlsx', 'DC-AUG-2017.xlsx', 'DC-SEP-2017.xlsx','DC-OCT-2017.xlsx', 'DC-NOV-2017.xlsx', 'DC-DEC-2017.xlsx']
zip_loop = zip(list_files, [i[3:6] for i in list_files])
# Final report DataFrame
df_report = pd.DataFrame()
@samirsaci
samirsaci / clean.py
Created April 27, 2021 15:05
Account Clean
# Function to open and clean
def clean(file_raw, month):
# Open the file and start from line 5
df_raw = pd.read_excel(file_raw, header = 5)
df_raw.head()
# Remove First Lines
df_clean = df_raw.copy()
df_clean = df_clean.iloc[4:]
@samirsaci
samirsaci / perform_month.py
Created April 27, 2021 15:09
Account perform
def process_month(df_clean, month):
# Type
df_clean['Type'] = df_clean[['Renting', 'Investment']].apply(
lambda t: 'Rent.' if t['Renting']=='X' else 'Invest.' if t['Investment']=='X' else 'Purch.', axis = 1)
# Quantity
dict_qty = dict(zip(['Rent.', 'Purch.', 'Invest.'], ['Rental Units', 'Purchasing Units', 'Invests. Units']))
df_clean['Qty'] = df_clean.apply(lambda t: t[dict_qty[t['Type']]], axis = 1)
@samirsaci
samirsaci / parameters.py
Created April 30, 2021 14:45
Bakery Optimization Project
from pulp import *
# Optimize your Bakery Model
# Parameters
items = ["A", "B", "C", "D", "E", "F"]
profit = {"A":6, "B":4.4, "C":7.5, "D":0.9, "E":1.2, "F":2.2}
baker = {"A":50, "B":0, "C":45, "D":35, "E":25, "F":0}
oven = {"A":45, "B":0, "C":90, "D":20, "E":45, "F":0}
display = {"A":4, "B":1.5, "C":3, "D":1, "E":1, "F":1}