Exemplo n.º 1
0
        public static PathGenetic GenerateChild(Instance instance, Random rnd, PathGenetic mother, PathGenetic father, List <int>[] listArray)
        {
            PathGenetic child;

            int[] pathChild = new int[instance.NNodes];

            //This variable defines the point of breaking the path of the father and that of the mother
            int crossover = (rnd.Next(0, instance.NNodes));

            for (int i = 0; i < instance.NNodes; i++)
            {
                if (i > crossover)
                {
                    pathChild[i] = mother.path[i];
                }
                else
                {
                    pathChild[i] = father.path[i];
                }
            }


            //With a probability of 0.01 we make a mutation
            if (rnd.Next(0, 101) == 100)
            {
                Mutation(instance, rnd, pathChild);
            }

            //The repair method ensures that the child is a permissible path
            child = Repair(instance, pathChild, listArray);


            if (ProbabilityTwoOpt(instance, rnd) == 1)
            {
                child.path = InterfaceForTwoOpt(child.path);

                TSP.TwoOpt(instance, child);

                child.path = Reverse(child.path);
            }
            return(child);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length < 4)
            {
                throw new System.Exception("Input args should be at least 2");
            }

            //Writing the inpute parameters
            if (VERBOSE >= 9)
            {
                Console.Write("Input parameters: ");
                for (int i = 0; i < args.Length; i++)
                {
                    Console.Write(args[i].ToString() + " ");
                }
                Console.WriteLine("\n\n\n");
            }

            /*
             * Saving input parameters inside an Instance variable
             * Attention: if file name or timilimit are missing
             * an exception will be throw
             */
            Instance inst = new Instance();

            ParseInst(inst, args);

            //Reading the input file and storing its data into inst
            Populate(inst);


            //Starting the clock
            clock = new Stopwatch();
            clock.Start();


            //Starting the elaboration of the TSP problem
            //The boolean returning value tells if everything went fine
            if (!TSP.TSPOpt(inst, clock))
            {
                throw new System.Exception("Impossible to find the optimal solution for the given instance");
            }


            //Stopping the clock
            clock.Stop();

            //Printing the time needed to find the optimal solution
            //If it's equal to the input timelimit the solution is not the optimal
            Console.WriteLine("The optimal solution was found in " + clock.ElapsedMilliseconds / 1000.0 + " s");


            //Only to check the output
            Console.ReadLine();

            foreach (string file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dat").Where(item => item.EndsWith(".dat")))
            {
                File.Delete(file);
            }

            foreach (string file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*.lp").Where(item => item.EndsWith(".lp")))
            {
                File.Delete(file);
            }
        }