Beispiel #1
0
// test generating random individuals
func TestGenerate(t *testing.T) {
	pset := initPset(false)
	pset.Add(V(0), V(1))
	gen := gp.GenRamped(pset, 1, 3)
	gp.SetSeed(0)
	for i := 0; i < 10; i++ {
		ind := gen.Generate()
		res := ind.Code.Eval(V(6), V(7))
		t.Log(ind.Code, ind.Code.Format(), "(6,7) =>", res)
	}
}
Beispiel #2
0
// main GP routine
func main() {
	// get options
	var maxSize, maxDepth int
	var dataFile string
	flag.IntVar(&maxSize, "size", 0, "maximum tree size - zero for none")
	flag.IntVar(&maxDepth, "depth", 0, "maximum tree depth - zero for none")
	flag.StringVar(&dataFile, "trainset", "poly.dat", "file with training function")
	opts := util.DefaultOptions
	util.ParseFlags(&opts)

	// create primitive set
	ercMin, ercMax, trainSet := getData(dataFile)
	pset := gp.CreatePrimSet(1, "x")
	pset.Add(num.Add, num.Sub, num.Mul, num.Div)
	pset.Add(num.Ephemeral("ERC", ercGen(ercMin, ercMax)))

	// setup model
	problem := &gp.Model{
		PrimitiveSet:  pset,
		Generator:     gp.GenRamped(pset, 1, 3),
		PopSize:       opts.PopSize,
		Fitness:       fitnessFunc(trainSet),
		Offspring:     gp.Tournament(opts.TournSize),
		Mutate:        gp.MutUniform(gp.GenGrow(pset, 0, 2)),
		MutateProb:    opts.MutateProb,
		Crossover:     gp.CxOnePoint(),
		CrossoverProb: opts.CrossoverProb,
		Threads:       opts.Threads,
	}
	if maxDepth > 0 {
		problem.AddDecorator(gp.DepthLimit(maxDepth))
	}
	if maxSize > 0 {
		problem.AddDecorator(gp.SizeLimit(maxSize))
	}
	problem.PrintParams("== GP Symbolic Regression for ", dataFile, "==")

	// run
	logger := stats.NewLogger(opts.MaxGen, opts.TargetFitness)
	if opts.Plot {
		gp.GraphDPI = "60"
		logger.RegisterPlot("graph", plotTarget(trainSet), plotBest(trainSet))
		stats.MainLoop(problem, logger, ":8080", "../web")
	} else {
		fmt.Println()
		logger.PrintStats = true
		logger.PrintBest = opts.Verbose
		problem.Run(logger)
	}
}