How to use the nengo.Ensemble function in nengo

To help you get started, we’ve selected a few nengo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github studywolf / blog / Nengo scripting / Nengo 2 / arm_control / nengo_arm.py View on Github external
<svg viewBox="0 0 100 100" height="100%" width="100%">
            <line style="stroke:black" y2="{y2}" x2="{x2}" y1="{y1}" x1="{x1}"></line>
            <line style="stroke:black" y2="{y3}" x2="{x3}" y1="{y2}" x1="{x2}"></line>
            <circle fill="black" stroke-width="1" stroke="black" r="1.5" cy="{y3}" cx="{x3}"></circle>
        </svg>
        '''.format(**locals())
        # end of visualization code ---------------------------------------------

        return data
    arm_node = nengo.Node(output=arm_func, size_in=2)

    # specify torque input to arm
    input_node = nengo.Node(output=[1, .1])

    # to send a target to an ensemble which then connections to the arm
    ens = nengo.Ensemble(n_neurons=500, dimensions=2, radius=20)
    nengo.Connection(input_node, ens[:2]) # to send target info to ensemble

    # connect ens to arm
    nengo.Connection(ens, arm_node)#, function=some_function) 
    # --------------------------------------------------------

if run_in_GUI:
    # to run in GUI, comment out next 4 lines for running without GUI
    import nengo_gui
    nengo_gui.GUI(model=model, filename=__file__, locals=locals(), 
                  interactive=False, allow_file_change=False).start()
    import sys
    sys.exit()
else:  
    # to run in command line
    with model:
github simondlevy / MulticopterSim / Extras / nengo / newgui.py View on Github external
copter = Multicopter()
    copter.start()
    
    q_target = nengo.Node(lambda t: q_target_value, label='q_target')
    q = nengo.Node(lambda t: q_value, label='q')
    dq = nengo.Node(lambda t: dq_value, label='dq')

    dq_target = nengo.Node(None, size_in=1, label='dq_target')
    nengo.Connection(q_target, dq_target, synapse=None, transform=1) # direct feed-forward synapse
    nengo.Connection(q_target, dq_target, synapse=0, transform=-1)   # minimal time-step delay

    q_err = nengo.Ensemble(n_neurons=N_NEURONS, dimensions=1, label='q_err')
    nengo.Connection(q_target, q_err, synapse=None)
    nengo.Connection(q, q_err, synapse=None, transform=-1)

    q_err_integral = nengo.Ensemble(n_neurons=N_NEURONS, dimensions=1, radius=INTEGRAL_RADIUS,
            label='q_err_integral')
    nengo.Connection(q_err, q_err_integral, synapse=INTEGRAL_SYNAPSE, transform=INTEGRAL_SYNAPSE)
    nengo.Connection(q_err_integral, q_err_integral, synapse=INTEGRAL_SYNAPSE, transform=1)

    x = nengo.Ensemble(n_neurons=1200, dimensions=3, radius=30)

    u = nengo.Node(None, size_in=1, label='u')    # output
    
    nengo.Connection(q_err, u, transform=KP, synapse=None)

    nengo.Connection(q_err_integral, u, transform=KI, synapse=None)
    
    dq_err = nengo.Ensemble(n_neurons=N_NEURONS, dimensions=1, label='dq_err')
    nengo.Connection(dq_target, dq_err, synapse=None)
    nengo.Connection(dq, dq_err, synapse=None, transform=-1)
github simondlevy / MulticopterSim / Extras / nengo / nengo_pid_controller.py View on Github external
with model:

            q_target = nengo.Node(lambda t: self.q_target_value, label='q_target')
            q = nengo.Node(lambda t: self.q_value, label='q')
            dq = nengo.Node(lambda t: self.dq_value, label='dq')
            
            dq_target = nengo.Node(None, size_in=self.n_dims, label='dq_target')
            nengo.Connection(q_target, dq_target, synapse=None, transform=1) # direct feed-forward synapse
            nengo.Connection(q_target, dq_target, synapse=0, transform=-1)   # minimal time-step delay
            
            q_err = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=self.n_dims, label='q_err')
            nengo.Connection(q_target, q_err, synapse=None)
            nengo.Connection(q, q_err, synapse=None, transform=-1)

            q_err_integral = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=self.n_dims, radius=self.integral_radius,
                    label='q_err_integral')
            nengo.Connection(q_err, q_err_integral, synapse=self.integral_synapse, transform=self.integral_synapse)
            nengo.Connection(q_err_integral, q_err_integral, synapse=self.integral_synapse, transform=1)
            
            u = nengo.Node(None, size_in=self.n_dims, label='u')    # output
            
            nengo.Connection(q_err, u, transform=self.Kp, synapse=None)

            nengo.Connection(q_err_integral, u, transform=self.Ki, synapse=None)
            
            dq_err = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=self.n_dims, label='dq_err')
            nengo.Connection(dq_target, dq_err, synapse=None)
            nengo.Connection(dq, dq_err, synapse=None, transform=-1)
            
            nengo.Connection(dq_err, u, transform=self.Kd, synapse=None)
github adityagilra / FOLLOW / input_rec_transform_nengo_plot.py View on Github external
zeroidxs = np.where(rate==0)[0]                                     # indices of neurons that don't fire at all
    
    ## build an ensemble exactly as in the test file simulation
    ##  and find the bises of those neurons
    ## NOTE: Set the seeds and other params manually below,
    ##  as they could be ambiguous from name of the file
    import nengo
    Nexc, N, reprRadius, nrngain = 3000, 2, 5, 2
    seedR0, seedR2 = 2, 4
    gain_bias_set = True
    #biaslow, biashigh = 1 - nrngain, 1 + nrngain
    biaslow, biashigh = -nrngain, nrngain
    print('building model')
    mainModel = nengo.Network(label="Single layer network", seed=seedR0)
    with mainModel:
        ratorOut = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadius,
                            neuron_type=nengo.neurons.LIF(),
                            bias=nengo.dists.Uniform(biaslow,biashigh), gain=np.ones(Nexc)*nrngain, 
                            #max_rates=nengo.dists.Uniform(200, 400),
                            noise=None, seed=seedR2, label='ratorOut' )
    sim = nengo.Simulator(mainModel,dt)
    biases = sim.data[ratorOut].bias
    zerofiringbiases = biases[zeroidxs]
    gains = sim.data[ratorOut].gain
    zerofiringgains = gains[zeroidxs]
    
    if gain_bias_set: histrange, biasrange = 5, 5
    else: histrange, biasrange = 500, 100
    fig = plt.figure(facecolor='w')
    ax1 = plt.subplot(231)
    vals,_,_ = ax1.hist(gains,bins=50,range=(0,histrange),color='k',histtype='step')
    ax1.set_xlabel('all gains')
github simondlevy / MulticopterSim / Source / MulticopterSim / hackflight / python / nengo_pidcontrol.py View on Github external
with self.model:

            q_target = nengo.Node(lambda t: self.q_target_value, label='q_target')
            q = nengo.Node(lambda t: self.q_value, label='q')
            dq = nengo.Node(lambda t: self.dq_value, label='dq')
            
            dq_target = nengo.Node(None, size_in=1, label='dq_target')
            nengo.Connection(q_target, dq_target, synapse=None, transform=1) # direct feed-forward synapse
            nengo.Connection(q_target, dq_target, synapse=0, transform=-1)   # minimal time-step delay
            
            q_err = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=1, label='q_err')
            nengo.Connection(q_target, q_err, synapse=None)
            nengo.Connection(q, q_err, synapse=None, transform=-1)

            q_err_integral = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=1, radius=integral_radius,
                    label='q_err_integral')
            nengo.Connection(q_err, q_err_integral, synapse=integral_synapse, transform=integral_synapse)
            nengo.Connection(q_err_integral, q_err_integral, synapse=integral_synapse, transform=1)
            
            u = nengo.Node(None, size_in=1, label='u')    # output
            
            nengo.Connection(q_err, u, transform=self.Kp, synapse=None)

            nengo.Connection(q_err_integral, u, transform=Ki, synapse=None)
            
            dq_err = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=1, label='dq_err')
            nengo.Connection(dq_target, dq_err, synapse=None)
            nengo.Connection(dq, dq_err, synapse=None, transform=-1)
            
            nengo.Connection(dq_err, u, transform=self.Kd, synapse=None)
github adityagilra / FOLLOW / input_ff_rec_transform_nengo_ocl.py View on Github external
seed=seedR2)
        else:
            processIn = None
            processOut = None
        nodeIn = nengo.Node( size_in=N, output = lambda timeval,currval: inpfn(timeval) )
        # with zero bias, at reprRadius, if you want 50Hz, gain=1.685, if 100Hz, gain=3.033, if 400Hz, 40.5
        #nrngain = 1.5#2#3.033
        # input layer from which feedforward weights to ratorOut are computed
        ratorIn = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadiusIn,
                            neuron_type=nengo.neurons.LIF(),
                            #bias=nengo.dists.Uniform(-nrngain,nrngain), gain=np.ones(Nexc)*nrngain,
                            max_rates=nengo.dists.Uniform(200, 400),
                            noise=processIn, seed=seedR1, label='ratorIn' )
        nengo.Connection(nodeIn, ratorIn, synapse=None)         # No filtering here as no filtering/delay in the plant/arm
        # another layer with learning incorporated
        ratorOut = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadius,
                            neuron_type=nengo.neurons.LIF(),
                            #bias=nengo.dists.Uniform(-nrngain,nrngain), gain=np.ones(Nexc)*nrngain, 
                            max_rates=nengo.dists.Uniform(200, 400),
                            noise=processOut, seed=seedR2, label='ratorOut' )
        
        if trialClamp:
            # clamp ratorIn and ratorOut at the end of each trial (Tperiod) for 100ms.
            #  Error clamped below during end of the trial for 100ms.
            clampValsZeros = np.zeros(Nexc)
            clampValsNegs = -100.*np.ones(Nexc)
            endTrialClamp = nengo.Node(lambda t: clampValsZeros if (t%Tperiod)&lt;(Tperiod-Tclamp) else clampValsNegs)
            nengo.Connection(endTrialClamp,ratorIn.neurons,synapse=1e-3)
            nengo.Connection(endTrialClamp,ratorOut.neurons,synapse=1e-3)
                                                                # fast synapse for fast-reacting clamp
        
        if plastDecoders:
github arnav-gudibande / nengopacman / world.py View on Github external
start = 2*i
        end = ((2*(i+1))-1)+1

        # Connected the world's move sensor to pacman's move sensor
        nengo.Connection(sensor[start:end], pacPlayer.move)

        # Created Food ensemble
        food = nengo.Ensemble(n_neurons = 100, dimensions = 2)
        nengo.Connection(pacPlayer.detect_food, food, synapse = 0.)

        # Connected pacman's food node to the world's ensemble
        nengo.Connection(food[0], sensor[end-1], transform = 2, synapse = 0.)
        nengo.Connection(food[1], sensor[start], transform = 3, synapse = 0.)

        # Created obstacles ensemble
        obstacles = nengo.Ensemble(n_neurons = 50, dimensions = 3, radius = 4)
        nengo.Connection(pacPlayer.obstacles[[1,2,3]], obstacles, transform = 0.5, synapse = 0.)

        # turn away from walls
        def avoid(x):
            return 1*(x[2] - x[0])
        # Connected the obstacles node to the world's ensemble
        nengo.Connection(obstacles, sensor[end-1], function=avoid, synapse = 0.)

        # avoid crashing into walls
        def ahead(x):
            return 1*(x[1] - 0.5)
        # Connected the obstacles node to the world's ensemble
        nengo.Connection(obstacles, sensor[start], function=ahead, synapse = 0.)

        enemy = nengo.Ensemble(n_neurons = 50, dimensions = 2)
        nengo.Connection(pacPlayer.detect_enemy, enemy, synapse = 0.)
github arnav-gudibande / nengopacman / pacman.py View on Github external
# sense food
            # - angle is the direction to the food
            # - radius is the strength (1.0/distance)
            food = nengo.Ensemble(n_neurons=100, dimensions=2)
            nengo.Connection(pacnet.detect_food, food, synapse = 0.)

            # turn towards food
            nengo.Connection(food[0], move[1], transform=2)
            # move towards food
            nengo.Connection(food[1], move[0], transform=3)

            # sense obstacles
            # - distance to obstacles on left, front-left, front, front-right, and right
            # - maximum distance is 4
            obstacles = nengo.Ensemble(n_neurons=50, dimensions=3, radius=4)
            nengo.Connection(pacnet.obstacles[[1,2,3]], obstacles, transform=0.5, synapse = 0.)

            # turn away from walls
            def avoid(x):
                return 1*(x[2] - x[0])
            nengo.Connection(obstacles, move[1], function=avoid)

            # avoid crashing into walls
            def ahead(x):
                return 1*(x[1] - 0.5)
            nengo.Connection(obstacles, move[0], function=ahead)

            # detect enemies
            enemy = nengo.Ensemble(n_neurons=50, dimensions=2)
            nengo.Connection(pacnet.detect_enemy, enemy, synapse = 0.)
github adityagilra / FOLLOW / input_ff_rec_transform_nengo_ocl.py View on Github external
processIn = nengo.processes.WhiteNoise(
                default_size_out=Nexc, default_dt = dt,
                dist=nengo.dists.Gaussian(0 if (dynNoiseMean is None) else dynNoiseMean, dynNoise),
                seed=seedR1)
            processOut = nengo.processes.WhiteNoise(
                default_size_out=Nexc, default_dt = dt,
                dist=nengo.dists.Gaussian(0 if (dynNoiseMean is None) else dynNoiseMean, dynNoise),
                seed=seedR2)
        else:
            processIn = None
            processOut = None
        nodeIn = nengo.Node( size_in=N, output = lambda timeval,currval: inpfn(timeval) )
        # with zero bias, at reprRadius, if you want 50Hz, gain=1.685, if 100Hz, gain=3.033, if 400Hz, 40.5
        #nrngain = 1.5#2#3.033
        # input layer from which feedforward weights to ratorOut are computed
        ratorIn = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadiusIn,
                            neuron_type=nengo.neurons.LIF(),
                            #bias=nengo.dists.Uniform(-nrngain,nrngain), gain=np.ones(Nexc)*nrngain,
                            max_rates=nengo.dists.Uniform(200, 400),
                            noise=processIn, seed=seedR1, label='ratorIn' )
        nengo.Connection(nodeIn, ratorIn, synapse=None)         # No filtering here as no filtering/delay in the plant/arm
        # another layer with learning incorporated
        ratorOut = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadius,
                            neuron_type=nengo.neurons.LIF(),
                            #bias=nengo.dists.Uniform(-nrngain,nrngain), gain=np.ones(Nexc)*nrngain, 
                            max_rates=nengo.dists.Uniform(200, 400),
                            noise=processOut, seed=seedR2, label='ratorOut' )
        
        if trialClamp:
            # clamp ratorIn and ratorOut at the end of each trial (Tperiod) for 100ms.
            #  Error clamped below during end of the trial for 100ms.
            clampValsZeros = np.zeros(Nexc)
github simondlevy / MulticopterSim / Extras / nengo / nengo_pid_controller.py View on Github external
self.n_neurons = n_neurons
        self.integral_synapse = integral_synapse
        self.integral_radius = integral_radius
        self.seed = seed
        
        with model:

            q_target = nengo.Node(lambda t: self.q_target_value, label='q_target')
            q = nengo.Node(lambda t: self.q_value, label='q')
            dq = nengo.Node(lambda t: self.dq_value, label='dq')
            
            dq_target = nengo.Node(None, size_in=self.n_dims, label='dq_target')
            nengo.Connection(q_target, dq_target, synapse=None, transform=1) # direct feed-forward synapse
            nengo.Connection(q_target, dq_target, synapse=0, transform=-1)   # minimal time-step delay
            
            q_err = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=self.n_dims, label='q_err')
            nengo.Connection(q_target, q_err, synapse=None)
            nengo.Connection(q, q_err, synapse=None, transform=-1)

            q_err_integral = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=self.n_dims, radius=self.integral_radius,
                    label='q_err_integral')
            nengo.Connection(q_err, q_err_integral, synapse=self.integral_synapse, transform=self.integral_synapse)
            nengo.Connection(q_err_integral, q_err_integral, synapse=self.integral_synapse, transform=1)
            
            u = nengo.Node(None, size_in=self.n_dims, label='u')    # output
            
            nengo.Connection(q_err, u, transform=self.Kp, synapse=None)

            nengo.Connection(q_err_integral, u, transform=self.Ki, synapse=None)
            
            dq_err = nengo.Ensemble(n_neurons=self.n_neurons, dimensions=self.n_dims, label='dq_err')
            nengo.Connection(dq_target, dq_err, synapse=None)