Exemple #1
0
 def initiateSynapses(self, sdr):
     sdrlength = sdr.length if isinstance(sdr, SDR) else sdr[1]
     if sdrlength >= len(self.segments):
         indices = list(xrange(sdrlength))
         if isinstance(self, ProximalDendriteTree):
             if self.neuron.indx in indices:
                 print(self.neuron.indx)
                 print("preventing self loop")
                 indices.remove(self.neuron.indx) 
             indices = rng.sample(indices, len(self.segments))
         else:
             indices = rng.sample(indices, len(self.segments))
         for i,indx in enumerate(indices):
             self.segments[i]._indx = indx
     elif sdrlength < len(self.segments):
         indices = list(xrange(len(self.segments)))
         if isinstance(self, ProximalDendriteTree):
             if self.neuron.indx in indices:
                 print(self.neuron.indx)
                 print("preventing self loop")
                 indices.remove(self.neuron.indx) 
             indices = rng.sample(indices, sdrlength)
         self.segments = [self.segments[i] for i in indices]
         for indx, i in enumerate(self.segments):
             i._indx = indx
     if isinstance(self, ProximalDendriteTree):
         self.synapses = [Synapse(sdr[0], segment, inc=0.001, dec=0.005) for segment in self.segments]
     else:
         self.synapses = [Synapse(sdr[0], segment) for segment in self.segments]
Exemple #2
0
 def map_data(self, data):
     return [
         Synapse('rocket_top', data.rocket_top, self.weights[0], []),
         Synapse('wall_direction', data.wall_direction,
                 self.weights[1], []),
         Synapse('wall_left', data.wall_left, self.weights[2], []),
         Synapse('wall_length', data.wall_length,
                 self.weights[3], []),
     ]
Exemple #3
0
    def generation_crossover(self, datas):
        new_gen = []

        for i in range(0, self.count_individuals):
            should_mutate = random() < self.mutation_prob
            if should_mutate:
                self.root_neurons[i].mutate()
            should_struct_mutate = random() < self.mutation_struct_prob
            if should_struct_mutate:
                self.root_neurons[i].mutate_struct()

        sorted(datas, key=lambda d: d.score, reverse=True)
        for i in range(0, len(datas)):
            for j in range(0, len(datas)):
                if i == j:
                    continue
                root_synapses = self.root_neurons[i].synapses
                for syn in root_synapses:
                    syn.weight = (
                        syn.weight + self.root_neurons[j].synapses[root_synapses.index(syn)].weight) / 2
                root_neuron = Neuron(0, root_synapses)
                f_genes, s_genes = []
                self.root_neurons[i].iterate_children_recursive(
                    lambda neuron, next_neuron, syn: f_genes.append((neuron, next_neuron, syn)))
                self.root_neurons[j].iterate_children_recursive(
                    lambda neuron, next_neuron, syn: s_genes.append((neuron, next_neuron, syn)))

                for gene in f_genes:
                    for o_gene in s_genes:
                        if gene[0].id == o_gene[0].id and gene[1].id == o_gene[1].id or random() < self.crossover_unique_gene_transfer_prob:
                            match_neuron = root_neuron.find_child(
                                lambda n: n.id == o_gene[0].id) if o_gene[0].id != 0 else False
                            ancestor_genes = root_neuron
                            new_syn = Synapse(o_gene[2].label
                                              (gene[2].input + o_gene[2].input) / 2, (gene[2].weight + o_gene[2].weight) / 2, [])
                            if match_neuron is None:
                                match_neuron = Neuron(o_gene[0].id, [new_syn])
                                match_ancestor_genes = filter(
                                    lambda g: g[1].id == match_neuron.id, s_genes)
                                if len(match_ancestor_genes) > 0:
                                    ancestor_genes = match_ancestor_genes[0]

                            new_neuron = Neuron(o_gene[1].id + len(map(lambda n:
                                                                       map(lambda s: s.next_neurons, n.synapses), match_neuron)) + 1, [new_syn])

                            if ancestor_genes is root_neuron:
                                root_neuron.synapses.append(
                                    Synapse(new_syn.label, new_syn.input, new_syn.weight, [new_neuron]))
                            else:
                                ancestor_genes[2].next_neurons.append(
                                    new_neuron)
                    if len(new_gen) == self.count_individuals:
                        break

        self.root_neurons = new_gen
        self.adjust_weights(datas)
