SHARE
TWEET

Untitled

a guest Sep 19th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from  scipy import stats
  2. import numpy as np,  random
  3. from src.utils import  test_exist
  4.  
  5. class Agent:
  6.     def initial(self, attrs, data):
  7.         for attr, val in zip(attrs, data):
  8.             setattr(self, attr, val)
  9.  
  10. class Shop(Agent):
  11.     def __init__(self,env):
  12.         env.register_shop(self)
  13.         self.transactions = {}
  14.  
  15. class Client(Agent):
  16.     def __init__(self, env):
  17.         env.register_client(self)
  18.         self.transactions = {}
  19.         self.env = env
  20.         # OSTOV
  21.         self.point_c = 0
  22.  
  23.         self.counter_b = 0
  24.         self.counter_e = 0
  25.         self.counter_l = 0
  26.         self.wealth = 0
  27.  
  28.         self.oborot = 0
  29.         self.first_date = 0
  30.         self.last_date = 0
  31.         self.fullness = 0
  32.         self.prob_day = 0
  33.         self.probs_begin = -1
  34.         self.probs_end = -1
  35.  
  36.         self.day_begin = -1
  37.         self.day_b_square = 0
  38.         self.day_end = -1
  39.         self.day_e_square = 0
  40.         self.lunch = -1
  41.  
  42.         self.lunch_b = 12
  43.         self.lunch_e = 16
  44.  
  45.         self.visits = {}
  46.  
  47.         self.check = {}
  48.  
  49.     def identify_day(self, point):
  50.         if self.point_c == 0:
  51.             self.first_date = point['date']
  52.             self.go_probs = {k:0 for k in range(0,24)}
  53.             self.go_probs[point['h']] = 1
  54.  
  55.         else:
  56.             diff = abs((point['date'] - self.first_date).days)
  57.             if diff == 0:
  58.                 self.go_probs[point['h']] = 1
  59.             else:
  60.                 for k, v in self.go_probs.items():
  61.                     if k == point['h']:
  62.                         self.go_probs[k] = (v * diff + 1) / (diff+1)
  63.                     else:
  64.                         self.go_probs[k] = (v * diff) / (diff+1)
  65.     def identify_oborot(self, point):
  66.  
  67.         if self.point_c == 0:
  68.             self.oborot = point['w'] / 24  # Было бы неплохо в будущем смотреть на среднее для такой суммы (нет)
  69.             self.first_date = point['date']
  70.             self.last_date = point['date']
  71.             self.fullness = point['w']
  72.         elif self.point_c == 1:
  73.             delta_h = abs((point['date'] - self.last_date).total_seconds() // 3600)
  74.             orverall_h = 24
  75.             self.oborot = (orverall_h * self.oborot + point['w']) / (orverall_h + delta_h)
  76.             self.last_date = point['date']
  77.             self.fullness = point['w']
  78.         else:
  79.             try:
  80.                 delta_h = abs((point['date'] - self.last_date).total_seconds() // 3600)
  81.                 orverall_h = abs((self.first_date - self.last_date).total_seconds() // 3600)
  82.                 self.oborot = (orverall_h * self.oborot + point['w']) / (orverall_h + delta_h)
  83.                 self.last_date = point['date']
  84.                 self.fullness = point['w']
  85.             except ZeroDivisionError:
  86.                 print(self.name, point['date'], self.last_date,  (self.first_date - self.last_date).total_seconds(),
  87.                       (point['date'] - self.last_date).total_seconds())
  88.  
  89.     def identify_wealth(self, point):
  90.  
  91.  
  92.         if self.point_c == 0:
  93.  
  94.             self.wealth = point['w'] / self.env.avg_prices[point['shop']]
  95.         else:
  96.             self.wealth = (self.wealth * self.point_c + point['w'] / self.env.avg_prices[point['shop']]) / (self.point_c + 1)
  97.  
  98.     def identify_history(self, point):
  99.         try:
  100.             self.visits[point['shop']] += 1
  101.         except KeyError:
  102.             self.visits[point['shop']] = 1
  103.         try:
  104.             self.check[point['shop']] = (self.check[point['shop']] * self.point_c + point['w']) / (self.point_c + 1)
  105.         except KeyError:
  106.             self.check[point['shop']] = point['w']
  107.  
  108.     def identify_shop_probs(self):
  109.         self.shop_names = list(self.visits.keys())
  110.         self.shop_probs = [self.visits[k] / sum(self.visits.values()) for k in self.shop_names]
  111.  
  112.     def identify(self, point):
  113.         if point['shop'] in self.env.avg_prices:
  114.             self.identify_day(point)
  115.             self.identify_wealth(point)
  116.             self.identify_history(point)
  117.             self.identify_oborot(point)
  118.             self.last_fullness = self.fullness
  119.             self.point_c += 1
  120.    
  121.     def go_shop(self, env):
  122.         # deciede to go or not:
  123.         self.fullness -= self.oborot
  124.         if self.fullness <= 0:
  125.             mb = np.random.uniform(0, 1)
  126.             print(self.name)
  127.  
  128.             if mb < self.go_probs[env.global_time]:
  129.                 shop_prob = np.random.uniform(0, 1)
  130.                 shop_map = {shop.name: shop for shop in env.shop_list}
  131.                 it = 0
  132.  
  133.                 while shop_prob > 0:
  134.                     shop_prob -= self.shop_probs[it]
  135.                     it += 1
  136.                 equals = [self.shop_names[a] for a in np.where(np.asarray(self.shop_probs) == self.shop_probs[it-1])[0]]
  137.  
  138.                 if len(equals) > 1 and self.shop_names[it - 1] in equals:
  139.                     f_wealth = lambda x: 1 - 2 ** (-self.wealth*np.mean(np.mean(list(env.avg_prices.values()))/env.avg_prices[x]))
  140.                     diff = [abs(f_wealth(x) - 0.5) for x in equals]
  141.                     go_to = shop_map[equals[diff.index(min(diff))]]
  142.                 else:
  143.                     go_to = shop_map[self.shop_names[it - 1]]
  144.  
  145.                 amount = self.wealth * env.avg_prices[go_to.name]
  146.                 self.fullness += amount * go_to.segment
  147.  
  148.                 # write event
  149.                 go_to.transactions.setdefault(env.week, []).append([self, env.global_time, amount])
  150.                 self.transactions.setdefault(env.week, []).append([go_to, env.iterat, env.global_time, amount])
  151.  
  152.                 env.cumulative_shops[env.iterat][go_to.name][0] += 1
  153.                 env.cumulative_shops[env.iterat][go_to.name][1] += amount
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top