def setUp(self): self.commodities = { "A": {}, "B": {}, "C": {}, "AB": {"A": 1, "B": 1}, "BC": {"B": 1, "C": 1}, "CA": {"C": 1, "A": 1}, } self.actors = [Actor("A"), Actor("B")] self.bazaar = Bazaar(self.commodities, self.actors)
def setUp(self): self.commodities = {"A": {}, "B": {}, "AB": {"A":1, "B": 1}} self.actor = Actor("AB") self.bazaar = Bazaar(self.commodities, [self.actor])
class TestActor(unittest.TestCase): def setUp(self): self.commodities = {"A": {}, "B": {}, "AB": {"A":1, "B": 1}} self.actor = Actor("AB") self.bazaar = Bazaar(self.commodities, [self.actor]) def test_init(self): self.assertEqual([0.5,1.5], self.actor.get_belief("A").to_list()) self.assertEqual([0.5,1.5], self.actor.get_belief("B").to_list()) self.assertEqual([1.5,4.5], self.actor.get_belief("AB").to_list()) self.assertEqual([0.5,1.5], self.actor.get_clearing_price("A")) self.assertEqual([0.5,1.5], self.actor.get_clearing_price("B")) self.assertEqual([1.5,4.5], self.actor.get_clearing_price("AB")) def test_get_price_belief(self): price = self.actor.get_belief("A") self.assertEqual([0.5, 1.5], price.to_list()) def test_update_price_belief(self): self.bazaar.set_history("price", "A", [8,8,8,8,8]) belief = self.actor.set_belief("A", Belief(3.5,4.5)) self.actor.update_belief(self.bazaar, "sell", "A", False) belief = self.actor.get_belief("A") self.assertEqual([5.3, 6.7], belief.to_list()) def test_get_clearing_favorability(self): result = self.actor.get_clearing_favorability(0,0,10) self.assertEqual(0, result) result = self.actor.get_clearing_favorability(10,0,10) self.assertEqual(1, result) result = self.actor.get_clearing_favorability(5,0,10) self.assertEqual(0.5, result) def test_determine_sale_quantity(self): self.actor.inventory.set_quantity("A", 10) self.actor.inventory.set_ideal_quantity("A", 5) self.actor.set_clearing_price("A", [1,10]) self.bazaar.set_history("price", "A", [10]*15) self.assertEqual(5, self.actor.determine_sale_quantity(self.bazaar, "A")) self.assertEqual(0, self.actor.determine_buy_quantity(self.bazaar, "A")) def test_determine_buy_quantity(self): self.actor.inventory.set_quantity("A", 5) self.actor.inventory.set_ideal_quantity("A", 10) self.actor.set_clearing_price("A", [1,10]) self.bazaar.set_history("price", "A", [1]*15) self.assertEqual(5, self.actor.determine_buy_quantity(self.bazaar, "A")) self.assertEqual(0, self.actor.determine_sale_quantity(self.bazaar, "A")) def test_determine_buy_sale_quantity_medium_market(self): self.actor.inventory.set_quantity("A", 5) self.actor.inventory.set_ideal_quantity("A", 10) self.actor.set_clearing_price("A", [3,5]) self.actor.inventory.set_quantity("B", 10) self.actor.inventory.set_ideal_quantity("B", 5) self.actor.set_clearing_price("B", [3,5]) self.bazaar.set_history("price", "A", [4]*15) self.bazaar.set_history("price", "B", [4]*15) self.assertEqual(2.5, self.actor.determine_buy_quantity(self.bazaar, "A")) self.assertEqual(0, self.actor.determine_sale_quantity(self.bazaar, "A")) self.assertEqual(0, self.actor.determine_buy_quantity(self.bazaar, "B")) self.assertEqual(2.5, self.actor.determine_sale_quantity(self.bazaar, "B")) def test_generate_orders(self): self.actor.inventory.set_quantity("A", 5) self.actor.inventory.set_ideal_quantity("A", 10) self.actor.set_clearing_price("A", [1,10]) self.bazaar.set_history("price", "A", [5]*15) self.actor.generate_offers(self.bazaar, "A") offer = self.bazaar.offers["A"]["bid"][0] self.assertAlmostEqual(len(self.bazaar.offers["A"]["bid"]), 1) self.assertAlmostEqual(offer.quantity, 2.777777777777777) self.assertAlmostEqual(offer.commodity_id, "A")
class TestBazaar(unittest.TestCase): def setUp(self): self.commodities = { "A": {}, "B": {}, "C": {}, "AB": {"A": 1, "B": 1}, "BC": {"B": 1, "C": 1}, "CA": {"C": 1, "A": 1}, } self.actors = [Actor("A"), Actor("B")] self.bazaar = Bazaar(self.commodities, self.actors) def test_init(self): commodities = {"A": {}, "B": {}, "AB": {"A": 1, "B": 1}} actors = [Actor("AB")] bazaar = Bazaar(commodities, actors) self.assertEqual(bazaar.commodity_count(), 3) self.assertEqual(bazaar.actor_count(), 1) def test_add_history(self): self.bazaar.add_history("price", "A", 1) self.assertEqual(self.bazaar.history["price"]["A"], [0.5, 1.5, 1]) def test_avg_history(self): # Added by Market init function # self.bazaar.add_history("price", "A", 0.5) # self.bazaar.add_history("price", "A", 1.5) self.bazaar.add_history("price", "A", 2) self.bazaar.add_history("price", "A", 3) self.bazaar.add_history("price", "A", 4) result = self.bazaar.avg_history("price", "A", 1) self.assertEqual(result, 4) result = self.bazaar.avg_history("price", "A", 2) self.assertEqual(result, 3.5) result = self.bazaar.avg_history("price", "A", 3) self.assertEqual(result, 3) result = self.bazaar.avg_history("price", "A", 4) self.assertEqual(result, 2.625) result = self.bazaar.avg_history("price", "A", 5) self.assertEqual(result, 2.2) result = self.bazaar.avg_history("price", "A", 6) self.assertEqual(result, 2.2) def test_set_history(self): self.bazaar.set_history("price", "A", [9, 9, 9, 9, 9, 9]) self.assertEqual([9, 9, 9, 9, 9, 9], self.bazaar.get_history("price", "A")) def test_resolve_offers_A(self): o_ask = Offer(self.actors[0], "A", 1, 10) o_bid = Offer(self.actors[1], "A", 1, 10) self.bazaar.ask(o_ask) self.bazaar.bid(o_bid) self.bazaar.resolve_offers("A") self.assertEqual(self.actors[0].get_inventory("A"), -1) self.assertEqual(self.actors[0].get_money(), 2010) self.assertEqual(self.actors[1].get_money(), 1990) self.assertEqual(self.actors[1].get_inventory("A"), 1) def test_resolve_offers_B(self): o_ask = Offer(self.actors[0], "A", 1, 5) o_bid = Offer(self.actors[1], "A", 1, 10) self.bazaar.ask(o_ask) self.bazaar.bid(o_bid) self.bazaar.resolve_offers("A") self.assertEqual(self.actors[0].get_inventory("A"), -1) self.assertEqual(self.actors[0].get_money(), 2007.5) self.assertEqual(self.actors[1].get_money(), 1992.5) self.assertEqual(self.actors[1].get_inventory("A"), 1) def test_simulate(self): commodities = { "A": {}, "B": {}, "C": {}, "AB": {"A": 1, "B": 1}, "BC": {"B": 1, "C": 1}, "CA": {"C": 1, "A": 1}, "ABBC": {"AB": 1, "BC": 1}, "BCCA": {"BC": 1, "CA": 1}, "CAAB": {"CA": 1, "AB": 1}, } actors = [ Actor("A"), Actor("A"), Actor("A"), Actor("B"), Actor("B"), Actor("B"), Actor("C"), Actor("C"), Actor("C"), Actor("AB"), Actor("AB"), Actor("AB"), Actor("BC"), Actor("BC"), Actor("BC"), Actor("CA"), Actor("CA"), Actor("CA"), Actor("ABBC"), Actor("ABBC"), Actor("ABBC"), Actor("BCCA"), Actor("BCCA"), Actor("BCCA"), Actor("CAAB"), Actor("CAAB"), Actor("CAAB"), ] bazaar = Bazaar(commodities, actors) bazaar.simulate(2000) pprint(bazaar.__dict__, None, 1, 200) pprint(actors[9].__dict__, None, 1, 200) pprint(bazaar.avg_history("price", "A", 1)) bazaar.simulate(1) pprint(actors[9].inventory.get_space_empty(), None, 1, 200) pprint(actors[9].get_belief("A"), None, 1, 200) pprint(actors[9].get_inventory("A"), None, 1, 200) pprint(actors[9].determine_buy_quantity(bazaar, "A"), None, 1, 200) bazaar.simulate(1) pprint(actors[9].inventory.get_space_empty(), None, 1, 200) pprint(actors[9].get_belief("A"), None, 1, 200) pprint(actors[9].get_inventory("A"), None, 1, 200) pprint(actors[9].determine_buy_quantity(bazaar, "A"), None, 1, 200) bazaar.simulate(1) pprint(actors[9].inventory.get_space_empty(), None, 1, 200) pprint(actors[9].get_belief("A"), None, 1, 200) pprint(actors[9].get_inventory("A"), None, 1, 200) pprint(actors[9].determine_buy_quantity(bazaar, "A"), None, 1, 200)
def test_simulate(self): commodities = { "A": {}, "B": {}, "C": {}, "AB": {"A": 1, "B": 1}, "BC": {"B": 1, "C": 1}, "CA": {"C": 1, "A": 1}, "ABBC": {"AB": 1, "BC": 1}, "BCCA": {"BC": 1, "CA": 1}, "CAAB": {"CA": 1, "AB": 1}, } actors = [ Actor("A"), Actor("A"), Actor("A"), Actor("B"), Actor("B"), Actor("B"), Actor("C"), Actor("C"), Actor("C"), Actor("AB"), Actor("AB"), Actor("AB"), Actor("BC"), Actor("BC"), Actor("BC"), Actor("CA"), Actor("CA"), Actor("CA"), Actor("ABBC"), Actor("ABBC"), Actor("ABBC"), Actor("BCCA"), Actor("BCCA"), Actor("BCCA"), Actor("CAAB"), Actor("CAAB"), Actor("CAAB"), ] bazaar = Bazaar(commodities, actors) bazaar.simulate(2000) pprint(bazaar.__dict__, None, 1, 200) pprint(actors[9].__dict__, None, 1, 200) pprint(bazaar.avg_history("price", "A", 1)) bazaar.simulate(1) pprint(actors[9].inventory.get_space_empty(), None, 1, 200) pprint(actors[9].get_belief("A"), None, 1, 200) pprint(actors[9].get_inventory("A"), None, 1, 200) pprint(actors[9].determine_buy_quantity(bazaar, "A"), None, 1, 200) bazaar.simulate(1) pprint(actors[9].inventory.get_space_empty(), None, 1, 200) pprint(actors[9].get_belief("A"), None, 1, 200) pprint(actors[9].get_inventory("A"), None, 1, 200) pprint(actors[9].determine_buy_quantity(bazaar, "A"), None, 1, 200) bazaar.simulate(1) pprint(actors[9].inventory.get_space_empty(), None, 1, 200) pprint(actors[9].get_belief("A"), None, 1, 200) pprint(actors[9].get_inventory("A"), None, 1, 200) pprint(actors[9].determine_buy_quantity(bazaar, "A"), None, 1, 200)
def test_init(self): commodities = {"A": {}, "B": {}, "AB": {"A": 1, "B": 1}} actors = [Actor("AB")] bazaar = Bazaar(commodities, actors) self.assertEqual(bazaar.commodity_count(), 3) self.assertEqual(bazaar.actor_count(), 1)