def runSimulation(inputArr):
    # Initialize the time steps required to emulate the total runtime iteratively
    totalDuration = 1000  # Total duration to run
    dt = 1  # Time between iterations
    timeToEnumerate = np.arange(1, totalDuration + 1, 1)  # Determine the time-step array [0, 0.125, 0.25, 0.375, ..., 800]

    # Generate a random pre-synaptic and post-synaptic spike train to send to the network
    trainGen = GenSpikeTrain(math.floor(totalDuration/dt))
    preTrain = trainGen.SpikeTrainGen()
    postTrain = trainGen.SpikeTrainGen()
    train = np.vstack((preTrain, postTrain))

    # Assign initial parameters
    # ---------------------------------
    tauX = 575
    tauY = 47
    tauPlus = 16.8
    tauMinus = 33.7
    # ---------------------------------
    A2Plus = inputArr[0]
    A2Minus = inputArr[1]
    A3Plus = inputArr[2]
    A3Minus = inputArr[3]
    # ---------------------------------
    r1 = genDE(tauPlus, True, dt)
    r2 = genDE(tauX, True, dt)
    o1 = genDE(tauMinus, False, dt)
    o2 = genDE(tauY, False, dt)
    # ---------------------------------
    syn = Synapse(0, A2Plus, A2Minus, A3Plus, A3Minus)
    # ---------------------------------
    synWeight = np.zeros(math.floor(totalDuration/dt))

    # Enumerate through the total runtime
    for timeIndex, currentTime in enumerate(timeToEnumerate):
        # Update the current state of t-pre and t-post
        tpre = (train[0, timeIndex] == 1)
        tpost = (train[1, timeIndex] == 1)
        # Update the current values of r1, r2, o1 and o2
        r1.updateSynapticWeight(tpre, tpost)
        r2.updateSynapticWeight(tpre, tpost)
        o1.updateSynapticWeight(tpre, tpost)
        o2.updateSynapticWeight(tpre, tpost)

        # Update the current synaptic weight
        syn.updateSynapticWeight(tpre, tpost, r1.currentValue, r2.currentValue, o1.currentValue, o2.currentValue)
        currentVal = syn.currentWeight
        synWeight[timeIndex] = currentVal

    totalWeightChange = synWeight[-1] - synWeight[0]
    print('Total Weight Change: [%f]' % (totalWeightChange))
    plt.plot(timeToEnumerate, synWeight)
    plt.title('Change in Synaptic Weight for Random Spike Train Inputs')
    plt.xlim([0,500])
    plt.show()
