Courses/CS 461/Winter 2006/Darren Iwaki/week 2

From CSWiki

Jump to: navigation, search

Contents

[edit] BIOLOGY \ FLOCKING

  • Emergent Phenomena

birds fly random and begin to fly together forming groups. The come close to forming a single group, but then break away into about 4 other groups, but never returning to the completely random motion of every bird in its own group.

  • Rules

1) birds align with another bird if the distance between them is less than the variable ‘minimum-separation’ 2) if there is no neighbor close enough, the bird turns away from the group or the ‘average-flockmate-heading’

  • Why

The birds tend to fly together because the search space is too small compared to the number of birds or the ‘minimum-separation’ is too low. If the ‘minimum-separation’ were higher then the pattern of the birds flying would be much more random.

[edit] BIOLOGY \ TERMITES

  • Emergent Phenomena

Termites: the termites make two piles of wood chips

  • Rules

The termites wander randomly and if they find a wood chip they pick it up. If the termites find another wood chip they find an empty space and put it down.

  • Why

Two piles form because the chips aren’t protected. If a termite starts making a pile another termite will pick up its chips. The center between the most amount of termites is where the piles will form. There are two piles because the piles are about the same size and the termites are just transferring chips between the two.

[edit] RABBIT GRASS WEEDS

  • Emergent Phenomena

The number of rabbits seems to remain the same when the weeds energy is set to zero and the weeds do not out number the amount of grass.

  • Rules

If the rabbit gains enough energy it reproduces. If the rabbit loses too much energy it dies.

  • Why

The rabbit population remains steady as long as the number of weeds *weeds energy is balanced with the number of grass * grass energy. If the weeds product outnumbers the grass product, the rabbit population decreases. If the grass product outnumbers the weed product, the rabbit population increases. The population continues to grow, but dips slightly because the chances of landing on a grass area that has not already been eaten decreases.

[edit] WolfSheepPredation-Mason

Found the learning curve to be really difficult. Had some time, but couldn't get the following things to work. Found out about Mason:

  • 1. Mason doesn't compile? I had to use 'javac' and compile the source code first. I couldn't 'add the libraries' to a java compiler like bluej. Thus I had no debugging help besides the compile-time errors.
  • 2. The simulations just stop if something is wrong.

Currently:

  • 1. can't figure out how to make new agents move. (example: if a sheep reproduces, the new sheep is seen, but the new sheep never moves)
  • 2. can't get rid of an agent. I used 'SparseGrid.remove(this)', but the agent still shows up.

Image:DarrenWolfSheepPredationDoc1.jpg Image:DarrenWolfsheepPredationDoc3.JPG

[edit] Source

grass.java package sim.app.wolfSheepPredation; import sim.engine.*; import sim.util.*; import ec.util.*; import java.util.*;


public class Grass implements Steppable {

   public int xdir;
   public int ydir;
   public int energy = 1;
     
   public Grass (int xdir, int ydir)
   {
       this.xdir = xdir;
       this.ydir = ydir;
   }
  //main body of simulator
   WolfSheepPredation wsp;
  
   public void step ( SimState state )
   {
       wsp = (WolfSheepPredation) state;
   
       Int2D location = wsp.entities.getObjectLocation(this);
       wsp.entities.setObjectLocation(this, location);
       die();
   }
   
   public void die ()
   {
       // die or not
       if ( energy <= 0 )  remove();
   }
   
   public void remove()
   {
       wsp.entities.remove(this);
   }  

}

sheep.java package sim.app.wolfSheepPredation; import sim.engine.*; import sim.util.*; import ec.util.*; import java.util.*;


