Ejemplo n.º 1
0
    public void CompareWeightsReturnsExpectedResultOfDifferent()
    {
        // Arrange
        const int nInputs  = 9;
        const int nOutputs = 1;

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

        dna1.WeightsAndBiases.Should().HaveCount(100);
        dna1.WeightsAndBiases.Clear();
        dna1.WeightsAndBiases.AddRange(Enumerable.Repeat(1.00, 100));
        Dna dna2 = Dna.Clone(dna1);

        dna1.WeightsAndBiases.Should().Equal(dna2.WeightsAndBiases);
        dna2.WeightsAndBiases[2] = -49.00; // aggregate absolute weight difference of dna2 vs dna1 should now be 50
        dna1.WeightsAndBiases.Should().NotEqual(dna2.WeightsAndBiases);

        // Act
        var comparisonResult = DnaUtils.CompareWeights(dna1, dna2);

        // Assert
        float  percentWeightDiff      = comparisonResult.Item1;
        double percentWeightValueDiff = comparisonResult.Item2;

        percentWeightDiff.Should().Be(0.01f);     // ie. 1% of weights differ in value
        percentWeightValueDiff.Should().Be(0.5f); // ie. total weight values of dna2 differ by 50% (NOT 50% bigger)
    }
Ejemplo n.º 2
0
    public void CompareWeightsReturnsExpectedResultOfIdentical()
    {
        // Arrange
        const int nInputs  = 20;
        const int nOutputs = 8;

        int[] hiddenLayers = new int[] { 30, 4, 12 };
        Dna   dna1         = Dna.GenerateRandomDnaEncoding(nInputs, hiddenLayers, nOutputs, ActivationType.LeakyRelu, true);
        Dna   dna2         = Dna.Clone(dna1);

        // Act
        var comparisonResult = DnaUtils.CompareWeights(dna1, dna2);

        // Assert
        float  percentWeightDiff      = comparisonResult.Item1;
        double percentWeightValueDiff = comparisonResult.Item2;

        percentWeightDiff.Should().Be(0);
        percentWeightValueDiff.Should().Be(0);
    }
Ejemplo n.º 3
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();
    }