Run Multi Agent System

We initialize the data source and create the agent network, once the agents are fully up and running, run the dashboard code in a separate terminal to visualize the agents.

[1]:
import osbrain
from osbrain.agent import run_agent
from osbrain.agent import Agent

import pandas as pd
from datetime import datetime

import time

import pickle
import numpy as np
import random
from copy import copy

from .Agent_models.agents import Sensor, Aggregator, Predictor, DecisionMaker, SensorNetwork

# TYPES OF AGENT
# 0 - SENSOR NETWORK
# 1 - SENSOR
# 2 - AGGREGATOR
# 3 - PREDICTOR
# 4 - DECISIONMAKER
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-6053d4936c6f> in <module>
     13 from copy import copy
     14
---> 15 from .Agent_models.agents import Sensor, Aggregator, Predictor, DecisionMaker, SensorNetwork
     16
     17 # TYPES OF AGENT

ModuleNotFoundError: No module named '__main__.Agent_models'; '__main__' is not a package
[2]:
DemoMode= True
pickle_path = "pickles/"
data_input = pickle.load(open(pickle_path + "data_input_data_1Hz_full.p", "rb"))
data_output = pickle.load(open(pickle_path + "zema_outputs.p", "rb"))

X_data = data_input
Y_data = data_output
randomShuffling = True

if (randomShuffling == True):
    index_list = np.arange(X_data.shape[0])
    random.shuffle(index_list)
    Y_data = Y_data[index_list, :]
    X_data = X_data[index_list, :, :]
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-8d9935992e57> in <module>
      1 DemoMode= True
      2 pickle_path = "pickles/"
----> 3 data_input = pickle.load(open(pickle_path + "data_input_data_1Hz_full.p", "rb"))
      4 data_output = pickle.load(open(pickle_path + "zema_outputs.p", "rb"))
      5

FileNotFoundError: [Errno 2] No such file or directory: 'pickles/data_input_data_1Hz_full.p'

Starting server

[3]:

ns = osbrain.nameserver.run_nameserver(addr='127.0.0.1:14065')
Broadcast server running on 0.0.0.0:9091
NS running on 127.0.0.1:14065 (127.0.0.1)
URI = PYRO:Pyro.NameServer@127.0.0.1:14065

Creating Agent

We firstly create a SensorNetwork Agent which enable wrapper functions and manages agents

[4]:
sensor_network = run_agent('sensor_network', base=SensorNetwork)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-75226932f9c3> in <module>
----> 1 sensor_network = run_agent('sensor_network', base=SensorNetwork)

NameError: name 'SensorNetwork' is not defined

Sensor Agents

  1. Next, we create a Sensor Agent by sensor_network.addsimsensor(type,unit), and store into a list sensors.
  2. We set the data source of the Sensor Agent : sensor_new.set_generatorDataSet(dataSet) where dataSet is a 3-dimensional numpy array with: [Row x Sequence Length x Sensor]
[5]:
#add sensors
sensors=[]
sensorTypes = ["Temperature","Temperature","Temperature","Temperature","Vibration","EfficiencyFactor","VolumeFlow","VolumeFlow","Pressure","Pressure","Pressure","Pressure","Pressure","Pressure","MotorPower","CoolingEfficiency","CoolingPower"]
sensorUnits = ["C","C","C","C","mm/s","%","l/min","l/min","bar","bar","bar","bar","bar","bar","W","%","kW"]

for sensor_num in range(X_data.shape[2]):
    sensor_new = sensor_network.add_simsensor(type=sensorTypes[sensor_num], unit_v=sensorUnits[sensor_num])
    sensor_new.set_generatorDataSet(X_data[:,:,sensor_num])
    sensors.append(sensor_new)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-ada498f69a90> in <module>
      4 sensorUnits = ["C","C","C","C","mm/s","%","l/min","l/min","bar","bar","bar","bar","bar","bar","W","%","kW"]
      5
----> 6 for sensor_num in range(X_data.shape[2]):
      7     sensor_new = sensor_network.add_simsensor(type=sensorTypes[sensor_num], unit_v=sensorUnits[sensor_num])
      8     sensor_new.set_generatorDataSet(X_data[:,:,sensor_num])

NameError: name 'X_data' is not defined
  1. We can access the Sensor Agents stored in array sensors .
  2. Alternatively, the SensorNetwork Agent automatically keeps track of sensors added by it, we can access the list by calling get_attr('sensor_list')
  3. Here, we demonstrate a function of Sensor Agent which is read_generator which returns a random data row from the loaded dataset
[6]:
#the sensors are loaded into array sensors
sensor1 = sensors[0]
print(len(sensors))