/* A sheep */ public class Sheep implements Steppable {

   public boolean grabbed = false;         // sheep is grabbed by wolf ?
   public boolean randomize = true;        // always true.. not needed, left for added functionality
   public int xdir;                        // x coordinate of sheep
   public int ydir;                        // y coordinate of sheep
   public int sheepGainFromFood;           // how much energy sheep gains from food
   public int energy;                      // how much energy the sheep has
   public int sheepReproduce;              // % of sheep reproduce
   
   Random randomly = new Random(3); 
    
   public Sheep(int xdir, int ydir, int sheepGainFromFood, int sheepReproduce)
   {
       this.xdir = xdir;                              
       this.ydir = ydir;
       this.sheepGainFromFood = sheepGainFromFood;
       this.sheepReproduce = sheepReproduce;
       energy = randomly.nextInt ( 2 * sheepGainFromFood);
     
   }
   
   public void eatGrass( SimState state ) 
   {
       WolfSheepPredation wsp = (WolfSheepPredation) state;
       Bag items = wsp.entities.getObjectsAtLocation(xdir, ydir);
 
       if (items != null)
       {
           for ( int i = 0; i < items.size(); i ++)
           {
               if ( items.get(i) instanceof Grass)
               {
                   wsp.removeGrass((Grass)items.get(i));
                   ((Grass)items.get(i)).remove();
                   energy += sheepGainFromFood;
               }
               
           }
       }
   }
           
   
   public boolean isGrabbed() { return grabbed; }
   
   public void setAsGrabbed()
   { 
       grabbed = true; 
       remove ( );
   }
   
   public int getEnergy() { return energy; }
   
   public void reproduce ( SimState state )
   {
       Int2D location = wsp.entities.getObjectLocation(this);
       if (randomly.nextInt(100) < sheepReproduce) 
       {
           energy = energy/2;
           int newx = randomly.nextInt(3) - 1;
           int newy = randomly.nextInt(3) - 1;
           WolfSheepPredation wsp = (WolfSheepPredation) state;
           wsp.addSheep(newx + location.x, newy + location.y, sheepGainFromFood, sheepReproduce);
       }   
       
       
   }
   //main body of simulator
   WolfSheepPredation wsp;
   
   public void step ( SimState state )
   {
       wsp = (WolfSheepPredation) state;
   
       Int2D location = wsp.entities.getObjectLocation(this);
   
       if (randomize)
       {
           int degree = wsp.random.nextInt(50) - wsp.random.nextInt(50);
           if ((degree > -22.5) && (degree <= 22.5))   //straight right
           {
               xdir = 1;
               ydir = 0;
           }
           else if ((degree > 22.5) && (degree <= 67.5)) //right down
           {
               xdir = 1;
               ydir = -1;
           }
           else if ((degree > 67.5) && (degree <= 112.5))  //down
           {
               xdir = 0;
               ydir = 1;
           }
           else if ((degree > -67.5) && (degree <= -22.5)) //right up
           {
               xdir = 1;
               ydir = 1;
           }
           else if ((degree > -112.5) && (degree <= -67.5)) //up
           {
               xdir = -1;
               ydir = 0;
           }
           else
           {
               xdir = 0;
               ydir = 0;
           }
       }
       //move
       int newx = location.x + xdir;
       int newy = location.y + ydir;
       
       //if hits boundary loop to other side
       if (newx < 0) { newx = wsp.entities.getWidth()-1; }
       else if (newx >= wsp.entities.getWidth()) {newx=0;}
       if (newy < 0) { newy = wsp.entities.getHeight()-1; }
       else if (newy >= wsp.entities.getHeight()) {newy=0; }
       
       //set new location
       Int2D newloc = new Int2D(newx, newy);
       wsp.entities.setObjectLocation(this, newloc);
       reproduce( state );
       die();
   }
   
   public void die ()
   {
       // die or not
       if ( energy <= 0 )  remove();
   }
   
   public void remove()
   {
       wsp.entities.remove(this);
       wsp.removeSheep(this);
   } 

}

wolf.java package sim.app.wolfSheepPredation; import sim.engine.*; import sim.util.*; import java.util.*;

