Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sim.tsa;
- import java.util.ArrayList;
- import sim.tsa.TSAWorld.Path;
- import java.util.Random;
- import cern.jet.random.Uniform;
- public class TravellingSalesAnt {
- private TSAWorld space;
- private TSAModel model;
- private double length; // Length of all paths travelled by ant
- private ArrayList visited;
- private ArrayList unvisited;
- private ArrayList allPaths;
- private ArrayList potentialPaths;
- private TSACity currCity;
- private TSACity prevCity;
- private boolean firstRun;
- public TravellingSalesAnt(TSAWorld ss, TSAModel model) {
- firstRun = true;
- space = ss;
- this.model = model;
- visited = new ArrayList<TSACity>();
- potentialPaths = new ArrayList();
- unvisited = (ArrayList) space.getCitiesList().clone();
- allPaths = (ArrayList) space.getCitiesList().clone();
- length = 0;
- }
- public ArrayList getPath() { //Returns list of visited cities by Ant
- return visited;
- }
- public double getLength() { // Returns length of all paths travelled by ant
- return length;
- }
- public void step(TSACity start) { // Start is the first city the ant is placed on randomly by TSAModel (?)
- // First step the ant takes
- if(firstRun) {
- currCity = start;
- firstRun = false;
- }
- // For all other steps after the first run
- prevCity = currCity; // Keep track of last city visited
- currCity = findCity(); // Find the next city to visit
- this.step2();
- if(currCity != null){
- step(currCity);
- }
- }
- public void step2() {
- // add your code for pheromone distribution here
- if(prevCity != null && currCity != null) {
- space.deposit(prevCity, currCity, length);
- }
- }
- public void addToVisited(TSACity lastCityVisited) {
- visited.add(lastCityVisited);
- }
- public TSACity findCity() { // Determines next city we can visit from our current city
- double alpha = model.getPheromoneWeight();
- double beta = model.getDistanceWeight();
- if(visited.size() < allPaths.size() - 1) { // Check to see if we still have cities to visit
- addToVisited(currCity); // add it our list of visited cities
- if(unvisited.size() > 0) {
- unvisited.remove(prevCity);
- }
- // 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
- // if(((Path) space.getPathsList().get(i)).from == currCity && visited.contains(((Path)space.getPathsList().get(i)).to) == false){
- // potentialPaths.add(((Path) space.getPathsList().get(i)));
- // }
- // }
- //Calculate Probability using formula from notes
- double Bline = 0.0;
- for(int i=0; i< unvisited.size(); i++) { //Sum of...
- double tau = space.getPheromone(currCity, (TSACity) unvisited.get(i)); // pheromone level from current city to each unvistited city
- double eta = 1.0/(space.getDistance(currCity, (TSACity) unvisited.get(i))); // 1 over the distance between the current city and unvisited city
- Bline += Math.pow(tau, alpha)*Math.pow(eta, beta); // Cakculate bottom line of equation
- }
- int bestRouteIndex = 0;
- double bestProb = 0.0;
- //for current city work out Prob
- for(int j=0;j < unvisited.size(); j++)
- {
- double tau = space.getPheromone(currCity,(TSACity) unvisited.get(j));
- double eta = 1.0/(space.getDistance(currCity,(TSACity) unvisited.get(j)));
- double prob = ( Math.pow(tau ,alpha)*Math.pow(eta, beta) )/ Bline;
- //if best, keep track
- if(prob>=bestProb)
- {
- bestRouteIndex = j;
- bestProb= prob;
- }
- }
- //move to this position
- if(unvisited.size()>0)
- // for(int i=0; i < space.getPathsList().size(); i++) { //Get path length and add to total path length
- // if(((Path) space.getPathsList().get(i)).from == (TSACity) unvisited.get(bestroute)){
- // double distance = space.getDistance(((Path)space.getPathsList().get(i)).from, ((Path)space.getPathsList().get(i)).to);
- // length += distance;
- // }
- // }
- // double distance = space.getDistance(((Path)unvisited.get(bestroute)).from, ((Path)unvisited.get(bestroute)).to);
- // length += distance;
- return (TSACity) unvisited.get(bestRouteIndex);
- // Random rand = new Random();
- // //Pick random path from list of potential paths we found
- // int index = rand.nextInt((potentialPaths.size()-1 - 0) + 1) + 0;
- //
- // return ((Path)potentialPaths.get(index)).to;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement