public static void setColony(LPoint home, int antCount) { Colony.home = home; Colony.home.setHome(); Colony.antCount = antCount; Colony.ants = new List <Ant>(); Colony.generateAnts(); }
public static void CreateAColony(int antSize) { Random rnd = new Random(); int rLPoint = rnd.Next(World.locations.Count); LPoint selectedLPoint = World.locations[rLPoint]; selectedLPoint.isStartPoint = true; Colony.setColony(selectedLPoint, antSize); }
public Ant(LPoint home) { this.visitedPoints = new List <LPoint>(); this.visitedNeigbours = new List <Neighbor>(); this.visitedPoints.Add(home); this.id = Ant.idCount; Ant.idCount++; this.pathLenght = 0; }
public LPoint GetDestination(LPoint source) { if (source == this.points[0]) { return(this.points[1]); } else { return(this.points[0]); } }
public Neighbor(LPoint point1, LPoint point2) { this.id = Neighbor.giveId; Neighbor.giveId++; this.points = new List <LPoint>(); this.points.Add(point1); this.points.Add(point2); this.distance = MyMath.distanceLPoint(point1, point2); this.pheromoneValue = 1; }
public static void CreateRandomWorld(int locationCount) { World.locationCount = locationCount; Random rnd = new Random(); //chreate locations int rx = 0; int x = 0; int ry = 0; int y = 0; for (int i = 0; i < World.locationCount; i++) { rx = rnd.Next(World.witdhNumbers.Count); x = World.witdhNumbers[rx]; World.witdhNumbers.RemoveAt(rx); ry = rnd.Next(World.heightNumbers.Count); y = World.heightNumbers[ry]; World.heightNumbers.RemoveAt(ry); LPoint nLpoint = new LPoint(x + margin, y + margin); World.locations.Add(nLpoint); } foreach (LPoint item1 in World.locations) { foreach (LPoint item2 in World.locations) { if (!item2.Equals(item1)) { Neighbor nNeighbour = new Neighbor(item1, item2); if (!item1.isInNeighbors(nNeighbour) && !item2.isInNeighbors(nNeighbour)) { item1.addNeighbor(nNeighbour); item2.addNeighbor(nNeighbour); neighbors.Add(nNeighbour); } } } } }
public void takeAStep() { LPoint curentLocation = this.visitedPoints[this.visitedPoints.Count - 1]; List <Neighbor> notVisitedNeigbours = new List <Neighbor>(); double totalJValue = 0; foreach (Neighbor neigbour in curentLocation.neighbors) { if (neigbour.isEliminated) { continue; } neigbour.probability = 0; LPoint candidate = neigbour.GetDestination(curentLocation); if (!this.visitedPoints.Contains(candidate)) { neigbour.jValue = Math.Round(Math.Pow(neigbour.pheromoneValue, Colony.a_alpha), 10) * Math.Round(Math.Pow((1 / neigbour.distance), Colony.b_beta), 10); // totalJValue += Math.Round(neigbour.jValue, 6, MidpointRounding.AwayFromZero); totalJValue += neigbour.jValue; notVisitedNeigbours.Add(neigbour); } } if (notVisitedNeigbours.Count == 0)// eğer gezilmemiş komşu yoksa tur bitmiştir. { //başlangıca dönüş foreach (Neighbor neigbour in curentLocation.neighbors) { LPoint candidate = neigbour.GetDestination(curentLocation); if (candidate == Colony.home) { this.visitedNeigbours.Add(neigbour); break; } } this.EveluateFinishedPath(); return; } /* * RandomNumberGenerator rnd = null; * switch (World.generatorChoice) * { * case World.RandomNumberGenerators.Uniform: * rnd = new RandGenUniform(); * break; * case World.RandomNumberGenerators.Exponential: * rnd = new RandGenExponential(); * break; * case World.RandomNumberGenerators.Gamma: * rnd = new RandGenGamma(); * break; * default: * rnd = new RandGenBeta(); * break; * * }*/ double pickedRandom = World.rnd.NextDouble(); double rouletteWheel = 0; //notVisitedNeigbours=notVisitedNeigbours.OrderByDescending(p=>p.pheromoneValue).ToList(); Neighbor nextNeighbor = null; int rindex = 0; while (nextNeighbor == null) { rindex = (int)((World.rnd.NextDouble() * 100) % notVisitedNeigbours.Count); Neighbor neigbour = notVisitedNeigbours[rindex]; neigbour.probability = neigbour.jValue / totalJValue; rouletteWheel += neigbour.probability; if (pickedRandom < rouletteWheel) { nextNeighbor = neigbour; //this.Q_antPhrenomeAmount += nextNeighbor.pheromoneValue; break; } } /* foreach (Neighbor neigbour in notVisitedNeigbours) * { * * neigbour.probability = neigbour.jValue / totalJValue; * rouletteWheel += neigbour.probability; * if (pickedRandom < rouletteWheel) * { * nextNeighbor = neigbour; * this.Q_antPhrenomeAmount += nextNeighbor.pheromoneValue; * break; * } * }*/ this.visitedNeigbours.Add(nextNeighbor); this.pathLenght += nextNeighbor.distance; LPoint nextLocation = nextNeighbor.GetDestination(curentLocation); this.visitedPoints.Add(nextLocation); }
public static double distanceLPoint(LPoint p1, LPoint p2) { double result = Math.Round(Math.Sqrt(Math.Pow((p1.location.X - p2.location.X), 2) + Math.Pow((p1.location.Y - p2.location.Y), 2)), 1); return(result); }