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

Quick Search    Search Deep

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


1   /*
2    * Pair.java
3    *
4    * Created on July 8, 2002, 9:14 PM
5    */
6   package org.mrd.examples.simple;
7   
8   import org.mrd.random.Bernoulli;
9   import org.mrd.model.Agent;
10  import org.mrd.model.Landscape;
11  
12  import java.awt.Color;
13  import java.awt.Point;
14  import uchicago.src.sim.gui.*;
15  import uchicago.src.sim.gui.ColorMap;
16  import cern.jet.random.*;
17  import cern.jet.random.Uniform;
18  
19  /** Represents a Pair in the CAGN model. A pair is an abstraction in the model where
20   * only females are tracked. This makes an assumption that sex ratios in the model
21   * are always equal. This class encapsulates the behavior of pairs, they need to
22   * get tested for survival and reproduction during each iteration of the model.
23   *
24   * @author Mark Diggory <mdiggory@latte.harvard.edu>
25   */
26  
27  public class Pair extends Agent{
28      
29      /** Holds value of property currentAge. */
30      private int currentAge;
31  
32      /** Holds value of property survival (Survival Bernoulli Trail Generator). */
33      private Bernoulli survival;
34      
35      /** Holds value of property reproduction (Reproduction Bernoulli Trail Generator). */
36      private Bernoulli reproduction;
37      
38      /** Holds value of property distance (Dispersal Distance Poisson Trail Generator.. */
39      private Poisson distance;
40      
41      /** Holds value of property dispersalDistance. */
42      private double dispersalDistance;
43      
44      /** Holds value of property searchTime. */
45      private int searchTime;
46      
47      /** Holds value of property clutchSize. */
48      private int clutchSize;
49      
50      private Uniform angles;
51      /** Constructs a New Pair in this Landscape and Model/
52       */
53      public Pair() {
54          super();
55      }
56      
57      /** Cleans up this object when it is removed from the simulation
58       * @throws Throwable if anything goes wrong.
59       */
60      protected void finalize() throws Throwable {
61          landscape = null;
62          reproduction = null;
63          survival = null;
64          super.finalize();
65      }
66      
67      public Point disperse(){
68          
69          Point best_pt = null;
70          Point new_pt = null;
71          Agent p = null;
72          
73          int time = 0 ;
74          
75          while(time < getSearchTime()){
76              
77              double angle = angles.nextDouble();
78              double new_dist = distance.nextDouble();
79              
80              
81              new_pt = new Point((landscape.getSizeX() + ( x + (int)(Math.cos(angle)*new_dist))) % landscape.getSizeX(),
82              (landscape.getSizeY() + ( y + (int)(Math.sin(angle)*new_dist))) % landscape.getSizeY());
83              
84              p = landscape.getAgentAt(new_pt.x,new_pt.y);
85              
86              if(p == null){
87                  double new_hqi = landscape.getResourceValueAt((int)new_pt.getX(),(int)new_pt.getY());
88                  double best_hqi = -1;
89                  if(best_pt != null)
90                      best_hqi = landscape.getResourceValueAt((int)best_pt.getX(),(int)best_pt.getY());
91                  
92                  if(new_hqi > best_hqi){
93                      best_pt = new_pt;
94                  }
95              }
96              
97              time++;
98          }
99          
100         return best_pt;
101     }
102 
103     /** Step iterates this agent, behaviors this agent will perform are executed here.
104      * step() is called by the scheduler once per tick.
105      */
106     public void step() {
107         
108         /** test my survival */
109         if(!survival.nextBoolean()){
110             stage = DEAD;
111         }
112         
113         /** test my reproduction */
114         if(stage > DEAD){
115             if(reproduction.nextBoolean()){
116                 for(int i = 0;i < clutchSize;i++){
117                     
118                     /** try to find open habitat. */
119                     Point point = this.disperse();
120                     
121                     /** if we can't find open space in searchTime than agent doesn't get created */
122                     if( point != null ){
123                         Pair fledge = (Pair)getModel().buildAgent(Pair.class);
124                         landscape.putAgentAt((int)point.getX(),(int)point.getY(),fledge);
125                     }
126                 }
127             }
128             /** increase my age */
129             currentAge++; 
130         }
131     }
132     
133     /** Getter for property currentAge.
134      * @return int value representing this agents current age. */
135     public int getCurrentAge() {
136         return currentAge;
137     }
138     
139     /** Setter for property currentAge.
140      * @param currentAge value to adjust the age of this pair to.
141      */
142     public void setCurrentAge(int currentAge) {
143         this.currentAge = currentAge;
144     }
145     
146     /** Getter for property survivalProbability.
147      * @return Value of property survivalProbability.
148      */
149     public double getSurvivalProbability() {
150         if(survival != null){
151             return survival.getSuccessProbability();
152         }
153         return -1;
154     }
155 
156     /** Setter for property survivalProbability.
157      * @param survivalProbability New value of property survivalProbability.
158      */
159     public void setSurvivalProbability(double survivalProbability, edu.cornell.lassp.houle.RngPack.RandomElement randomGenerator) {
160        survival = new Bernoulli(survivalProbability,randomGenerator);
161     }
162     
163     /** Getter for property clutchSize.
164      * @return Value of property clutchSize.
165      */
166     public int getClutchSize() {
167         return clutchSize;
168     }
169     
170     /** Setter for property clutchSize.
171      * @param clutchSize New value of property clutchSize.
172      */
173     public void setClutchSize(int clutchSize) {
174         this.clutchSize = clutchSize;
175     }
176     
177     /** Getter for property reproductionProbability.
178      * @return Value of property reproductionProbability.
179      */
180     public double getReproductionProbability() {
181         if(reproduction != null)
182             return reproduction.getSuccessProbability();
183         
184         return -1;
185     }
186 
187     /** Setter for property reproductionProbability.
188      * @param reproductionProbability New value of property reproductionProbability.
189      */
190     public void setReproductionProbability(double reproductionProbability, edu.cornell.lassp.houle.RngPack.RandomElement randomGenerator) {
191         reproduction = new Bernoulli(reproductionProbability,randomGenerator);
192     }
193 
194     /** Getter for property searchTime.
195      * @return Value of property searchTime.
196      */
197     public int getSearchTime() {
198         return searchTime;
199     }
200     
201     /** Setter for property searchTime.
202      * @param searchTime New value of property searchTime.
203      */
204     public void setSearchTime(int searchTime) {
205         this.searchTime = searchTime;
206     }
207     
208     /** Getter for property dispersalDistance.
209      * @return Value of property dispersalDistance.
210      */
211     public double getDispersalDistance() {
212         return this.dispersalDistance;
213     }
214     
215     /** Setter for property dispersalDistance.
216      * @param dispersalDistance New value of property dispersalDistance.
217      */
218     public void setDispersalDistance(double dispersalDistance, edu.cornell.lassp.houle.RngPack.RandomElement randomGenerator) {
219         distance = new Poisson(dispersalDistance,randomGenerator);
220         this.dispersalDistance = dispersalDistance;
221     }
222     
223     /** Setter for property dispersalDistance.
224      * @param dispersalDistance New value of property dispersalDistance.
225      */
226     public void setDispersalAngleGenerator(edu.cornell.lassp.houle.RngPack.RandomElement randomGenerator) {
227         this.angles = new Uniform(0,1440,randomGenerator);
228     }
229 
230     
231     /** Used by the GUI to Draw this Agent on the Landscape Grid.
232      * draw() is called whenever the display is updated, assuming the agent
233      * is part of what is being displayed.
234      *
235      * @param g The SimGraphics Object that will be drawing this Agent.
236      */
237     public void draw(SimGraphics g) {
238         g.drawFastRoundRect(new Color(Color.HSBtoRGB(currentAge * (float) .05,(float)1.0, (float)1.0)));
239     }
240     
241 }