def is_equitable(self, slices): ''' Test that the proposed division is equitable :param slices: The proposed division of the resource :returns: True if equitable, False otherwise ''' pieces = slices.values() for user, piece in slices.items(): value = get_total_value(user, piece) if not all(get_total_value(user, p) == value for p in pieces): return False return True
def is_envy_free(self, slices): ''' Test that the proposed division is envy-free :param slices: The proposed division of the resource :returns: True if envy-free, False otherwise ''' pieces = slices.values() for user, piece in slices.items(): envied = max(get_total_value(user, p) for p in pieces) if envied > get_total_value(user, piece): return False return True
def test_division(self): ''' test that the algorithm divides correctly ''' vals = {'red': 10, 'blue': 20, 'green': 30} keys = vals.keys() cake = CollectionResource(keys) users = [] users.append( CollectionPreference('mark', { 'red': 10, 'blue': 20, 'green': 30 })) users.append( CollectionPreference('john', { 'red': 10, 'blue': 30, 'green': 20 })) users.append( CollectionPreference('anna', { 'red': 30, 'blue': 20, 'green': 10 })) algorithm = SealedBidsAuction(users, cake) divisions = algorithm.divide() for user, pieces in divisions.items(): self.assertEqual(30, get_total_value(user, pieces))
def print_results(divider, results, extras): ''' Given an algorithm and its results, print a nice report to the console. :param divider: The algorithm we divided with :param results: The division results :param extras: Any extra results to report ''' header = "-" * 50 print "\nAlgorithm Information\n", header for key, value in divider.settings().items(): print "*", key, "\t:", value print "\nAlgorithm Results\n", header for user, shares in results.items(): print "* share for %s: value(%s)" % (str(user), get_total_value(user, shares)) print " ", shares, "\n" if isinstance(extras, dict): print "Extra Results\n", header for key, value in extras.items(): print key, "\t:", value print print "Algorithm Result Validation\n", header print "* users\t\t:", len(results.keys()) #print "* optimal\t:", divider.is_optimal(results) print "* envy free\t:", divider.is_envy_free(results) print "* equitable\t:", divider.is_equitable(results) print "* proportional\t:", divider.is_proportional(results)
def is_proportional(self, slices): ''' Test that the proposed division is proportional :param slices: The proposed division of the resource :returns: True if proportional, False otherwise ''' share = slices.keys()[0].value_of(self.cake) / len(self.users) pieces = slices.values() for user, _ in slices.items(): if not all(get_total_value(user, p) >= share for p in pieces): return False return True
def test_division(self): ''' test that the algorithm divides correctly ''' vals = {'red':10, 'blue':20, 'green':30} keys = vals.keys() cake = CollectionResource(keys) users = [] users.append(CollectionPreference('mark', {'red':10, 'blue':20, 'green':30})) users.append(CollectionPreference('john', {'red':10, 'blue':30, 'green':20})) users.append(CollectionPreference('anna', {'red':30, 'blue':20, 'green':10})) algorithm = SealedBidsAuction(users, cake) divisions = algorithm.divide() for user, pieces in divisions.items(): self.assertEqual(30, get_total_value(user, pieces))
lambda: IntervalResource((F(0), F(1))) ] #------------------------------------------------------------ # test that the methods work #------------------------------------------------------------ print "\n","=" * 60 print "Algorithms Stress Test" print "=" * 60,"\n" for user_factory, cake_factory in zip(us, cs): print "-" * 60 print cake_factory().__class__.__name__ print "-" * 60 for algorithm_factory in algorithms: print "* %s" % algorithm_factory.__name__ for size in range(2, iterations): size = 2 # override number of users here value = F(1, size) cake = cake_factory() users = [user_factory(cake) for _ in range(size)] algorithm = algorithm_factory(users, cake) extras, results = None, algorithm.divide() if isinstance(results, tuple): results, extras = results for user, shares in results.items(): print " - share for %s: value(%s)" % (str(user), get_total_value(user, shares)) if extras: for k, v in extras.items(): print " ~", k, v print