To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/16.0/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 = 60000; 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(go) { 58 59 try { 60 // if(strength> 0.0) strength -= (strength * 0.1); 61 // if(strength <= 0.0001) { strength = 0; direction = -1;} 62 63 64 move.sleep(rate);//strength * 1000); 65 if(particles.size() > 0) particles.remove(0); 66 67 68 // if(strength > 0)System.out.println(strength); 69 //System.out.println(getGridSquare() + ": " + strength); 70// if(strength < 0.01) strength =0; 71// if(strength <= 0) { 72// stop(); 73// } 74 75 }catch (InterruptedException e) { 76 System.out.println(e); 77 } 78 79 } 80 } 81 82 83 public void increaseStrength(Ant ant, int direction) { 84 particles.add(new PheromoneParticle(ant, direction)); 85 } 86 87 88 89 public void increaseStrength(int direction) { 90 strength += 0.5; 91 Point thisP = getGridSquare().getCoordinateXY(); 92 // setDirection(direction); 93 GridVector squares = getGridSquare().getGridSquares(5); 94 for(GridSquare s : squares) { 95 Point p = s.getCoordinateXY(); 96 double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2); 97 98 s.getPheromone().increaseStrength(1/(c2*500)); 99 // if( s.getPheromone().getDirection() == -1) s.getPheromone().setDirection(s.getDirectionOf(this.getGridSquare())); 100 } 101 102 if(strength > maxStrength) strength = maxStrength; 103 } 104 105 public void increaseStrength() { 106 107 strength += 0.5; 108 Point thisP = getGridSquare().getCoordinateXY(); 109 110 GridVector squares = getGridSquare().getGridSquares(5); 111 for(GridSquare s : squares) { 112 Point p = s.getCoordinateXY(); 113 double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2); 114 115 s.getPheromone().increaseStrength(1/(c2*500)); 116 } 117 /* 118 GridVector s = getGridSquare().getGridSquares(1); 119 for(GridSquare gs: s) 120 gs.getPheromone().increaseStrength(0.01); 121 s = getGridSquare().getGridSquares(2); 122 for(GridSquare gs: s) 123 gs.getPheromone().increaseStrength(0.01); 124s = getGridSquare().getGridSquares(3); 125 for(GridSquare gs: s) 126 gs.getPheromone().increaseStrength(0.01); 127s = getGridSquare().getGridSquares(4); 128 for(GridSquare gs: s) 129 gs.getPheromone().increaseStrength(0.01); 130*/ 131 132 if(strength > maxStrength) strength = maxStrength; 133// if(strength > 0) { 134// 135// 136// start(); 137// }else{ 138// stop(); 139// } 140 } 141 142 public void increaseStrength(double n) { 143 144 strength += n; 145 if(strength > maxStrength) strength = maxStrength; 146// if(strength > 0) { 147// start(); 148// }else{ 149// stop(); 150// } 151 } 152 153 public Color getColor() { 154 155 if(particles.firstElement().getDirection() == PheromoneParticle.AWAY_FROM_NEST) { 156 return Color.blue; 157 }else{ 158 return Color.green; 159 } 160 161 /* 162 163 if(!go && strength == 0) return Color.black; 164 165 java.lang.Double blue = new java.lang.Double(((double)strength) * 10 + 100 < 255 ? ((double)strength) * 10 + 100 : 255); 166 if(direction == TOWARD_NEST) 167 168 return new Color(0, 0, blue.intValue()); 169 else 170 return new Color(0, blue.intValue(), 0);*/ 171 } 172 173 public double getStrength() { 174 return particles.size(); 175 // return strength; 176 } 177 178 public void setStrength(int strength) { 179 if(strength > 10) strength = 10; 180 this.strength = strength; 181 182 183 184// if(strength > 0) { 185// start(); 186// }else{ 187// stop(); 188// } 189 190 } 191 192 public void start() { 193 go = true; 194 if(move == null) { 195 move = new Thread(this); 196 move.setDaemon(true); 197 move.start(); 198 } 199 200 } 201 202 public void stop() { 203 go = false; 204 strength = 0; 205 //move = null; 206 } 207 208 public int getDirection() { 209 int toward_nest = 0; 210 int away_from_nest = 0; 211 return particles.size() > 0 ? particles.lastElement().getDirection() : -1; 212 /* 213 for(PheromoneParticle p : particles) { 214 if(p.getDirection() == PheromoneParticle.AWAY_FROM_NEST) 215 away_from_nest++; 216 else if(p.getDirection() == PheromoneParticle.TOWARD_NEST) 217 toward_nest++; 218 } 219 220 return toward_nest > away_from_nest ? toward_nest : away_from_nest; 221 */ 222 // return direction; 223 } 224 225 public boolean laidBy(Ant ant) { 226 for(PheromoneParticle p : particles) { 227 if(p.getAnt().equals(ant)) return true; 228 } 229 230 return false; 231 } 232 233 public int getDirection(Ant ant) { 234 int toward_nest = 0; 235 int away_from_nest = 0; 236 237 // return particles.lastElement().getDirection(); 238 239 240 241 for(int i = particles.size(); i >= 0; i--) { 242 PheromoneParticle p = particles.elementAt(i); 243 if(!p.getAnt().equals(ant)) continue; 244 245 if(p.getDirection() == PheromoneParticle.AWAY_FROM_NEST) 246 away_from_nest++; 247 else if(p.getDirection() == PheromoneParticle.TOWARD_NEST) 248 toward_nest++; 249 } 250 251 return toward_nest > away_from_nest ? toward_nest : away_from_nest; 252 } 253 254 public void setDirection(int direction) { 255 256 this.direction = direction; 257 } 258} 259
· Pheromone.java ends ·
To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/16.0/src/ants/Pheromone.java. This is a spam-protection measure; sorry for the inconvenience.