/* * (c) 1999-2000 The Brookings Institution All Rights Reserved * * Permission to use this software and its documentation for non-commercial * purposes and without fee is hereby granted, provided this copyright statement * is included. Please contact us for permission for redistribution and other uses. *  * BROOKINGS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,  * OR NON-INFRINGEMENT. BROOKINGS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY * LICENSEE AS A RESULT OF USING THIS SOFTWARE OR ITS DERIVATIVES. * * _Sugarscape_ * See _Growing Artificial Societies: Social Science from the Ground Up_ * Brookings Institution Press, The MIT Press * Joshua Epstein * jepstein@brook.edu * Robert Axtell * raxtell@brook.edu * * Miles Parker * mparker@brook.edu * http://www.brook.edu/es/dynamics/models/ascape * The Brookings Institution * Washington, D.C. *  * Alan Lockard * alockard@gmu.edu */package edu.brook.sugarscape;import java.lang.*;import edu.brook.ascape.model.*;import edu.brook.ascape.rule.*;import edu.brook.ascape.util.*;/** * This trade rule orders neighbors randomly, and repeatedly trades  * with each of the neighbors until potential gains from trade are * exhausted, then moves on to the next neighbor. Methods for making * and accepting offers are attributes of the trading agents, themselves. * * @author Alan Lockard * @version 1.0 */ public class TradeT extends Rule {    public TradeT() {        super("Trade");    }    Exchange exchange;    boolean offerIsAccepted;    public void execute(Agent agent) {        Cell[] neighbors = ((CellOccupant) agent).getNeighborsOnHost();        //pick a random series from the set of series that match the number of neighbors        //(Assumes von Neumann neighborhood, otherwise creates error)        int[] series = Utility.uniqueSeries[neighbors.length][randomToLimit(Utility.uniqueSeries[neighbors.length].length)];        for (int i = 0; i < series.length; i++) { 	    exchange = new Exchange(((SpiceAgent) agent), ((SpiceAgent) neighbors[i]));	    while (((SpiceAgent) neighbors[i]).acceptOffer (exchange)) {		executeTrade(exchange); 		exchange = new Exchange(((SpiceAgent) agent), ((SpiceAgent) neighbors[i]));            }        }    }    public void executeTrade (Exchange exchange) {//System.out.println ("before self= " + (exchange.self).getSugarStock() + " " + (exchange.self).getSpiceStock() + " partner= " + (exchange.partner).getSugarStock() + " " + (exchange.partner).getSpiceStock());        (exchange.self).setSugar(exchange.selfExPostSugar);        (exchange.self).setSpice(exchange.selfExPostSpice);        (exchange.partner).setSugar(exchange.partnerExPostSugar);        (exchange.partner).setSpice(exchange.partnerExPostSpice);	scape.getData().getStatCollector("Trades").addValue(0.0);	scape.getData().getStatCollector("Price").addValue(exchange.price);	scape.getData().getStatCollector("Log Price").addValue(exchange.logPrice);	scape.getData().getStatCollector("Self Gains from Trade").addValue(exchange.selfGainsFromTrade);	scape.getData().getStatCollector("Partner Gains from Trade").addValue(exchange.partnerGainsFromTrade);//System.out.println("T p=" + exchange.price + ", Qu= " + exchange.sugarQuantity //	+ ", Qi= " + exchange.spiceQuantity + ", " + exchange.isBuyingSugar() + " gain= " //	+ exchange.selfGainsFromTrade + " " +  exchange.partnerGainsFromTrade);//System.out.println ("after self= " + (exchange.self).getSugarStock() + " " + (exchange.self).getSpiceStock() + " partner= " + (exchange.partner).getSugarStock() + " " + (exchange.partner).getSpiceStock());    }    public boolean isRandomExecution() {        return false;    }            public boolean isCauseRemoval() {        return false;    }    }