def detTotalWeightChange(inputArr, spikeTrain, iterations, showPlot):
    # Initialize the time steps required to emulate the total runtime iteratively
    dt = 1  # Time between iterations
    train = spikeTrain
    totalDuration = train.shape[1]  # Total duration to run
    timeToEnumerate = np.arange(
        1, totalDuration + 1,
        1)  # Determine the time-step array [0, 0.125, 0.25, 0.375, ..., 800]
    # Assign initial parameters
    # ---------------------------------
    tauX = 1024
    tauY = 32
    tauPlus = 64
    tauMinus = 256
    # ---------------------------------
    A2Plus = inputArr[0]
    A2Minus = inputArr[1]
    A3Plus = inputArr[2]
    A3Minus = inputArr[3]
    # ---------------------------------
    r1 = genDE(tauPlus, True, dt)
    r2 = genDE(tauX, True, dt)
    o1 = genDE(tauMinus, False, dt)
    o2 = genDE(tauY, False, dt)
    # ---------------------------------
    syn = Synapse(0, A2Plus, A2Minus, A3Plus, A3Minus)
    synWeight = np.zeros((1, math.floor(totalDuration / dt)))
    # Enumerate through the total runtime
    for timeIndex, currentTime in enumerate(timeToEnumerate):
        # Update the current state of t-pre and t-post
        tpre = (train[0, timeIndex] == 1)
        tpost = (train[1, timeIndex] == 1)
        # Update the current values of r1, r2, o1 and o2
        r1.updateSynapticWeight(tpre, tpost)
        r2.updateSynapticWeight(tpre, tpost)
        o1.updateSynapticWeight(tpre, tpost)
        o2.updateSynapticWeight(tpre, tpost)
        # Update the current synaptic weight
        syn.updateSynapticWeight(tpre, tpost, r1.currentValue,
                                 r2.previousValue, o1.currentValue,
                                 o2.previousValue)
        currentVal = syn.currentWeight
        synWeight[0, timeIndex] = currentVal
    totalWeightChange = synWeight[0, -1] - synWeight[0, 0]
    if (showPlot):
        print('Total Weight Change: [%f]' % (totalWeightChange))
        print('Total Weight Change [n = 60]: [%f]' % (totalWeightChange * 60))
        plt.plot(timeToEnumerate,
                 synWeight[0, :],
                 label='Change in Synaptic Weight')
        plt.legend(loc='lower center')
        plt.title('Change in Synaptic Weight')
        plt.xlim([0, totalDuration])
        plt.show()
    return totalWeightChange * iterations
Exemple #6
0
 def gen_neuron():
     neuron = Neuron(gen_index, genetic.map_data(genetic.gen_data))
     synapse = Synapse(neurons[gen_index].output, random(), [])
     if len(neurons) > gen_index + 1:
         synapse.next_neurons.append(neurons[gen_index + 1])
     neuron.synapses.append(synapse)
     neurons.insert(gen_index, neuron)
     neurons[gen_index - 1].synapses.append(
         Synapse(random(), random(), [neuron]))
     for i in range(gen_index, len(neurons)):
         neurons[i].id += 1
Exemple #7
0
 def create_synapses(self):
     "Create an AMPA and an NMDA synapse in the spine"
     synapses = []
     # AMPA Syn
     ampaSyn = Synapse('ampa', self.psd)
     synapses.append(ampaSyn)
     
     #NMDA Syn
     nmdaSyn = Synapse('nmda', self.psd)
     synapses.append(nmdaSyn)
     
     return synapses
Exemple #8
0
    def mutate_add_neuron(self):
        tmp = Neuron()

        src = get_random_element(self.inputNodeList)
        syn_in = Synapse(src, tmp, random.uniform(-1, 1))
        src.add_output(syn_in)
        tmp.add_input(syn_in)

        dst = get_random_element(self.outputNodeList)
        syn_out = Synapse(tmp, dst, random.uniform(-1, 1))
        tmp.add_output(syn_out)
        dst.add_input(syn_out)

        self.inputNodeList.append(tmp)
        self.outputNodeList.append(tmp)
        self.inputOutputNodeList.append(tmp)
