1: package agent2d;

  3: import swarm.objectbase.SwarmImpl;
  4: import swarm.objectbase.Swarm;
  5: import swarm.activity.Activity;
  6: import swarm.activity.Schedule;
  7: import swarm.activity.ScheduleImpl;
  8: import swarm.defobj.Zone;
  9: import swarm.gui.Raster;

 11: import java.util.Hashtable;
 12: import java.util.Iterator;

 14: import swarm.Selector;
 15: import swarm.Globals;

 17: import Organization;
 18: import ObserverSwarm;

 20: public class Alex2d extends SocialAgent2d {
 21:   Hashtable people;
 22:   int xmean, ymean;
 23:   double xdeviation, ydeviation;

 25:   class Location {
 26:     int x;
 27:     int y;

 29:     Location (int x, int y) {
 30:       this.x = x;
 31:       this.y = y;
 32:     }
 33:   }

 35:   public Alex2d (Zone aZone, Organization org, int x, int y) {
 36:     super (aZone, org, x, y, 2, 4);

 38:     people = new Hashtable (10);
 39:   }

 41:   void noteOpinion (Agent2d neighbor) {
 42:     Location location = (Location) people.get (neighbor);
 43:     
 44:     if (location != null)
 45:       {
 46:         location.x = neighbor.x;
 47:         location.y = neighbor.y;
 48:       }
 49:     else
 50:       {
 51:         location = new Location (neighbor.x, neighbor.y);
 52:         people.put (neighbor, location);
 53:       }
 54:   }

 56:   int computeCentroidDirection () {
 57:     Iterator iterator = people.values ().iterator ();
 58:     int xsum = 0, ysum = 0, xsumsq = 0, ysumsq = 0;
 59:     int count = 0;
 60:     double xm, ym;
 61:     
 62:     while (iterator.hasNext ()) {
 63:       Location location = (Location) iterator.next ();
 64:       
 65:       xsum += location.x;
 66:       ysum += location.y;
 67:       xsumsq += location.x * location.x;
 68:       ysumsq += location.y * location.y;
 69:       count++;
 70:     }
 71:     if (count == 0)
 72:       xm = ym = 0;
 73:     else {
 74:       xm = (double) xsum / (double) count;
 75:       ym = (double) ysum / (double) count;
 76:     }
 77:     xmean = (int) xm;
 78:     ymean = (int) ym;

 80:     if (count > 1) {
 81:       xdeviation =
 82:         Math.sqrt ((double) count / (double) (count - 1) *
 83:                    ((double) xsumsq / (double) count - xm * xm));
 84:       ydeviation =
 85:         Math.sqrt ((double) count / (double) (count - 1) *
 86:                    ((double) ysumsq / (double) count - ym * ym));
 87:     } else {
 88:       xdeviation = ydeviation = 0.0;
 89:     }
 90:         
 91: ;
 92:     return (int) Math.toDegrees (Math.atan2 ((double) (ymean - y),
 93:                                              (double) (xmean - x)));
 94:   }
 95:   
 96:   public void stepSocialAgent (Agent2d neighbor) {
 97:     if (neighbor == null)
 98:       color = ObserverSwarm.AlexTourColor;
 99:     else {
100:       noteOpinion (neighbor);
101:       neighbor.frob (computeCentroidDirection ());
102:       color = ObserverSwarm.AlexTalkColor;
103:     }
104:     randomWalk ();
105:   }

107:   public Object drawSelfOn (Raster r) {
108:     r.ellipseX0$Y0$X1$Y1$Width$Color
109:       (xmean - (int) xdeviation, ymean - (int) ydeviation,
110:        xmean + (int) xdeviation, ymean + (int) ydeviation, 
111:        1,
112:        ObserverSwarm.AlexTargetColor);
113:     super.drawSelfOn (r);
114:     return this;
115:   }
116: }