/* A wolf */ public class Wolf implements Steppable {

   public boolean randomize = true;
   public int xdir;
   public int ydir;       
   public int energy;
   Random randomly = new Random(3); 
   public int wolfGainFromFood;
   
   
   public Wolf(int xdir, int ydir, int wolfGainFromFood, int wolfReproduce)
   {
       this.xdir = xdir;
       this.ydir = ydir;
       energy = randomly.nextInt ( 2 * wolfGainFromFood);
       this.wolfGainFromFood = wolfGainFromFood;
   }
   
   public void eatSheep(SimState state)
   {
       WolfSheepPredation wsp = (WolfSheepPredation) state;
       Bag items = wsp.entities.getObjectsAtLocation(xdir, ydir);
 
       if (items != null)
       {
           for ( int i = 0; i < items.size(); i ++)
           {
               if ( items.get(i) instanceof Sheep)
               {
                   ((Grass)items.get(i)).remove();
                   wsp.removeSheep ( (Sheep)items.get(i));
                   energy += wolfGainFromFood;
               }
           }
       }
   }        
   
   
   public int getEnergy() { return energy; }
   
   WolfSheepPredation wsp ;
   public void step ( SimState state )
   {
       wsp = (WolfSheepPredation) state;
   
       Int2D location = wsp.entities.getObjectLocation(this);
   
       if (randomize)
       {
           int degree = wsp.random.nextInt(50) - wsp.random.nextInt(50);
           if ((degree > -22.5) && (degree <= 22.5))
           {
               xdir = 1;
               ydir = 0;
           }
           else if ((degree > 22.5) && (degree <= 67.5))
           {
               xdir = 1;
               ydir = -1;
           }
           else if ((degree > 67.5) && (degree <= 112.5))
           {
               xdir = 0;
               ydir = 1;
           }
           else if ((degree > -67.5) && (degree <= -22.5))
           {
               xdir = 1;
               ydir = 1;
           }
           else if ((degree > -112.5) && (degree <= -67.5))
           {
               xdir = -1;
               ydir = 0;
           }
           else
           {
               xdir = 0;
               ydir = 0;
           }
       }
       //move
       int newx = location.x + xdir;
       int newy = location.y + ydir;
       
       //if hits boundary loop to other side
       if (newx < 0) { newx = wsp.entities.getWidth()-1; }
       else if (newx >= wsp.entities.getWidth()) {newx=0;}
       if (newy < 0) { newy = wsp.entities.getHeight()-1; }
       else if (newy >= wsp.entities.getHeight()) {newy=0; }
       
       energy -= 1;
       //set new location
       Int2D newloc = new Int2D(newx, newy);
       wsp.entities.setObjectLocation(this, newloc);
       eatSheep( state );
       die();
   }
   
   public void die ()
   {
       // die or not
       if ( energy <= 0 )  remove();
   }
   
   public void remove()
   {
       wsp.entities.remove(this);
   }
   

}

wolfSheepPredation.java package sim.app.wolfSheepPredation; import sim.engine.*; import sim.field.grid.*; import sim.util.*; import ec.util.*; import java.io.*; import java.util.*;

public class WolfSheepPredation extends SimState {

   public SparseGrid2D entities;       //sheep and wolves
 
   static Bag sheepSet = new Bag();
   static Bag wolfSet = new Bag();
   static Bag grassSet = new Bag();
   public int gridWidth = 100;
   public int gridHeight = 100;
   public int initialNumOfSheep = 100;
   public int initialNumOfWolves = 50;       //number of wolves
   public boolean grassOn = true;
   public int sheepReproduce = 4;
   public int wolfReproduce = 0;
   public int sheepGainFromFood = 40 ;
   public int wolfGainFromFood = 20;
   public int grassRegrowthTime = 5 ;
  
