예제 #1
0
// returns function to plot path of best individual
func createPlot(c *Config, size, delay int) func(gp.Population) []byte {
	sz := size / c.plotCols
	return func(pop gp.Population) []byte {
		ch := make(chan [][2]int)
		go func() {
			ant := run(c, pop.Best().Code)
			ch <- ant.path
		}()
		// draw grid
		plot := util.SVGPlot(size, size, sz)
		plot.AddGrid(c.plotCols, c.plotRows, delay, func(x, y int) string {
			if c.grid[y][x] == FOOD {
				return "fill:green"
			} else {
				return "fill:grey"
			}
		})
		// draw ant
		plot.Gid("ant")
		plot.Circle(c.startCol*sz+sz/2, c.startRow*sz+sz/2, int(0.4*float64(sz)), "fill:black")
		plot.Gend()
		plot.Animate("ant", <-ch,
			map[string]string{"fill:grey": "fill:brown", "fill:green": "fill:red"})
		return plot.Data()
	}
}
예제 #2
0
// returns function to plot path of best individual
func createPlot(g *Grid, size, delay int) func(gp.Population) []byte {
	styles := []string{"fill:grey", "fill:red", "fill:green", "fill:blue"}
	sz := size / g.cols
	return func(pop gp.Population) []byte {
		ch := make(chan [][][4]int)
		go func() {
			_, path := run(g, pop.Best().Code)
			ch <- path
		}()
		// draw grid
		plot := util.SVGPlot(size, size, sz)
		plot.AddGrid(g.cols, g.rows, delay, func(x, y int) string {
			return styles[g.cells[y][x].color+1]
		})
		// draw ants
		plot.Gid("ant")
		for _, ant := range g.ants() {
			plot.Circle(ant.col*sz+sz/2, ant.row*sz+sz/2, int(0.4*float64(sz)), "fill:none")
		}
		plot.Gend()
		plot.AnimateMulti("ant", <-ch, styles)
		return plot.Data()
	}
}