def __init__(self, name, net, has_subbehaviors=False): """Constructor Parameters: name --- A string that describes the behavior. net --- The nxsdk network object required to create the neurons. has_subbehaviors --- Set this flag to true if this behavior does not do any direct work but only connects to a set of subbehaviors. """ self.name = name self.net = net self.has_subbehaviors = has_subbehaviors # activated and sustained by input from task (or higher-level intention node), turns off without that input self.node_prior_intention = Node("Prior Intention", net) # activated by input from prior intention node, turns off without that input self.node_intention = Node("Intention", net) # a dictionary for CoS memory nodes self.nodes_cos_memory = {} # a dictionary for CoS nodes self.nodes_cos = {} # a dictionary for precondition nodes self.preconditions = {} # the prior intention node activates the intention node connect(self.node_prior_intention, self.node_intention, 1.1) # if this behavior only has subbehaviors, add a single CoS signal # it will later be fed from the CoS signals of all subbehaviors if (self.has_subbehaviors): single_cos_name = "done" self.add_cos(single_cos_name) self.single_node_cos = self.nodes_cos[single_cos_name]
def add_cos(self, cos_name, cos_input=None): """Adds a condition-of-satisfaction (CoS) to the behavior. Parameters: name --- A string that describes the CoS. cos_input --- The input that provides the CoS signal. Leave empty if you want to add it manually later. """ # activated by input from BOTH intention node and "sensory" input, turns off without either cos_node = Node(cos_name, self.net) self.nodes_cos[cos_name] = cos_node # activated by input from BOTH task and cos node, remains on without cos input, turns off without task input cos_memory_node = Node(cos_name + " Memory", self.net, self_excitation=0.3) self.nodes_cos_memory[cos_name] = cos_memory_node int_cos_weight = 0.6 if (not self.has_subbehaviors) else 1.0 connect(self.node_intention, cos_node, int_cos_weight) connect(cos_node, cos_memory_node, 0.3) connect(cos_memory_node, self.node_intention, -1.0) if (cos_input != None): connect(cos_input, cos_node, 0.6)
def add_precondition(self, name, precondition_behaviors, cos_names, task_input, logical_or=False, register_groups=None): """Connects a list of other behaviors in such a way that they are on the next lower hierarchical level with respect to the current behavior. Parameters: name --- A string description of the precondition. precondition_behaviors --- A list of behaviors whose successful execution are a precondition to start executing this behavior. By default, all precondition behaviors have to be successfully executed to start executing this behavior. cos_names --- A list of lists of CoS names, e.g., [["CoS 1, first behavior", "CoS 2, first behavior"], ["CoS 1, second behavior"]] task_input --- Input activating the precondition nodes. logical_or --- By setting this flag to true, this behavior requires only one of the preconditions to start execution. """ precondition_node = Node(name, self.net) self.preconditions[name] = precondition_node if (register_groups != None): register_groups[name] = precondition_node.neurons connect(precondition_node, self.node_intention, -1.1) #precondition_node.visualization = Node.PreconditionVisualization() #precondition_node.visualization.set_target_behavior(self.name) #precondition_node.visualization.task_node = task_input for i in range(len(precondition_behaviors)): for j in range(len(cos_names[i])): weight = -1.1 if (not logical_or): weight = weight / len(precondition_behaviors) connect( precondition_behaviors[i].nodes_cos_memory[ cos_names[i][j]], precondition_node, weight) #precondition_node.visualization.add_source_cos(precondition_behaviors[i].name, cos_names[i][j]) connect(task_input, precondition_node, 1.1)
from dft_loihi.inputs.simulated_input import HomogeneousPiecewiseStaticInput from dft_loihi.dft.util import connect # set up the network net = nxsdk.net.net.NxNet() neurons_per_node = 1 simulated_input = HomogeneousPiecewiseStaticInput("input", net, neurons_per_node) simulated_input.add_spike_rate(0, 100) simulated_input.add_spike_rate(500, 100) simulated_input.add_spike_rate(2000, 100) simulated_input.add_spike_rate(500, 100) simulated_input.add_spike_rate(0.0, 100) simulated_input.create() node = Node("node", net, self_excitation=1.1) connect(simulated_input, node, 1.1) # run the network time_steps = 500 net.run(time_steps) net.disconnect() # plot results plotter = Plotter() plotter.add_input_plot(simulated_input) plotter.add_node_plot(node) plotter.plot()
def draw_behavior(x, y, behavior): offset_x_neurons = 2 offset_y_neurons = 2 # behavior label offset_x_center = (len(behavior.nodes_cos) * offset_x_neurons) / 2. matplotlib.pyplot.text(x + offset_x_center, y - 2 * offset_y_neurons, behavior.name.replace("state_machine.", "").replace("_", " "), ha='center', va='bottom', transform=ax.transData) # intention label #matplotlib.pyplot.text(x, # y + offset_y_neurons, # "start", # ha='center', # va='bottom', # transform=ax.transData) # prior intention behavior.node_prior_intention.visualization = Node.Visualization( x, y, False) behavior.node_prior_intention.visualization.draw() # intention behavior.node_intention.visualization = Node.Visualization( x, y - offset_y_neurons, False) behavior.node_intention.visualization.draw() draw_axon(behavior.node_prior_intention.visualization, behavior.node_intention.visualization) for j, key in enumerate(behavior.nodes_cos.keys()): x += offset_x_neurons # cos label #matplotlib.pyplot.text(x, # y + offset_y_neurons, # key, # ha='center', # va='bottom', # transform=ax.transData) # cos memory node_cos_memory = behavior.nodes_cos_memory[key] node_cos_memory.visualization = Node.Visualization(x, y, False) node_cos_memory.visualization.draw() # cos node_cos = behavior.nodes_cos[key] node_cos.visualization = Node.Visualization( x, y - offset_y_neurons, False) node_cos.visualization.draw() curved = True if (j > 0) else False draw_axon(behavior.node_intention.visualization, node_cos.visualization, curved=curved) draw_axon(node_cos_memory.visualization, behavior.node_intention.visualization, inhibitory=True) draw_axon(node_cos.visualization, node_cos_memory.visualization) return x
def __init__(self, net): self.net = net # return values self.probed_groups = { } # a dictionary of all compartments that produce output for YARP self.out_groups = { } # a dictionary of all compartments that produce output to other networks on Loihi self.input_from_yarp = { } # a dictionary of all compartments that receive input from YARP self.input_from_loihi = { } # a dictionary of all compartments that receive input from other networks on Loihi self.behavior_dictionary = {} self.groups = {} # a dictionary of all the compartment groups behavior_look = self.create_behavior("look", output=self.probed_groups, has_subbehaviors=True) behavior_look_at_object = self.create_behavior( "state_machine.look_at_object", cos_names=["done"], output=self.probed_groups, input=self.input_from_yarp) behavior_recognize_object = self.create_behavior( "state_machine.recognize_object", cos_names=["known", "unknown"], output=self.probed_groups, input=self.input_from_loihi) behavior_learn_new_object = self.create_behavior( "state_machine.learn_new_object", cos_names=["done"], output=self.probed_groups, input=self.input_from_yarp) behavior_dummy = self.create_behavior("state_machine.dummy", cos_names=["done"], output=self.probed_groups, input=self.input_from_yarp) # make this dummy behavior instantly create its own CoS signal connect(behavior_dummy.node_intention, behavior_dummy.nodes_cos["done"], 1.1) behavior_look.add_subbehaviors( [behavior_look_at_object, behavior_recognize_object]) behavior_look.add_subbehaviors( [behavior_learn_new_object, behavior_dummy], logical_or=True) behavior_recognize_object.add_precondition( "prec.look_at_object:recognize_object", [behavior_look_at_object], [["done"]], behavior_look.node_intention, register_groups=self.groups) self.probed_groups[ "state_machine.recognize_object.prec.look_at_object"] = behavior_recognize_object.preconditions[ "prec.look_at_object:recognize_object"] behavior_dummy.add_precondition("prec.recognize_object:dummy", [behavior_recognize_object], [["known"]], behavior_look.node_intention, register_groups=self.groups) self.probed_groups[ "state_machine.dummy.prec.recognize_object"] = behavior_dummy.preconditions[ "prec.recognize_object:dummy"] behavior_learn_new_object.add_precondition( "prec.recognize_object:learn_new_object", [behavior_recognize_object], [["unknown"]], behavior_look.node_intention, register_groups=self.groups) self.probed_groups[ "state_machine.learn_new_object.prec.recognize_object"] = behavior_learn_new_object.preconditions[ "prec.recognize_object:learn_new_object"] behavior_query = self.create_behavior("query", output=self.probed_groups, has_subbehaviors=True) behavior_query_memory = self.create_behavior( "state_machine.query_memory", cos_names=["done"], output=self.probed_groups, input=self.input_from_yarp) behavior_query.add_subbehaviors([behavior_query_memory]) query_behavior = False look_bias = 100 query_bias = 0 if (query_behavior): look_bias = 0 query_bias = 100 # top node that activates everything if it receives input node_look_name = "Node look" node_look = Node(node_look_name, net, biasMant=look_bias, biasExp=7) self.groups[node_look_name] = node_look behavior_look.add_boost(node_look) connect(behavior_look.nodes_cos_memory["done"], node_look, -1.5) node_query_name = "Node query" node_query = Node(node_query_name, net, biasMant=query_bias, biasExp=7) self.groups[node_query_name] = node_query behavior_query.add_boost(node_query) connect(behavior_query.nodes_cos_memory["done"], node_query, -1.5)
# set up the network net = nxsdk.net.net.NxNet() neurons_per_node = 1 homogeneous_input = HomogeneousPiecewiseStaticInput("input to node", net, shape=neurons_per_node) homogeneous_input.add_spike_rate(0, 200) homogeneous_input.add_spike_rate(1000, 200) homogeneous_input.add_spike_rate(0, 100) homogeneous_input.create() node = Node("node", net, number_of_neurons=neurons_per_node, tau_voltage=2, tau_current=10, self_excitation=0.08) connect(homogeneous_input, node, 0.5, mask="one-to-one") field_domain = [-5, 5] field_shape = 15 gauss_input = GaussPiecewiseStaticInput("input to field", net, domain=field_domain, shape=field_shape) gauss_input.add_spike_rate(0, 2.5, 1.5, 100) gauss_input.add_spike_rate(800, 2.5, 1.5, 300) gauss_input.add_spike_rate(0, 2.5, 1.5, 100) gauss_input.create()
from dft_loihi.inputs.simulated_input import HomogeneousPiecewiseStaticInput from dft_loihi.dft.util import connect # set up the network net = nxsdk.net.net.NxNet() neurons_per_node = 1 simulated_input = HomogeneousPiecewiseStaticInput("input", net, neurons_per_node) simulated_input.add_spike_rate(0, 100) simulated_input.add_spike_rate(500, 100) simulated_input.add_spike_rate(2000, 100) simulated_input.add_spike_rate(500, 100) simulated_input.add_spike_rate(0.0, 100) simulated_input.create() node = Node("node", net) connect(simulated_input, node, 1.1) # run the network time_steps = 500 net.run(time_steps) net.disconnect() # plot results plotter = Plotter() plotter.add_input_plot(simulated_input) plotter.add_node_plot(node) plotter.plot()