Skip to content

Commit

Permalink
Merge pull request #5 from yaricom/goNeat-v4-migration
Browse files Browse the repository at this point in the history
Updated to use the latest version of `goNEAT`. Bump version.
  • Loading branch information
yaricom committed Nov 10, 2023
2 parents 4b4cb7c + 3bd489a commit f927808
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 142 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.21

- name: Build
run: go build -v ./...
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[![version](https://img.shields.io/github/v/tag/yaricom/goNEAT_NS.svg?sort=semver)](https://github.com/yaricom/goNEAT_NS/releases/latest)
[![Build Status](https://travis-ci.org/yaricom/goNEAT_NS.svg?branch=master)](https://travis-ci.org/yaricom/goNEAT_NS) [![GoDoc](https://godoc.org/github.com/yaricom/goNEAT_NS/neatns?status.svg)](https://godoc.org/github.com/yaricom/goNEAT_NS/neatns) [![Go version](https://img.shields.io/badge/go-1.17-blue.svg)](https://github.com/moovweb/gvm) [![license](https://img.shields.io/github/license/yaricom/goNEAT_NS.svg)](https://github.com/yaricom/goNEAT_NS/blob/master/LICENSE) [![yaricom/goNEAT](https://tokei.rs/b1/github/yaricom/goNEAT_NS?category=lines)](https://github.com/yaricom/goNEAT_NS)

| Branch | Tests | Coverage | Linting | Code Security |
|--------|--------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------|----------------------------------------------------------------------------|
| Branch | Tests | Coverage | Linting | Code Security |
|--------|-------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| master | [![CI](https://github.com/yaricom/goNEAT_NS/actions/workflows/ci.yml/badge.svg)](https://github.com/yaricom/goNEAT_NS/actions/workflows/ci.yml) | [![codecov](https://codecov.io/gh/yaricom/goNEAT_NS/branch/master/graph/badge.svg?token=as31613DnV)](https://codecov.io/gh/yaricom/goNEAT_NS) | [![Lint](https://github.com/yaricom/goNEAT_NS/actions/workflows/lint.yml/badge.svg)](https://github.com/yaricom/goNEAT_NS/actions/workflows/lint.yml) | [![CodeQL](https://github.com/yaricom/goNEAT_NS/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/yaricom/goNEAT_NS/actions/workflows/codeql-analysis.yml) |

## Overview
Expand All @@ -24,7 +24,7 @@ research is to test this hypothesis.

## Minimum Requirements

The source code written and compiled against GO 1.17.x.
The source code written and compiled against GO 1.21.x.

## Installation

Expand Down
4 changes: 4 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore maze runners
ignore:
- "examples/maze/maze_ns.go"
- "examples/maze/maze_obj.go"
35 changes: 20 additions & 15 deletions examples/maze/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ package maze

import (
"fmt"
"github.com/yaricom/goNEAT/v3/neat"
"github.com/yaricom/goNEAT/v3/neat/genetics"
"github.com/yaricom/goNEAT/v3/neat/network"
"github.com/yaricom/goNEAT_NS/v3/neatns"
"github.com/pkg/errors"
"github.com/yaricom/goNEAT/v4/neat"
"github.com/yaricom/goNEAT/v4/neat/genetics"
"github.com/yaricom/goNEAT/v4/neat/network"
"github.com/yaricom/goNEAT_NS/v4/neatns"
"math"
)

Expand Down Expand Up @@ -49,7 +50,11 @@ func mazeSimulationEvaluate(env *Environment, org *genetics.Organism, record *Ag
nItem := neatns.NewNoveltyItem()

// get Organism phenotype's network depth
netDepth, err := org.Phenotype.MaxActivationDepthFast(1) // The max depth of the network to be activated
phenotype, err := org.Phenotype()
if err != nil {
return nil, false, err
}
netDepth, err := phenotype.MaxActivationDepthWithCap(1) // The max depth of the network to be activated
if err != nil {
neat.DebugLog(fmt.Sprintf(
"Failed to estimate maximal depth of the network. Using default depth: %d", netDepth))
Expand All @@ -62,15 +67,15 @@ func mazeSimulationEvaluate(env *Environment, org *genetics.Organism, record *Ag

// initialize maze simulation's environment specific to the provided organism - this will be a copy
// of primordial environment provided
orgEnv, err := mazeSimulationInit(*env, org, netDepth)
orgEnv, err := mazeSimulationInit(*env, phenotype, netDepth)
if err != nil {
return nil, false, err
}

// do a specified amount of time steps emulations or while exit not found
steps := 0
for i := 0; i < orgEnv.TimeSteps && !orgEnv.ExitFound; i++ {
if err = mazeSimulationStep(orgEnv, org, netDepth); err != nil {
if err = mazeSimulationStep(orgEnv, phenotype, netDepth); err != nil {
return nil, false, err
}
// store agent path points at given sample size
Expand Down Expand Up @@ -117,9 +122,9 @@ func mazeSimulationEvaluate(env *Environment, org *genetics.Organism, record *Ag

// To initialize the maze simulation within provided environment copy and for given organism.
// Returns new environment for simulation against given organism
func mazeSimulationInit(env Environment, org *genetics.Organism, netDepth int) (*Environment, error) {
func mazeSimulationInit(env Environment, phenotype *network.Network, netDepth int) (*Environment, error) {
// flush the neural net
if _, err := org.Phenotype.Flush(); err != nil {
if _, err := phenotype.Flush(); err != nil {
neat.ErrorLog("Failed to flush phenotype")
return nil, err
}
Expand All @@ -132,14 +137,14 @@ func mazeSimulationInit(env Environment, org *genetics.Organism, netDepth int) (
// create neural net inputs from environment
if inputs, err := env.GetInputs(); err != nil {
return nil, err
} else if err = org.Phenotype.LoadSensors(inputs); err != nil { // load into neural net
} else if err = phenotype.LoadSensors(inputs); err != nil { // load into neural net
return nil, err
}

// propagate input through the phenotype net

// Use depth to ensure full relaxation
if _, err := org.Phenotype.ForwardSteps(netDepth); err != nil && err != network.ErrNetExceededMaxActivationAttempts {
if _, err := phenotype.ForwardSteps(netDepth); err != nil && !errors.Is(err, network.ErrNetExceededMaxActivationAttempts) {
neat.ErrorLog(fmt.Sprintf("Failed to activate network at simulation init: %s", err))
return nil, err
}
Expand All @@ -148,21 +153,21 @@ func mazeSimulationInit(env Environment, org *genetics.Organism, netDepth int) (
}

// To execute a time step of the maze simulation evaluation within given Environment for provided Organism
func mazeSimulationStep(env *Environment, org *genetics.Organism, netDepth int) error {
func mazeSimulationStep(env *Environment, phenotype *network.Network, netDepth int) error {
// get simulation parameters as inputs to organism's network
if inputs, err := env.GetInputs(); err != nil {
return err
} else if err = org.Phenotype.LoadSensors(inputs); err != nil {
} else if err = phenotype.LoadSensors(inputs); err != nil {
neat.ErrorLog("Failed to load sensors")
return err
}
if _, err := org.Phenotype.ForwardSteps(netDepth); err != nil && err != network.ErrNetExceededMaxActivationAttempts {
if _, err := phenotype.ForwardSteps(netDepth); err != nil && !errors.Is(err, network.ErrNetExceededMaxActivationAttempts) {
neat.ErrorLog(fmt.Sprintf("Failed to activate network at simulation init: %s", err))
return err
}

// use the net's outputs to change heading and velocity of maze agent
if err := env.ApplyOutputs(org.Phenotype.Outputs[0].Activation, org.Phenotype.Outputs[1].Activation); err != nil {
if err := env.ApplyOutputs(phenotype.Outputs[0].Activation, phenotype.Outputs[1].Activation); err != nil {
neat.ErrorLog("Failed to apply outputs")
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/maze/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package maze

import (
"github.com/stretchr/testify/assert"
"github.com/yaricom/goNEAT/v3/neat"
"github.com/yaricom/goNEAT/v4/neat"
"testing"
)

Expand Down
25 changes: 12 additions & 13 deletions examples/maze/maze_ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package maze
import (
"context"
"fmt"
"github.com/yaricom/goNEAT/v3/experiment"
"github.com/yaricom/goNEAT/v3/experiment/utils"
"github.com/yaricom/goNEAT/v3/neat"
"github.com/yaricom/goNEAT/v3/neat/genetics"
"github.com/yaricom/goNEAT_NS/v3/neatns"
"github.com/pkg/errors"
"github.com/yaricom/goNEAT/v4/experiment"
"github.com/yaricom/goNEAT/v4/experiment/utils"
"github.com/yaricom/goNEAT/v4/neat"
"github.com/yaricom/goNEAT/v4/neat/genetics"
"github.com/yaricom/goNEAT_NS/v4/neatns"
"math"
"os"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ type noveltySearchEvaluator struct {
compatAdjustFreq int
}

func (e noveltySearchEvaluator) TrialRunStarted(trial *experiment.Trial) {
func (e *noveltySearchEvaluator) TrialRunStarted(trial *experiment.Trial) {
opts := neatns.DefaultNoveltyArchiveOptions()
opts.KNNNoveltyScore = 10
trialSim = mazeSimResults{
Expand All @@ -58,17 +59,17 @@ func (e noveltySearchEvaluator) TrialRunStarted(trial *experiment.Trial) {
}
}

func (e noveltySearchEvaluator) TrialRunFinished(_ *experiment.Trial) {
func (e *noveltySearchEvaluator) TrialRunFinished(_ *experiment.Trial) {
// the last epoch executed
e.storeRecorded()
}

func (e noveltySearchEvaluator) EpochEvaluated(_ *experiment.Trial, _ *experiment.Generation) {
func (e *noveltySearchEvaluator) EpochEvaluated(_ *experiment.Trial, _ *experiment.Generation) {
// just stub
}

// GenerationEvaluate this method evaluates one epoch for given population and prints results into output directory if any.
func (e noveltySearchEvaluator) GenerationEvaluate(ctx context.Context, pop *genetics.Population, epoch *experiment.Generation) error {
func (e *noveltySearchEvaluator) GenerationEvaluate(ctx context.Context, pop *genetics.Population, epoch *experiment.Generation) error {
options, ok := neat.FromContext(ctx)
if !ok {
return neat.ErrNEATOptionsNotFound
Expand Down Expand Up @@ -110,9 +111,7 @@ func (e noveltySearchEvaluator) GenerationEvaluate(ctx context.Context, pop *gen
if epoch.Solved {
// print winner organism
org := epoch.Champion
if depth, err := org.Phenotype.MaxActivationDepthFast(0); err == nil {
neat.InfoLog(fmt.Sprintf("Activation depth of the winner: %d\n", depth))
}
utils.PrintActivationDepth(org, true)

genomeFile := "mazens_winner"
// Prints the winner organism's Genome to the file!
Expand Down Expand Up @@ -189,7 +188,7 @@ func (e *noveltySearchEvaluator) orgEvaluate(org *genetics.Organism, pop *geneti
// evaluate individual organism and get novelty point
nItem, solved, err := mazeSimulationEvaluate(e.mazeEnv, org, &record, nil)
if err != nil {
if err == ErrOutputIsNaN {
if errors.Is(err, ErrOutputIsNaN) {
// corrupted genome, but OK to continue evolutionary process
return false, nil
}
Expand Down
25 changes: 12 additions & 13 deletions examples/maze/maze_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package maze
import (
"context"
"fmt"
"github.com/yaricom/goNEAT/v3/experiment"
"github.com/yaricom/goNEAT/v3/experiment/utils"
"github.com/yaricom/goNEAT/v3/neat"
"github.com/yaricom/goNEAT/v3/neat/genetics"
"github.com/yaricom/goNEAT_NS/v3/neatns"
"github.com/pkg/errors"
"github.com/yaricom/goNEAT/v4/experiment"
"github.com/yaricom/goNEAT/v4/experiment/utils"
"github.com/yaricom/goNEAT/v4/neat"
"github.com/yaricom/goNEAT/v4/neat/genetics"
"github.com/yaricom/goNEAT_NS/v4/neatns"
"os"
)

Expand Down Expand Up @@ -38,25 +39,25 @@ type objectiveEvaluator struct {
compatAdjustFreq int
}

func (e objectiveEvaluator) TrialRunStarted(trial *experiment.Trial) {
func (e *objectiveEvaluator) TrialRunStarted(trial *experiment.Trial) {
trialSim = mazeSimResults{
trialID: trial.Id,
records: new(RecordStore),
archive: neatns.NewNoveltyArchive(archiveThresh, noveltyMetric, neatns.DefaultNoveltyArchiveOptions()),
}
}

func (e objectiveEvaluator) TrialRunFinished(_ *experiment.Trial) {
func (e *objectiveEvaluator) TrialRunFinished(_ *experiment.Trial) {
// the last epoch executed
e.storeRecorded()
}

func (e objectiveEvaluator) EpochEvaluated(_ *experiment.Trial, _ *experiment.Generation) {
func (e *objectiveEvaluator) EpochEvaluated(_ *experiment.Trial, _ *experiment.Generation) {
// just stub
}

// GenerationEvaluate evaluates one epoch for given population and prints results into output directory if any.
func (e objectiveEvaluator) GenerationEvaluate(ctx context.Context, pop *genetics.Population, epoch *experiment.Generation) error {
func (e *objectiveEvaluator) GenerationEvaluate(ctx context.Context, pop *genetics.Population, epoch *experiment.Generation) error {
options, ok := neat.FromContext(ctx)
if !ok {
return neat.ErrNEATOptionsNotFound
Expand Down Expand Up @@ -90,9 +91,7 @@ func (e objectiveEvaluator) GenerationEvaluate(ctx context.Context, pop *genetic
if epoch.Solved {
// print winner organism
org := epoch.Champion
if depth, err := org.Phenotype.MaxActivationDepthFast(0); err == nil {
neat.InfoLog(fmt.Sprintf("Activation depth of the winner: %d\n", depth))
}
utils.PrintActivationDepth(org, true)

genomeFile := "maze_obj_winner"
// Prints the winner organism to file!
Expand Down Expand Up @@ -154,7 +153,7 @@ func (e *objectiveEvaluator) orgEvaluate(org *genetics.Organism, _ *genetics.Pop
// evaluate individual organism and get novelty point holding simulation results
nItem, solved, err := mazeSimulationEvaluate(e.mazeEnv, org, &record, nil)
if err != nil {
if err == ErrOutputIsNaN {
if errors.Is(err, ErrOutputIsNaN) {
// corrupted genome, but OK to continue evolutionary process
return false, nil
}
Expand Down
8 changes: 4 additions & 4 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"flag"
"fmt"
"github.com/yaricom/goNEAT/v3/experiment"
"github.com/yaricom/goNEAT/v3/neat"
"github.com/yaricom/goNEAT/v3/neat/genetics"
"github.com/yaricom/goNEAT_NS/v3/examples/maze"
"github.com/yaricom/goNEAT/v4/experiment"
"github.com/yaricom/goNEAT/v4/neat"
"github.com/yaricom/goNEAT/v4/neat/genetics"
"github.com/yaricom/goNEAT_NS/v4/examples/maze"
"log"
"math/rand"
"os"
Expand Down
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module github.com/yaricom/goNEAT_NS/v3
module github.com/yaricom/goNEAT_NS/v4

go 1.17
go 1.21

require (
github.com/fogleman/gg v1.3.0
github.com/stretchr/testify v1.7.1
github.com/yaricom/goNEAT/v3 v3.0.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
github.com/yaricom/goNEAT/v4 v4.0.1
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sbinet/npyio v0.6.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3 // indirect
golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
gonum.org/v1/gonum v0.11.0 // indirect
github.com/sbinet/npyio v0.7.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/image v0.6.0 // indirect
gonum.org/v1/gonum v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit f927808

Please sign in to comment.