Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from scipy import stats
- import numpy as np, random
- from src.utils import test_exist
- class Agent:
- def initial(self, attrs, data):
- for attr, val in zip(attrs, data):
- setattr(self, attr, val)
- class Shop(Agent):
- def __init__(self,env):
- env.register_shop(self)
- self.transactions = {}
- class Client(Agent):
- def __init__(self, env):
- env.register_client(self)
- self.transactions = {}
- self.env = env
- # OSTOV
- self.point_c = 0
- self.counter_b = 0
- self.counter_e = 0
- self.counter_l = 0
- self.wealth = 0
- self.oborot = 0
- self.first_date = 0
- self.last_date = 0
- self.fullness = 0
- self.prob_day = 0
- self.probs_begin = -1
- self.probs_end = -1
- self.day_begin = -1
- self.day_b_square = 0
- self.day_end = -1
- self.day_e_square = 0
- self.lunch = -1
- self.lunch_b = 12
- self.lunch_e = 16
- self.visits = {}
- self.check = {}
- def identify_day(self, point):
- if self.point_c == 0:
- self.first_date = point['date']
- self.go_probs = {k:0 for k in range(0,24)}
- self.go_probs[point['h']] = 1
- else:
- diff = abs((point['date'] - self.first_date).days)
- if diff == 0:
- self.go_probs[point['h']] = 1
- else:
- for k, v in self.go_probs.items():
- if k == point['h']:
- self.go_probs[k] = (v * diff + 1) / (diff+1)
- else:
- self.go_probs[k] = (v * diff) / (diff+1)
- def identify_oborot(self, point):
- if self.point_c == 0:
- self.oborot = point['w'] / 24 # Было бы неплохо в будущем смотреть на среднее для такой суммы (нет)
- self.first_date = point['date']
- self.last_date = point['date']
- self.fullness = point['w']
- elif self.point_c == 1:
- delta_h = abs((point['date'] - self.last_date).total_seconds() // 3600)
- orverall_h = 24
- self.oborot = (orverall_h * self.oborot + point['w']) / (orverall_h + delta_h)
- self.last_date = point['date']
- self.fullness = point['w']
- else:
- try:
- delta_h = abs((point['date'] - self.last_date).total_seconds() // 3600)
- orverall_h = abs((self.first_date - self.last_date).total_seconds() // 3600)
- self.oborot = (orverall_h * self.oborot + point['w']) / (orverall_h + delta_h)
- self.last_date = point['date']
- self.fullness = point['w']
- except ZeroDivisionError:
- print(self.name, point['date'], self.last_date, (self.first_date - self.last_date).total_seconds(),
- (point['date'] - self.last_date).total_seconds())
- def identify_wealth(self, point):
- if self.point_c == 0:
- self.wealth = point['w'] / self.env.avg_prices[point['shop']]
- else:
- self.wealth = (self.wealth * self.point_c + point['w'] / self.env.avg_prices[point['shop']]) / (self.point_c + 1)
- def identify_history(self, point):
- try:
- self.visits[point['shop']] += 1
- except KeyError:
- self.visits[point['shop']] = 1
- try:
- self.check[point['shop']] = (self.check[point['shop']] * self.point_c + point['w']) / (self.point_c + 1)
- except KeyError:
- self.check[point['shop']] = point['w']
- def identify_shop_probs(self):
- self.shop_names = list(self.visits.keys())
- self.shop_probs = [self.visits[k] / sum(self.visits.values()) for k in self.shop_names]
- def identify(self, point):
- if point['shop'] in self.env.avg_prices:
- self.identify_day(point)
- self.identify_wealth(point)
- self.identify_history(point)
- self.identify_oborot(point)
- self.last_fullness = self.fullness
- self.point_c += 1
- def go_shop(self, env):
- # deciede to go or not:
- self.fullness -= self.oborot
- if self.fullness <= 0:
- mb = np.random.uniform(0, 1)
- print(self.name)
- if mb < self.go_probs[env.global_time]:
- shop_prob = np.random.uniform(0, 1)
- shop_map = {shop.name: shop for shop in env.shop_list}
- it = 0
- while shop_prob > 0:
- shop_prob -= self.shop_probs[it]
- it += 1
- equals = [self.shop_names[a] for a in np.where(np.asarray(self.shop_probs) == self.shop_probs[it-1])[0]]
- if len(equals) > 1 and self.shop_names[it - 1] in equals:
- f_wealth = lambda x: 1 - 2 ** (-self.wealth*np.mean(np.mean(list(env.avg_prices.values()))/env.avg_prices[x]))
- diff = [abs(f_wealth(x) - 0.5) for x in equals]
- go_to = shop_map[equals[diff.index(min(diff))]]
- else:
- go_to = shop_map[self.shop_names[it - 1]]
- amount = self.wealth * env.avg_prices[go_to.name]
- self.fullness += amount * go_to.segment
- # write event
- go_to.transactions.setdefault(env.week, []).append([self, env.global_time, amount])
- self.transactions.setdefault(env.week, []).append([go_to, env.iterat, env.global_time, amount])
- env.cumulative_shops[env.iterat][go_to.name][0] += 1
- env.cumulative_shops[env.iterat][go_to.name][1] += amount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement