To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/4/src/antgraph/Pheromone.java. This is a spam-protection measure; sorry for the inconvenience.

· Pheromone.java ·

   1/*
   2 * Pheromone.java
   3 *
   4 * Created on 04 March 2007, 02:37
   5 *
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package antgraph;
  11
  12import antgraph.gui.GraphJFrame;
  13import java.util.List;
  14import java.util.Stack;
  15import java.util.Vector;
  16
  17/**
  18 *
  19 * @author James
  20 */
  21public class Pheromone implements Runnable {
  22    
  23    private List<PheromoneParticle> particles = new Vector<PheromoneParticle>();
  24
  25    private int iTowardsNest = 0;
  26    private int iAwayFromNest = 0;
  27    
  28    private Thread move;
  29    private int rate = 10;
  30    
  31    public static enum Direction { TOWARD_NEST, AWAY_FROM_NEST, NONE; }
  32    
  33    /** Creates a new instance of Pheromone */
  34    public Pheromone() {
  35        this(true);
  36    }
  37    
  38    public Pheromone(boolean start) {
  39        if(start) {
  40            move = new Thread(this);
  41            move.setDaemon(true);
  42
  43        }
  44    }
  45    
  46    public void run() {
  47        while(move != null) {
  48            
  49            try {
  50           
  51                if(GraphJFrame.getGraph().getNest() != null && 
  52                         GraphJFrame.getGraph().getNest().countFood() >= GraphJFrame.FOOD) stop();
  53                move.sleep(getRate());
  54                decrease();
  55                  
  56            }catch (InterruptedException e) {
  57                System.out.println(e);
  58            }
  59            
  60        }
  61    }   
  62    
  63    public Direction getDirection(Ant ant) {
  64        for(int i = particles.size() - 1; i >= 0; i++) {
  65            if(particles.get(i).getAnt().equals(ant))
  66                return particles.get(i).getDirection();
  67        }
  68        return Direction.NONE;
  69    }
  70    
  71    public void decrease() {
  72
  73       if(particles.size() > 0) {
  74           if(particles.get(0).getDirection() == Direction.TOWARD_NEST) {
  75                iTowardsNest--;
  76            }else if(particles.get(0).getDirection() == Direction.AWAY_FROM_NEST) {
  77                iAwayFromNest--;
  78            }
  79
  80            particles.remove(0); 
  81
  82       }
  83
  84    }
  85
  86    public void increaseStrength(Ant ant, Direction direction) {
  87        particles.add(new PheromoneParticle(ant, direction));
  88
  89       if(direction == Direction.TOWARD_NEST) {
  90            iTowardsNest++;
  91        }else if(direction == Direction.AWAY_FROM_NEST) {
  92            iAwayFromNest++;
  93        }
  94    }
  95    
  96    public synchronized void increaseStrength(PheromoneParticle p) {
  97        this.increaseStrength(p.getAnt(), p.getDirection());
  98    }
  99    
 100    public Pheromone getPheromone(Direction direction) {
 101        Pheromone p = new Pheromone(false);
 102        for(PheromoneParticle pp : particles) {
 103            if(pp.getDirection() == direction) p.increaseStrength(pp);
 104        }
 105        
 106        return p;        
 107    }
 108
 109    public int getStrength() {
 110        return getStrength(Direction.TOWARD_NEST) + getStrength(Direction.AWAY_FROM_NEST);
 111    }    
 112
 113    
 114    public int getStrength(Direction direction) {
 115       if(direction == direction.TOWARD_NEST) {
 116           return iTowardsNest;
 117        }else if(direction == Direction.AWAY_FROM_NEST) {
 118           return iAwayFromNest;
 119        }
 120       return 0;
 121
 122    }
 123    
 124    public void start() {
 125        if(move == null) {
 126            move = new Thread(this);
 127            move.setDaemon(true);
 128            move.start();
 129        }
 130            
 131    }
 132    
 133    public void stop() {
 134        move = null;
 135    }    
 136 
 137
 138    public Direction getDirection() {
 139       if(particles.size() == 0) return Direction.NONE;
 140         
 141        int toward_nest = getStrength(Direction.TOWARD_NEST);
 142        int away_from_nest = getStrength(Direction.AWAY_FROM_NEST);
 143        
 144        return toward_nest > away_from_nest ? Direction.TOWARD_NEST : Direction.AWAY_FROM_NEST;
 145
 146    }    
 147    
 148    public boolean laidBy(Ant ant) {
 149        for(PheromoneParticle p : particles) {
 150            if(p.getAnt().equals(ant)) return true;
 151        }
 152        
 153        return false;
 154    }    
 155    
 156    public String toString() {
 157        StringBuffer sb = new StringBuffer();
 158        
 159        sb.append("T: " + getStrength(Direction.TOWARD_NEST));
 160        sb.append(", ");
 161        sb.append("A: " + getStrength(Direction.AWAY_FROM_NEST));
 162        sb.append(", ");
 163        sb.append("A+T: " + getStrength());       
 164        sb.append(", D: "  + getDirection());
 165        return sb.toString();
 166    }
 167
 168    public int getRate() {
 169        return rate;
 170    }
 171
 172    public void setRate(int rate) {
 173        this.rate = rate;
 174    }
 175}
 176

· Pheromone.java ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.