github.com/yaricom/goNEAT@v0.0.0-20210507221059-e2110b885482/neat/neat_test.go (about)

     1  package neat
     2  
     3  import (
     4  	"testing"
     5  	"os"
     6  	"fmt"
     7  	"github.com/yaricom/goNEAT/neat/utils"
     8  )
     9  
    10  func TestLoadContext(t *testing.T) {
    11  	config, err := os.Open("../data/xor_test.neat")
    12  	if err != nil {
    13  		t.Error("Failed to open config file", err)
    14  	}
    15  
    16  	// Load Neat Context
    17  	nc := LoadContext(config)
    18  	checkNeatContext(nc, t)
    19  }
    20  
    21  func TestNeatContext_LoadContext(t *testing.T) {
    22  	config, err := os.Open("../data/xor_test.neat.yml")
    23  	if err != nil {
    24  		t.Error("Failed to open config file", err)
    25  	}
    26  
    27  	// Load YAML context
    28  	nc := NewNeatContext()
    29  	err = nc.LoadContext(config)
    30  	if err != nil {
    31  		t.Error(err)
    32  	}
    33  
    34  	checkNeatContext(nc, t)
    35  
    36  	// check activators
    37  	if len(nc.NodeActivators) != 4 {
    38  		t.Error(fmt.Sprintf("len(nc.NodeActivators) != 4, but: %d", len(nc.NodeActivators)))
    39  		return
    40  	}
    41  	activators := []utils.NodeActivationType{utils.SigmoidBipolarActivation,
    42  		utils.GaussianBipolarActivation, utils.LinearAbsActivation, utils.SineActivation}
    43  	probs := []float64{0.25, 0.35, 0.15, 0.25}
    44  	for i, a := range activators {
    45  		if nc.NodeActivators[i] != a {
    46  			t.Error(fmt.Sprintf("Wrong CPPN activator type at: %d", i))
    47  		}
    48  		if nc.NodeActivatorsProb[i] != probs[i] {
    49  			t.Error("nc.NodeActivatorsProb[i] != probs[i]", nc.NodeActivatorsProb[i], probs[i])
    50  		}
    51  	}
    52  }
    53  
    54  func checkNeatContext(nc *NeatContext, t *testing.T) {
    55  	if nc.TraitParamMutProb != 0.5 {
    56  		t.Error("nc.TraitParamMutProb != 0.5", nc.TraitParamMutProb)
    57  	}
    58  	if nc.TraitMutationPower != 1.0 {
    59  		t.Error("nc.TraitMutationPower != 1.0", nc.TraitMutationPower)
    60  	}
    61  	if nc.WeightMutPower != 2.5 {
    62  		t.Error("nc.WeightMutPower != 2.5", nc.WeightMutPower != 2.5)
    63  	}
    64  	if nc.DisjointCoeff != 1.0 {
    65  		t.Error("nc.DisjointCoeff != 1.0", nc.DisjointCoeff)
    66  	}
    67  	if nc.ExcessCoeff != 1.0 {
    68  		t.Error("nc.ExcessCoeff != 1.0", nc.ExcessCoeff)
    69  	}
    70  	if nc.MutdiffCoeff != 0.4 {
    71  		t.Error("nc.MutdiffCoeff", nc.MutdiffCoeff)
    72  	}
    73  	if nc.CompatThreshold != 3.0 {
    74  		t.Error("CompatThreshold", nc.CompatThreshold)
    75  	}
    76  	if nc.AgeSignificance != 1.0 {
    77  		t.Error("AgeSignificance", nc.AgeSignificance)
    78  	}
    79  	if nc.SurvivalThresh != 0.2 {
    80  		t.Error("SurvivalThresh", nc.SurvivalThresh)
    81  	}
    82  	if nc.MutateOnlyProb != 0.25 {
    83  		t.Error("MutateOnlyProb", nc.MutateOnlyProb)
    84  	}
    85  	if nc.MutateRandomTraitProb != 0.1 {
    86  		t.Error("MutateRandomTraitProb", nc.MutateRandomTraitProb)
    87  	}
    88  	if nc.MutateLinkTraitProb != 0.1 {
    89  		t.Error("MutateLinkTraitProb", nc.MutateLinkTraitProb)
    90  	}
    91  	if nc.MutateNodeTraitProb != 0.1 {
    92  		t.Error("MutateNodeTraitProb", nc.MutateNodeTraitProb)
    93  	}
    94  	if nc.MutateLinkWeightsProb != 0.9 {
    95  		t.Error("MutateLinkWeightsProb", nc.MutateLinkWeightsProb)
    96  	}
    97  	if nc.MutateToggleEnableProb != 0.0 {
    98  		t.Error("MutateToggleEnableProb", nc.MutateToggleEnableProb)
    99  	}
   100  	if nc.MutateGeneReenableProb != 0.0 {
   101  		t.Error("MutateGeneReenableProb", nc.MutateGeneReenableProb)
   102  	}
   103  	if nc.MutateAddNodeProb != 0.03 {
   104  		t.Error("MutateAddNodeProb", nc.MutateAddNodeProb)
   105  	}
   106  	if nc.MutateAddLinkProb != 0.08 {
   107  		t.Error("MutateAddLinkProb", nc.MutateAddLinkProb)
   108  	}
   109  	if nc.MutateConnectSensors != 0.5 {
   110  		t.Error("MutateConnectSensors", nc.MutateConnectSensors)
   111  	}
   112  	if nc.InterspeciesMateRate != 0.001 {
   113  		t.Error("InterspeciesMateRate", nc.InterspeciesMateRate)
   114  	}
   115  	if nc.MateMultipointProb != 0.3 {
   116  		t.Error("MateMultipointProb", nc.MateMultipointProb)
   117  	}
   118  	if nc.MateMultipointAvgProb != 0.3 {
   119  		t.Error("MateMultipointAvgProb", nc.MateMultipointAvgProb)
   120  	}
   121  	if nc.MateSinglepointProb != 0.3 {
   122  		t.Error("MateSinglepointProb", nc.MateSinglepointProb)
   123  	}
   124  	if nc.MateOnlyProb != 0.2 {
   125  		t.Error("MateOnlyProb", nc.MateOnlyProb)
   126  	}
   127  	if nc.RecurOnlyProb != 0.0 {
   128  		t.Error("RecurOnlyProb", nc.RecurOnlyProb)
   129  	}
   130  	if nc.PopSize != 200 {
   131  		t.Error("PopSize", nc.PopSize)
   132  	}
   133  	if nc.DropOffAge != 50 {
   134  		t.Error("DropOffAge", nc.DropOffAge)
   135  	}
   136  	if nc.NewLinkTries != 50 {
   137  		t.Error("NewLinkTries", nc.NewLinkTries)
   138  	}
   139  	if nc.PrintEvery != 10 {
   140  		t.Error("PrintEvery", nc.PrintEvery)
   141  	}
   142  	if nc.BabiesStolen != 0 {
   143  		t.Error("BabiesStolen", nc.BabiesStolen)
   144  	}
   145  	if nc.NumRuns != 100 {
   146  		t.Error("NumRuns", nc.NumRuns)
   147  	}
   148  	if nc.NumGenerations != 100 {
   149  		t.Error("NumGenerations", nc.NumGenerations)
   150  	}
   151  	if nc.EpochExecutorType != 0 {
   152  		t.Error("EpochExecutorType", nc.EpochExecutorType)
   153  	}
   154  	if nc.GenCompatMethod != 1 {
   155  		t.Error("GenCompatMethod", nc.GenCompatMethod)
   156  	}
   157  }