Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.02 KB | None | 0 0
  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.         '''
  66.        if point['h'] < self.lunch_b and self.counter_b == 0:
  67.            self.day_begin = point['h'] + 1
  68.            self.counter_b += 1
  69.            return
  70.  
  71.        elif point['h'] > self.lunch_e and self.counter_e == 0:
  72.            self.day_end = point['h'] - 1
  73.            self.counter_e += 1
  74.            return
  75.  
  76.        elif self.counter_l == 0:
  77.            self.lunch = point['h']
  78.            self.counter_l += 1
  79.            return
  80.  
  81.        else:
  82.  
  83.            if point['h'] < self.lunch_b and self.counter_b > 0:
  84.                self.counter_b += 1
  85.                temp_begin = self.day_begin + (point['h'] + 1 - self.day_begin) * 1. / self.counter_b
  86.                self.day_b_square = (self.counter_b - 1) / self.counter_b * self.day_b_square + (self.counter_b - 1) * (
  87.                            temp_begin - self.day_begin) ** 2
  88.                self.day_begin = temp_begin
  89.  
  90.                return
  91.  
  92.            elif point['h'] > self.lunch_e and self.counter_e > 0:
  93.  
  94.                self.counter_e += 1
  95.                temp_end = self.day_end + (point['h'] - 1 - self.day_end) * 1. / self.counter_e
  96.                # self.day_e_square = (self.day_e_square + (point['h']-1 - self.day_end)*(point['h']-1 - temp_end)) /self.counter_e
  97.  
  98.                self.day_e_square = (self.counter_e - 1) / self.counter_e * self.day_e_square + (self.counter_e - 1) * (
  99.                            temp_end - self.day_end) ** 2
  100.                self.day_end = temp_end
  101.                return
  102.  
  103.            elif self.counter_l > 0:
  104.                self.lunch = (self.lunch * self.counter_l + point['h']) / (self.counter_l + 1)
  105.                self.counter_l += 1
  106.                return
  107.        '''
  108.     def identify_oborot(self, point):
  109.  
  110.         if self.point_c == 0:
  111.             self.oborot = point['w'] / 24  # Было бы неплохо в будущем смотреть на среднее для такой суммы (нет)
  112.             self.first_date = point['date']
  113.             self.last_date = point['date']
  114.             self.fullness = point['w']
  115.         elif self.point_c == 1:
  116.             delta_h = abs((point['date'] - self.last_date).total_seconds() // 3600)
  117.             orverall_h = 24
  118.             self.oborot = (orverall_h * self.oborot + point['w']) / (orverall_h + delta_h)
  119.             self.last_date = point['date']
  120.             self.fullness = point['w']
  121.         else:
  122.             try:
  123.                 delta_h = abs((point['date'] - self.last_date).total_seconds() // 3600)
  124.                 orverall_h = abs((self.first_date - self.last_date).total_seconds() // 3600)
  125.                 self.oborot = (orverall_h * self.oborot + point['w']) / (orverall_h + delta_h)
  126.                 self.last_date = point['date']
  127.                 self.fullness = point['w']
  128.             except ZeroDivisionError:
  129.                 print(self.name, point['date'], self.last_date,  (self.first_date - self.last_date).total_seconds(),
  130.                       (point['date'] - self.last_date).total_seconds())
  131.  
  132.     def identify_wealth(self, point):
  133.  
  134.  
  135.         if self.point_c == 0:
  136.  
  137.             self.wealth = point['w'] / self.env.avg_prices[point['shop']]
  138.         else:
  139.             self.wealth = (self.wealth * self.point_c + point['w'] / self.env.avg_prices[point['shop']]) / (self.point_c + 1)
  140.  
  141.     def identify_history(self, point):
  142.         try:
  143.             self.visits[point['shop']] += 1
  144.         except KeyError:
  145.             self.visits[point['shop']] = 1
  146.         try:
  147.             self.check[point['shop']] = (self.check[point['shop']] * self.point_c + point['w']) / (self.point_c + 1)
  148.         except KeyError:
  149.             self.check[point['shop']] = point['w']
  150.  
  151.     def identify_shop_probs(self):
  152.         self.shop_names = list(self.visits.keys())
  153.         self.shop_probs = [self.visits[k] / sum(self.visits.values()) for k in self.shop_names]
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.     def identify(self, point):
  161.         if point['shop'] in self.env.avg_prices:
  162.             self.identify_day(point)
  163.             self.identify_wealth(point)
  164.             self.identify_history(point)
  165.             self.identify_oborot(point)
  166.             self.last_fullness = self.fullness
  167.             self.point_c += 1
  168.  
  169.  
  170.  
  171.  
  172.         # if self.day_begin > self.lunch:
  173.         #    self.lunch = self.day_begin+4
  174.         # if self.day_end < self.lunch:
  175.         #    self.lunch = self.day_end-2
  176.  
  177.     def go_shop(self, env):
  178.  
  179.         # deciede to go or not:
  180.  
  181.         self.fullness -= self.oborot
  182.         if self.fullness <= 0:
  183.             mb = np.random.uniform(0, 1)
  184.             '''
  185.            if not test_exist(self.go_probs, env.global_time):
  186.                if env.global_time < self.lunch:
  187.                    self.go_probs[env.global_time] = self.probs_begin.pdf(env.global_time)
  188.                else:
  189.                    self.go_probs[env.global_time] = self.probs_end.pdf(env.global_time)
  190.            '''
  191.             print(self.name)
  192.  
  193.             if mb < self.go_probs[env.global_time]:
  194.  
  195.                 shop_prob = np.random.uniform(0, 1)
  196.  
  197.                 shop_map = {shop.name: shop for shop in env.shop_list}
  198.  
  199.                 it = 0
  200.  
  201.                 while shop_prob > 0:
  202.                     shop_prob -= self.shop_probs[it]
  203.                     it += 1
  204.  
  205.                 equals = [self.shop_names[a] for a in np.where(np.asarray(self.shop_probs) == self.shop_probs[it-1])[0]]
  206.  
  207.  
  208.                 if len(equals) > 1 and self.shop_names[it - 1] in equals:
  209.  
  210.                     f_wealth = lambda x: 1 - 2 ** (-self.wealth*np.mean(np.mean(list(env.avg_prices.values()))/env.avg_prices[x]))
  211.  
  212.  
  213.                     diff = [abs(f_wealth(x) - 0.5) for x in equals]
  214.                     go_to = shop_map[equals[diff.index(min(diff))]]
  215.                 else:
  216.                     go_to = shop_map[self.shop_names[it - 1]]
  217.  
  218.  
  219.                 # define target shop and amount
  220.  
  221.                 amount = self.wealth * env.avg_prices[go_to.name]
  222.                 self.fullness += amount * go_to.segment
  223.  
  224.                 # write event
  225.                 go_to.transactions.setdefault(env.week, []).append([self, env.global_time, amount])
  226.                 self.transactions.setdefault(env.week, []).append([go_to, env.iterat, env.global_time, amount])
  227.  
  228.                 env.cumulative_shops[env.iterat][go_to.name][0] += 1
  229.                 env.cumulative_shops[env.iterat][go_to.name][1] += amount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement