@Test
  public void testEmptyPricing() {
    // when there is nobody to search just go at random.
    PurchasesDepartment dept = mock(PurchasesDepartment.class);
    // search will always return null
    when(dept.getAvailableBudget()).thenReturn(100);
    when(dept.getGoodType()).thenReturn(UndifferentiatedGoodType.GENERIC);

    MersenneTwisterFast random = new MersenneTwisterFast(0);
    when(dept.getRandom()).thenReturn(random);
    // we need a stub market
    Market market = mock(Market.class);
    when(market.getLastPrice()).thenReturn(-1);
    when(dept.getMarket()).thenReturn(market);

    BidPricingStrategy pricing = new UrgentPriceFollowerStrategy(dept);
    for (int i = 0;
        i < 10000;
        i++) // because there is no last price in the market we get to randomize
    {
      int maxPrice = pricing.maxPrice(UndifferentiatedGoodType.GENERIC);
      assertTrue(maxPrice >= 0);
      assertTrue(maxPrice <= 100);
    }

    // there should be about 10% of the data in any decile
    int decile = 0;
    for (int i = 0; i < 10000; i++) {
      int maxPrice = pricing.maxPrice(UndifferentiatedGoodType.GENERIC);
      if (maxPrice >= 40 && maxPrice < 50) decile++;
    }
    assertTrue(decile >= 900 && decile <= 1100);
  }
  @Test
  public void testUrgencyStub() {
    // we are going to check that prices change when urgency chnge

    PurchasesDepartment dept = mock(PurchasesDepartment.class);
    when(dept.getGoodType()).thenReturn(UndifferentiatedGoodType.GENERIC);
    when(dept.getAvailableBudget()).thenReturn(100);
    MersenneTwisterFast random = new MersenneTwisterFast(0);
    when(dept.getRandom()).thenReturn(random);
    // we need a stub market
    Market market = mock(Market.class);
    when(market.getLastPrice()).thenReturn(50); // so last price is 50
    when(dept.getMarket()).thenReturn(market);
    BidPricingStrategy pricing = new UrgentPriceFollowerStrategy(dept);

    // give fake danger signal!
    when(dept.rateCurrentLevel()).thenReturn(Level.DANGER);
    for (int i = 0; i < 10; i++)
      assertEquals(pricing.maxPrice(UndifferentiatedGoodType.GENERIC), 60);

    // give fake barely signal!
    when(dept.rateCurrentLevel()).thenReturn(Level.BARELY);
    for (int i = 0; i < 10; i++)
      assertEquals(pricing.maxPrice(UndifferentiatedGoodType.GENERIC), 50);

    // give fake acceptable signal!
    when(dept.rateCurrentLevel()).thenReturn(Level.ACCEPTABLE);
    for (int i = 0; i < 10; i++)
      assertEquals(pricing.maxPrice(UndifferentiatedGoodType.GENERIC), 40);

    // give fake too much signal!
    when(dept.rateCurrentLevel()).thenReturn(Level.TOOMUCH);
    for (int i = 0; i < 10; i++)
      assertEquals(pricing.maxPrice(UndifferentiatedGoodType.GENERIC), 25);
  }
  @Test
  public void testUrgencyFull() throws NoSuchFieldException, IllegalAccessException {
    // we are going to check that prices change when urgency change
    // the only stub is the market
    Market market = mock(Market.class);
    when(market.getLastPrice()).thenReturn(50); // so last price is 50
    model.schedule =
        mock(
            Schedule
                .class); // we also mock the schedule to avoid the inventory control from spamming
                         // buy orders in the schedule

    when(market.getGoodType()).thenReturn(UndifferentiatedGoodType.GENERIC);
    Firm f = new Firm(model);

    PurchasesDepartment dept =
        PurchasesDepartment.getPurchasesDepartment(
                100,
                f,
                market,
                FixedInventoryControl.class,
                UrgentPriceFollowerStrategy.class,
                null,
                null)
            .getDepartment();

    // when there is nobody to search just go at random.
    Field field = PurchasesDepartment.class.getDeclaredField("pricingStrategy");
    field.setAccessible(true);
    BidPricingStrategy pricing = (BidPricingStrategy) field.get(dept);

    // assuming the fixed inventory control wants 6

    // right now it's danger
    for (int i = 0; i < 10; i++)
      assertEquals(pricing.maxPrice(UndifferentiatedGoodType.GENERIC), 60);

    // barely
    for (int i = 0; i < 3; i++)
      f.receive(Good.getInstanceOfUndifferentiatedGood(UndifferentiatedGoodType.GENERIC), null);
    for (int i = 0; i < 10; i++)
      assertEquals(pricing.maxPrice(UndifferentiatedGoodType.GENERIC), 50);

    // acceptable
    for (int i = 0; i < 3; i++)
      f.receive(Good.getInstanceOfUndifferentiatedGood(UndifferentiatedGoodType.GENERIC), null);
    for (int i = 0; i < 10; i++)
      assertEquals(pricing.maxPrice(UndifferentiatedGoodType.GENERIC), 40);

    // too much
    for (int i = 0; i < 30; i++)
      f.receive(Good.getInstanceOfUndifferentiatedGood(UndifferentiatedGoodType.GENERIC), null);
    for (int i = 0; i < 10; i++)
      assertEquals(pricing.maxPrice(UndifferentiatedGoodType.GENERIC), 25);
  }
Exemplo n.º 4
0
 /** how "far" sales inventory are from target. */
 @Override
 public float estimateSupplyGap(GoodType type) {
   if (market == null) return 0;
   if (!soldToday && market.getLastPrice() >= saleQuote) return 1;
   else return 0;
 }