/* * (c) 1998-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. */package edu.brook.sugarscape;import java.lang.*;import java.util.*;import java.awt.Color;import edu.brook.ascape.model.*;import edu.brook.ascape.rule.*;import edu.brook.ascape.util.*;/** * A basic sugarscape citizen. Provides basic funtionality for sugarscape agents * as well as all desired functionality that could be included in base class without * compromising good design or supporting unnecessary member variables. * Despite the relativly large size of this class it is actually * quite simple; much of the code is simply getters and setters for various  * initialization paramaters. *  * @author Miles T. Parker * @version 1.0 */public class SugarAgent extends CellOccupant {        public final static Rule DEATH_STARVATION_RULE = new Rule("Death Starvation") {        public void execute(Agent agent) {            if ((((SugarAgent) agent).getSugar().getStock() <= 0)) {                agent.die();            }        }    };        public final static Rule DEATH_STARVATION_OLD_AGE_RULE = new Rule("Death Starvation or Old Age") {        public void execute(Agent agent) {            if ((((SugarAgent) agent).getSugar().getStock() <= 0) || (((SugarAgent) agent).getAge() >= ((SugarAgent) agent).getDeathAge())) {                agent.die();            }        }    };    public final static Rule DEATH_REPLACEMENT_RULE = new Rule("Death Replacement") {        public void execute(Agent agent) {            if ((((SugarAgent) agent).getSugarStock() <= 0) || (((SugarAgent) agent).getAge() >= ((SugarAgent) agent).getDeathAge())) {                agent.die();                Agent newAgent = ((ScapeVector) agent.getScape()).newAgent();            }        }    };    public final static Rule HARVEST_RULE = new Rule("Harvest") {        public void execute(Agent agent) {            ((SugarAgent) agent).harvest();        }    };    //Agent attributes    protected int age;    protected int vision;    protected int sugarMetabolism;    protected int deathAge;        protected boolean initialColorRandom;	protected Color color;    public CommodityStock sugar;		/**	 * Begining population values. Vision is random draw, coordinate placement is random	 * in scape.	 */	public void initialize() {	    super.initialize();        sugar = new CommodityStock();        sugar.setName("sugar");        sugar.setOwner(this);	    Cell cell = getHostScape().findRandomUnoccupiedCell();	    if (cell != null) {            moveTo((HostCell) cell);        }        setDeathAge(randomInRange(((GAS_Base) getRoot()).getMinDeathAge(), ((GAS_Base) getRoot()).getMaxDeathAge()));        setAge(randomInRange(0, getDeathAge()));        setVision(randomInRange(((GAS_Base) getRoot()).getMinVision(), ((GAS_Base) getRoot()).getMaxVision()));        setSugarMetabolism(randomInRange(((GAS_Base) getRoot()).getMinSugarMetabolism(), ((GAS_Base) getRoot()).getMaxSugarMetabolism()));        setSugar(randomInRange(((GAS_Base) getRoot()).getMinInitialSugar(), ((GAS_Base) getRoot()).getMaxInitialSugar()));        if (!initialColorRandom) {            if (getColor() == null) {                setColor(Color.red);            }        }        else { //Random initial color requested        	color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random());        }	}	    /**     * Directs agents to assign themselves a random color at initialization.     */    public void setInitialColorRandom(boolean initialColorRandom) {        this.initialColorRandom = initialColorRandom;    }        public void scapeCreated() {        StatCollector[] stats = {new StatCollectorCSAMM() {            public double getValue(Object object) {                return ((SugarAgent) object).getSugar().getStock();            }            public String getName() {                return "Sugar";            }        },        new StatCollectorCSAMM() {            public double getValue(Object object) {                return -((SugarAgent) object).getAge();            }            public String getName() {                return "Age";            }        },        new StatCollectorCSAMM() {            public double getValue(Object object) {                return ((SugarAgent) object).getVision();            }            public String getName() {                return "Vision";            }        },        new StatCollectorCSAMM() {            public double getValue(Object object) {                return ((SugarAgent) object).getSugarMetabolism();            }            public String getName() {                return "Sugar Metabolism";            }        }};        scape.addStatCollectors(stats);    }        public void harvest() {        putSugar(((SugarCell) getHostCell()).takeSugar());    }        public void movement() {        Cell[] visibleCells = getHostScape().getCellsNear(getHostCell(), getVision(), true);        float bestValue = ((SugarCell) visibleCells[0]).getPerceivedValue();        if (bestValue != SugarCell.MAX_SUGAR) {            for (int i = 1; i < visibleCells.length; i++) {                if (((SugarCell) visibleCells[i]).getPerceivedValue() > bestValue && ((HostCell) visibleCells[i]).isAvailable()) {                    bestValue = ((SugarCell) visibleCells[i]).getPerceivedValue();                    if (bestValue == SugarCell.MAX_SUGAR) {                        break;                    }                }            }        }        if (((SugarCell) visibleCells[0]).getPerceivedValue() != bestValue) {            SugarCell sugar = null;            for (int i = 1; i <= getVision(); i++) {                int series = randomToLimit(24);                for (int j = 0; j < 4; j++) {                    SugarCell current = (SugarCell) visibleCells[(i * 4 - Utility.uniqueSeries[4][series][j]) + 1];                    if ((current.getPerceivedValue() == bestValue) && current.isAvailable()) {                        moveTo(current);                        return;                    }                }            }        }    }                public void metabolism() {        sugar.reduceStock(sugarMetabolism);        age++;    }        public void putSugar(float sucrose) {		sugar.putStock(sucrose);    }        public float getSugarStock() {        return sugar.getStock();    }    public CommodityStock getSugar() {        return sugar;    }    public void setSugar(float amount) {        sugar.setStock(amount);    }        public float takeSugar() {	    return sugar.takeStock();    }        public float takeSugar(float amount) {	    return sugar.takeStock(amount);    }        public void addSugar(float amount) {        this.sugar.addStock(amount);    }        public int getVision() {        return vision;    }        public void setVision(int vision) {        this.vision = vision;    }        public int getSugarMetabolism() {        return sugarMetabolism;    }        public void setSugarMetabolism(int metabolism) {        this.sugarMetabolism = metabolism;    }        public int getAge() {        return age;    }        public void setAge(int age) {        this.age = age;    }        public int getDeathAge() {        return deathAge;    }        public void setDeathAge(int deathAge) {        this.deathAge = deathAge;    }        public Color getColor() {        return color;    }        public void setColor(Color color) {        this.color = color;    }}
