Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mrd/models/simple/Pair.java


1   /*
2    * Copyright (C) 2002-2003, Mark Diggory
3    *
4    * This file is part of the CAGN model project.
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. License
19   * information is also available at http://www.gnu.org.
20   *
21   */
22  package org.mrd.models.simple;
23  
24  import org.mrd.random.Bernoulli;
25  import org.mrd.repast.landscape.*;
26  import edu.cornell.lassp.houle.RngPack.RandomElement;
27  import java.awt.Color;
28  import java.awt.Point;
29  import uchicago.src.sim.engine.Stepable;
30  import uchicago.src.sim.gui.*;
31  import cern.jet.random.*;
32  import cern.jet.random.Uniform;
33  import org.apache.commons.pool.*;
34  import org.apache.commons.logging.*;
35  
36  /** Represents a Pair in the CAGN model. A pair is an abstraction in the model where
37   * only females are tracked. This makes an assumption that sex ratios in the model
38   * are always equal. This class encapsulates the behavior of pairs, they need to
39   * get tested for survival and reproduction during each iteration of the model.
40   *
41   * @author Mark Diggory <mdiggory@latte.harvard.edu>
42   */
43  
44  public class Pair extends LandscapeAgent implements Stepable {
45      
46      protected Log log = LogFactory.getLog(this.getClass().getName());
47      
48      /** Holds value of property survival (Survival Bernoulli Trail Generator). */
49      protected Bernoulli survival;
50      
51      /** Holds value of property reproduction (Reproduction Bernoulli Trail Generator). */
52      protected Bernoulli reproduction;
53      
54      /** Holds value of property distance (Dispersal Distance Poisson Trail Generator). */
55      protected Poisson dispersalDistance;
56      
57      /** Holds value of property angle (Dispersal Angle Generator). */
58      protected Uniform dispersalAngle;
59      
60      /** Holds value of property searchTime. */
61      protected int searchTime;
62      
63      /** Holds value of property clutchSize. */
64      protected int clutchSize;
65      
66      /** Holds value of property actualDisperal. */
67      private double currentDispersalDist;
68      
69      /** Holds value of property currentAge. */
70      protected int currentAge;
71      
72      /** Holds value of property pool. */
73      private ObjectPool pool;
74      
75      /** Constructs an new pair in the current model and landscape
76       * @param model The CagnModel
77       * @param landscape The Landscape
78       */
79      public Pair(Landscape landscape, RandomElement survivalGen, RandomElement reproductionGen, RandomElement dispersalAngleGen, RandomElement dispersalDistGen) {
80          super(landscape);
81          survival = new Bernoulli(1.0,survivalGen);
82          reproduction = new Bernoulli(1.0,reproductionGen);
83          dispersalAngle = new Uniform(0,1440,dispersalAngleGen);
84          dispersalDistance = new Poisson(1.0,dispersalDistGen);
85      }
86      
87      /** Searches for a dispersal point from the this agents current position.
88       *
89       * 1.) Used in the model to initialize the landscape without the risk of
90       * emigration from the landscape due to lack of available spaces.
91       *
92       * 2.) Used by fledgling to search for an adequate position to disperse to
93       *
94       * @return The new Point where the Pair can disperse to or null if a point could not be
95       * found in time.
96       */
97      public Point searchFrom(int current_x, int current_y){
98          Point best_pt = null;
99          Point new_pt = null;
100         int time = 0 ;
101         
102         while(time < searchTime){
103             
104             double new_angle = dispersalAngle.nextDouble();
105             double new_dist = dispersalDistance.nextDouble();
106             
107             new_pt = new Point((landscape.getSizeX() + ( current_x + (int)(Math.cos(new_angle)*new_dist))) % landscape.getSizeX(),
108             (landscape.getSizeY() + ( current_y + (int)(Math.sin(new_angle)*new_dist))) % landscape.getSizeY());
109             
110             if(landscape.getAgentAt(new_pt.x,new_pt.y) == null){
111                 double new_hqi = landscape.getResourceValueAt((int)new_pt.getX(),(int)new_pt.getY());
112                 double best_hqi = -1;
113                 if(best_pt != null)
114                     best_hqi = landscape.getResourceValueAt((int)best_pt.getX(),(int)best_pt.getY());
115                 
116                 if(new_hqi > best_hqi){
117                     best_pt = new_pt;
118                     currentDispersalDist = new_dist;
119                 }
120             }
121             time++;
122         }
123         return best_pt;   
124     }
125     
126     /** Used by fledgling pair to disperse to its new lication. This method allows the
127      * pair to use its own disperse method to determine the dispersal point instead of
128      * its parents disperse method. If the fledgling fails to locate a viable position
129      * it is removed from the model.
130      *
131      * @param parent_x the x location of the parent
132      *
133      * @param parent_y the y location of the parent
134      */
135     public void disperse(int parent_x, int parent_y) throws java.lang.Exception{
136         
137         //this.x = parent_x;
138         //this.y = parent_y;
139         
140         Point best_pt = this.searchFrom(parent_x,parent_y);
141         landscape.putAgentAt((int)best_pt.getX(),(int)best_pt.getY(),this);
142     }
143     
144     /** Step iterates this agent, behaviors this agent will perform are executed here.
145      * step() is called by the scheduler once per tick.
146      */
147     public void step() {
148         
149         try{
150             /** test my survival */
151             if(!survival.nextBoolean()){
152                 stage = DEAD;
153             }else{
154                 /** test my reproduction */
155                 if(reproduction.nextBoolean()){
156                     for(int i = 0;i < clutchSize;i++){
157                         /* the fledgling attempts to disperse, if it fails 
158                          there is and exception thrown and it is returned to the pool */
159                         Pair fledge = (Pair)pool.borrowObject();
160                         try{
161                             fledge.disperse(x,y);
162                         }catch(Exception ex){
163                             pool.returnObject(fledge);
164                         }
165                     }
166                 }
167                 /** increase my age */
168                 currentAge++;
169             }
170         }catch(Exception e){
171             log.error(e.getMessage(),e);
172         }
173     }
174     
175     /** Getter for property currentAge.
176      * @return int value representing this agents current age. */
177     public int getCurrentAge() {
178         return currentAge;
179     }
180     
181     /** Setter for property currentAge.
182      * @param currentAge value to adjust the age of this pair to.
183      */
184     public void setCurrentAge(int currentAge) {
185         this.currentAge = currentAge;
186     }
187     
188     /** Getter for property survivalProbability.
189      * @return Value of property survivalProbability.
190      */
191     public double getSurvivalProbability() {
192         return survival.getSuccessProbability();
193     }
194     
195     /** Setter for property survivalProbability.
196      * @param survivalProbability New value of property survivalProbability.
197      */
198     public void setSurvivalProbability(double successProbability) {
199         survival.setSuccessProbability(successProbability);
200     }
201     
202     
203     /** Getter for property clutchSize.
204      * @return Value of property clutchSize.
205      */
206     public int getClutchSize() {
207         return clutchSize;
208     }
209     
210     /** Setter for property clutchSize.
211      * @param clutchSize New value of property clutchSize.
212      */
213     public void setClutchSize(int clutchSize) {
214         this.clutchSize = clutchSize;
215     }
216     
217     /** Getter for property reproductionProbability.
218      * @return Value of property reproductionProbability.
219      */
220     public double getReproductionProbability() {
221         return reproduction.getSuccessProbability();
222     }
223     
224     /** Setter for property reproductionProbability.
225      * @param reproductionProbability New value of property reproductionProbability.
226      */
227     public void setReproductionProbability(double successProbability) {
228         reproduction.setSuccessProbability(successProbability);
229     }
230     
231     /** Getter for property searchTime.
232      * @return Value of property searchTime.
233      */
234     public int getSearchTime() {
235         return searchTime;
236     }
237     
238     /** Setter for property searchTime.
239      * @param searchTime New value of property searchTime.
240      */
241     public void setSearchTime(int searchTime) {
242         this.searchTime = searchTime;
243     }
244     
245     
246     /** Setter for property dispersalDistance.
247      * @param randomGenerator Generator used to generate dispersal distances.
248      * @param dispersalDistance New value of property dispersalDistance.
249      */
250     public void setMeanDispersalDistance(double meanDispersalDistance) {
251         dispersalDistance.setMean(meanDispersalDistance);
252     }
253     
254     /** Used by the GUI to Draw this Agent on the Landscape Grid.
255      * draw() is called whenever the display is updated, assuming the agent
256      * is part of what is being displayed.
257      *
258      * @param g The SimGraphics Object that will be drawing this Agent.
259      */
260     public void draw(SimGraphics g) {
261         g.drawFastRoundRect(new Color(Color.HSBtoRGB(currentAge * (float) .05,(float)1.0, (float)1.0)));
262     }
263     
264     /** Getter for property actualDisperal.
265      * @return Value of property actualDisperal.
266      */
267     public double getCurrentDispersalDist() {
268         return this.currentDispersalDist;
269     }
270     
271     /** Getter for property pool.
272      * @return Value of property pool.
273      */
274     public ObjectPool getPool() {
275         return this.pool;
276     }
277     
278     /** Setter for property pool.
279      * @param pool New value of property pool.
280      */
281     public void setPool(ObjectPool pool) {
282         this.pool = pool;
283     }
284     
285 }