예제 #1
0
    public void EqualsTest()
    {
        // Arrange
        const int nInputs  = 9;
        const int nOutputs = 1;

        int[] hiddenLayers       = new int[] { 9 };
        Dna   dna1               = Dna.GenerateRandomDnaEncoding(nInputs, hiddenLayers, nOutputs, ActivationType.LeakyRelu, true);
        Dna   dna1ValueClone     = Dna.Clone(dna1);
        Dna   dna1ReferenceClone = dna1;
        Dna   dna2               = Dna.Clone(dna1);

        dna2.WeightsAndBiases[0] = 50;

        // Act/Assert
        dna1.Equals(dna1).Should().BeTrue();

        dna1.Equals(dna1ReferenceClone).Should().BeTrue();
        dna1ReferenceClone.Equals(dna1).Should().BeTrue();

        dna1.Equals(dna1ValueClone).Should().BeTrue();
        dna1ValueClone.Equals(dna1).Should().BeTrue();

        dna1.Equals(dna2).Should().BeFalse();
        dna2.Equals(dna1).Should().BeFalse();

        dna1ValueClone.Equals(dna2).Should().BeFalse();
        dna2.Equals(dna1ValueClone).Should().BeFalse();

        dna1ReferenceClone.Equals(dna2).Should().BeFalse();
        dna2.Equals(dna1ReferenceClone).Should().BeFalse();

        dna1.WeightsAndBiases[1] = 100;

        dna1.Equals(dna1ReferenceClone).Should().BeTrue(); // reference stays equal
        dna1ReferenceClone.Equals(dna1).Should().BeTrue();

        dna1.Equals(dna1ValueClone).Should().BeFalse(); // value changes
        dna1ValueClone.Equals(dna1).Should().BeFalse();
    }
예제 #2
0
    static void AssertDnaHeterogeneity(List <Dna> population)
    {
        population.Should().OnlyHaveUniqueItems(); // by reference

        var occurrences = population.ToDictionary(dna => dna, (_) => 1);

        for (int i = 0; i < population.Count; i++)
        {
            Dna pop = population[i];
            for (int j = 0; j < population.Count; j++)
            {
                if (i != j && pop.Equals(population[j]))
                {
                    occurrences[pop] += 1;
                }
            }
        }

        occurrences.Values.Should().OnlyContain(occurrenceCount => occurrenceCount == 1);
    }