當前位置: 首頁>>代碼示例>>Golang>>正文


Golang neat.Context類代碼示例

本文整理匯總了Golang中github.com/rqme/neat.Context的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Context類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: generateFirst

// Generates the initial population
func generateFirst(ctx neat.Context, cfg ClassicSettings) (next neat.Population, err error) {
	// Create the first generation
	next = neat.Population{
		Generation: 0,
		Species:    make([]neat.Species, 1, 10),
		Genomes:    make([]neat.Genome, cfg.PopulationSize()),
	}

	// Create the genomes
	wg := new(sync.WaitGroup)
	for i := 0; i < len(next.Genomes); i++ {
		wg.Add(1)
		go func(i int) {
			genome := createSeed(ctx, cfg)
			genome.ID = ctx.NextID()
			genome.SpeciesIdx = 0
			next.Genomes[i] = genome
			wg.Done()
		}(i)
	}
	wg.Wait()

	// Create the initial species
	next.Species[0] = neat.Species{Example: next.Genomes[0]}

	return
}
開發者ID:NioTeX,項目名稱:neat,代碼行數:28,代碼來源:common.go

示例2: Restore

func (a *File) Restore(ctx neat.Context) error {

	// Restore the settings
	name := a.makePath("config")
	f, err := os.Open(name)
	if err != nil {
		return err
	}
	d := json.NewDecoder(f)
	if err = d.Decode(&ctx); err != nil {
		f.Close()
		return err
	}
	f.Close()

	// Restore the state values
	for k, v := range ctx.State() {
		name := a.makePath(k)
		if _, err := os.Stat(name); os.IsNotExist(err) {
			continue
		}

		f, err = os.Open(name)
		if err != nil {
			return err
		}
		d = json.NewDecoder(f)
		if err = d.Decode(&v); err != nil {
			f.Close()
			return err
		}
		f.Close()
	}
	return nil
}
開發者ID:NioTeX,項目名稱:neat,代碼行數:35,代碼來源:file.go

示例3: SetContext

func (s *Novelty) SetContext(x neat.Context) error {
	x.State()["novelty-behaviors"] = &s.behaviors
	if cx, ok := s.Searcher.(neat.Contextable); ok {
		return cx.SetContext(x)
	}
	return nil
}
開發者ID:NioTeX,項目名稱:neat,代碼行數:7,代碼來源:novelty.go

示例4: Archive

func (a *File) Archive(ctx neat.Context) error {
	// Save the settings
	name := a.makePath("config")
	var dir = path.Dir(name)
	if _, err := os.Stat(dir); err != nil {
		if os.IsNotExist(err) {
			os.MkdirAll(dir, os.ModePerm)
		} else {
			// other error
		}
	}
	f, err := os.Create(name)
	if err != nil {
		panic(err)
		return err
	}
	b, err := json.MarshalIndent(ctx, "", " ")
	if err != nil {
		f.Close()
		return err
	}
	f.Write(b)
	f.Close()

	// Save the state values
	for k, v := range ctx.State() {
		name := a.makePath(k)
		f, err = os.Create(name)
		if err != nil {
			return err
		}
		b, err := json.MarshalIndent(v, "", "  ")
		if err != nil {
			f.Close()
			return err
		}
		f.Write(b)
		f.Close()
	}
	return nil
}
開發者ID:NioTeX,項目名稱:neat,代碼行數:41,代碼來源:file.go

示例5: SetContext

func (v *Web) SetContext(x neat.Context) error {
	v.ctx = x
	x.State()["web-fitness"] = &v.fitness
	x.State()["web-complexity"] = &v.complexity
	x.State()["web-species"] = &v.species
	x.State()["web-best"] = &v.best
	return nil
}
開發者ID:NioTeX,項目名稱:neat,代碼行數:8,代碼來源:web.go

示例6: createSeed

