/* * (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 solicits offers from all partners, takes best  * offer, and counteroffers remaining partners the chance to * trade on those same, most favorable terms * * @author Alan Lockard * @version 1.0 */ public class TradeT3 extends TradeT2 {    Exchange2 exchanges[] = new Exchange2[4]; // max number Von Neuman neighbors    Exchange2 exchange;    public void execute(Agent agent) {        Cell[] neighbors = ((CellOccupant) agent).getNeighborsOnHost();        // If only 1 neighbor, life is easy        if (neighbors.length == 1) {	    exchange = new Exchange2(((SpiceAgent) agent), ((SpiceAgent) neighbors[0]));	    if (((SpiceAgent) neighbors[0]).acceptOffer ((Exchange) exchange)) {		executeTrade((Exchange) exchange);            }	}	else { // Find best offer. Note that order is irrelevant, needn't be randomized.	    int imax = -1;	    float maxDif = 0;	    float transDif;	    for (int i = 0; i < neighbors.length; i++) {		exchanges[i] = new Exchange2(((SpiceAgent) agent), ((SpiceAgent) neighbors[i]));		transDif = (float) Math.abs((exchanges[i]).logPrice - Math.log((exchanges[i]).selfExAnteMRS));		if (transDif > maxDif) {		    maxDif = transDif;		    imax = i;		}	    }	    if (imax >= 0) { // At least one beneficial trade found                // Deal with trading partner offering best price	        if (((SpiceAgent) neighbors[imax]).acceptOffer ((Exchange) exchanges[imax])) {		    executeTrade((Exchange) exchanges[imax]);                }	        // give trading partners a chance to match the best price	        for (int i = 0; i < neighbors.length; i++) {		    if (i == imax) continue;		    exchange = new Exchange2((SpiceAgent) agent, (SpiceAgent) neighbors[i], (exchanges[imax]).price);	            if (((SpiceAgent) neighbors[i]).acceptOffer ((Exchange) exchange)) {		        executeTrade((Exchange) exchange);		    }                }	    }	}    }   	   }
