This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (80 loc) · 3.54 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'''
Main file. To read and preprocess data, simulate scenarios for order demand,
and formulate and solve the Glover Assingment problem. Please see README.pdf for more details.
Author: David Torres Sanchez
Email: [email protected]
'''
import pandas as pd
from math import exp
from datetime import datetime, timedelta
class expand(object):
pass
class run(object):
"""docstring for orders"""
def __init__(self, scenarios=1):
self.scenarios = scenarios
self.orders = expand()
self.delivery_times = expand()
self.last_slot = expand()
self.results = expand()
self._build()
def _build(self):
self._load_data()
self._preprocess()
self._fit_distrs()
self._build_model()
def _load_data(self):
print "Loading data"
self.orders.data = pd.read_csv('./input/orders.csv')
self.delivery_times.data = pd.read_csv(
'./input/estimated_delivery_times.csv')
self.last_slot.data = pd.read_csv('./input/last_slot_booked.csv')
def _preprocess(self):
from classes import Slot # import slot object
self.orders.slots = [] # initialise list of slot objects
self.orders.slots = [
Slot(i, # id
datetime.strptime( # start time in datetime format
self.orders.data['time_frame'][i], "%Y-%m-%d %H:%M:%S"),
0, # duration (provisional)
exp(self.orders.data['orders_log'][i]), # demand
0) # leave probability (provisional)
for i in range(0, 287)] # len(self.orders.data)
# Fill probabilites and delivery times using other data sets
for slot in self.orders.slots:
for entry in self.delivery_times.data.iterrows():
# if the delivery day and hour matches get delivery time
if entry[1].day == slot.start_t.weekday() and\
entry[1].hour == slot.start_t.hour:
slot.end_t = slot.start_t + \
timedelta(
seconds=entry[1].delivery_time) # update end time
# update leaving probability
slot.leave = \
self.last_slot.data['last_slot_booked'][slot.start_t.hour]
def _fit_distrs(self):
from classes import Slot
from fit import sample_pymc
self.results.fits = {}
for s in range(0, self.scenarios):
print "Generating data for scenario %r" % (s)
self.results.fits[s] = {}
# Initialiase lis of empty Slot objects, otherwise updates won't work!
self.results.fits[s]['slots'] = [
Slot(0, 0, 0, 0, 0) for i in range(0, len(self.orders.slots))]
demand_samples, leave_samples, scenario_prob = \
sample_pymc(s, self.orders.slots)
self.results.fits[s]['glovers'] = int(max(demand_samples))
self.results.fits[s]['prob'] = scenario_prob
count = 0
# update demand and leave values for each slot
for slot in self.results.fits[s]['slots']:
slot.i_d = self.orders.slots[count].i_d
slot.start_t = self.orders.slots[count].start_t
slot.end_t = self.orders.slots[count].end_t
slot.demand = demand_samples[count]
slot.leave = leave_samples[count]
count += 1
def _build_model(self):
from formulation import generate
generate(self.results.fits)