Exemple #9
0
 def synapseOn(self, otherObject, activation = None, *args, **keywordArgs):
     """
     Create a chemical :class:`synapse <Netwok.Synapse.Synapse>` with the other object.
     
     The otherObject parameter can be a :class:`neuron <Network.Neuron.Neuron>`, a :class:`neurite <Network.Neurite.Neurite>` or list containing a combination of the two.
     
     The activation parameter must be one of 'excitatory', 'inhibitory' or None (i.e. unknown).
     
     The :class:`synapse <Network.Synapse.Synapse>` object that is created.
     """
     
     if isinstance(otherObject, (list, set, tuple)):
         otherObjects = otherObject
     else:
         otherObjects = [otherObject]
     
     from neuron import Neuron
     
     for otherObject in otherObjects:
         if not isinstance(otherObject, (Neuron, Neurite)) or otherObject.network != self.network:
             raise ValueError, 'Synapses can only be made with neurons or neurites in the same network.'
     synapse = Synapse(self.network, self, otherObjects, activation, *args, **keywordArgs)
     self._synapses += [synapse]
     for otherObject in otherObjects:
         otherObject._synapses += [synapse]
     self.network.addObject(synapse)
     return synapse
Exemple #10
0
    def mutate_add_synapse(self):
        src = get_random_element(self.inputNodeList)
        dst = src
        while src == dst:
            dst = get_random_element(self.outputNodeList)

        syn = Synapse(src, dst, random.uniform(-1, 1))
        src.add_output(syn)
        dst.add_input(syn)
Exemple #11
0
    def init_synapses(self):
        # link inputs and first hidden layer
        for input_ndx, input_neuron in enumerate(self.inputs):
            for neuron in self.hidden_layers[0]:
                self.synapses[0].append(Synapse(input_neuron, neuron))

        # link hidden layers to each other
        if len(self.hidden_layers) > 1:
            for curr_ndx in range(len(self.hidden_layers) - 1):
                for neuron in self.hidden_layers[curr_ndx]:
                    for next_neuron in self.hidden_layers[curr_ndx + 1]:
                        self.synapses[curr_ndx + 1].append(
                            Synapse(neuron, next_neuron))

        # link last layer to output
        for neuron in self.hidden_layers[len(self.hidden_layers) - 1]:
            self.synapses[len(self.synapses) - 1].append(
                Synapse(neuron, self.output))
def axon_replenish(rs=[0.1, 0.5, 1.0]):
    data = []
    for r in rs:
        syn = Synapse(verbose=args.verbose)
        axon = syn.create_axon(
                    replenish_rate=r,
                    reuptake_rate=0.0,
                    verbose=args.verbose)
        axon.set_concentration(0.0)
        axon_recording = []

        for _ in xrange(50):
            axon_recording.append(axon.get_concentration())
            axon.replenish()
        axon_recording.append(axon.get_concentration())
        data.append(("replenish %s" % str(r), axon_recording))

    if not args.silent: plot(data, title="Replenish (replenish rate)")
 def addSynapses(self, LocationFile, settings):
     """Add synapses to the locations specified in LocationFile"""
     print('....adding synapses: ', LocationFile)
     synapseList = []
     XYZ = f.readLocation(LocationFile)
     for Num in range(len(XYZ)):
         [sec, D] = f.findSectionForLocation(self.h, XYZ[Num, :])
         syn = Synapse(self.h, sec, D, settings)
         synapseList.append(syn)
     return synapseList
Exemple #14
0
 def explore(ascending):
     for i in range(gen_index, len(neurons) if ascending else 0):
         for syn in neurons[i].synapses:
             for next_neuron in syn.next_neurons:
                 new_neuron_bond = [not any(o_neuron.id == next_neuron.id or o_neuron.id <= i
                                            for o_neuron in neurons) for neuron in neurons]
                 if new_neuron_bond != False and len(new_neuron_bond) > 0:
                     next_neuron.synapses.append(
                         Synapse(next_neuron.output, random(), new_neuron_bond))
                     return True
     return False
