@Override
 public void turnOff() {
   super.turnOff();
   assert !isActive();
   // remove all quotes
   eatEverythingAndRemoveQuotes();
   // deregister yourself
   market.deregisterSeller(this);
 }
  @Test
  public void scenario1() {

    DummyBuyer buyer1 = new DummyBuyer(model, 10, market);
    market.registerBuyer(buyer1);
    DummyBuyer buyer2 = new DummyBuyer(model, 20, market);
    market.registerBuyer(buyer2);
    DummyBuyer buyer3 = new DummyBuyer(model, 30, market);
    market.registerBuyer(buyer3);
    DummyBuyer buyer4 = new DummyBuyer(model, 40, market);
    market.registerBuyer(buyer4);
    DummyBuyer buyer5 = new DummyBuyer(model, 50, market);
    market.registerBuyer(buyer5);

    List<EconomicAgent> sample = Arrays.asList(toTest.sampleBuyers());
    assertTrue(sample.size() == 5);
    assertTrue(sample.contains(buyer1));
    assertTrue(sample.contains(buyer2));
    assertTrue(sample.contains(buyer3));
    assertTrue(sample.contains(buyer4));
    assertTrue(sample.contains(buyer5));

    EconomicAgent bestBuyer = toTest.getBestInSampleBuyer();
    assertEquals(buyer5, bestBuyer);
    assertEquals(50, bestBuyer.askedForABuyOffer(UndifferentiatedGoodType.GENERIC));

    buyer5.setQuotedPrice(5);

    toTest.reactToSuccess(buyer5, PurchaseResult.SUCCESS);

    bestBuyer = toTest.getBestInSampleBuyer();
    assertEquals(buyer5, bestBuyer);
    assertEquals(5, bestBuyer.askedForABuyOffer(UndifferentiatedGoodType.GENERIC));

    toTest.reactToFailure(buyer5, PurchaseResult.SUCCESS);

    bestBuyer = toTest.getBestInSampleBuyer();
    assertEquals(buyer4, bestBuyer);
    assertEquals(40, bestBuyer.askedForABuyOffer(UndifferentiatedGoodType.GENERIC));
  }
Example #3
0
  /**
   * This method is called whenever two agents agree on an exchange. It can be called by the market
   * or the agents themselves.
   *
   * @param buyer the buyer
   * @param seller the seller
   * @param good the good being exchanged
   * @param price the price
   */
  public PurchaseResult trade(
      EconomicAgent buyer,
      EconomicAgent seller,
      Good good,
      int price,
      Quote buyerQuote,
      Quote sellerQuote) {
    Preconditions.checkArgument(buyer != seller, "buyer and seller are the same person!");
    assert getBuyers().contains(buyer) : buyer.toString() + " ----- " + buyers;
    assert getSellers().contains(seller);
    double sellerCost =
        good.getLastValidPrice(); // record the price of the good BEFORE it is traded, so we can
    // learn its markup

    PurchaseResult result = policy.trade(buyer, seller, good, price, buyerQuote, sellerQuote, this);

    if (result == PurchaseResult.SUCCESS) {

      // record
      lastPrice = price;
      lastMarkup = (price - sellerCost) / sellerCost;
      todaySumOfClosingPrices += price;
      lastFilledAsk = sellerQuote.getPriceQuoted();
      lastFilledBid = buyerQuote.getPriceQuoted();
      weeklyVolume++;
      todayVolume++;

      // tell the listeners!
      for (TradeListener tl : tradeListeners)
        tl.tradeEvent(buyer, seller, good, price, sellerQuote, buyerQuote);

      // if there is GUI tell the records
      if (MacroII.hasGUI()) {
        // record it on the timeline
        // todo logtodo

        // register it on the network!
        registerTradeOnNetwork(seller, buyer, good.getType(), 1);
      }
    }

    return result;
  }
Example #4
0
 /**
  * utility method for summing up all consumption by all buyers
  *
  * @return
  */
 public int countTodayConsumptionByRegisteredBuyers() {
   int sum = 0;
   for (EconomicAgent a : buyers) sum += a.getTodayConsumption(getGoodType());
   return sum;
 }
Example #5
0
 public int sumSupplyGaps() {
   int sum = 0;
   for (EconomicAgent a : sellers) sum += a.estimateSupplyGap(getGoodType());
   return sum;
 }
Example #6
0
 public int sumDemandGaps() {
   int sum = 0;
   for (EconomicAgent a : buyers) sum += a.estimateDemandGap(getGoodType());
   return sum;
 }
Example #7
0
 /**
  * utility method for summing up all production by all sellers
  *
  * @return
  */
 public int countTodayProductionByRegisteredSellers() {
   int sum = 0;
   for (EconomicAgent a : sellers) sum += a.getTodayProduction(getGoodType());
   return sum;
 }
Example #8
0
 /**
  * utility method for summing up all production by all sellers
  *
  * @return
  */
 public int countTodayInventoryByRegisteredBuyers() {
   int sum = 0;
   for (EconomicAgent a : buyers) sum += a.hasHowMany(goodType);
   return sum;
 }