   public boolean getGrassOn() { return grassOn; }
   public int getInitialNumOfWolves() { return initialNumOfWolves; }
   public int getInitialNumOfSheep() { return initialNumOfSheep; }
   public int getSheepReproduce() { return sheepReproduce; }
   public int getWolfReproduce() { return wolfReproduce; }
   public int getSheepGainFromFood() { return sheepGainFromFood; }
   public int getWolfGainFromFood() { return wolfGainFromFood; }
   public int getGrassRegrowthTime() { return grassRegrowthTime; }
   
   public void setGrassOn(boolean test) {grassOn = test; }
   public void setInitialNumOfWolves(int num){ initialNumOfWolves = num; }
   public void setInitialNumOfSheep(int num){ initialNumOfSheep = num; }
   public void setSheepReproduce(int x) { sheepReproduce = x; }
   public void setWolfReproduce(int y) { wolfReproduce = y; }
   public void setSheepGainFromFood(int z) { sheepGainFromFood = z; }
   public void setWolfGainFromFood(int a) { wolfGainFromFood = a; }
   public void setGrassRegrowthTime(int b) { grassRegrowthTime = b; }
   
   
   public WolfSheepPredation(long seed)
   {
       super(new MersenneTwisterFast(seed), new Schedule(3));
      
   }
   
   public void removeSheep( Sheep s)
   {
       sheepSet.remove(s);
   }
   
   public void removeGrass ( Grass g )
   {
       grassSet.remove(g);
   }
   
   public void removeWolf ( Wolf w )
   {
       wolfSet.remove(w);
   }
   
   public void addWolf(int x, int y, int gain, int reproduce)
   {
       Wolf temp = new Wolf (x, y, gain, reproduce);
       wolfSet.add( temp  );
       Int2D newloc = new Int2D(x,y);
       entities.setObjectLocation ( temp , newloc);
   }
   
   public void addSheep(int x, int y, int gain, int reproduce)
   {
       Sheep temp = new Sheep (x, y, gain, reproduce);
       sheepSet.add( temp  );
       Int2D newloc = new Int2D(x,y);
       entities.setObjectLocation ( temp , newloc);
   }
   
   public void addGrass(int x, int y)
   {
       Grass temp = new Grass (x, y);
       grassSet.add( temp  );
       Int2D newloc = new Int2D(x,y);
       entities.setObjectLocation ( temp , newloc);
   }
   
   public void start()
   {
       super.start();
       entities = new SparseGrid2D(gridWidth, gridHeight);
       setupGrass();
      
       for(int i=0 ; i<initialNumOfSheep ; i++)
       {
           Sheep s = new Sheep(random.nextInt(3) - 1, random.nextInt(3) - 1, sheepGainFromFood, sheepReproduce);  // random direction
           sheepSet.add(s);
       }
       
       for (int i=0 ; i < initialNumOfWolves ; i++)
       {
           Wolf w = new Wolf(random.nextInt(3) - 1, random.nextInt(3) - 1, wolfGainFromFood, wolfReproduce);  // random direction
           wolfSet.add(w);
       }
       
       for ( int i = 0; i < sheepSet.size(); i++)
       {
           Sheep s1 = (Sheep) sheepSet.get(i);
           schedule.scheduleRepeating(s1);
           entities.setObjectLocation(s1, new Int2D(random.nextInt(gridWidth),random.nextInt(gridHeight)));  // random location
       }
    
       for (int i = 0; i < wolfSet.size(); i++)
       {
           Wolf w1 = (Wolf) wolfSet.get(i);        
           schedule.scheduleRepeating(w1);
           entities.setObjectLocation(w1,new Int2D(random.nextInt(gridWidth),random.nextInt(gridHeight)));  // random location
       }
       
          
       
   }
   
   public void setupGrass()
   {
       if (grassOn)
       {
           for (int i= 0; i < gridWidth; i++)
           {
               for ( int j = 0; j< gridHeight; j++)
               {
                   if (random.nextInt(2) == 0)
                   {
                       Grass g = new Grass ( i , j);
                       grassSet.add(g);
                       entities.setObjectLocation(g,new Int2D(i,j));  // random location
                   }
               }
           }
       }
   }
   
   
       
