Esempio n. 1
0
 public void DoRandomWalkThread(int start, int end, Landscape landscape, ResearchParameters parameters, IOperator op,
                                StringBuilder dataBuilder, Action <string, float> callback, string connectionId, float step)
 {
     for (int j = start; j < end; ++j)
     {
         var    rwResult = landscape.RandomWalk(parameters.RandomWalkSteps, op);
         float  ac       = Autocorrelation.Run(rwResult);
         float  ic       = InformationContent.Run(rwResult, parameters.Sensitivity);
         float  pic      = PartialInformationContent.Run(rwResult, parameters.Sensitivity);
         float  dbi      = DensityBasinInformation.Run(rwResult, parameters.Sensitivity);
         string line     =
             (float.IsNaN(ac)  ? FLOAT_PATTERN : ac.ToString(FLOAT_PATTERN)) + SEPARATOR +
             (float.IsNaN(ic)  ? FLOAT_PATTERN : ic.ToString(FLOAT_PATTERN)) + SEPARATOR +
             (float.IsNaN(pic) ? FLOAT_PATTERN : pic.ToString(FLOAT_PATTERN)) + SEPARATOR +
             (float.IsNaN(dbi) ? FLOAT_PATTERN : dbi.ToString(FLOAT_PATTERN));
         dataBuilder.AppendLine(line);
         callback(connectionId, step);
     }
 }
        public void Test1()
        {
            AbstractChromosomeFactory factory = new SolutionFactory();

            int[] routeWeights = new int[]
            {
                20000, 50000, 120000, 200000, 350000
            };
            int distanceWeight = 1;

            string[] customerTypes = new string[] { "C1", "C2", "R1", "R2", "RC1", "RC2" };
            Dictionary <string, int> customerNumbers = new Dictionary <string, int>()
            {
                { "2", 20000 }, { "4", 50000 }, { "6", 120000 }, { "8", 200000 }, { "10", 350000 }
            };

            string[] customerInstances = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

            CrossoverOperator[] crossoverOps = new CrossoverOperator[]
            {
                new OrderCrossover(), new PartiallyMatchedCrossover(), new CycleCrossover(), new UniformBasedOrderCrossover()
            };

            MutationOperator[] mutationOps = new MutationOperator[]
            {
                new SwapOperator(), new InsertionOperator(), new InversionOperator(), new DisplacementOperator()
            };

            int randomWalkNumber = 2000, randomWalkSteps = 5000;

            string floatPattern = "0.000", separator = ",";
            float  epsilon = 0.05f;

            foreach (var type in customerTypes)
            {
                foreach (var number in customerNumbers)
                {
                    foreach (var instance in customerInstances)
                    {
                        string          instanceId = type + '_' + number.Key + '_' + instance;
                        VrptwProblem    problem    = reader.ReadFromFile(FILE_PATH + @"\" + instanceId + ".txt");
                        FitnessFunction ff         = new FitnessFunction(number.Value, distanceWeight);
                        Landscape       landscape  = new Landscape(problem, factory, ff);
                        foreach (var op in crossoverOps)
                        {
                            string path = RESULT_PATH + @"\" + instanceId + "_" + op.GetId() + ".csv";
                            if (!File.Exists(path))
                            {
                                File.Create(path).Close();
                                File.ReadAllText(path);
                                using (TextWriter tw = new StreamWriter(path))
                                {
                                    tw.WriteLine("AC, IC, PIC, DBI");
                                    for (int i = 0; i < randomWalkNumber; ++i)
                                    {
                                        var    rwResult = landscape.RandomWalk(randomWalkSteps, op);
                                        float  ac       = Autocorrelation.Run(rwResult);
                                        float  ic       = InformationContent.Run(rwResult, epsilon);
                                        float  pic      = PartialInformationContent.Run(rwResult, epsilon);
                                        float  dbi      = DensityBasinInformation.Run(rwResult, epsilon);
                                        string line     =
                                            ac.ToString(floatPattern) + separator +
                                            ic.ToString(floatPattern) + separator +
                                            pic.ToString(floatPattern) + separator +
                                            dbi.ToString(floatPattern);
                                        tw.WriteLine(line);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }