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

· Pheromone.java ·

   1/*
   2 * Pheromone.java
   3 *
   4 * Created on 15 October 2006, 23:31
   5 *
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package ants;
  11
  12import ants.PheromoneParticle;
  13import java.awt.Color;
  14import java.awt.Point;
  15import java.util.HashMap;
  16import java.util.Vector;
  17
  18/**
  19 *
  20 * @author James Hamilton
  21 */
  22public class Pheromone extends MyObject implements Runnable {
  23    
  24    
  25    private Vector<PheromoneParticle> particles = new Vector<PheromoneParticle>();
  26    
  27    
  28    private double strength = 0;
  29    private Thread move;
  30    private int rate = 90000;
  31    private boolean go = true;
  32    private int maxStrength = 3;
  33    
  34    private int direction = -1;
  35    
  36    public static final int AWAY_FROM_NEST = 1;
  37    public static final int TOWARD_NEST = 2;
  38    
  39    /** Creates a new instance of Pheromone */
  40    public Pheromone() {
  41        setStrength(0);
  42                    move = new Thread(this);
  43            move.setDaemon(true);
  44            move.start();
  45    }
  46    
  47    public Pheromone(GridSquare gs) {
  48        super(gs);
  49        move = new Thread(this);
  50        move.setDaemon(true);
  51        move.start();
  52        setStrength(0);
  53
  54    }
  55    
  56    public void run() {
  57        while(direction != -1) {
  58            
  59            try {
  60            
  61                    move.sleep(rate);//strength * 1000);
  62                    if(particles.size() > 0)  particles.remove(0);
  63                       
  64            }catch (InterruptedException e) {
  65                System.out.println(e);
  66            }
  67            
  68        }
  69    }
  70    
  71    
  72    public void increaseStrength(Ant ant, int direction) {
  73        particles.add(new PheromoneParticle(ant, direction));
  74    }
  75    
  76    
  77    
  78    public void increaseStrength(int direction) {
  79        strength += 0.5;
  80        Point thisP = getGridSquare().getCoordinateXY();
  81        GridVector squares = getGridSquare().getGridSquares(5);
  82        for(GridSquare s : squares) {
  83             Point p = s.getCoordinateXY();
  84             double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);
  85             
  86             s.getPheromone().increaseStrength(1/(c2*500));
  87        }  
  88        
  89        if(strength > maxStrength) strength = maxStrength;
  90    }
  91    
  92    public void increaseStrength() {
  93       
  94        strength += 0.5;
  95        Point thisP = getGridSquare().getCoordinateXY();
  96        
  97        GridVector squares = getGridSquare().getGridSquares(5);
  98        for(GridSquare s : squares) {
  99             Point p = s.getCoordinateXY();
 100             double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);
 101             
 102             s.getPheromone().increaseStrength(1/(c2*500));
 103        }
 104            
 105         if(strength > maxStrength) strength = maxStrength;
 106    }
 107
 108    public void increaseStrength(double n) {
 109       
 110        strength += n;
 111         if(strength > maxStrength) strength = maxStrength;
 112    }    
 113    
 114    public Color getColor() {
 115     
 116        int direction = getDirection();
 117        if(direction == -1) {
 118            return Color.black;
 119        }else if(direction == PheromoneParticle.AWAY_FROM_NEST) {
 120            return Color.blue;
 121        }else{
 122            return Color.green;
 123        }
 124    }
 125
 126    public double getStrength() {
 127        return particles.size();
 128    }
 129
 130    public void setStrength(int strength) {
 131        if(strength > 10) strength = 10;
 132        this.strength = strength;
 133    }
 134    
 135    public void start() {
 136        go = true;
 137        if(move == null) {
 138                            move = new Thread(this);
 139            move.setDaemon(true);
 140            move.start();
 141        }
 142            
 143    }
 144    
 145    public void stop() {
 146        go = false;
 147        strength = 0;
 148    }
 149
 150    public int getDirection() {
 151         if(particles.size() == 0) return -1;
 152         
 153        int toward_nest = 0;
 154        int away_from_nest = 0;
 155        
 156        for(PheromoneParticle p : particles) {
 157            if(p.getDirection() == PheromoneParticle.AWAY_FROM_NEST)
 158                away_from_nest++;
 159            else if(p.getDirection() == PheromoneParticle.TOWARD_NEST)
 160                toward_nest++;
 161        }
 162              
 163        return toward_nest > away_from_nest ? PheromoneParticle.TOWARD_NEST : PheromoneParticle.AWAY_FROM_NEST;
 164    }
 165    
 166    public boolean laidBy(Ant ant) {
 167        for(PheromoneParticle p : particles) {
 168            if(p.getAnt().equals(ant)) return true;
 169        }
 170        
 171        return false;
 172    }
 173
 174    public int getDirection(Ant ant) {
 175        if(particles.size() == 0) return -1;
 176        
 177        int toward_nest = 0;
 178        int away_from_nest = 0;
 179     
 180        for(int i = particles.size(); i >= 0; i--) {
 181            PheromoneParticle p = particles.elementAt(i);
 182            if(!p.getAnt().equals(ant)) continue;
 183            
 184            if(p.getDirection() == PheromoneParticle.AWAY_FROM_NEST)
 185                away_from_nest++;
 186            else if(p.getDirection() == PheromoneParticle.TOWARD_NEST)
 187                toward_nest++;
 188        }
 189        
 190         return toward_nest > away_from_nest ? PheromoneParticle.TOWARD_NEST : PheromoneParticle.AWAY_FROM_NEST;
 191    }
 192    
 193    public void setDirection(int direction) {
 194       
 195        this.direction = direction;
 196    }
 197    
 198    public String toString() {
 199        int toward_nest = 0;
 200        int away_from_nest = 0;
 201        
 202        for(PheromoneParticle p : particles) {
 203            if(p.getDirection() == PheromoneParticle.AWAY_FROM_NEST)
 204                away_from_nest++;
 205            else if(p.getDirection() == PheromoneParticle.TOWARD_NEST)
 206                toward_nest++;
 207        }
 208        return "T: " + toward_nest  + ", A: " + away_from_nest + ", direction: " + getDirection();
 209    }
 210}
 211

· Pheromone.java ends ·

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