-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (102 loc) · 2.54 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"errors"
"fmt"
"io"
"os"
"os/signal"
"github.com/urfave/cli"
"github.com/wmiller848/GoGP/context"
)
func run(pipe io.Reader, score float64, inputs, population, generations int, auto, visual bool) error {
if inputs <= 0 {
return errors.New("Count mut be greater then 0")
}
ctx := context.New()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
fitest := ctx.Fitest()
prgm, _ := fitest.MarshalProgram()
fmt.Printf("%+v\n", string(prgm))
os.Exit(0)
}
}()
if visual {
go func() {
ctx.RunWithInlineScore(pipe, score, inputs, population, generations, auto)
}()
ctx.NewTerminal()
fitest := ctx.Fitest()
prgm, _ := fitest.MarshalProgram()
fmt.Printf("%+v\n", string(prgm))
} else {
_, fitest := ctx.RunWithInlineScore(pipe, score, inputs, population, generations, auto)
prgm, _ := fitest.MarshalProgram()
fmt.Printf("%+v\n", string(prgm))
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "GoGP"
app.Usage = "Learn a program for matching data based off training data"
app.Commands = []cli.Command{
{
Name: "learn",
Aliases: []string{"l"},
Flags: []cli.Flag{
cli.IntFlag{
Name: "count, c",
Usage: "Number of input fields to learn",
EnvVar: "GOGP_COUNT",
Value: 0,
},
cli.Float64Flag{
Name: "score, s",
Usage: "Float value for desired percentage score of program. 0.10 = 10%",
EnvVar: "GOGP_SCORE",
Value: 0.90,
},
cli.IntFlag{
Name: "population, p",
Usage: "Number of programs to keep in the pool",
EnvVar: "GOGP_POPULATION",
Value: 20,
},
cli.IntFlag{
Name: "generations, g",
Usage: "Number of generations to iterate through",
EnvVar: "GOGP_GENERATIONS",
Value: 100,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Output more then just the evolved program",
EnvVar: "GOGP_VERBOSE",
},
cli.BoolFlag{
Name: "auto, a",
Usage: "Run until a reasonable score is found",
EnvVar: "GOGP_AUTO",
},
},
Action: func(c *cli.Context) error {
args := c.Args()
var pipe io.Reader
if len(args) == 0 {
pipe = os.Stdin
} else if len(args) == 1 {
// Handle file io
} else {
fmt.Println("Too many arguments, provide path to one file.")
return errors.New("to many arguments")
}
run(pipe, c.Float64("score"), c.Int("count"), c.Int("population"), c.Int("generations"), !c.Bool("auto"), c.Bool("verbose"))
return nil
},
},
}
app.Run(os.Args)
}