Пример #1
0
 def setUp(self):
   self.helper = BidderHelper()
   self.window_size = 5
   self.commitment = 0.8
   self.reputation = 0.5
   self.long_success_list = [1,0,1,0,1,0,1]
   self.short_success_list = [1,0,1,0]
   self.cost = 0.5
Пример #2
0
class BidderHelperTests(unittest.TestCase):
  def setUp(self):
    self.helper = BidderHelper()
    self.window_size = 5
    self.commitment = 0.8
    self.reputation = 0.5
    self.long_success_list = [1,0,1,0,1,0,1]
    self.short_success_list = [1,0,1,0]
    self.cost = 0.5

  def test_method_returns_myopic_method(self):
    method = self.helper.method({'method': 'myopic'})
    self.assertEqual(method.func, self.helper.myopic_bidding)
    self.assertEqual(method.args, ())
  
  def test_method_returns_lebodics_method(self):
    method = self.helper.method({'method':'lebodic', 'window_size':self.window_size})
    self.assertEqual(method.func, self.helper.lebodics_reputation_update)
    self.assertEqual(method.args, (self.window_size,))
  
  def test_method_returns_mcdiarmids_method(self):
    method = self.helper.method({'method':'mcdiarmid', 'commitment':self.commitment})
    self.assertEqual(method.func, self.helper.mcdiarmids_reputation_update)
    self.assertEqual(method.args, (self.commitment,))
  
  def test_method_raises_unknown_method_error(self):
    with self.assertRaises(errors.UnknownMethodError):
      self.helper.method({'method': 'unknown'})
    with self.assertRaises(errors.UnknownMethodError):
      self.helper.method({'something': None})
    with self.assertRaises(errors.UnknownMethodError):
      self.helper.method({'method': 'lebodic'})

  def test_myopic_bidding_for_price_weight_0(self):
    bid = self.helper.myopic_bidding(0.0, 0.5, 0.25, 0.5)
    self.assertEqual(bid, float('inf'))
  
  def test_myopic_bidding_for_price_weight_1(self):
    bid = self.helper.myopic_bidding(1.0, self.cost, 0.25, 0.5)
    self.assertEqual(bid, (1 + self.cost)/2)

  def test_myopic_bidding_for_equal_reputations(self):
    bid = self.helper.myopic_bidding(0.5, self.cost, self.reputation, self.reputation)
    self.assertEqual(bid, (1 + self.cost)/2)
  
  def test_myopic_bidding_for_remaining_cases(self):
    price_weight = 0.5
    bid = self.helper.myopic_bidding(price_weight, self.cost, self.reputation, 0.75)
    def th_cost(bid):
      if bid == 13/16:
        return 0.75
      else:
        return 0.75 + 1 / ((13*8 - 128*bid) * (-(81*2)/63) * np.exp(-16/63 + 1/(13-16*bid)) + 4*(7*8 - 64*bid))
    th_bids = np.linspace(145/(32*8), 13/16, 1000)
    diff = list(map(lambda x: np.abs(x-0.5), map(th_cost, th_bids)))
    th_bid = th_bids[diff.index(min(diff))]
    self.assertEqual(bid, (th_bid - price_weight * self.cost)/price_weight)

  def test_lebodics_reputation_update(self):
    reputation = self.helper.lebodics_reputation_update(self.window_size,
        self.reputation,
        self.long_success_list)
    self.assertEqual(reputation, 0.4)
    reputation = self.helper.lebodics_reputation_update(self.window_size,
        self.reputation,
        self.short_success_list)
    self.assertEqual(reputation, self.reputation)

  def test_mcdiarmids_reputation_update(self):
    reputation = self.helper.mcdiarmids_reputation_update(self.commitment,
        self.reputation,
        self.long_success_list)
    self.assertEqual(reputation, self.reputation - 0.01)
    reputation = self.helper.mcdiarmids_reputation_update(self.commitment,
        self.reputation,
        self.short_success_list)
    self.assertEqual(reputation, self.reputation + self.commitment / 100 / (1-self.commitment))