public void DisplayTest() { var range = Enumerable.Range(-5, 11).ToArray(); var geneSet = (from n in range from d in range.Where(i => i != 0) select new Fraction(n, d)) .Distinct().OrderBy(g => g); IReadOnlyList <Fraction> FnGenesToInputs(IReadOnlyList <Fraction> genes1) => genes1; var genes = geneSet.OrderBy(g => Rand.Random.Next()).Take(4).ToList(); var fitness = new Fitness(new Fraction(42)); var chromosome = new Chromosome <Fraction, Fitness>(genes, fitness); var watch = Stopwatch.StartNew(); Display(chromosome, watch, FnGenesToInputs); }
private static void SolveUnknown(int numUnknowns, Fraction[] geneSet, FnEquationDelegate[] equations, FnGenesToImputsDelegate fnGenesToImputs) { var watch = Stopwatch.StartNew(); var maxAge = 50; var window = new Window(Math.Max(1, geneSet.Length / (2 * maxAge)), Math.Max(1, geneSet.Length / 3), geneSet.Length / 2); var geneIndexes = Enumerable.Range(0, numUnknowns).ToArray(); var sortedGeneSet = geneSet.OrderBy(gene => gene).ToList(); void FnDispaly(Chromosome <Fraction, Fitness> candidate) => Display(candidate, watch, fnGenesToImputs); Fitness FnGetFitness(IReadOnlyList <Fraction> genes) => GetFitness(genes, equations); void FnMutate(List <Fraction> genes) => Mutate(genes, sortedGeneSet, window, geneIndexes); var optimalFitness = new Fitness(new Fraction(0)); var best = Genetic <Fraction, Fitness> .GetBest(FnGetFitness, numUnknowns, optimalFitness, geneSet, FnDispaly, FnMutate, null, maxAge); Assert.IsTrue(optimalFitness.CompareTo(best.Fitness) <= 0); Assert.AreEqual(0.0, (float)best.Fitness.TotalDifference, 0.0001); }
public void ReferenceEqualsTest() { var fitness = new Fitness(new Fraction(3, 2)); Assert.IsTrue(fitness.CompareTo(fitness) == 0); }
public void ToStringTest() { var fitness = new Fitness(new Fraction(2, 3)); Assert.AreEqual("diff: 0.67", fitness.ToString()); }