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 connect_in(self, out_group): cos_weight = 0.6 connect(out_group["software.look_at_object.done"], self.input_from_yarp["state_machine.look_at_object.done"], cos_weight) connect(out_group["object_recognition.recognize_object.known"], self.input_from_loihi["state_machine.recognize_object.known"], cos_weight) connect( out_group["object_recognition.recognize_object.unknown"], self.input_from_loihi["state_machine.recognize_object.unknown"], cos_weight) connect(out_group["software.learn_new_object.done"], self.input_from_yarp["state_machine.learn_new_object.done"], cos_weight) connect(out_group["software.query_memory.done"], self.input_from_yarp["state_machine.query_memory.done"], cos_weight)
def add_subbehaviors(self, sub_behaviors, logical_or=False): """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: sub_behaviors --- A list of subbehaviors. By default, all their CoS signals have to be achieved in order to trigger this behavior's CoS. logical_or --- By setting this flag to true, this behavior requires only one of the subbehaviors' CoS to finish. """ for sub_behavior in sub_behaviors: connect(self.node_intention, sub_behavior.node_prior_intention, 1.1) weight = -1.1 if logical_or: weight = weight / len(sub_behaviors) connect(sub_behavior.node_prior_intention, self.single_node_cos, weight) for sub_cos_memory in sub_behavior.nodes_cos_memory.values(): connect(self.node_intention, sub_cos_memory, 0.8) connect(sub_cos_memory, self.single_node_cos, 1.2)
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()
gauss_input2.create() kernel = SelectiveKernel(amp_exc=0.495, width_exc=2.5, global_inh=0.2, border_type="inhibition") field = Field("field", net, domain=field_domain, shape=field_shape, kernel=kernel, tau_voltage=2, tau_current=10, delay=3) connect(gauss_input1, field, 0.1, mask="one-to-one") connect(gauss_input2, field, 0.1, mask="one-to-one") # run the network time_steps = 500 net.run(time_steps) net.disconnect() # plot results plotter = Plotter() plotter.add_input_plot(gauss_input1) plotter.add_input_plot(gauss_input2) plotter.add_field_plot(field) plotter.plot()
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)
def add_boost(self, boost_input): """Adds an artificial boost input to the behavior, which may activate it; relevant for top-level behaviors.""" connect(boost_input, self.node_prior_intention, 1.1) for cos_memory in self.nodes_cos_memory.values(): connect(boost_input, cos_memory, 0.8)
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() kernel = MultiPeakKernel(amp_exc=0.47, width_exc=2.5, amp_inh=-0.35,
neurons_per_node = 1 simulated_input = HomogeneousPiecewiseStaticInput("input", net, shape=neurons_per_node) simulated_input.add_spike_rate(0, 100) simulated_input.add_spike_rate(3000, 100) simulated_input.add_spike_rate(8000, 100) simulated_input.add_spike_rate(3000, 100) simulated_input.add_spike_rate(0, 100) simulated_input.create() node = Node("node", net, number_of_neurons=neurons_per_node, tau_voltage=2, tau_current=10, self_excitation=0.08) connect(simulated_input, node, 0.1, mask="one-to-one") # run the network time_steps = 500 net.run(time_steps) net.disconnect() # plot results plotter = Plotter() plotter.add_input_plot(simulated_input) plotter.add_field_plot(node) plotter.plot()