Example #1
0
func (s Substrate) Decode() (neat.Network, error) {

	// Create neurons from the nodes
	ns := make([]network.Neuron, len(s.Nodes))
	nm := make(map[int]int, len(s.Nodes))
	for i, sn := range s.Nodes {
		nm[sn.id] = i
		ns[i] = network.Neuron{NeuronType: sn.NeuronType}
		ns[i].X, ns[i].Y = sn.Position[0], sn.Position[1] // TODO: Improve this as all layers will be collapsed
		switch sn.NeuronType {
		case neat.Input, neat.Bias:
			ns[i].ActivationType = neat.Direct
		default:
			ns[i].ActivationType = neat.Sigmoid
		}
	}

	// Create synapses from the connections
	cs := make([]network.Synapse, len(s.Conns))
	for i, sc := range s.Conns {
		cs[i] = network.Synapse{
			Source: nm[sc.Source],
			Target: nm[sc.Target],
			Weight: sc.Weight,
		}
	}

	// Return the new network
	return network.New(ns, cs)
}
Example #2
0
func (d Classic) decode(g neat.Genome) (net neat.Network, err error) {

	// Identify the genes
	nodes, conns := g.GenesByPosition()

	// Create the neurons
	nmap := make(map[int]int)
	neurons := make([]network.Neuron, len(nodes))
	for i, ng := range nodes {
		nmap[ng.Innovation] = i
		neurons[i] = network.Neuron{NeuronType: ng.NeuronType, ActivationType: ng.ActivationType, X: ng.X, Y: ng.Y}
	}

	// Create the synapses
	//forward := true // Keep track of conenctions to determine if this is a feed-forward only network
	synapses := make([]network.Synapse, 0, len(conns))
	for _, cg := range conns {
		if cg.Enabled {
			//src, tgt := nodes[nmap[cg.Source]], nodes[nmap[cg.Target]]
			//forward = forward && src.Y < tgt.Y
			synapses = append(synapses, network.Synapse{
				Source: nmap[cg.Source],
				Target: nmap[cg.Target],
				Weight: cg.Weight,
			})
		}
	}

	net, err = network.New(neurons, synapses)
	return
}