/// <inheritdoc/> public override void Copy(IGenome source) { IntegerArrayGenome sourceInt = (IntegerArrayGenome)source; EngineArray.ArrayCopy(sourceInt.data, this.data); this.Score = source.Score; this.AdjustedScore = source.AdjustedScore; }
public void TestCompare() { BasicGenome genome1 = new IntegerArrayGenome(1); genome1.AdjustedScore = 10; genome1.Score = 4; BasicGenome genome2 = new IntegerArrayGenome(1); genome2.AdjustedScore = 4; genome2.Score = 10; MinimizeAdjustedScoreComp comp = new MinimizeAdjustedScoreComp(); Assert.IsTrue(comp.Compare(genome1, genome2) > 0); }
/// <summary> /// Get a list of the genes that have not been taken before. This is useful /// if you do not wish the same gene to appear more than once in a /// genome. /// </summary> /// <param name="source">The pool of genes to select from.</param> /// <param name="taken"> An array of the taken genes.</param> /// <returns>Those genes in source that are not taken.</returns> private static int GetNotTaken(IntegerArrayGenome source, HashSet<int> taken) { foreach (int trial in source.Data) { if (!taken.Contains(trial)) { taken.Add(trial); return trial; } } throw new GeneticError("Ran out of integers to select."); }
/// <summary> /// Construct the genome by copying another. /// </summary> /// <param name="other">The other genome.</param> public IntegerArrayGenome(IntegerArrayGenome other) { this.data = (int[])other.Data.Clone(); }
/// <inheritdoc/> public void Copy(IArrayGenome source, int sourceIndex, int targetIndex) { IntegerArrayGenome sourceInt = (IntegerArrayGenome)source; this.data[targetIndex] = sourceInt.data[sourceIndex]; }
private IntegerArrayGenome RandomGenome() { Random rnd = new Random(); IntegerArrayGenome result = new IntegerArrayGenome(cities.Length); int[] organism = result.Data; bool[] taken = new bool[cities.Length]; for (int i = 0; i < organism.Length - 1; i++) { int icandidate; do { icandidate = (int) (rnd.NextDouble() * organism.Length); } while (taken[icandidate]); organism[i] = icandidate; taken[icandidate] = true; if (i == organism.Length - 2) { icandidate = 0; while (taken[icandidate]) { icandidate++; } organism[i + 1] = icandidate; } } return result; }