Skip to content

Commit

Permalink
feat(MET4FoF redundancy): tutorial 7 added
Browse files Browse the repository at this point in the history
  • Loading branch information
anupam-prasad committed Mar 15, 2021
1 parent 23d4a91 commit caa62ca
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
5 changes: 2 additions & 3 deletions agentMET4FOF_redundancy/redundancyAgents1.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def agent_loop(self):
streams content and push it via invoking :py:func:`AgentMET4FOF.send_output`.
"""
if self.current_state == "Running":
self.set_output_data(channel="default", data=self._data_stream._next_sample_generator())
self.set_output_data(channel="default", data=self._data_stream._next_sample_generator(batch_size=10))
super().agent_loop()

@property
Expand Down Expand Up @@ -185,7 +185,7 @@ def agent_loop(self):
print('Not enough data for redundancy agent evaluation.')
return 0

buff = self.buffer.popleft(self.n_pr) # take n_pr entries out from the buffer
buff = self.buffer.popleft(self.n_pr) # take n_pr entries out from the buffer

t_data_arr2d = np.full(shape=(self.n_pr, n_sensors), fill_value=np.nan)
ut_data_arr2d = np.full(shape=(self.n_pr, n_sensors), fill_value=np.nan)
Expand Down Expand Up @@ -246,7 +246,6 @@ def agent_loop(self):
data = np.array([t_data_arr2d[-1, 0], ut_data_arr2d[-1, 0], ybest[0], uybest[0]])

# Send the data
# data = np.array([1,2,3,4])
if len(data.shape) == 1:
data = data.reshape((1, len(data)))

Expand Down
72 changes: 72 additions & 0 deletions agentMET4FOF_tutorials/tutorial_7_redundancyAgents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Example 1 of using a Redundancy Agent.
Four signals are generated and data is supplied to the Redundancy Agent.
The Redundancy Agent calculates the best consistent estimate taking into account the supplied uncertainties.
"""

import numpy as np
from agentMET4FOF.agents import AgentNetwork
from agentMET4FOF.metrological_agents import MetrologicalMonitorAgent

from agentMET4FOF.metrological_streams import MetrologicalMultiWaveGenerator
from agentMET4FOF_redundancy.redundancyAgents1 import MetrologicalMultiWaveGeneratorAgent, RedundancyAgent


def demonstrate_redundancy_agent_four_signals():
"""
At the start of the main module all important parameters are defined. Then the agents are defined and the network
is started. The network and the calculated results can be monitored in a browser at the address http://127.0.0.1:8050/.
"""
# parameters
batch_size = 10
n_pr = batch_size
fsam = 100
intercept = 10
freqs = [6, 10, 8, 12]
phases = [1, 2, 3, 4]
ampls = [0.3, 0.2, 0.5, 0.4]
exp_unc_abs = 0.2
problim = 0.95

# start agent network server
agent_network = AgentNetwork(dashboard_modules=True)

# Initialize signal generating class outside of agent framework.
signal_arr = [MetrologicalMultiWaveGenerator(sfreq=fsam, freq_arr=np.array([freq]), intercept=intercept,
ampl_arr=np.array([ampl]), phase_ini_arr=np.array([phi]),
value_unc=exp_unc_abs) for freq, phi, ampl in zip(freqs, phases, ampls)]

# Data source agents.
source_agents = []
sensor_key_list = []
for count, signal in enumerate(signal_arr):
sensor_key_list += ["Sensor" + str(count+1)]
source_agents += [agent_network.add_agent(name=sensor_key_list[count], agentType=MetrologicalMultiWaveGeneratorAgent)]
source_agents[count].init_parameters(signal=signal, batch_size=batch_size)


# Redundant data processing agent
redundancy_name1 = "RedundancyAgent1" # Name cannot contain spaces!!
redundancy_agent1 = agent_network.add_agent(name=redundancy_name1, agentType=RedundancyAgent)
redundancy_agent1.init_parameters1(sensor_key_list=sensor_key_list, calc_type="lcs", n_pr=n_pr, problim=problim)

# Initialize metrologically enabled plotting agent.
monitor_agent1 = agent_network.add_agent(name="MonitorAgent_SensorValues", agentType=MetrologicalMonitorAgent) # Name cannot contain spaces!!
monitor_agent2 = agent_network.add_agent(name="MonitorAgent_RedundantEstimate", agentType=MetrologicalMonitorAgent)

# Bind agents.
for source_agent in source_agents:
source_agent.bind_output(monitor_agent1)
source_agent.bind_output(redundancy_agent1)

redundancy_agent1.bind_output(monitor_agent2)

# Set all agents states to "Running".
agent_network.set_running_state()

# Allow for shutting down the network after execution.
return agent_network


if __name__ == "__main__":
demonstrate_redundancy_agent_four_signals()

0 comments on commit caa62ca

Please sign in to comment.