Exemple #1
0
        private IChromosome CreateChild(IChromosome parent, List <int> pair)
        {
            var parentCP   = parent as CPChromosome;
            var parentList = parentCP.GetValues().ToList();
            var childList  = new List <int>();

            childList.Add(pair[0]);
            childList.Add(pair[1]);

            parentList.ForEach(v =>
            {
                if (!childList.Contains(v))
                {
                    childList.Add(v);
                }
            });

            var child = new CPChromosome(childList.Count, childList.ToArray());

            return(child);
        }
Exemple #2
0
        public CPSetup(int populationSize, string filename = "")
        {
            if (String.IsNullOrEmpty(filename))
            {
                Console.WriteLine("Podaj nazwę pliku z grafem: ");
                var builder = new StringBuilder();
                builder.Append("Data/");
                builder.Append(Console.ReadLine());
                filename = builder.ToString();
            }
            Graph = new Graph(@filename);
            int[] startValues = Graph.OddVertices.ToArray();
            Fitness     = new CPFitness();
            Chromosome  = new CPChromosome(startValues.Length, startValues, populationSize);
            Population  = new Population(populationSize, populationSize, Chromosome);
            Selection   = new EliteSelection();
            Crossover   = new CPCrossover();
            Mutation    = new CPMutation();
            Termination = new FitnessStagnationTermination(50);

            GA             = new GeneticAlgorithm(Population, Fitness, Selection, Crossover, Mutation);
            GA.Termination = Termination;
        }
Exemple #3
0
        public CPSetup(IPopulation population, ISelection selection, ICrossover crossover, IMutation mutation, ITermination termination,
                       string filename = "")
        {
            if (String.IsNullOrEmpty(filename))
            {
                Console.WriteLine("Podaj nazwę pliku z grafem: ");
                var builder = new StringBuilder();
                builder.Append("Data/");
                builder.Append(Console.ReadLine());
                filename = builder.ToString();
            }
            Graph = new Graph(@filename);
            int[] startValues = Graph.OddVertices.ToArray();
            Fitness     = new CPFitness();
            Chromosome  = new CPChromosome(startValues.Length, startValues, population.MaxSize);
            Population  = population;
            Selection   = selection;
            Crossover   = crossover;
            Mutation    = mutation;
            Termination = termination;

            GA             = new GeneticAlgorithm(Population, Fitness, Selection, Crossover, Mutation);
            GA.Termination = Termination;
        }