def axon_reuptake(rs=[0.1, 0.5, 1.0], print_synaptic_cleft=False):
    data = []
    for r in rs:
        syn = Synapse(verbose=args.verbose)
        syn.synaptic_cleft.add_concentration(0.5)

        axon = syn.create_axon(
                    replenish_rate=0.0,
                    reuptake_rate=r,
                    verbose=args.verbose)
        axon.set_concentration(0.0)

        record_components = [("reuptake %s" % str(r), axon)]
        if print_synaptic_cleft:
            record_components.append((
                "synaptic cleft %s" % str(r),
                syn.synaptic_cleft))

        data += simulate_synapse(syn,
            record_components = record_components,
            iterations = 100)
    if not args.silent: plot(data, title="Reuptake (reuptake rate)")
    def create_synapse(presynaptic, postsynaptic, active_molecules=None,
            transporter=Transporters.GLUTAMATE, receptor=Receptors.AMPA,
            enzyme_concentration=1.0,
            axon_delay=0, dendrite_strength=0.0015):
        synapse = Synapse(
            postsynaptic_id=postsynaptic.neuron_id,
            initial_enzyme_concentration=enzyme_concentration,
            active_molecules=active_molecules)
        axon = synapse.create_axon(
                    transporter=transporter,
                    replenish_rate=0.1,
                    reuptake_rate=0.5,
                    capacity=1.0,
                    delay=axon_delay)
        dendrite = synapse.create_dendrite(
                    receptor=receptor,
                    density=0.25,
                    strength=dendrite_strength)

        presynaptic.axons.append(axon)
        presynaptic.synapses.append(synapse)
        presynaptic.synapses_stable.append(True)
        postsynaptic.dendrites.append(dendrite)
        return synapse
Exemple #17
0
    def synapses(self):
        """
        Synapse objects are always re-created as new connections could be
        created outside of the Neuron object. NEST feature..

        :return:    list of Synapse objects (includes synapse with Layer)
        """
        connections = nest.GetConnections([self.id])

        # define a filter for non-neuron connections (spike detectors etc.)
        get_conn_type = lambda conn: nest.GetStatus([conn[1]], 'node_type')[0]
        is_synapse = lambda conn: get_conn_type(conn).name == 'neuron'

        synaptic_connections = filter(is_synapse, connections)
        return [Synapse(*list(node)) for node in synaptic_connections]
Exemple #18
0
    def generate_neurons(self):
        # done cube by cube
        cell = 0
        ni = 0
        for _i in range(self.dimensions[0]):
            for _j in range(self.dimensions[1]):
                for _k in range(self.dimensions[2]):
                    ntype_code = self.neuron_types[self.structure[cell]]
                    for _d in range(ntype_code['density']):
                        self.neurons.append(
                            Neuron(ni, ntype_code, [_i, _j, _k]))
                        ni += 1
                    cell += 1

        # adding synapses
        n_neurons = len(self.neurons)
        for isn, source_neuron in enumerate(self.neurons):
            sc = source_neuron.cube
            for syn in range(source_neuron.code['synapses']):
                while (True):
                    idn = random.randint(0, n_neurons - 1)
                    if idn == isn:  # avoids self-loops. larger loops are OK
                        continue

                    # synapse length is given by cieling of distance between cubes.
                    destination_neuron = self.neurons[idn]
                    dc = destination_neuron.cube
                    distance = math.ceil(
                        pow((dc[0] - sc[0]) * (dc[0] - sc[0]) +
                            (dc[1] - sc[1]) * (dc[1] - sc[1]) +
                            (dc[2] - sc[2]) * (dc[2] - sc[2]), 0.5))
                    prob_conn = random.random()
                    if prob_conn > source_neuron.code['dendron_length'][
                            distance]:
                        continue

                    source_neuron.add_synapse(
                        Synapse(syn,
                                destination_neuron,
                                weight=random.random(),
                                distance=distance))
                    break
