Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. from datetime import timedelta, datetime
  2. from src.agent import Client
  3. from plotly.offline import plot
  4. from  scipy import stats
  5. import plotly.graph_objs as go
  6. import os
  7.  
  8. class Singleton(object):
  9.     _instance = None
  10.  
  11.     def __new__(class_, *args, **kwargs):
  12.         if not isinstance(class_._instance, class_):
  13.             class_._instance = object.__new__(class_, *args, **kwargs)
  14.         return class_._instance
  15.  
  16. class Environment(Singleton):
  17.     def __init__(self):
  18.  
  19.         self.global_time = 0
  20.         self.week = 0
  21.         self.day = 0
  22.         self.shop_list = []
  23.         self.client_list = []
  24.         self.cumulative_shops = {}
  25.         self.avg_prices = {'auchan': 624.0,
  26.              'magnit': 283.0,
  27.              'pyaterochka': 359.0,
  28.              'lenta': 957.0,
  29.              'dixy': 263.0,
  30.              'okey': 911.0,
  31.              'perekrestok': 376.0,
  32.              'spar': 326.0,
  33.              'semishagoff': 294.0,
  34.              'prisma': 457.0,
  35.              'semya': 227.0,
  36.              'real': 254.0}
  37.  
  38.     def register_shop(self, shop):
  39.         self.shop_list.append(shop)
  40.  
  41.     def register_client(self, client):
  42.         self.client_list.append(client)
  43.  
  44.     def iteration(self, iterat):
  45.         self.iterat = iterat
  46.         self.cumulative_shops[iterat] = {shop.name: [0, 0] for shop in self.shop_list}
  47.         for client in self.client_list:
  48.             client.go_shop(self)
  49.  
  50.         if self.global_time <= 22:
  51.             self.global_time += 1
  52.         else:
  53.             self.global_time = 0
  54.             if self.day <= 5:
  55.                 self.day += 1
  56.             else:
  57.                 self.day = 0
  58.                 self.week += 1
  59.  
  60.     def populate_user(self,user, data, first_week):
  61.         transactions = sorted(data, key=lambda x: datetime.strptime(x['TRANS_DATE'], '%d.%m.%Y'))
  62.         transactions = [x for x in transactions if
  63.                          datetime.strptime(x['TRANS_DATE'], '%d.%m.%Y') <= (first_week)]
  64.  
  65.         if len(transactions) > 0:
  66.             c = Client(self)
  67.             res = True
  68.             c.initial(['name'], [user])
  69.  
  70.             for trans in transactions:
  71.                 point = {'date': datetime.strptime(trans['TRANS_DATE'] + ' ' + trans['TRANS_TIME'], '%d.%m.%Y %H:%M:%S'),
  72.                          'h': int(datetime.strptime(trans['TRANS_TIME'], '%H:%M:%S').hour), #+(float(datetime.strptime(trans['TRANS_TIME'], '%H:%M:%S').minute) / 60),
  73.                          'w': trans['AMOUNT_EQ'],
  74.                          'shop': trans['shop']}
  75.                 c.identify(point)
  76.                 if point['date'].hour == 0 and point['date'].minute == 0:
  77.                     res = False
  78.  
  79.             c.identify_shop_probs()
  80.             return res
  81.  
  82.     def populate(self, user_data, first_week):
  83.         self.first_week = first_week
  84.        
  85.         bad = set()
  86.         for user in user_data:
  87.             flag = self.populate_user(user,user_data[user],self.first_week)
  88.             if not flag:
  89.                 bad.add(user)
  90.         print('Users initialised:', len(self.client_list))
  91.         print('Users without transactions for selected period:', len(user_data) - len(self.client_list))
  92.         #print('List of users with bad time:', bad)
  93.  
  94.     def plot_3d(self):
  95.         x, y, z, text = [], [], [], []
  96.         for client in self.client_list:
  97.             x.append(client.day_begin)
  98.             y.append(client.lunch)
  99.             z.append(client.day_end)
  100.             text.append(client.name)
  101.         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