/
nand.go
52 lines (45 loc) · 1.07 KB
/
nand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2014 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"math/rand"
"time"
"github.com/gmlewis/gep/v2/functions"
"github.com/gmlewis/gep/v2/gene"
"github.com/gmlewis/gep/v2/genome"
"github.com/gmlewis/gep/v2/model"
)
var nandTests = []struct {
in []bool
out bool
}{
{[]bool{false, false}, true},
{[]bool{false, true}, true},
{[]bool{true, false}, true},
{[]bool{true, true}, false},
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func validateNand(g *genome.Genome) float64 {
correct := 0
for _, n := range nandTests {
r := g.EvalBool(n.in)
if r == n.out {
correct++
}
}
return 1000.0 * float64(correct) / float64(len(nandTests))
}
func main() {
funcs := []gene.FuncWeight{
{Symbol: "Not", Weight: 1},
{Symbol: "And", Weight: 5},
{Symbol: "Or", Weight: 5},
}
e := model.New(funcs, functions.Bool, 30, 7, 1, 2, 0, "Or", validateNand)
s := e.Evolve(1000)
fmt.Printf("nand solution: %v, score=%v\n", s, validateNand(s))
}