To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/1/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 java.util.ArrayList; 13import java.util.List; 14 15/** 16 * 17 * @author James 18 */ 19public class Pheromone implements Runnable { 20 21 private List<PheromoneParticle> particles = new ArrayList<PheromoneParticle>(); 22 private Thread move; 23 private int rate = 90000; 24 25 public static enum Direction { TOWARD_NEST, AWAY_FROM_NEST, NONE; } 26 27 /** Creates a new instance of Pheromone */ 28 public Pheromone() { 29 move = new Thread(this); 30 move.setDaemon(true); 31 move.start(); 32 } 33 34 public void run() { 35 while(move != null) { 36 37 try { 38 39 move.sleep(getRate()); 40 if(particles.size() > 0) particles.remove(0); 41 42 }catch (InterruptedException e) { 43 System.out.println(e); 44 } 45 46 } 47 } 48 49 public void increaseStrength(Ant ant, Direction direction) { 50 particles.add(new PheromoneParticle(ant, direction)); 51 } 52 53 public double getStrength() { 54 return particles.size(); 55 } 56 57 public void start() { 58 if(move == null) { 59 move = new Thread(this); 60 move.setDaemon(true); 61 move.start(); 62 } 63 64 } 65 66 public void stop() { 67 move = null; 68 } 69 70 71 public Direction getDirection() { 72 if(particles.size() == 0) return Direction.NONE; 73 74 int toward_nest = 0; 75 int away_from_nest = 0; 76 77 for(PheromoneParticle p : particles) { 78 if(p.getDirection() == Direction.AWAY_FROM_NEST) 79 away_from_nest++; 80 else if(p.getDirection() == Direction.TOWARD_NEST) 81 toward_nest++; 82 } 83 84 return toward_nest > away_from_nest ? Direction.TOWARD_NEST : Direction.AWAY_FROM_NEST; 85 86 } 87 88 public Direction getDirection(Ant ant) { 89 if(particles.size() == 0) return Direction.NONE; 90 91 int toward_nest = 0; 92 int away_from_nest = 0; 93 94 for(PheromoneParticle p : particles) { 95 if(!p.getAnt().equals(ant)) continue; 96 97 if(p.getDirection() == Direction.AWAY_FROM_NEST) 98 away_from_nest++; 99 else if(p.getDirection() == Direction.TOWARD_NEST) 100 toward_nest++; 101 } 102 103 return toward_nest > away_from_nest ? Direction.TOWARD_NEST : Direction.AWAY_FROM_NEST; 104 105 } 106 107 public boolean laidBy(Ant ant) { 108 for(PheromoneParticle p : particles) { 109 if(p.getAnt().equals(ant)) return true; 110 } 111 112 return false; 113 } 114 115 public String toString() { 116 return getDirection().toString(); 117 } 118 119 public int getRate() { 120 return rate; 121 } 122 123 public void setRate(int rate) { 124 this.rate = rate; 125 } 126} 127
· Pheromone.java ends ·
To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/1/src/antgraph/Pheromone.java. This is a spam-protection measure; sorry for the inconvenience.