// Returns a genome build from the parameters
func createSeed(ctx neat.Context, cfg ClassicSettings) (adam neat.Genome) {
	// Create the genome
	inputs := cfg.NumInputs()
	outputs := cfg.NumOutputs()
	adam = neat.Genome{
		Nodes: make(map[int]neat.Node, 1+inputs+outputs),
	}
	nodes := make([]neat.Node, len(adam.Nodes))
	node := neat.Node{NeuronType: neat.Bias, ActivationType: neat.Direct, X: 0, Y: 0}
	node.Innovation = ctx.Innovation(neat.NodeInnovation, node.Key())
	adam.Nodes[node.Innovation] = node
	nodes = append(nodes, node)
	for i := 0; i < inputs; i++ {
		node = neat.Node{NeuronType: neat.Input, ActivationType: neat.Direct, X: float64(i+1) / float64(inputs), Y: 0}
		node.Innovation = ctx.Innovation(neat.NodeInnovation, node.Key())
		adam.Nodes[node.Innovation] = node
		nodes = append(nodes, node)
	}
	x := 0.5
	for i := 0; i < outputs; i++ {
		if outputs > 1 {
			x = float64(i) / float64(outputs-1)
		}
		node = neat.Node{NeuronType: neat.Output, ActivationType: cfg.OutputActivation(), X: x, Y: 1}
		node.Innovation = ctx.Innovation(neat.NodeInnovation, node.Key())
		adam.Nodes[node.Innovation] = node
		nodes = append(nodes, node)
	}

	rng := rand.New(rand.NewSource(rand.Int63()))
	// adam.Conns = make(map[int]neat.Connection, (1+inputs)*outputs)
	// for i := 0; i < 1+inputs; i++ {
	// 	for j := 0; j < outputs; j++ {
	// 		w := (rng.Float64()*2.0 - 1.0) * cfg.WeightRange()
	// 		conn := neat.Connection{Source: nodes[i].Innovation, Target: nodes[j+1+inputs].Innovation, Enabled: true, Weight: w}
	// 		conn.Innovation = ctx.Innovation(neat.ConnInnovation, conn.Key())
	// 		adam.Conns[conn.Innovation] = conn
	// 	}
	// }

	ts := cfg.Traits()
	adam.Traits = make([]float64, len(ts))
	for i, trait := range ts {
		adam.Traits[i] = rng.Float64()*(trait.Max-trait.Min) + trait.Min // TODO: Get setting values from configuration
	}
	return adam
}
開發者ID:NioTeX,項目名稱:neat,代碼行數:48,代碼來源:seed.go

示例7: createOffspring

func createOffspring(ctx neat.Context, cfg ClassicSettings, cross bool, rng *rand.Rand, pool map[int]Improvements, cnts map[int]int, next *neat.Population) (err error) {
	var child neat.Genome
	for idx, cnt := range cnts {
		l := pool[idx]
		for i := 0; i < cnt; i++ {
			p1, p2 := pickParents(cfg, cross, rng, l, pool)
			if p1.ID == p2.ID {
				child = neat.CopyGenome(p1)
			} else {
				child, err = ctx.Crosser().Cross(p1, p2)
				if err != nil {
					return
				}
			}
			child.ID = ctx.NextID()
			child.Birth = next.Generation
			err = ctx.Mutator().Mutate(&child)
			next.Genomes = append(next.Genomes, child)
		}
	}
	return
}
開發者ID:NioTeX,項目名稱:neat,代碼行數:22,代碼來源:common.go

示例8: NewExperiment

func NewExperiment(ctx neat.Context, cfg neat.ExperimentSettings, t int) (exp *neat.Experiment, err error) {

	// Create the experiment
	exp = &neat.Experiment{ExperimentSettings: cfg}
	exp.SetContext(ctx)

	// Restore the saved setting and, if available, state
	if *ConfigName == "" {
		*ConfigName = os.Args[0] // Use the executable's name
	}
	rst := &archiver.File{
		FileSettings: ConfigSettings{path: *ConfigPath, name: *ConfigName},
	}
	if err = rst.Restore(ctx); err != nil {
		return
	}

	// Update helpers with trial number
	if t > NoTrials {
		hs := []interface{}{
			ctx.Archiver(),
			ctx.Comparer(),
			ctx.Crosser(),
			ctx.Decoder(),
			ctx.Evaluator(),
			ctx.Generator(),
			ctx.Mutator(),
			ctx.Searcher(),
			ctx.Speciater(),
			ctx.Visualizer(),
		}
		for _, h := range hs {
			if th, ok := h.(neat.Trialable); ok {
				th.SetTrial(t)
			}
		}
	}

	// Load ids and innovations
	if ph, ok := ctx.(neat.Populatable); ok {
		ph.SetPopulation(exp.Population())
	}
	return
}
開發者ID:NioTeX,項目名稱:neat,代碼行數:44,代碼來源:experiment.go


注:本文中的github.com/rqme/neat.Context類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。