public ASAntInteraction(int alpha, int beta, double rho, int numOfAnts, List <City> cities) : base(alpha, beta, rho, numOfAnts, cities) { pheromoneTrailInitialValue = numOfAnts / Distances.CalculateNNHeuristic(); Pheromones = new Pheromones(cities.Count, pheromoneTrailInitialValue); Pheromones.Init(); choiceInfo = new ChoiceInfo(cities.Count); choiceInfo.UpdateChoiceInfo(Pheromones, Distances, alpha, beta); }
public void Move() { //TODO: Diese Variable sollte an zentraler Stelle stehen: Wert für Pheromonstärke (die Ziffer). Pheromones.Add(new Pheromone(this.position, this.color)); //TODO: Pheromone sollten stärker werden, je häufiger eine Ant darüber läuft. //TODO: Pheromone sollten Einfluss auf die Navigation der Ant haben. if (steps < stepLimit) { steps++; } else { steps = 0; GeneralDirection = rnd.Next(0, 5); } //TODO: Diese Variable sollte an zentraler Stelle stehen: Schrittweite einer Ant int step = 3; int direction = rnd.Next(0, 5); if (direction != generalDirection - 4) { if (direction == GeneralDirection) { step = step * 2; } switch (direction) { case 0: //move north this.position.Y -= step; break; case 1: //move east this.position.X += step; break; case 2: //move south this.position.Y += step; break; case 3: //move west this.position.X -= step; break; default: break; } } }
public MinMaxAntInteraction(int alpha, int beta, double rho, int numOfAnts, List <City> cities, double pBest) : base(alpha, beta, rho, numOfAnts, cities) { this.pBest = pBest; pheromoneTrailInitialValue = 1.0f / (rho * Distances.CalculateNNHeuristic()); Pheromones = new Pheromones(cities.Count, pheromoneTrailInitialValue, pBest); Pheromones.Init(); choiceInfo = new ChoiceInfo(cities.Count); choiceInfo.UpdateChoiceInfo(Pheromones, Distances, alpha, beta); }
// the local pheromone update is done after each step (each ant one city step) private void LocalPheromoneUpdate(int antIndex, int currentCityAmount) { int prevCity = Ants[antIndex].GetCityOfTour(currentCityAmount - 1); int currentCity = Ants[antIndex].GetCityOfTour(currentCityAmount); Pheromones.SetPheromone(prevCity, currentCity, ((1.0f - rho) * Pheromones.GetPheromone(prevCity, currentCity)) + (rho * tau0)); Pheromones.SetPheromone(currentCity, prevCity, Pheromones.GetPheromone(prevCity, currentCity)); choiceInfo.SetChoice(prevCity, currentCity, Math.Pow(Pheromones.GetPheromone(prevCity, currentCity), alpha) * Math.Pow((1.0 / Distances.GetDistance(prevCity, currentCity)), beta)); choiceInfo.SetChoice(currentCity, prevCity, choiceInfo.GetChoice(prevCity, currentCity)); }
// deposit procedure of pheromones protected void DepositPheromones(int antIndex, double increaseFactor) { for (int i = 0; i < cities.Count; i++) { int j = Ants[antIndex].GetCityOfTour(i); int l = Ants[antIndex].GetCityOfTour(i + 1); Pheromones.IncreasePheromoneAs(j, l, increaseFactor); Pheromones.IncreasePheromoneAs(l, j, increaseFactor); } }
// choice update public void updateChoiceInfo(Pheromones pheromones, Distances distances, int alpha, int beta) { for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { choiceInfo[i][j] = Math.Pow(pheromones.getPheromone(i, j), alpha) * Math.Pow((1.0 / distances.getDistance(i, j)), beta); choiceInfo[j][i] = choiceInfo[i][j]; } } }
public ACSAntInteraction(int alpha, int beta, double rho, int numOfAnts, List <City> cities, double acsQ0) : base(alpha, beta, rho, numOfAnts, cities) { this.acsQ0 = acsQ0; tau0 = 1.0f / (cities.Count * Distances.CalculateNNHeuristic()); pheromoneTrailInitialValue = this.tau0; Pheromones = new Pheromones(cities.Count, pheromoneTrailInitialValue); Pheromones.Init(); choiceInfo = new ChoiceInfo(cities.Count); choiceInfo.UpdateChoiceInfo(Pheromones, Distances, alpha, beta); }
// evaporation of pheromones protected void EvaporatePheromones(int antIndex) { double decreaseFactor = 1.0 - rho; for (int i = 0; i < cities.Count; i++) { int j = Ants[antIndex].GetCityOfTour(i); int l = Ants[antIndex].GetCityOfTour(i + 1); Pheromones.DecreasePheromoneAs(j, l, decreaseFactor); Pheromones.DecreasePheromoneAs(l, j, decreaseFactor); } }
// evaporation of pheromones protected void EvaporatePheromones() { double decreaseFactor = 1.0 - rho; for (int i = 0; i < cities.Count; i++) { for (int j = i + 1; j < cities.Count; j++) { Pheromones.DecreasePheromoneAs(i, j, decreaseFactor); Pheromones.DecreasePheromoneAs(j, i, decreaseFactor); } } }
public AntInteraction(int alpha, int beta, double rho, double q, int numOfAnts, List <City> cities) { this.cities = cities; this.alpha = alpha; this.beta = beta; this.rho = rho; this.q = q; this.numOfAnts = numOfAnts; distances = new Distances(cities); initAnts(); pheromones = new Pheromones(cities.Count); choiceInfo = new ChoiceInfo(cities.Count); choiceInfo.updateChoiceInfo(pheromones, distances, alpha, beta); Debug.Log("Choices: " + choiceInfo.ToString); }
// choice update public void UpdateChoiceInfo(Pheromones pheromones, Distances distances, int alpha, int beta) { for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { choiceInfo[i][j] = Math.Pow(pheromones.GetPheromone(i, j), alpha) * Math.Pow((1.0 / distances.GetDistance(i, j)), beta); choiceInfo[j][i] = choiceInfo[i][j]; /* // To avoid small or high values * if (choiceInfo[i][j] <= 0.000001) * choiceInfo[i][j] = 0.000001; * else if (choiceInfo[i][j] > (double.MaxValue / 100)) * choiceInfo[i][j] = double.MaxValue / 100; */ } } }
public override void UpdatePheromones() { int bestAntIndex = FindBestAnt().Id; EvaporatePheromones(); double increaseFactor = (1.0 / Ants[bestAntIndex].TourLength); //Debug.Log("Best Ant: " + bestAntIndex + " Increase Factor: "+ increaseFactor); //Debug.Log("Tour " + Ants[bestAntIndex].ToString); DepositPheromones(bestAntIndex, increaseFactor); Pheromones.UpdateTrailLimits(Ants[bestAntIndex].TourLength, rho, pBest); Pheromones.CheckPheromoneTrailLimits(); // Debug.Log("Pheromones: " + Pheromones.ToString); FinishIteration(); }
public void RemoveFlag(Pheromones signal) { switch (signal) { case Pheromones.ALL: flagWorker.transform.position = new Vector3(0,0,-1); flagSoldier.transform.position = new Vector3(0,0,-1); flagScout.transform.position = new Vector3(0,0,-1); break; case Pheromones.USEFUL: flagWorker.transform.position = new Vector3(0,0,-1); isFlagWorker = false; break; case Pheromones.ATTACK: flagSoldier.transform.position = new Vector3(0,0,-1); isFlagSoldier = false; break; case Pheromones.SCOUT: flagScout.transform.position = new Vector3(0,0,-1); isFlagScout = false; break; } }
public void PutPheromone(Vector3 point, Pheromones signal, float price) { int sectorX = Mathf.RoundToInt (point.x); int sectorY = Mathf.RoundToInt (point.y); switch (signal) { case Pheromones.ALL: if(PutUsefulPheromone(sectorX,sectorY,10,10) || PutHivePheromone(sectorX,sectorY,10)||PutEnemyPheromone(sectorX,sectorY,10)) { SpendFromStorage(price); } break; case Pheromones.USEFUL: if(PutUsefulPheromone(sectorX,sectorY,20,20)) { SpendFromStorage(price); } break; case Pheromones.ATTACK: if(PutEnemyPheromone(sectorX,sectorY,50)) { SpendFromStorage(price); } break; } }
public void PutFlag(Vector3 point, Pheromones signal, float price) { int sectorX = Mathf.RoundToInt (point.x); int sectorY = Mathf.RoundToInt (point.y); switch (signal) { case Pheromones.ALL: flagWorker.transform.position = new Vector3(sectorX,sectorY,0); flagSoldier.transform.position = new Vector3(sectorX,sectorY,0); flagScout.transform.position = new Vector3(sectorX,sectorY,0); break; case Pheromones.USEFUL: flagWorker.transform.position = new Vector3(sectorX,sectorY,0); break; case Pheromones.ATTACK: flagSoldier.transform.position = new Vector3(sectorX,sectorY,0); isFlagSoldier = true; break; case Pheromones.SCOUT: flagScout.transform.position = new Vector3(sectorX,sectorY,0); break; } SpendFromStorage(price); }
//init the pheromones private void initPheromones() { pheromones = new Pheromones(cities.Count, pheromoneTrailInitialValue); pheromones.init(); }