예제 #1
0
from neat.genome import NodeType, Genome, NodeGene, ConnectionGene

g1 = Genome([
    NodeGene(1, NodeType.SENSOR),
    NodeGene(2, NodeType.BIAS),
    NodeGene(3, NodeType.OUTPUT),
    NodeGene(5, NodeType.HIDDEN)
])
g2 = Genome([
    NodeGene(1, NodeType.SENSOR),
    NodeGene(2, NodeType.BIAS),
    NodeGene(3, NodeType.OUTPUT),
    NodeGene(4, NodeType.HIDDEN)
])

g1.addConnection(g1.node(1), g1.node(3), None, 1)
g1.addConnection(g1.node(2), g1.node(3), None, 2, False)
g1.addConnection(g1.node(2), g1.node(5), None, 5)
g1.addConnection(g1.node(5), g1.node(3), None, 6)

g2.addConnection(g2.node(1), g2.node(3), None, 1, False)
g2.addConnection(g2.node(2), g2.node(3), None, 2)
g2.addConnection(g2.node(4), g2.node(3), None, 3)
g2.addConnection(g2.node(1), g2.node(4), None, 4)
g2.addConnection(g2.node(2), g2.node(4), None, 7)

print(g2.distance(g1))
from neat.genome import NodeGene, ConnectionGene, Genome
from neat.network import Node, Network
from neat.nodetype import NodeType

genome = Genome([NodeGene(NodeType.SENSOR), NodeGene(NodeType.SENSOR), NodeGene(NodeType.BIAS),
                 NodeGene(NodeType.HIDDEN), NodeGene(NodeType.HIDDEN),
                 NodeGene(NodeType.HIDDEN), NodeGene(NodeType.BIAS), NodeGene(NodeType.OUTPUT)])

x_1 = genome.node(1)
x_2 = genome.node(2)
b_1 = genome.node(3)

hiddens = [genome.node(x) for x in range(4,7)]
b_2 = genome.node(7)

h = genome.node(8)

for n, weight in enumerate((-1.9623, -2.2216, 3.1370)):
    genome.addConnection(x_1, hiddens[n], weight)

for n, weight in enumerate((-1.7715, -2.6616, 2.7087)):
    genome.addConnection(x_2, hiddens[n], weight)

for n, weight in enumerate((2.1962, 3.1903, -3.9824)):
    genome.addConnection(b_1, hiddens[n], weight)

for n, weight in enumerate((-3.9746, -6.0582, 6.5607)):
    genome.addConnection(hiddens[n], h, weight)

genome.addConnection(b_2, h, 0.092103)
예제 #3
0
from neat.pool import NetworkPool
from neat.network import Network
from neat.genome import NodeGene, ConnectionGene, Genome
from neat import configuration

if __name__ == '__main__':
    config = configuration.loadConfig("NEAT.toml")

    # xor initial network

    genome = Genome([
        NodeGene(NodeType.SENSOR),
        NodeGene(NodeType.SENSOR),
        NodeGene(NodeType.BIAS),
        NodeGene(NodeType.OUTPUT)
    ])

    genome.addConnection(genome.node(1), genome.node(4))
    genome.addConnection(genome.node(2), genome.node(4))
    genome.addConnection(genome.node(3), genome.node(4))

    pool = NetworkPool(genome)

    pool.run([(1, 0), (0, 1), (1, 1), (0, 0)],
             lambda ip, op: 1 - abs(op[0] - (ip[0] ^ ip[1])))
예제 #4
0
from neat.pool import NetworkPool
from neat.genome import Genome, NodeGene, ConnectionGene
from neat.nodetype import NodeType
from neat import configuration
import pickle
import pprint

configuration.loadConfig("NEAT.toml")

initialGenome = Genome([
    NodeGene(NodeType.SENSOR),
    NodeGene(NodeType.SENSOR),
    NodeGene(NodeType.BIAS),
    NodeGene(NodeType.OUTPUT)
])
initialGenome.addConnection(initialGenome.node(1), initialGenome.node(4))
initialGenome.addConnection(initialGenome.node(2), initialGenome.node(4))
initialGenome.addConnection(initialGenome.node(3), initialGenome.node(4))


def fitnessFunction(input, output):
    out = output[0]
    correct_answer = input[0] ^ input[1]

    return 1 - abs(out - correct_answer)


openFiles = {}


def writeSpeciesData(gen, species, f):