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 }); }
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(); }