Exemple #19
0
def main(hparams):
    metagraph = Metagraph(hparams)
    modelfn = Modelfn(hparams)
    nucleus = Nucleus(hparams, modelfn)
    dendrite = Dendrite(hparams, metagraph)
    dataset = Dataset(hparams)
    neuron = Neuron(hparams, nucleus, dendrite, dataset)
    synapse = Synapse(hparams, neuron, metagraph)

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    bittensor_grpc.add_BittensorServicer_to_server(synapse, server)
    server.add_insecure_port(hparams.bind_address + ":" + hparams.port)
    server.start()

    neuron.start_training()

    tl = Timeloop()
    set_timed_loops(tl, hparams, neuron)
    tl.start(block=False)
    logger.info('Started Timers.')

    try:
        logger.info('Begin wait on main...')
        while True:
            logger.debug('heartbeat')
            time.sleep(100)

    except KeyboardInterrupt:
        logger.debug('Neuron stopped with keyboard interrupt.')
        server.stop(2)
        del neuron
        del metagraph
        del synapse

    except Exception as e:
        logger.error('Neuron stopped with interrupt on error: ' + str(e))
        server.stop(2)
        del neuron
        del metagraph
Exemple #20
0
 def connect_from(self, previousNeuron):
     self.synapses_from.append(Synapse(previousNeuron))
Exemple #21
0
 def connect_to(self, nextNeuron):
     self.synapses_to.append(Synapse(nextNeuron))
Exemple #22
0
 def connect(self, post_neuron, initial_weight):
     self.axon_synapses += [Synapse(self, post_neuron, initial_weight)]
Exemple #23
0
from synapse import Synapse
from sqlalchemy.exc import OperationalError

# Initialize database
try:
    db.session.execute("SELECT * FROM 'starmap' LIMIT 1")
except OperationalError:
    app.logger.info("Initializing database")
    db.create_all()
    db.session.add(Starmap(app.config['SOMA_ID']))
    db.session.commit()

if app.config['USE_DEBUG_SERVER']:
    # flask development server
    app.run(app.config['LOCAL_HOSTNAME'], app.config['LOCAL_PORT'])
else:
    shutdown = gevent.event.Event()

    # Synapse
    app.logger.info("Starting Synapses")
    synapse = Synapse(('0.0.0.0', app.config['SYNAPSE_PORT']))
    synapse.start()

    # gevent server
    if not app.config['NO_UI']:
        app.logger.info("Starting Web-UI")
        local_server = WSGIServer(('', app.config['LOCAL_PORT']), app)
        local_server.start()

    shutdown.wait()
Exemple #24
0
from synapse import Synapse

synapse = Synapse()


@synapse.connect('/hello')
def robo(name, robot=False):
    if robot:
        return f"{name} is a robot"
    else:
        return f"{name} is not a robot"


@synapse.connect('/ok')
def ok(okn):
    return ( f"{okn}" )

# robo('meain')

synapse.start(debug=True)
# synapse.start()

import matplotlib.pyplot as plt

from neuron import LIF
from simulation import Simulation
from synapse import Synapse

# in_lif  =   LIF(0,  10, 6,  2,  lambda : 2,  4)
# out_lif =   LIF(0,  10, 6,  2,  lambda : 0,1,[in_lif])
sim = Simulation(200, 2000)
in_lif = LIF(sim, 0, 10, 6, 2, lambda: 2, [])
out_lif = LIF(sim, 0, 10, 6, 2, lambda: 0, [])
syn = Synapse(sim, in_lif, out_lif, 8, 0, 4, 1)

sim.run_simulation()

print(in_lif.v_arr)
print(out_lif.v_arr)

fig, (ax1, ax2) = plt.subplots(2,
                               sharex=True,
                               gridspec_kw={'height_ratios': [1, 3]})

ax1.plot(sim.t_arr, in_lif.s_arr, marker='.', markersize=20, linestyle='none')
ax1.plot(sim.t_arr, out_lif.s_arr, marker='.', markersize=20, linestyle='none')
ax1.set(ylabel='Spikes')
ax1.axis([0, sim.t_max, 0.5, 1.5])
ax1.set_yticklabels([])
ax1.set_yticks([])

