/* * (c) 1999 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 trades once * with each of the neighbors. Methods for making * and accepting offers are attributes of the trading agents, themselves. * * @author Alan Lockard * @version 1.0 */ public class TradeT4 extends TradeT2 {    Exchange2 exchange;    int response;    NegotiatingAgent self, other, temp;    SpiceAgent debugo;    float price;    float offerList[] = new float[Exchange2.MAXNUMOFFERS+1];    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)];        big_loop: for (int i = 0; i < series.length; i++) {             int tradeIteration = 0;            self = (NegotiatingAgent) agent;            other = (NegotiatingAgent) neighbors[i];	    exchange = new Exchange2 ((SpiceAgent) self, (SpiceAgent) other);	    response = ((NegotiatingAgent) neighbors[i]).offerResponse ((Exchange2) exchange);	    if (response == Exchange2.ACCEPT) {//System.out.println("1 selfMRS = " + exchange.selfExAnteMRS + ", pMRS = " + exchange.partnerExAnteMRS + ", isBS = " + exchange.isBuyingSugar() + ", price = " + exchange.price + ", response = " + response);		executeTrade((Exchange) exchange);//System.out.println("1    ACCEPTED");		continue big_loop;            }            else if (response == Exchange2.REJECT) {//System.out.println("1    REJECTED");		continue big_loop; // offer rejected outright	    }            while (response == Exchange2.COUNTER){ // counter offer made                price = other.counterOffer (exchange);//System.out.print("+ CounterOffer returned " + price + ", exchange.price = " + exchange.price);		offerList[tradeIteration] = exchange.price;//System.out.println(" offerList[" + tradeIteration + "] = " + exchange.price);                tradeIteration++;                temp = self;                self = other;                other = temp;                exchange = new Exchange2 (self, other, price, offerList, tradeIteration);                response = ((NegotiatingAgent) neighbors[i]).offerResponse ((Exchange2) exchange);//System.out.println("2 response = " + response + " iteration = " + tradeIteration);//int k = tradeIteration-1;//System.out.println("2   selfMRS = " + exchange.selfExAnteMRS + ", pMRS = " + exchange.partnerExAnteMRS + ", isBS = " + exchange.isBuyingSugar() + ", price = " + exchange.price + ", last offer[" + k + "] = " + offerList[k] + ", response = " + response + ", cntrofr = " + other.counterOffer (exchange));            }	        if (response == Exchange2.ACCEPT) {		        executeTrade((Exchange) exchange);//System.out.println("2      ACCEPTED");//for (int k = 0; k < tradeIteration; k++) {//System.out.println("   " + exchange.offerList[k]);//}//System.out.println("final price = " + exchange.price);		        continue big_loop;            }            else {//System.out.println("2      REJECTED");		continue big_loop; // final offer rejected outright	    }        }    }}
