コード例 #1
0
 private void eatEverythingAndRemoveQuotes() {
   int money =
       hasHowMany(market.getMoney()); // save the amount of money you have so you don't lose it
   consumeAll();
   if (money > 0) receiveMany(market.getMoney(), money); // retrieve the money you had before
   market.removeAllSellQuoteBySeller(this);
 }
コード例 #2
0
  @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);
  }
コード例 #3
0
  @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);
  }
コード例 #4
0
  /**
   * if it has anything to sell, the seller tries to sell one of its goods
   *
   * @param market
   */
  private void sellIfPossible(Market market) {
    GoodType typeSold = market.getGoodType();
    if (hasAny(typeSold)) {
      Good toSell = peekGood(typeSold);
      assert toSell != null;

      market.submitSellQuote(this, minPrice, toSell);
    }
  }
コード例 #5
0
 private void createNewGoods(DailyGoodTree goodTree) {
   if (market.getGoodType().isDifferentiated()) {
     for (int i = 0; i < dailySupply; i++) {
       receive(
           Good.getInstanceOfDifferentiatedGood(market.getGoodType(), goodTree, minPrice), null);
     }
   } else {
     receiveMany((UndifferentiatedGoodType) market.getGoodType(), dailySupply);
   }
 }
コード例 #6
0
  @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);
  }
コード例 #7
0
  public DailyGoodTree(MacroII model, int dailySupply, int minPrice, Market market) {
    super(model);
    Preconditions.checkArgument(minPrice >= 0);
    Preconditions.checkArgument(dailySupply > 0, "daily supply must be positive");
    this.minPrice = minPrice;
    this.market = market;
    this.dailySupply = dailySupply;
    market.registerSeller(this);

    startSteppables(model);
    name = market.getGoodType() + "Tree, price: " + minPrice;
  }
コード例 #8
0
 @Override
 public void turnOff() {
   super.turnOff();
   assert !isActive();
   // remove all quotes
   eatEverythingAndRemoveQuotes();
   // deregister yourself
   market.deregisterSeller(this);
 }
コード例 #9
0
  /**
   * Called by a buyer that wants to buy directly from this agent, not going through the market.
   *
   * @param buyerQuote the quote (including the offer price) of the buyer
   * @param sellerQuote the quote (including the offer price) of the seller; I expect the buyer to
   *     have achieved this through asked for an offer function
   * @return a purchaseResult including whether the trade was succesful and if so the final price
   */
  @Override
  public PurchaseResult shopHere(Quote buyerQuote, Quote sellerQuote) {
    int finalPrice = market.price(sellerQuote.getPriceQuoted(), buyerQuote.getPriceQuoted());
    assert sellerQuote.getGood() != null;
    assert this.has(sellerQuote.getGood());

    // exchange hostages
    market.trade(
        buyerQuote.getAgent(), this, sellerQuote.getGood(), finalPrice, buyerQuote, sellerQuote);
    this.logEvent(
        this,
        MarketEvents.SOLD,
        this.getModel().getCurrentSimulationTimeInMillis(),
        "price: " + finalPrice + ", through buyFromHere()"); // sold a good
    assert !this.has(sellerQuote.getGood());

    PurchaseResult toReturn = PurchaseResult.SUCCESS;
    toReturn.setPriceTrade(finalPrice);
    return toReturn;
  }
コード例 #10
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;
 }
コード例 #11
0
 public void setMinPrice(int minPrice) {
   this.minPrice = minPrice;
   name = market.getGoodType() + "Tree, price: " + minPrice;
 }