def create_agent(self, network): if network.check_consistency() is False: return self.INCONSISTENT if network.check_grounding() is False: return self.NOT_GROUNDED for module, super_module in network.super_modules.items(): if super_module in network.module_dictionary: network.unit_dic[super_module].add_submodule( module, network.unit_dic[module]) if app_logger.isEnabledFor(logging.DEBUG): app_logger.debug("Adding a module {} to {}.".format( module, super_module)) # Main logic top_module = brica1.Module() for unit_key in network.unit_dic.keys(): if unit_key not in network.super_modules: if isinstance(network.unit_dic[unit_key], brica1.Module): top_module.add_submodule(unit_key, network.unit_dic[unit_key]) if app_logger.isEnabledFor(logging.DEBUG): app_logger.debug( "Adding a module {} to a BriCA agent.".format( unit_key)) # agent = brica1.Agent(scheduler) agent = brica1.Agent() agent.add_submodule("__Runtime_Top_Module", top_module) self.unit_dic = network.unit_dic return agent
def test_lagging(self): s = brica1.RealTimeSyncScheduler() ca = brica1.Agent(s) c = TimeReporterComponent() CompSet = brica1.ComponentSet() CompSet.add_component("C", c, 0) ModA = brica1.Module() ModA.add_component("CompSet", CompSet) ca.add_submodule("ModA", ModA) s.set_interval(0.1) c.delay = 0 ca.step() scheduled_time = s.current_time + s.interval ca.step() self.assertGreater(s.current_time, scheduled_time) self.assertLessEqual(abs(s.current_time - scheduled_time), TOLERANCE) self.assertFalse(s.lagged) c_interval = 0.2 c.delay = c_interval ca.step() self.assertTrue(s.lagged)
def create_agent(self, scheduler): """ Add top level modules to a BriCA agent (to be called after load_file). Args: A brica1.Scheduler object Returns: A BriCA agent """ # Checking grounding sub_modules = self.__sub_modules() for unit_key in self.__unit_dic: if not unit_key in sub_modules: try: module = self.__unit_dic[unit_key] component = module.get_component(unit_key) # TODO: Setting aliases here according to the current spec. for port in module.in_ports: length = module.get_in_port(port).buffer.shape[0] component.make_in_port(port, length) component.alias_in_port(module, port, port) for port in module.out_ports: length = module.get_out_port(port).buffer.shape[0] component.make_out_port(port, length) component.alias_out_port(module, port, port) except KeyError: print >> sys.stderr, "Module " + unit_key + " at the bottom not grounded as a Component!" return # Checking undefined modules for unit_key in self.__super_modules: if not unit_key in self.__unit_dic: print >> sys.stderr, "SuperModule " + unit_key + " not defined!" for unit_key in sub_modules: if not unit_key in self.__unit_dic: print >> sys.stderr, "SuperModule " + unit_key + " not defined!" # Main logic top_module = brica1.Module() for unit_key in self.__unit_dic.keys(): if not unit_key in self.__super_modules: if isinstance(self.__unit_dic[unit_key], brica1.Module): top_module.add_submodule(unit_key, self.__unit_dic[unit_key]) print "Adding a module " + unit_key + " to a BriCA agent." agent = brica1.Agent(scheduler) agent.add_submodule("__Runtime_Top_Module", top_module) return agent
def _(bm): s = brica1.VirtualTimeSyncScheduler(1.0) agent = brica1.Agent(s) compA = brica1.ConstantComponent() compB = brica1.NullComponent() mod = brica1.Module(); mod.add_component('compA', compA) mod.add_component('compB', compB) compA.make_out_port('out', 256*256*3) compB.make_in_port('in', 256*256*3) brica1.connect((compA, 'out'), (compB, 'in')) agent.add_submodule('mod', mod) for _ in bm: agent.step()
def relayer(): Comp = brica1.PipeComponent() Comp.make_in_port("in", 3) Comp.make_out_port("out", 3) Comp.set_map("in", "out") Mod = brica1.Module() Mod.make_in_port("in", 3) Mod.make_out_port("out", 3) Mod.add_component("Comp", Comp) Scheduler = brica1.RealTimeSyncScheduler() Agent = brica1.Agent(Scheduler) Agent.make_in_port("in", 3) Agent.make_out_port("out", 3) Agent.add_submodule("Mod", Mod) brica1.alias_in_port((Agent, "in"), (Mod, "in")) brica1.alias_out_port((Agent, "out"), (Mod, "out")) brica1.alias_in_port((Mod, "in"), (Comp, "in")) brica1.alias_out_port((Mod, "out"), (Comp, "out")) def converter(msg): return np.array(list(msg.data), dtype=np.int16) Adapter = brica1.ros.ROSAdapter("relayer") Adapter.setup_subscriber("chatter0", Int16MultiArray, "out", 3, converter) Adapter.setup_publisher("chatter1", "in", 3) brica1.connect((Adapter, "out"), (Agent, "in")) brica1.connect((Agent, "out"), (Adapter, "in")) while not rospy.is_shutdown(): Agent.step() print "Agent in port: {}".format(Agent.get_in_port("in").buffer) print "Agent out port: {}".format(Agent.get_out_port("out").buffer)
mnist = data.load_mnist_data() mnist['data'] = mnist['data'].astype(np.float32) mnist['data'] /= 255 mnist['target'] = mnist['target'].astype(np.int32) N_train = 60000 x_train, x_test = np.split(mnist['data'], [N_train]) y_train, y_test = np.split(mnist['target'], [N_train]) N_test = y_test.size f = open('sda.pkl', 'rb') stacked_autoencoder = pickle.load(f) f.close() scheduler = brica1.VirtualTimeSyncScheduler() agent = brica1.Agent(scheduler) module = brica1.Module() module.add_component("stacked_autoencoder", stacked_autoencoder) agent.add_submodule("module", module) time = 0.0 sum_loss1 = 0 sum_loss2 = 0 sum_loss3 = 0 sum_loss4 = 0 sum_accuracy = 0 for batchnum in xrange(0, N_test, batchsize): x_batch = x_test[batchnum:batchnum + batchsize] y_batch = y_test[batchnum:batchnum + batchsize]
# Connect the components brica1.connect((feeder, "out0"), (svm, "in0")) brica1.connect((feeder, "out0"), (RFC, "in0")) brica1.connect((svm, "out0"), (SR, "in0")) brica1.connect((RFC, "out0"), (SR, "in1")) # Add components to module mod = brica1.Module() mod.add_component("feeder", feeder) mod.add_component("svm", svm) mod.add_component("RFC", RFC) mod.add_component("SR", SR) # Setup scheduler and agent s = brica1.VirtualTimeSyncScheduler() a = brica1.Agent(s) a.add_submodule("mod", mod) # Test the classifier svm_result = [] RFC_result = [] svm_vs_RFC = [] for i in xrange(len(X)): feeder.set_state("out0", X[i]) # Set data feeder to training data i a.step() # Execute prediction svm_result.append(svm.get_out_port("out0").buffer[0]) RFC_result.append(RFC.get_out_port("out0").buffer[0])