Пример #1
0
// build and run model
func main() {
	// get options
	var maxSize, maxDepth int
	var trailFile 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(&trailFile, "trail", "santafe_trail.txt", "trail definition file")
	opts := util.DefaultOptions
	util.ParseFlags(&opts)

	// create primitive set
	config := readTrail(trailFile)
	pset := gp.CreatePrimSet(0)
	pset.Add(progN{&gp.BaseFunc{"prog2", 2}})
	pset.Add(progN{&gp.BaseFunc{"prog3", 3}})
	pset.Add(ifFood{&gp.BaseFunc{"if_food", 2}})
	pset.Add(Terminal("left", turn(-1)))
	pset.Add(Terminal("right", turn(1)))
	pset.Add(Terminal("step", step))

	// setup model
	problem := &gp.Model{
		PrimitiveSet:  pset,
		Generator:     gp.GenFull(pset, 1, 2),
		PopSize:       opts.PopSize,
		Fitness:       fitnessFunc(config),
		Offspring:     gp.Tournament(opts.TournSize),
		Mutate:        gp.MutUniform(gp.GenFull(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("== Artificial ant ==")

	logger := stats.NewLogger(opts.MaxGen, opts.TargetFitness)
	if opts.Verbose {
		logger.OnDone = func(best *gp.Individual) {
			ant := run(config, best.Code)
			fmt.Println(ant.grid)
		}
	}

	// run
	if opts.Plot {
		logger.RegisterSVGPlot("best", createPlot(config, 500, 10))
		stats.MainLoop(problem, logger, ":8080", "../web")
	} else {
		fmt.Println()
		logger.PrintStats = true
		logger.PrintBest = opts.Verbose
		problem.Run(logger)
	}
}
Пример #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)
	}
}
Пример #3
0
// build and run model
func main() {
	// get options
	var maxSize, maxDepth int
	var configFile 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(&configFile, "config", "desert.txt", "grid definition file")
	opts := util.DefaultOptions
	util.ParseFlags(&opts)

	// create primitive set
	grid := readGrid(configFile)
	pset := gp.CreatePrimSet(0)
	pset.Add(Terminal("x", func(ant *Ant) int { return ant.col }))
	pset.Add(Terminal("y", func(ant *Ant) int { return ant.row }))
	pset.Add(Terminal("carrying", func(ant *Ant) int { return ant.carrying }))
	pset.Add(Terminal("color", func(ant *Ant) int { return ant.grid.cells[ant.row][ant.col].color }))
	pset.Add(Terminal("go-n", move(0)))
	pset.Add(Terminal("go-e", move(1)))
	pset.Add(Terminal("go-s", move(2)))
	pset.Add(Terminal("go-w", move(3)))
	pset.Add(Terminal("go-rand", func(ant *Ant) int { return move(ant.grid.rng.Intn(4))(ant) }))
	pset.Add(Terminal("pickup", pickUp))
	pset.Add(IfElse("iflte", 4, ifLessThanOrEqual))
	pset.Add(IfElse("ifltz", 3, ifLessThanZero))
	pset.Add(IfElse("ifdrop", 2, ifDrop))

	// setup model
	problem := &gp.Model{
		PrimitiveSet:  pset,
		Generator:     gp.GenFull(pset, 1, 2),
		PopSize:       opts.PopSize,
		Fitness:       fitnessFunc(grid),
		Offspring:     gp.Tournament(opts.TournSize),
		Mutate:        gp.MutUniform(gp.GenFull(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("== Artificial ant ==")

	logger := stats.NewLogger(opts.MaxGen, opts.TargetFitness)
	if opts.Verbose {
		logger.OnDone = func(best *gp.Individual) {
			g, _ := run(grid, best.Code)
			fmt.Println(g)
		}
	}

	// run
	if opts.Plot {
		logger.RegisterSVGPlot("best", createPlot(grid, 500, 40))
		stats.MainLoop(problem, logger, ":8080", "../web")
	} else {
		fmt.Println()
		logger.PrintStats = true
		logger.PrintBest = opts.Verbose
		problem.Run(logger)
	}
}