Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import nengo
import pacman_world
reload(pacman_world)
from random import randint
import numpy as np
import random
import re
import body
import maze
# Nengo Network
model = nengo.Network()
mymap = maze.generateMaze(4, 10)
# Initliazing the nengo model, network and game map
with model:
pacman1 = body.Player("pacman", 1, 2, "yellow", 70, 20)
pacman2 = body.Player("pacman", 2, 2, "yellow", 70, 20)
pacman3 = body.Player("pacman", 3, 2, "yellow", 70, 20)
pacmen = [pacman1, pacman2, pacman3]
myGhost = body.Player("ghost", "seeking", 2, "red", 5, 5)
myGhost2 = body.Player("ghost", "seeking", 2, "green", 5, 5)
myGhost3 = body.Player("ghost", "seeking", 2, "blue", 5, 5)
ghostList = []
ghostList.append(myGhost2)
pacman1 = body.Player("pacman", 1, 2, "yellow", 70, 20)
pacman2 = body.Player("pacman", 2, 2, "yellow", 70, 20)
pacman3 = body.Player("pacman", 3, 2, "yellow", 70, 20)
pacmen = [pacman1, pacman2, pacman3]
myGhost = body.Player("ghost", "seeking", 2, "red", 5, 5)
myGhost2 = body.Player("ghost", "seeking", 2, "green", 5, 5)
myGhost3 = body.Player("ghost", "seeking", 2, "blue", 5, 5)
ghostList = []
ghostList.append(myGhost2)
ghostList.append(myGhost3)
world = pacman_world.PacmanWorld(mymap, pacmen, myGhost, ghostList)
for (i, pac) in enumerate(pacmen):
player = nengo.Network("Pacman " + str(i+1))
with player:
# pacnets contain "pacman" networks
pacnet = world.pacnets[i]
# create the movement control
# - dimensions are speed (forward|backward) and rotation (left|right)
move = nengo.Ensemble(n_neurons=100, dimensions=2, radius=3)
nengo.Connection(move, pacnet.move, synapse = 0.)
# 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.)
('_testFrom'+str(continueTmax)+inputStr if testLearned else '') + \
'_'+str(Tmax)+'s'
print('data will be saved to', dataFileName, '_.shelve')
if continueLearning or testLearned:
weightsSaveFileName = baseFileName + '_'+str(continueTmax+Tmax)+'s_endweights.shelve'
weightsLoadFileName = baseFileName + '_'+str(continueTmax)+'s_endweights.shelve'
else:
weightsSaveFileName = baseFileName + '_'+str(Tmax)+'s_endweights.shelve'
weightsLoadFileName = baseFileName + '_'+str(Tmax)+'s_endweights.shelve'
if __name__ == "__main__":
#########################
### Create Nengo network
#########################
print('building model')
mainModel = nengo.Network(label="Single layer network", seed=seedR0)
with mainModel:
# create white noise processes for neurons in the ff and rec ensembles
if dynNoise is not None:
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
def buildpid():
# Create Nengo network
model = nengo.Network()
# Create PID controller
pid = NengoPidController(model, KP, KD, KI)
return model, pid
import nengo
import pacman_world
reload(pacman_world)
from random import randint
import numpy as np
import random
import re
import cellular
import body
import maze
# Nengo Network
model = nengo.Network()
mymap = maze.generateMaze(num_rows=3, num_cols=10, num_ghosts=3, seed=1)
world = nengo.Network()
# Initliazing the nengo model, network and game map
myPacman = body.Player("pacman", "eating", 2, "yellow", 70, 20)
myPacman2 = body.Player("pacman", "eating", 2, "yellow", 70, 20)
myGhost = body.Player("ghost", "seeking", 2, "red", 5, 5)
myGhost2 = body.Player("ghost", "seeking", 2, "red", 5, 5)
ghostList = []
with model:
pacPlayer = pacman_world.PacmanWorld(mymap, myPacman, myGhost, ghostList)
pacPlayer2 = pacman_world.PacmanWorld(mymap, myPacman2, myGhost, ghostList)
gamePacmen = [pacPlayer, pacPlayer2]
if analog:
# account for continuous lowpass filter
A = tau * A + np.eye(2)
B = tau * B
else:
# discretize state matrices
Ad = expm(A*dt)
Bd = np.dot(np.linalg.inv(A), np.dot((Ad - np.eye(2)), B))
# account for discrete lowpass filter
a = np.exp(-dt/tau)
A = 1.0 / (1.0 - a) * (Ad - a * np.eye(2))
B = 1.0 / (1.0 - a) * Bd
if net is None:
net = nengo.Network(label='Point Attractor')
config = nengo.Config(nengo.Connection, nengo.Ensemble)
config[nengo.Connection].synapse = nengo.Lowpass(tau)
with config, net:
net.ydy = nengo.Ensemble(n_neurons=n_neurons, dimensions=2,
# set it up so neurons are tuned to one dimensions only
encoders=nengo.dists.Choice([[1, 0], [-1, 0], [0, 1], [0, -1]]))
# set up Ax part of point attractor
nengo.Connection(net.ydy, net.ydy, transform=A)
# hook up input
net.input = nengo.Node(size_in=1, size_out=1)
# set up Bu part of point attractor
nengo.Connection(net.input, net.ydy, transform=B)
# hook up output
def __init__(self, Kp, Kd, Ki=0, sim_time=.01, n_neurons=500, integral_synapse=0.1, integral_radius=1, seed=None):
self.q_value = 0
self.q_target_value = 0
self.dq_value = 0
self.last_actual = 0
self.output_value = 0
self.model = nengo.Network(seed=seed)
self.Kp = Kp
self.Kd = Kd
self.sim_time = sim_time
self.n_neurons = n_neurons
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
# add in forcing function
nengo.Connection(net.ydy[0], net.output, synapse=None)
return net
if __name__ == '__main__':
import matplotlib.pyplot as plt
plt.figure(figsize=(5, 12))
dt = 1e-3
time = 5 # number of seconds to run simulation
alphas = [10.0, 100.0, 1000.0]
for ii, alpha in enumerate(alphas):
probe_results = []
model = nengo.Network()
beta = alpha / 4.0
for option in [True, False]:
with model:
def goal_func(t):
return float(int(t)) / time * 2 - 1
goal = nengo.Node(output=goal_func)
pa = generate(n_neurons=1000, analog=option,
alpha=alpha, beta=beta, dt=dt)
nengo.Connection(goal, pa.input, synapse=None)
probe_ans = nengo.Probe(goal)
probe = nengo.Probe(pa.output, synapse=.01)
sim = nengo.Simulator(model, dt=dt)
sim.run(time)
probe_results.append(np.copy(sim.data[probe]))
agent_poly = ('