コード例 #1
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());
            }
        }
コード例 #2
0
        public static async Task <Chromosome> DoMa(string filename, IOperator op)
        {
            var problem    = new VrptwProblemReader().ReadFromFile(filename);
            var crossovers = new List <CrossoverOperator>();
            var mutations  = new List <MutationOperator> {
            };

            if (op is CrossoverOperator)
            {
                crossovers.Add(op as CrossoverOperator);
            }
            else
            {
                mutations.Add(op as MutationOperator);
            }

            var parameters = new Parameters()
            {
                ChromosomeFactory    = new SolutionFactory(),
                ConvergenceLimit     = 0.15f,
                CrossoverOperators   = crossovers,
                CrossoverProbability = 0.8f,
                EliteChildrenCount   = 2,
                Fitness                    = new FitnessFunction(200000, 1),
                FitnessStrategy            = FitnessStrategy.MINIMIZE,
                GeneCount                  = problem.GeneCount(),
                Heuristics                 = new SimulatedAnnealing(),
                HeuristicsParameters       = new float[] { 100, 0.90f, 50 },
                MaxIterations              = 300,
                MutationOperators          = mutations,
                MutationProbability        = 0.2f,
                PopulationSize             = 200,
                PreservedChromosomesNumber = 2,
                Selection                  = new TournamentSelection()
            };
            MemeticAlgorithm ma  = new MemeticAlgorithm();
            Chromosome       sol = await ma.Run(problem, parameters, CallBackMa, op.GetId(), true);

            return(sol);
        }