예제 #1
0
        public ICandidate[] CrossOver(ICandidate parentA, ICandidate parentB, int crossOverIndex)
        {
            //true true, false , false, true
            var piA = parentA.GetGenomes();
            var piB = parentB.GetGenomes();

            var childAItems = piA.Substring(0, crossOverIndex) + piB.Substring(crossOverIndex, geneCount - crossOverIndex);
            var childBItems = piB.Substring(0, crossOverIndex) + piA.Substring(crossOverIndex, geneCount - crossOverIndex);

            var childA = new Knapsack(childAItems);
            var childB = new Knapsack(childBItems);

            return(new Knapsack[] { childA, childB });
        }
예제 #2
0
        static void Main(string[] args)
        {
            //setup variables
            var nrOfItems      = 100;
            var weightLimit    = 1000;
            var lowerLimit     = 0;
            var upperLimit     = 300;
            var mutationRate   = 0.5;
            var populationSize = 2500;
            var crossOverRate  = 0.5;

            Knapsack.Initialize(weightLimit, nrOfItems);
            ItemList.Initialize(lowerLimit, upperLimit, nrOfItems);

            IGeneticAlgorithmImpl solver = new Solver(ItemList.Items, populationSize, crossOverRate, mutationRate);

            solver.Run();
        }