コード例 #1
0
ファイル: painted_desert.go プロジェクト: jnb666/gogp
// run the code - step each ant in turn
func run(g *Grid, code gp.Expr) (*Grid, [][][4]int) {
	runFunc := code.Eval().(func(*Ant) int)
	grid := g.clone()
	ants := grid.ants()
	pathList := make([][][4]int, 0, grid.steps)
	for i := 0; i < grid.steps; i++ {
		grid.path = make([][4]int, 0, len(ants))
		for _, ant := range ants {
			if ant.moves < grid.maxMoves {
				row, col := ant.row, ant.col
				runFunc(ant)
				if ant.col != col || ant.row != row {
					grid.path = append(grid.path, [4]int{ant.id, ant.col, ant.row, 0})
				}
			}
		}
		if len(grid.path) > 0 {
			pathList = append(pathList, grid.path)
		}
	}
	grid.path = make([][4]int, 0, len(ants))
	finalise(ants)
	if len(grid.path) > 0 {
		pathList = append(pathList, grid.path)
	}
	return grid, pathList
}
コード例 #2
0
ファイル: artificial_ant.go プロジェクト: jnb666/gogp
// run the code
func run(conf *Config, code gp.Expr) *Ant {
	ant := newAnt(conf)
	runFunc := code.Eval().(func(*Ant))
	for ant.moves < ant.maxMoves {
		runFunc(ant)
	}
	return ant
}
コード例 #3
0
ファイル: example_test.go プロジェクト: jnb666/gogp
// calc least squares difference and return as normalised fitness from 0->1
func (e eval) GetFitness(code gp.Expr) (float64, bool) {
	diff := 0.0
	for x := -1.0; x <= 1.0; x += 0.1 {
		val := float64(code.Eval(num.V(x)).(num.V))
		fun := x*x*x*x + x*x*x + x*x + x
		diff += (val - fun) * (val - fun)
	}
	return 1.0 / (1.0 + diff), true
}