#access sensors by either way
sensor_network.get_attr('sensor_list')[0].read_generator()
sensor1.read_generator()
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-fe35f12d5c0e> in <module>
      1 #the sensors are loaded into array sensors
----> 2 sensor1 = sensors[0]
      3 print(len(sensors))
      4
      5 #access sensors by either way

IndexError: list index out of range

Aggregator Agents

  1. We add an Aggregator Agent to the sensor_network by calling the function .add_aggregator(sensorList) where sensorList is an optional list of Sensor Agents which automatically binds the aggregator to the Sensor Agents.
  2. Aggregator Agent can bind to Sensor Agent in runtime by calling .bind_sensors(sensorList).
[7]:
#add aggregators and bind them to sensors
aggregator1 = sensor_network.add_aggregator(sensors)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-f1ff1ca81bf3> in <module>
      1 #add aggregators and bind them to sensors
----> 2 aggregator1 = sensor_network.add_aggregator(sensors)

NameError: name 'sensor_network' is not defined

Predictor Agents

  1. Similarly, we can add Predictor Agent by .add_predictor(aggregator) with the optional aggregator to be binded to.
  2. For each Predictor Agent, we load the prediction model by .load_predictor_model(model) where model is a trained ML_Wrapper with signature such as .predict_model_wUnc(x_test,num_samples) where x_test is the data input and num_samples is the number of samples for Monte Carlo sampling.
  3. Here, we load the previously pickled prediction model.
[8]:
#add predictor and bind to aggregator
predictor1 = sensor_network.add_predictor(aggregator=aggregator1)
predictor2 = sensor_network.add_predictor(aggregator=aggregator1)
predictor3 = sensor_network.add_predictor(aggregator=aggregator1)
predictor4 = sensor_network.add_predictor(aggregator=aggregator1)
predictor5 = sensor_network.add_predictor(aggregator=aggregator1)

#load predictor models
predictor1.load_predictor_model(pickle.load(open("pickles/" + "bnn_wrapper_0.p", "rb")))
predictor2.load_predictor_model(pickle.load(open("pickles/" + "bnn_wrapper_1.p", "rb")))
predictor3.load_predictor_model(pickle.load(open("pickles/" + "bnn_wrapper_2.p", "rb")))
predictor4.load_predictor_model(pickle.load(open("pickles/" + "bnn_wrapper_3.p", "rb")))
predictor5.load_predictor_model(pickle.load(open("pickles/" + "bnn_wrapper_4.p", "rb")))

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-cfeb5649323c> in <module>
      1 #add predictor and bind to aggregator
----> 2 predictor1 = sensor_network.add_predictor(aggregator=aggregator1)
      3 predictor2 = sensor_network.add_predictor(aggregator=aggregator1)
      4 predictor3 = sensor_network.add_predictor(aggregator=aggregator1)
      5 predictor4 = sensor_network.add_predictor(aggregator=aggregator1)

NameError: name 'sensor_network' is not defined

DecisionMaker Agent

  1. We add Decision Maker Agent calling .add_decisionMaker() on SensorNetwork agent
  2. The DM Agent is binded to every predictor by calling .bind_predictor(predictor) function
[9]:
decisionMaker = sensor_network.add_decisionMaker()
decisionMaker.bind_predictor(predictor1)
decisionMaker.bind_predictor(predictor2)
decisionMaker.bind_predictor(predictor3)
decisionMaker.bind_predictor(predictor4)
decisionMaker.bind_predictor(predictor5)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-180d2cc1fe2f> in <module>
----> 1 decisionMaker = sensor_network.add_decisionMaker()
      2 decisionMaker.bind_predictor(predictor1)
      3 decisionMaker.bind_predictor(predictor2)
      4 decisionMaker.bind_predictor(predictor3)
      5 decisionMaker.bind_predictor(predictor4)

NameError: name 'sensor_network' is not defined

Demo

  1. For demo, we run an infinite loop which continuously runs the .request_sensors_data()
  2. Due to the bindings, the requested data will immediately be propagated to all binded Predictor Agents and to Decision Maker Agent
  3. While this is running, run the dashboard code in a separate terminal to visualize the multi-agent testbed
[10]:
#send request to aggregator agents for data from sensors

if DemoMode:
    for i in range(99999999999):
        aggregator1.request_sensors_data()
        time.sleep(3)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-c6d4a27820dd> in <module>
      3 if DemoMode:
      4     for i in range(99999999999):
----> 5         aggregator1.request_sensors_data()
      6         time.sleep(3)

NameError: name 'aggregator1' is not defined
[ ]: