Exemple #1
0
        public Chromosome Run(IProblem problem, Parameters parameters)
        {
            Parameters        = parameters;
            chromosomeFactory = parameters.ChromosomeFactory;
            heuristics        = parameters.Heuristics;
            var population = Initialization(problem, parameters);
            int counter    = 0;
            var watch      = System.Diagnostics.Stopwatch.StartNew();

            watch.Start();
            bool       conv = false;
            Chromosome theBestOne = population[0], localBest;

            do
            {
                population = Evolve(problem, parameters, population);
                population = Improve(problem, parameters, population);
                if (conv = IsConvergent(population, parameters))
                {
                    population = Restart(problem, population, parameters);
                }
                localBest  = GetTheBestChromosome(population);
                theBestOne = theBestOne.CompareTo(localBest) < 0 ? theBestOne : localBest;
            } while (!StopCondition(++counter, parameters));
            //Chromosome theBestSolution = GetTheBestChromosome(population);
            Console.WriteLine("\nThe best solution: " + theBestOne.ToString());
            return(theBestOne);
        }
Exemple #2
0
        /// <summary>
        /// Runs the memetic algorithm to solve the problem.
        /// </summary>
        /// <param name="problem">The problem.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns>The best found solution.</returns>
        public async Task <Chromosome> Run(IProblem problem, Parameters parameters, Action <string, Chromosome, int, float, long, int> callback, string id, bool genetic)
        {
            Parameters        = parameters;
            chromosomeFactory = parameters.ChromosomeFactory;
            heuristics        = parameters.Heuristics;

            Population population = new Population(parameters.PopulationSize);
            Chromosome solution   = null;
            int        index      = 0;

            while (index < parameters.PopulationSize)
            {
                solution = parameters.ChromosomeFactory.RandomSolution(parameters.GeneCount, problem);
                if (!population.Contains(solution))
                {
                    population[index] = solution;
                    ++index;
                }
            }
            var watch = System.Diagnostics.Stopwatch.StartNew();

            watch.Start();
            Chromosome theBestOne = population[0], localBest;
            int        iteration = 0, restarts = 0;
            await Task.Run(() =>
            {
                do
                {
                    if (source.IsCancellationRequested)
                    {
                        break;
                    }
                    population = Evolve(problem, parameters, population);
                    if (IsConvergent(population, parameters))
                    {
                        population = Restart(problem, population, parameters);
                        ++restarts;
                    }
                    localBest  = GetTheBestChromosome(population);
                    theBestOne = theBestOne.CompareTo(localBest) < 0 ? theBestOne : localBest;
                    callback(id, theBestOne, iteration, (theBestOne as Solution).TotalDistance(),
                             watch.ElapsedMilliseconds, restarts);
                } while (!StopCondition(++iteration, parameters));
            }, source.Token);

            source.Dispose();
            source = new CancellationTokenSource();
            return(theBestOne);
        }
Exemple #3
0
        public Task <Chromosome> Run(IProblem problem, Parameters parameters, Action <string, Chromosome, int, float, long, int> callback, string id)
        {
            chromosomeFactory = parameters.ChromosomeFactory;
            heuristics        = parameters.Heuristics;
            var population = Initialization(problem, parameters);
            int iteration = 0, restarts = 0;
            var watch = System.Diagnostics.Stopwatch.StartNew();

            watch.Start();
            Chromosome theBestOne = population[0], localBest;
            var        result = Task.Run(() =>
            {
                do
                {
                    if (source.IsCancellationRequested)
                    {
                        break;
                    }
                    population = Evolve(problem, parameters, population);
                    population = Improve(problem, parameters, population);
                    if (IsConvergent(population, parameters))
                    {
                        population = Restart(problem, population, parameters);
                        ++restarts;
                    }
                    localBest  = GetTheBestChromosome(population);
                    theBestOne = theBestOne.CompareTo(localBest) < 0 ? theBestOne : localBest;
                    callback(id, theBestOne, iteration, (theBestOne as Solution).TotalDistance(),
                             watch.ElapsedMilliseconds, restarts);
                } while (!StopCondition(++iteration, parameters));
                return(theBestOne);
            }, source.Token);

            //Chromosome theBestSolution = GetTheBestChromosome(population);
            source.Dispose();
            source = new CancellationTokenSource();
            return(result);
        }
    private void Awake()//When the program starts
    {
        Heuristic = new ManhattanDistance();

        Options.onValueChanged.AddListener((int value) =>
        {
            if (value == 0)
            {
                Heuristic = new ManhattanDistance();
            }
            else if (value == 1)
            {
                Heuristic = new DiagonalDistance();
            }
            else if (value == 2)
            {
                Heuristic = new EuclideanDistance();
            }
        });

        ObjPosition   = ObjToMove.transform;
        GridReference = GetComponent <Grid>();//Get a reference to the game manager
    }