ax2.plot(sim.t_arr, in_lif.v_arr)
ax2.plot(sim.t_arr, out_lif.v_arr)
                                  delay_MS=lambda: 5,
                                  current_floor=lambda: 6,
                                  weight=noise_wrapper(0.5, 0.5, 0, 1),
                                  probTotal=400)

    for syn in SIM.synapses:
        syn.weight = max(
            syn.stdp_weight_min,
            min(syn.stdp_weight_max,
                np.random.normal(syn.weight, syn.weight * sig)))

    for neu in MidLayer.neurons:
        Synapse(SIM,
                neu,
                OutLayer.neurons[0],
                tau_s=1,
                delay_MS=0,
                current_floor=2,
                weight=1)

    SIM.run_simulation()
    print("")
    output_neuron_varrs.append(OutLayer.neurons[0].v_arr)

# pickle.dump(output_neuron_varrs, open("output_neuron_varrsSTDPON.p", "wb"))

# output_neuron_varrs = pickle.load( open( "output_neuron_varrsSTDPOFF.p", "rb" ) )

kul_arr: List[List[float]] = []
for idx, varr in enumerate(iterable=output_neuron_varrs, start=1):
    kul_arr.append(np.zeros(tempsim.t_itmax))
Exemple #27
0
    def load_network(self, o):
        sensor_nodes = o["sensor_nodes"]
        hidden_nodes = o["hidden_nodes"]
        output_nodes = o["output_nodes"]
        self.run = int(o["generations"])
        network = Network()
        network.k = int(o["network"])
        network.EPSILON = float(o["epsilon"])
        network.MUTATION_RATE = float(o["mutation_rate"])

        synapses_visited = []
        network.actuatorList = []
        network.sensorList = []
        network.inputNodeList = []
        network.outputNodeList = []
        network.inputOutputNodeList = []

        neurons = []
        for n in sensor_nodes:
            _id = list(n.keys())[0]
            neuron = Neuron()
            neuron.k = int(_id)
            network.sensorList.append(neuron)
            network.inputNodeList.append(neuron)
            neurons.append(neuron)
        for n in output_nodes:
            _id = list(n.keys())[0]
            neuron = Neuron()
            neuron.k = int(_id)
            network.actuatorList.append(neuron)
            network.outputNodeList.append(neuron)
            neurons.append(neuron)
        for n in hidden_nodes:
            _id = list(n.keys())[0]
            neuron = Neuron()
            neuron.k = int(_id)
            network.inputOutputNodeList.append(neuron)
            neurons.append(neuron)

        # Sensor Nodes
        for n in sensor_nodes:
            _id = list(n.keys())[0]
            neuron = self.find_neuron(neurons, int(_id))

            for s in n[_id]["output_synapses"]:
                for key, val in s.items():
                    if key in synapses_visited or key == "other_end":
                        continue

                    other_end = self.find_neuron(neurons, int(s["other_end"]))
                    syn = Synapse(neuron, other_end, float(val))
                    syn.k = int(key)
                    synapses_visited.append(key)
                    neuron.outputList.append(syn)
                    other_end.inputList.append(syn)

        # Output Nodes
        for n in output_nodes:
            _id = list(n.keys())[0]
            neuron = self.find_neuron(neurons, int(_id))

            for s in n[_id]["input_synapses"]:
                for key, val in s.items():
                    if key in synapses_visited or key == "other_end":
                        continue

                    other_end = self.find_neuron(neurons, int(s["other_end"]))
                    syn = Synapse(other_end, neuron, float(val))
                    syn.k = int(key)
                    synapses_visited.append(key)
                    neuron.inputList.append(syn)
                    other_end.outputList.append(syn)

        # Hidden Nodes
        for n in hidden_nodes:
            _id = list(n.keys())[0]
            neuron = self.find_neuron(neurons, int(_id))

            for s in n[_id]["input_synapses"]:
                for key, val in s.items():
                    if key in synapses_visited or key == "other_end":
                        continue

                    other_end = self.find_neuron(neurons, int(s["other_end"]))
                    syn = Synapse(other_end, neuron, float(val))
                    syn.k = int(key)
                    synapses_visited.append(key)
                    neuron.inputList.append(syn)
                    other_end.outputList.append(syn)

            for s in n[_id]["output_synapses"]:
                for key, val in s.items():
                    if key in synapses_visited or key == "other_end":
                        continue

                    other_end = self.find_neuron(neurons, int(s["other_end"]))
                    syn = Synapse(neuron, other_end, float(val))
                    syn.k = int(key)
                    synapses_visited.append(key)
                    neuron.outputList.append(syn)
                    other_end.inputList.append(syn)

        for snake in self.snakeList:
            snake.network = network.copy()

        return
