daily pastebin goal
12%
SHARE
TWEET

GLMechanism

a guest May 16th, 2018 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class GLResourceDistribution(gym.Env):
  2.  
  3.     def __init__(self):
  4.         self.stakes = {}
  5.  
  6.         self.ACTIONS = {}
  7.  
  8.         moves = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
  9.         count = 0
  10.         for i in moves:
  11.             for j in moves:
  12.                 for k in moves:
  13.                     if (i + j + k >= 100) and (i + j + k == 110) and (i >= j) and (i >= k):
  14.                         local_stakes = [i + 5, j, k]
  15.                         self.ACTIONS[count] = local_stakes
  16.                         count += 1
  17.  
  18.         self.action_space = spaces.Discrete(count)
  19.         self.observation_space = spaces.Box(low=np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
  20.                                             high=np.array([1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0]))
  21.         self.rew = 0
  22.         self.rews = {}
  23.         self.seed()
  24.         self.steps = []
  25.         self.is_done = {}
  26.         self.in_second_lock = {}
  27.         self.last_stakes = {}
  28.         self.beta = 0.0005
  29.         self.alpha = 1
  30.         self.x = {}
  31.         self.epoch = 0
  32.  
  33.     def seed(self, seed=None):
  34.         self.np_random, seed = seeding.np_random(seed)
  35.         return [seed]
  36.  
  37.     def get_action_space(self):
  38.         return self.action_space
  39.  
  40.     def get_observation_space(self):
  41.         return self.observation_space
  42.  
  43.     def get_rew(self):
  44.         return self.rew
  45.  
  46.     def get_rew(self, id):
  47.         return self.rews[id]
  48.  
  49.     def set_stake(self, id, action):
  50.         self.stakes[id] = action
  51.  
  52.     def get_stakes_len(self):
  53.         return len(self.stakes)
  54.  
  55.     def get_stakes(self):
  56.         return self.stakes
  57.  
  58.     def clear_stakes(self):
  59.         self.stakes = {}
  60.  
  61.     def get_last_stakes(self):
  62.         return self.last_stakes
  63.  
  64.     def step(self, u):
  65.  
  66.         self.stakes[1] = self.ACTIONS[u]
  67.         print (str(self.epoch) + " " + str(self.stakes[1]))
  68.         self.epoch += 1
  69.  
  70.         second_stake = np.random.rand(3)
  71.         third_stake = np.random.rand(3)
  72.  
  73.         second_stake = second_stake / sum(second_stake)
  74.         third_stake = third_stake / sum(third_stake)
  75.  
  76.         second_stake *= 115
  77.         third_stake *= 115
  78.  
  79.         self.stakes[2] = second_stake
  80.         self.stakes[3] = third_stake
  81.  
  82.         self.last_stakes = self.stakes
  83.  
  84.         s_matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  85.  
  86.         ids = [1, 2, 3]
  87.  
  88.         for id in ids:
  89.             s_matrix[id - 1][0] = self.stakes[id][0]
  90.             s_matrix[id - 1][1] = self.stakes[id][1]
  91.             s_matrix[id - 1][2] = self.stakes[id][2]
  92.  
  93.  
  94.         self.x = {}
  95.         for id in ids:
  96.             payoff = (s_matrix[0][id - 1] + s_matrix[1][id - 1] + s_matrix[2][id - 1]) / 3
  97.             self.x[id] = payoff
  98.  
  99.         penalty = {}
  100.         for id in ids:
  101.             penalty[id] = self.beta * (((self.x[1] - s_matrix[id - 1][0]) ** 2) + ((self.x[2] - s_matrix[id - 1][1]) ** 2) + ((self.x[3] - s_matrix[id - 1][2]) ** 2))
  102.  
  103.         penalty_sum = penalty[1] + penalty[2] + penalty[3]
  104.         for id in ids:
  105.             refund = penalty[id] - self.alpha * penalty_sum / 3
  106.             r = 25
  107.             if id == 1:
  108.                 r = 1
  109.             if id == 2:
  110.                 r = 9
  111.             self.rews[id] = np.sqrt(self.x[id] + r) - refund
  112.  
  113.  
  114.         return self._get_obs(), self.x[1] / np.exp(penalty[1]), False, {}
  115.  
  116.     def reset(self):
  117.         self.rews[1] = 0
  118.         self.rews[2] = 0
  119.         self.rews[3] = 0
  120.         return np.array([0, 0, 0, 0, 0, 0])
  121.  
  122.     def _get_obs(self):
  123.         return np.array([self.stakes[2][0], self.stakes[2][1], self.stakes[2][2], self.stakes[3][0], self.stakes[3][1], self.stakes[3][2]])
  124.  
  125.     def get_obs(self):
  126.         return self._get_obs()
  127.  
  128.     def render(self, mode='human'):
  129.         pass
  130.  
  131.     def close(self):
  132.         if self.viewer: self.viewer.close()
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