Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import timedelta, datetime
- from src.agent import Client
- from plotly.offline import plot
- from scipy import stats
- import plotly.graph_objs as go
- import os
- class Singleton(object):
- _instance = None
- def __new__(class_, *args, **kwargs):
- if not isinstance(class_._instance, class_):
- class_._instance = object.__new__(class_, *args, **kwargs)
- return class_._instance
- class Environment(Singleton):
- def __init__(self):
- self.global_time = 0
- self.week = 0
- self.day = 0
- self.shop_list = []
- self.client_list = []
- self.cumulative_shops = {}
- self.avg_prices = {'auchan': 624.0,
- 'magnit': 283.0,
- 'pyaterochka': 359.0,
- 'lenta': 957.0,
- 'dixy': 263.0,
- 'okey': 911.0,
- 'perekrestok': 376.0,
- 'spar': 326.0,
- 'semishagoff': 294.0,
- 'prisma': 457.0,
- 'semya': 227.0,
- 'real': 254.0}
- def register_shop(self, shop):
- self.shop_list.append(shop)
- def register_client(self, client):
- self.client_list.append(client)
- def iteration(self, iterat):
- self.iterat = iterat
- self.cumulative_shops[iterat] = {shop.name: [0, 0] for shop in self.shop_list}
- for client in self.client_list:
- client.go_shop(self)
- if self.global_time <= 22:
- self.global_time += 1
- else:
- self.global_time = 0
- if self.day <= 5:
- self.day += 1
- else:
- self.day = 0
- self.week += 1
- def populate_user(self,user, data, first_week):
- transactions = sorted(data, key=lambda x: datetime.strptime(x['TRANS_DATE'], '%d.%m.%Y'))
- transactions = [x for x in transactions if
- datetime.strptime(x['TRANS_DATE'], '%d.%m.%Y') <= (first_week)]
- if len(transactions) > 0:
- c = Client(self)
- res = True
- c.initial(['name'], [user])
- for trans in transactions:
- point = {'date': datetime.strptime(trans['TRANS_DATE'] + ' ' + trans['TRANS_TIME'], '%d.%m.%Y %H:%M:%S'),
- 'h': int(datetime.strptime(trans['TRANS_TIME'], '%H:%M:%S').hour), #+(float(datetime.strptime(trans['TRANS_TIME'], '%H:%M:%S').minute) / 60),
- 'w': trans['AMOUNT_EQ'],
- 'shop': trans['shop']}
- c.identify(point)
- if point['date'].hour == 0 and point['date'].minute == 0:
- res = False
- c.identify_shop_probs()
- return res
- def populate(self, user_data, first_week):
- self.first_week = first_week
- bad = set()
- for user in user_data:
- flag = self.populate_user(user,user_data[user],self.first_week)
- if not flag:
- bad.add(user)
- print('Users initialised:', len(self.client_list))
- print('Users without transactions for selected period:', len(user_data) - len(self.client_list))
- #print('List of users with bad time:', bad)
- def plot_3d(self):
- x, y, z, text = [], [], [], []
- for client in self.client_list:
- x.append(client.day_begin)
- y.append(client.lunch)
- z.append(client.day_end)
- text.append(client.name)
- plot([go.Scatter3d(x=x, y=y, z=z, text=text, opacity=0.7, mode='markers')], filename='plots/3d.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement