コード例 #1
0
        public SolutionInstance GetBestNeighbors()
        {
            SolutionInstance best = Neighbors.OrderBy(c => c.Cost).First();

            if (best.Cost < Cost)
            {
                return(best.GetBestNeighbors());
            }
            return(this);
        }
コード例 #2
0
        internal SolutionInstance DoCreateInstance(IReadOnlyList <int> coords)
        {
            var s = CreateInstance(coords);

            if (_bestEver == null || _bestEver.Cost > s.Cost)
            {
                _bestEver = s;
            }
            return(s);
        }
コード例 #3
0
        public SolutionInstance GetRandomInstance()
        {
            SolutionInstance s = CreateInstance(Dimensions.Select(c => _random.Next(c)).ToArray()).GetBestNeighbors();

            if (_bestResult == null || s.Cost < _bestResult.Cost)
            {
                _bestResult = s;
            }
            return(s);
        }
コード例 #4
0
        public SolutionInstance CreateInstance(IReadOnlyList <int> coordinates)
        {
            var s = DoCreateInstance(coordinates);

            if (_theBest == null || s.Cost < _theBest.Cost)
            {
                _theBest = s;
            }
            return(s);
        }
コード例 #5
0
        public override IEnumerable <SolutionInstance> GeneratePopulation(IEnumerable <SolutionInstance> previousPopulation, int populationSize)
        {
            List <SolutionInstance> newPopulation = new List <SolutionInstance>();

            if (previousPopulation == null)
            {
                for (int i = 0; i < populationSize; i++)
                {
                    SolutionInstance randomInstance = _space.GetRandomInstance();
                    newPopulation.Add(randomInstance);
                }
                return(newPopulation);
            }

            // Arbitrary number
            int breakingIndex = _space.Random.Next(1, _space.Dimensions.Count() - 1);

            int breedingsDone;

            if (_introduceDiversity && ComputeDiversity(previousPopulation) < _diversityThreshold)
            {
                breedingsDone = (int)Math.Round((populationSize / 2 * (1 - _diversityFactor)));
            }
            else
            {
                breedingsDone = populationSize / 2;
            }

            // Each crossover brings two new children
            for (int i = 0; i < breedingsDone; i++)
            {
                SolutionInstance[] parents = previousPopulation.Skip(_space.Random.Next(previousPopulation.Count() - 1)).Take(2).ToArray();
                SolutionInstance   father  = parents[0];
                SolutionInstance   mother  = parents[1];

                int[] childOneCoordinates = father.Coordinates.Take(breakingIndex).Concat(mother.Coordinates.Skip(breakingIndex).Take(_space.Dimensions.Count() - breakingIndex)).ToArray();
                int[] childTwoCoordinates = mother.Coordinates.Take(breakingIndex).Concat(father.Coordinates.Skip(breakingIndex).Take(_space.Dimensions.Count() - breakingIndex)).ToArray();

                SolutionInstance childOne = _space.CreateInstance(childOneCoordinates);
                SolutionInstance childTwo = _space.CreateInstance(childTwoCoordinates);
                newPopulation.Add(childOne);
                newPopulation.Add(childTwo);
            }

            int newPopulationSize = newPopulation.Count();

            while (newPopulationSize < populationSize)
            {
                newPopulation.Add(_space.GetRandomInstance());
                newPopulationSize++;
            }
            return(newPopulation);
        }
コード例 #6
0
        public static SolutionInstance Best(this IEnumerable <SolutionInstance> candidates)
        {
            SolutionInstance best = null;

            foreach (var s in candidates)
            {
                if (best == null || s.Cost < best.Cost)
                {
                    best = s;
                }
            }
            return(best);
        }
コード例 #7
0
        /// <summary>
        /// Solves the problem associated with the solution space.
        /// </summary>
        /// <returns></returns>
        public SolutionInstance Solve(Func <SolutionInstance, IEnumerable <SolutionInstance>, bool> endCondition, int populationSizePerIteration, int maxIterations)
        {
            SolutionInstance best = null;

            IEnumerable <SolutionInstance> population = null;

            int currentIteration = 0;

            while (!endCondition(best, population) && currentIteration < maxIterations)
            {
                population = GeneratePopulation(population, populationSizePerIteration);
                population = FindBestIndividualsInPopulation(population);
                best       = _space.TheBest;
                currentIteration++;
            }
            return(best);
        }
コード例 #8
0
        public override IEnumerable <SolutionInstance> FindBestIndividualsInPopulation(IEnumerable <SolutionInstance> population)
        {
            double threshold = population.Average((individual) => individual.Cost);

            return(population.Select((individual) => {
                SolutionInstance bestNeighbor = individual.BestAmongNeigbors();
                if (bestNeighbor.Cost < individual.Cost)
                {
                    return bestNeighbor;
                }
                else
                {
                    // Instead of only returning the individual, let's create a whole new individual to give us more diversity
                    return _space.GetRandomInstance();
                }
            }).Where((individual) => individual.Cost <= threshold).ToArray());
        }
コード例 #9
0
 public SolutionInstance CrossWith(SolutionInstance mom)
 {
     return(null);
 }