   public static void main(String[] args)
   {
       doLoop(WolfSheepPredation.class, args);
       System.exit(0);
   }    

}


wolfSheepPredationwithUI.java package sim.app.wolfSheepPredation; import sim.engine.*; import sim.display.*; import sim.portrayal.grid.*; import sim.portrayal.network.*; import sim.field.continuous.*; import sim.portrayal.Inspector; import java.awt.*; import javax.swing.*; import sim.portrayal.continuous.*;

public class WolfSheepPredationWithUI extends GUIState

   {
   public Display2D display;
   public JFrame displayFrame;
   SparseGridPortrayal2D particlesPortrayal = new SparseGridPortrayal2D();   

// ContinuousPortrayal2D particlesPortrayal [];

   public static void main(String[] args)
       {
       WolfSheepPredationWithUI t = new WolfSheepPredationWithUI();
       Console c = new Console(t);
       c.setVisible(true);
       }
   
   public WolfSheepPredationWithUI() { super(new WolfSheepPredation(System.currentTimeMillis())); }
   
   public WolfSheepPredationWithUI(SimState state) { super(state); }
   
   public String getName() { return "WolfSheepPredation"; }
   
   public Object getSimulationInspectedObject() { return state;}
   
   public String getInfo()
       {
return "

[edit] WolfSheepPredation

An odd little particle-interaction example.";

       }
   
   public void quit()
       {
       super.quit();
       
       if (displayFrame!=null) displayFrame.dispose();
       displayFrame = null;  // let gc
       display = null;       // let gc
       }
   public void start()
       {
       super.start();
       // set up our portrayals
       setupPortrayals();
       }
   
   public void load(SimState state)
       {
       super.load(state);
       // we now have new grids.  Set up the portrayals to reflect that
       setupPortrayals();
       }
       
   // This is called by start() and by load() because they both had this code
   // so I didn't have to type it twice :-)
   public void setupPortrayals()
       {
           //set color
           display.setBackdrop(Color.green);
       // tell the portrayals what to
       // portray and how to portray them
       WolfSheepPredation wsp = (WolfSheepPredation) state;
      // particlesPortrayal.setField(((WolfSheepPredation)state).entities);
      particlesPortrayal.setField(wsp.entities);
       particlesPortrayal.setPortrayalForClass( 
           Sheep.class, new sim.portrayal.simple.OvalPortrayal2D(Color.white));
        particlesPortrayal.setPortrayalForClass( 
           Wolf.class, new sim.portrayal.simple.OvalPortrayal2D(Color.black));    
       particlesPortrayal.setPortrayalForClass( 
           Grass.class, new sim.portrayal.simple.RectanglePortrayal2D(Color.red));    
      // nodePortrayal.setField( wsp.entities );
       //particlesPortrayal.setPortrayalForAll( new sim.portrayal.simple.OvalPortrayal2D(Color.white) );
          
      // particlesPortrayal.setField(((WolfSheepPredation)state).wolves);
       //particlesPortrayal2.setPortrayalForAll( new sim.portrayal.simple.RectanglePortrayal2D(Color.black) );           
       // reschedule the displayer*/
       display.reset();
               
       // redraw the display
       display.repaint();
       }
   
   public void init(Controller c)
       {
       super.init(c);
       
       // Make the Display2D.  We'll have it display stuff later.
       display = new Display2D(400,400,this,1); // at 400x400, we've got 4x4 per array position
       displayFrame = display.createFrame();
       c.registerFrame(displayFrame);   // register the frame so it appears in the "Display" list
       displayFrame.setVisible(true);
       display.attach(particlesPortrayal, "sheep");
       // specify the backdrop color  -- what gets painted behind the displays
       display.setBackdrop(Color.green);
       // attach the portrayals
      // display.attach(trailsPortrayal,"Trails");
       display.attach(particlesPortrayal,"Particles");
       }
}

Personal tools