Пример #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);
     }
 }
Пример #2
0
        public void RunSingleResearch(Landscape landscape, ResearchParameters parameters, IOperator op, string instanceId,
                                      string inputPath, string outputPath, Action <string, float> callback, string connectionId, float step)
        {
            string path = outputPath + @"\" + instanceId + "_" + op.GetId() + ".csv";

            if (File.Exists(path))
            {
                callback(connectionId, step * (parameters.RandomWalkNumber));
                return;
            }
            StringBuilder dataBuilder  = new StringBuilder();
            int           threadNumber = Environment.ProcessorCount;

            Thread[] rwThreads      = new Thread[threadNumber];
            int      tasksPerThread = parameters.RandomWalkNumber / threadNumber;

            for (int i = 0; i < threadNumber; ++i)
            {
                int start = i * tasksPerThread;
                int end   = (i + 1) * tasksPerThread;
                if (i == threadNumber - 1)
                {
                    end += parameters.RandomWalkNumber % threadNumber;
                }
                rwThreads[i] = new Thread(() =>
                {
                    DoRandomWalkThread(start, end, landscape, parameters, op,
                                       dataBuilder, callback, connectionId, step);
                });
                rwThreads[i].Start();
            }
            foreach (Thread thread in rwThreads)
            {
                thread.Join();
            }
            File.Create(path).Close();
            using (TextWriter tw = new StreamWriter(path))
            {
                tw.WriteLine(CSV_HEADER);
                tw.Write(dataBuilder.ToString());
            }
        }
Пример #3
0
        public void Run(ResearchParameters parameters, string inputPath, string outputPath, Action <string, float> callback, string connectionId)
        {
            int distanceWeight = 1;
            Dictionary <string, int> customerNumbers = new Dictionary <string, int>()
            {
                { "1", parameters.RoadWeight200 },
                { "2", parameters.RoadWeight200 },
                //{ "4", parameters.RoadWeight400 },
                //{ "6", parameters.RoadWeight600 }, { "8", parameters.RoadWeight800 },
                //{ "10", parameters.RoadWeight1000 }
            };

            float step = 1.0f /
                         (CUSTOMER_TYPES.Count() * customerNumbers.Count * CUSTOMER_INSTANCES.Count() *
                          (parameters.CrossoverOperators.Count + parameters.MutationOperators.Count) * parameters.RandomWalkNumber);

            foreach (var type in CUSTOMER_TYPES)
            {
                foreach (var number in customerNumbers)
                {
                    for (int index = 0; index < CUSTOMER_INSTANCES.Count(); ++index)
                    {
                        string          instanceId = type + '_' + number.Key + '_' + CUSTOMER_INSTANCES[index];
                        VrptwProblem    problem    = reader.ReadFromFile(inputPath + @"\" + instanceId + ".txt");
                        FitnessFunction ff         = new FitnessFunction(number.Value, distanceWeight);
                        Landscape       landscape  = new Landscape(problem, factory, ff);
                        foreach (var op in parameters.CrossoverOperators)
                        {
                            RunSingleResearch(landscape, parameters, op, instanceId, inputPath, outputPath,
                                              callback, connectionId, step);
                        }
                        foreach (var op in parameters.MutationOperators)
                        {
                            RunSingleResearch(landscape, parameters, op, instanceId, inputPath, outputPath,
                                              callback, connectionId, step);
                        }
                    }
                }
            }
        }