Advertisement
ConorB4

Untitled

Nov 2nd, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package sim.tsa;
  2.  
  3. import java.util.ArrayList;
  4. import sim.tsa.TSAWorld.Path;
  5. import java.util.Random;
  6. import cern.jet.random.Uniform;
  7.  
  8. public class TravellingSalesAnt {
  9.  
  10.   private TSAWorld space;
  11.   private TSAModel model;
  12.   private double length; // Length of all paths travelled by ant
  13.   private ArrayList visited;
  14.   private ArrayList unvisited;
  15.   private ArrayList allPaths;
  16.   private ArrayList potentialPaths;
  17.   private TSACity currCity;
  18.   private TSACity prevCity;
  19.   private boolean firstRun;
  20.  
  21.   public TravellingSalesAnt(TSAWorld ss, TSAModel model) {
  22.     firstRun = true;
  23.     space = ss;
  24.     this.model = model;
  25.     visited = new ArrayList<TSACity>();
  26.     potentialPaths = new ArrayList();
  27.     unvisited = (ArrayList) space.getCitiesList().clone();
  28.     allPaths = (ArrayList) space.getCitiesList().clone();
  29.     length = 0;
  30.   }
  31.  
  32.   public ArrayList getPath() { //Returns list of visited cities by Ant
  33.     return visited;
  34.   }
  35.  
  36.   public double getLength() { // Returns length of all paths travelled by ant
  37.     return length;
  38.    }
  39.  
  40.   public void step(TSACity start) { // Start is the first city the ant is placed on randomly by TSAModel (?)
  41.  
  42.     // First step the ant takes
  43.     if(firstRun) {
  44.       currCity = start;
  45.       firstRun = false;
  46.     }
  47.  
  48.     // For all other steps after the first run
  49.  
  50.     prevCity = currCity; // Keep track of last city visited
  51.     currCity = findCity(); // Find the next city to visit
  52.  
  53.     this.step2();
  54.     if(currCity != null){
  55.       step(currCity);
  56.     }
  57.  
  58.   }
  59.  
  60.   public void step2() {
  61.     // add your code for pheromone distribution here
  62.     if(prevCity != null && currCity != null) {
  63.       space.deposit(prevCity, currCity, length);
  64.     }
  65.   }
  66.  
  67.   public void addToVisited(TSACity lastCityVisited) {
  68.     visited.add(lastCityVisited);
  69.   }
  70.  
  71.   public TSACity findCity() { // Determines next city we can visit from our current city
  72.  
  73.     double alpha = model.getPheromoneWeight();
  74.     double beta = model.getDistanceWeight();
  75.  
  76.     if(visited.size() < allPaths.size() - 1) { // Check to see if we still have cities to visit
  77.       addToVisited(currCity); // add it our list of visited cities
  78.       if(unvisited.size() > 0) {
  79.         unvisited.remove(prevCity);
  80.       }
  81.  
  82.     // for(int i=0; i < space.getPathsList().size(); i++) { //For each path in the world we see if the start is the same as our city and if we have visisted its destination. if not -> add to potential list of paths we can use
  83.     //   if(((Path) space.getPathsList().get(i)).from == currCity && visited.contains(((Path)space.getPathsList().get(i)).to) == false){
  84.     //     potentialPaths.add(((Path) space.getPathsList().get(i)));
  85.     //   }
  86.     // }
  87.  
  88.     //Calculate Probability using formula from notes
  89.     double Bline = 0.0;
  90.     for(int i=0; i< unvisited.size(); i++) { //Sum of...
  91.       double tau = space.getPheromone(currCity, (TSACity) unvisited.get(i)); // pheromone level from current city to each unvistited city
  92.       double eta = 1.0/(space.getDistance(currCity, (TSACity) unvisited.get(i))); // 1 over the distance between the current city and unvisited city
  93.       Bline += Math.pow(tau, alpha)*Math.pow(eta, beta); // Cakculate bottom line of equation
  94.     }
  95.  
  96.     int bestRouteIndex = 0;
  97.         double bestProb = 0.0;
  98.         //for current city work out Prob
  99.         for(int j=0;j < unvisited.size(); j++)
  100.         {
  101.             double tau = space.getPheromone(currCity,(TSACity) unvisited.get(j));
  102.             double eta  = 1.0/(space.getDistance(currCity,(TSACity) unvisited.get(j)));
  103.             double prob = ( Math.pow(tau ,alpha)*Math.pow(eta, beta) )/ Bline;
  104.             //if best, keep track
  105.             if(prob>=bestProb)
  106.             {
  107.                 bestRouteIndex = j;
  108.                 bestProb= prob;
  109.             }
  110.         }
  111.  
  112.     //move to this position
  113.         if(unvisited.size()>0)
  114.       // for(int i=0; i < space.getPathsList().size(); i++) { //Get path length and add to total path length
  115.       //   if(((Path) space.getPathsList().get(i)).from == (TSACity) unvisited.get(bestroute)){
  116.       //     double distance = space.getDistance(((Path)space.getPathsList().get(i)).from, ((Path)space.getPathsList().get(i)).to);
  117.       //     length += distance;
  118.       //   }
  119.       // }
  120.       // double distance = space.getDistance(((Path)unvisited.get(bestroute)).from, ((Path)unvisited.get(bestroute)).to);
  121.       // length += distance;
  122.       return (TSACity) unvisited.get(bestRouteIndex);
  123.  
  124.   //   Random rand = new Random();
  125.   //   //Pick random path from list of potential paths we found
  126.   //   int index = rand.nextInt((potentialPaths.size()-1 - 0) + 1) + 0;
  127.    //
  128.   //  return ((Path)potentialPaths.get(index)).to;
  129.     }
  130.    return null;
  131.   }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement