static void Main(string[] args)
        {
            TravellingSalesmanMap TSP = new TravellingSalesmanMap(TravellingSalesmanMap.MapInstance.RAT99);

            RunACO(TSP);
            //RunPSO(TSP);
        }
 public PSO(TravellingSalesmanMap TSP, int particleCount, int? maxSteps = null, double? targetTravelDistance = null)
 {
     this.TSP = TSP;
     this.Swarm = new TSPParticleSwarm(TSP, particleCount);
     this.MaxSteps = maxSteps;
     this.TargetTravelDistance = targetTravelDistance;
 }
 //Ha alguma garantia de que essas particulas aleatorias sejam circuitos hamiltonianos?
 public static TSPParticle RandomGenerate(TSPParticleSwarm containingSwarm, TravellingSalesmanMap Map)
 {
     TSPParticle newParticle = new TSPParticle(containingSwarm, Map);
     newParticle.Velocity = TSPVelocity.RandomGenerate(Map);
     newParticle.Position = new TSPPosition(Aleatoriety.GetRandomIntegerSequencePermutation(1, Map.CityCount).ToList());
     newParticle.EvaluateSelf();
     return newParticle;
 }
        public AntProblem(TravellingSalesmanMap TSP)
            : base(TSP.map)
        {
            PheromoneMatrix = new double[TSP.CityCount, TSP.CityCount];
            for (int p1 = 0; p1 < TSP.CityCount; p1++)
                for (int p2 = p1; p2 < TSP.CityCount; p2++)
                    PheromoneMatrix[p1, p2] = 1;

            pheromoneWeight = 1;
            distanceWeight = 5;
        }
 //construtor do ACO
 //recebe os parâmetros a serem utilizados nas funções de movimento da formiga e de atualização de feromônio
 public ACO(TravellingSalesmanMap TSP, int numAnts)
 {
     //inicializar variáveis
     problem = new AntProblem(TSP);
     bestSolutionFound = new TSPSolution();
     taxaEvaporacao = 0.9;
     //cria as formigas e coloca cada uma em um vértice aleatório do grafo
     System.Random random = new Random();
     ants = new List<Ant>();
     for (int i = 0; i < numAnts; i++)
         //ants.Add(new Ant(random.Next(problem.CityCount) + 1));
         ants.Add(new Ant((i + 1)%(TSP.CityCount + 1)));
 }
        static void RunPSO(TravellingSalesmanMap TSP)
        {
            //cria novo PSO
            PSO pso = new PSO(TSP, 100, 10000, TSP.OptimalTravelDistance);

            //roda o PSO
            TSPSolution s = (TSPSolution) pso.Run();

            //imprime a solução
            Console.WriteLine("Optimal Solution: " + TSP.OptimalTravelDistance);
            Console.WriteLine("Solution Found: " + s.Fitness);
            Console.ReadKey();
        }
        static void RunACO(TravellingSalesmanMap TSP)
        {
            //cria novo ACO
            ACO aco = new ACO(TSP, TSP.CityCount);

            //roda o ACO
            TSPSolution s = aco.Run();

            //imprime a solução
            Console.WriteLine("Optimal Solution: " + TSP.OptimalTravelDistance);
            Console.WriteLine("Solution Found: " + s.Fitness);
            Console.Write("\nSteps: ");
            foreach(int visitedCity in s.TravelPlan)
                Console.Write(TSP.GetCityAlias(visitedCity) + " => ");
            Console.Write(TSP.GetCityAlias(s.TravelPlan.First()));
            Console.ReadKey();
        }
        protected TravellingSalesmanMap Map; // { get; set; }

        #endregion Fields

        #region Constructors

        //Referencia a melhor particula atualmente
        //public TSPParticle gBest;
        /*
        public TSPParticle(TSPParticle particle) : base() {
            this.Position = particle.Position;
            this.Velocity = particle.Velocity;
            this.Fitness = particle.Fitness;
        }

        private TSPParticle() : base() {

        }
        */
        private TSPParticle(ParticleSwarm containingSwarm, TravellingSalesmanMap Map)
            : base(containingSwarm)
        {
            this.Map = Map;
        }
 public TSPParticleSwarm(TravellingSalesmanMap TSP, int particleCount)
     : base(particleCount)
 {
     this.Map = TSP;
 }
 public TSPSolution(List<int> steps, TravellingSalesmanMap problema)
 {
     this.TravelPlan = steps;
     problem = problema;
     Evaluate();
 }
 public TSPSolution(List <int> steps, TravellingSalesmanMap problema)
 {
     this.TravelPlan = steps;
     problem         = problema;
     Evaluate();
 }
 public static TSPVelocity RandomGenerate(TravellingSalesmanMap Map)
 {
     // 0.0 <= Valor < 1.0
     double speed = Aleatoriety.GetRandomDouble();
     int permutationsCount = (int)(speed * (Map.CityCount / 2)) + 1;
     List<Tuple<int, int>> permutations = new List<Tuple<int,int>>();
     for(int i = 0; i < permutationsCount; ++i) {
         Tuple<int, int> permutation = Aleatoriety.GetTwoDiferentValuesInRange(0, Map.CityCount);
         permutations.Add(permutation);
     }
     return new TSPVelocity(permutations);
 }