Wiki

Clone wiki

PyiB2c / Behavioral networks

Behavioral networks

Return to Home

Once we have created the behaviors we want to include in the network, we can defined the connections between them.

Create an instance of the object

The first thing to do is to create an instance of the object pyib2c.Network.NetworkBehaviorClass()

#!python

network_behavior = pyib2c.Network.NetworkBehaviorClass()
The class will look up the input and output information of all the behaviors listed in 'behaviors_dictionary.xml' and show the information in the command line.

Input and Output information

Similarly to the behaviors module, we have to defined the input and the output variables of the network. For a detailed description of these variables, refer to Creating a behavior from scratch.

Example (see example_2.py):

#!python

network_behavior.input_info = {'Size': np.array([np.zeros([1]),
                                                 pyib2c.Tools.Agents.PoseClasses.PoseV1Class(),
                                                 np.zeros([2]),
                                                 np.zeros([8])]),
                               'Type': np.array(['Time', 'Pose', 'Position2D', 'ProximitySensors']),
                               'Description': ['Simulation time [s]',
                                               'Pose of the agent',
                                               '2D position of the target',
                                               'Proximity measurements, 8 values']}

network_behavior.output_info = {'Size': np.array([np.zeros([2])]),
                                'Type': 'Velocity2D',
                                'Description': '2D Velocity Command'}

Specify the behaviors inside the network

The behaviors inside the network are specified in the dictionary variable behaviors_list['NameOfTheBehavior'], and the fusion modules in fusion_list['TypeOfFusion']. Note that there are three types of fusion modules: Max, Weighted and Weighted_max (refer to Introduction to iB2c behavioral networks)

Example (see example_2.py):

#!python

network_behavior.behaviors_list['HeadToPointBehavior'] = 1
network_behavior.behaviors_list['DecreaseVelocityBehavior'] = 1
network_behavior.behaviors_list['AvoidObstaclesBehavior'] = 1
network_behavior.fusion_list['Weighted'] = 1
In this example, 3 behaviors modules have been added (HeadToPointBehavior, DecreaseVelocityBehavior, AvoidObstaclesBehavior, one of each) and one Weighted fusion module. Note that the name of the modules must be the same that the one specify in the 'behaviors_dictionary.xml' file.

Generate network

Right after defining the modules inside the network, the following method must be executed:

#!python

network_behavior.generate()
The network class will assign an unique 'ID' to each module and show them in the console, for instance (see example_2.py):

Screenshot from 2018-02-07 11-27-38.png

Note that in input of the network is always assigned ID=1, and the output is always the last ID.

Create connections

Once we know which ID corresponds to which module, we can define the connections between them with the method add_connection(self, type, ID_out, ID_in=0, list_variables=-1):

  • type: - string - specifies which type of connection to be added:
    • I_O : from an output of a module to an input of another
    • Stimulation : from the activity port of a module to the stimulation port of another
    • Inhibition : from the activity port of a module to the inhibition port of another
    • Activity_Network : from the activity port of a module with the activity port of the whole network
    • Rating_Network : from the rating port of a module with the rating port of the whole network
  • ID_out: - int - indicated the ID of the initial point of the connection (where the information comes from)
  • ID_in: - int - indicates the ID of the final point of the connection (where the information goes to)
  • list_variables: - list[list] - indicates in which position the variable can be found, and where it must be placed in the final input, i.e.: [[i_1, j_1], [i_2, j_2]] is taking the variable number i_1 at the output of the initial module, and placing it at the position j_1 in the final behavior, and taking also the variable number i_2, and placing it in the position j_2. Not passing this variables (or passing it =-1) will imply that all the variables will be taken, and will be passed in the same order.

For example (see example_2.py):

#!python

network_behavior.add_connection('I_O', 1, 5, list_variables=[[1, 0], [2, 1]])
network_behavior.add_connection('I_O', 1, 4, list_variables=[[0, 0], [1, 1], [2, 2]])
network_behavior.add_connection('I_O', 5, 4, list_variables=[[0, 3]])
network_behavior.add_connection('I_O', 4, 2)

network_behavior.add_connection('I_O', 1, 3, list_variables=[[1, 0], [3, 1]])
network_behavior.add_connection('I_O', 3, 2)

network_behavior.add_connection('I_O', 2, 6)

network_behavior.add_connection('Stimulation', 1, 2)
network_behavior.add_connection('Inhibition', 1, 2)
network_behavior.add_connection('Inhibition', 3, 4)
network_behavior.add_connection('Inhibition', 1, 4)

network_behavior.add_connection('Activity_Network', 2)
network_behavior.add_connection('Rating_Network', 2)
We have connected the output of the ID=1 (which correspond to the input of the network) to the input port of the ID=5 (the DecreaseVelocityBehavior module). We take the variable located at position 1 (i.e. the pose of the agent, see the input definition variable of the network) and place it at position 0. We also take the variable at position 2 (the position of the target) and place it at position 1.

Note that the input of ID=4 (in this example it is HeadToPointBehavior) is made up by the variables 0, 1 and 2 from the ID=1 (input of the network), and the variable 0 from the ID=5 (DecreaseVelocityBehavior).

The ouput of the network is connected to the ID=2, i.e. to the fusion module. Note that in this connection we do not have to specify the variables and their order to be passed.

In the case of the Stimulation and the Inhibition, we can also specify which activity variable to pass, so that one module can synchronize different modules by stimulating/inhibiting them. Connecting more than one stimulation/inhibition port to the same activation is also possible. For instance, in the example we have connected to the inhibition port of ID=4 the activities from ID=3 and ID=1.

Connecting any module to a Fusion module implies that the activity and the rating are also passed as inputs.

Finally, for the Activity_Network and the Rating_Network we only specify the ID_in module, since the final module is the output of the network.

It is important to remark that if a behavior stimulation is not connected, the behavior is always stimulated. On the contrary, if the inhibition port is not connected, the behavior is never inhibited.

Generate network

The last step is to execute the method:

#!python

network_behavior.finish_network()

Assign values to a variable in a behavior

If we want to assign values to specific variables inside a behavior module, we can use the method in the class assign_value_to_parameter(self, ID, parameter_name, value):

  • ID: - int - ID of the module we are referring to
  • parameter_name: - str - name of the parameter to be set
  • value: - - - the value we want to assign the the variable

For example:

#!python

network_behavior.assign_value_to_parameter(4, 'v_n', 10)
Here we are assigning the value 10 to the variable named as v_n in the behavior module ID=4.

Execute network

To execute the network, we have first to construct the input variable, as defined in the input_info variable. Additionally, we can specify the stimulation and its inhibition. Similarly to the behavior module, if the stimulation is not specified, the network is always stimulated, whereas if the inhibition is not specified, the network is not inhibited. Then, we can call the method calc_outputs(self, inputs_network=np.zeros(0), stimulations_network=np.ones(1), inhibitions_network=np.zeros(1)).

For example (see example_2.py):

#!python

inputs_network = np.array([t, pose, target, sensors_state])
network_behavior.calc_outputs(inputs_network=inputs_network)
In this example, we have not specified the stimulations or the inhibitions.

Retrieve output, activity and rating

When the network has been executed, the output, activity and rating are retrieve with:

#!python

V_c = network_behavior.get_output()[0]
activity = network_behavior.get_activity()
rating = network_behavior.get_rating()
Return to Home

Updated