Exemple #28
0
    def copy(self):
        q = []
        m = {}
        copy_network = Network()

        original_actuator_list = self.actuatorList

        copy_actuator_list = [Neuron(), Neuron(), Neuron(), Neuron()]
        copy_network.actuatorList = copy_actuator_list

        for i in range(len(original_actuator_list)):
            q.append(original_actuator_list[i])
            m[original_actuator_list[i]] = copy_actuator_list[i]
            copy_actuator_list[i].k = original_actuator_list[i].k

        copy_io_nodes = []
        copy_i_nodes = []
        copy_o_nodes = []
        copy_sensors = []

        for sensor in self.sensorList:
            tmp = Neuron()
            m[sensor] = tmp
            tmp.k = sensor.k
            q.append(sensor)

            copy_sensors.append(tmp)
            copy_i_nodes.append(tmp)

        while len(q) > 0:
            original_node = q.pop()

            i_syns = original_node.inputList
            o_syns = original_node.outputList

            copy_node = m[original_node]
            copy_node.k = original_node.k

            for syn in i_syns:
                input_original = syn.sourceNeuron

                if input_original in list(m.keys()):
                    input_copy = m[input_original]

                    exists = False
                    for n in input_copy.outputList:
                        if n.sourceNeuron == input_copy and n.destinationNeuron == copy_node:
                            exists = True
                            break
                    if not exists:
                        copy_syn = Synapse(input_copy, copy_node, syn.weight)
                        copy_node.add_input(copy_syn)
                        input_copy.add_output(copy_syn)
                else:
                    input_copy = Neuron()
                    input_copy.k = input_original.k
                    copy_io_nodes.append(input_copy)

                    copy_syn = Synapse(input_copy, copy_node, syn.weight)
                    input_copy.add_output(copy_syn)
                    copy_node.add_input(copy_syn)
                    m[input_original] = input_copy
                    q.append(input_original)

            for syn in o_syns:
                output_original = syn.destinationNeuron

                if output_original in list(m.keys()):
                    output_copy = m[output_original]

                    exists = False
                    for n in output_copy.inputList:
                        if n.sourceNeuron == copy_node and n.destinationNeuron == output_copy:
                            exists = True
                            break
                    if not exists:
                        copy_syn = Synapse(copy_node, output_copy, syn.weight)
                        copy_node.add_output(copy_syn)
                        output_copy.add_input(copy_syn)
                else:
                    output_copy = Neuron()
                    output_copy.k = output_original.k
                    copy_io_nodes.append(output_copy)

                    copy_syn = Synapse(copy_node, output_copy, syn.weight)
                    output_copy.add_input(copy_syn)
                    copy_node.add_output(copy_syn)
                    m[output_original] = output_copy
                    q.append(output_original)

        for n in copy_io_nodes:
            copy_i_nodes.append(n)
            copy_o_nodes.append(n)
        copy_o_nodes.extend(copy_actuator_list)

        copy_network.sensorList = copy_sensors
        copy_network.inputNodeList = copy_i_nodes
        copy_network.outputNodeList = copy_o_nodes
        copy_network.inputOutputNodeList = copy_io_nodes

        return copy_network