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

     1  package genetics
     2  
     3  import (
     4  	"testing"
     5  	"github.com/yaricom/goNEAT/neat"
     6  	"math/rand"
     7  )
     8  
     9  func runSequentialPopulationEpochExecutor_NextEpoch(pop *Population, conf *neat.NeatContext) error {
    10  	ex := SequentialPopulationEpochExecutor{}
    11  
    12  	for i := 0; i < 100; i++ {
    13  		err := ex.NextEpoch(i + 1, pop, conf)
    14  		if err != nil {
    15  			return err
    16  		}
    17  	}
    18  	return nil
    19  
    20  }
    21  
    22  func runParallelPopulationEpochExecutor_NextEpoch(pop *Population, conf *neat.NeatContext) error {
    23  	ex := ParallelPopulationEpochExecutor{}
    24  
    25  	for i := 0; i < 100; i++ {
    26  		err := ex.NextEpoch(i + 1, pop, conf)
    27  		if err != nil {
    28  			return err
    29  		}
    30  	}
    31  	return nil
    32  }
    33  
    34  func TestPopulationEpochExecutor_NextEpoch(t *testing.T) {
    35  	rand.Seed(42)
    36  	in, out, nmax, n := 3, 2, 15, 3
    37  	recurrent := false
    38  	link_prob := 0.8
    39  	conf := neat.NeatContext{
    40  		CompatThreshold:0.5,
    41  		DropOffAge:1,
    42  		PopSize: 30,
    43  		BabiesStolen:10,
    44  		RecurOnlyProb:0.2,
    45  	}
    46  	neat.LogLevel = neat.LogLevelInfo
    47  	gen := newGenomeRand(1, in, out, n, nmax, recurrent, link_prob)
    48  	pop, err := NewPopulation(gen, &conf)
    49  	if err != nil {
    50  		t.Error(err)
    51  	}
    52  	if pop == nil {
    53  		t.Error("pop == nil")
    54  	}
    55  
    56  	// test sequential executor
    57  	err = runSequentialPopulationEpochExecutor_NextEpoch(pop, &conf)
    58  	if err != nil {
    59  		t.Error(err)
    60  	}
    61  
    62  	// test parallel executor
    63  	err = runParallelPopulationEpochExecutor_NextEpoch(pop, &conf)
    64  	if err != nil {
    65  		t.Error(err)
    66  	}
    67  }