def test_sendRevievedFrom(self): comm = InProcessCommunicationLayer() a1 = infrastructure.Agent('a1', comm) a2 = infrastructure.Agent('a2', comm) comm.register(a1.name, a1) comm.register(a2.name, a2) # use monkey patching on instance to set the _on_start method on a1 # simply send a message to a2 def a1_start(self): print('starting a1') print('sending msg to ') self.send_msg('a1', 'a2', 'msg') a1._on_start = types.MethodType(a1_start, a1) # Monkey patching a2 to check for message arrival a2.received_from = None def handle_message(self, sender, dest, msg): print('Receiving message from {} : {}'.format(sender, msg)) self.received_from = sender a2._handle_message = types.MethodType(handle_message, a2) # Running the test a1.start() a2.start() time.sleep(0.1) self.assertEqual(a2.received_from, 'a1') a1.stop() a2.stop()
def distribue_agent_for_all(variables, factors): comm = infrastructure.communication.InProcessCommunicationLayer() node_agents = [] for v in variables: f_for_variable = [f.name for f in factors if v.name in [i.name for i in f.dimensions]] a = infrastructure.Agent('Var_' + v.name, comm) a.add_computation(maxsum.VariableAlgo(v, f_for_variable)) node_agents.append(a) for f in factors: a = infrastructure.Agent('Fact_' + f.name, comm) a.add_computation(maxsum.FactorAlgo(f)) node_agents.append(a) return node_agents
def test_sendmsg_two_neighbors(self): comm = infrastructure.communication.InProcessCommunicationLayer() a1 = infrastructure.Agent('a1', comm) a2 = infrastructure.Agent('a2', comm) a3 = infrastructure.Agent('a3', comm) comm.register(a1.name, a1) comm.register(a2.name, a2) comm.register(a3.name, a3) # use monkey patching on instance to set the _on_start method on a1 # simply send a message to all neighbors def a1_start(self): print('starting a1') for n in ['a2', 'a3']: print('sending msg to ' + n) self.send_msg('a1', n, 'msg') a1._on_start = types.MethodType(a1_start, a1) # Monkey patching a2 & a3 to check for message arrival a2.received = False a3.received = False def handle_message(self, sender, dest, msg): print('Receiving message ' + msg) if msg == 'msg': self.received = True a2._handle_message = types.MethodType(handle_message, a2) a3._handle_message = types.MethodType(handle_message, a3) # Running the test a1.start() a2.start() a3.start() time.sleep(0.1) self.assertTrue(a2.received) self.assertTrue(a3.received) a1.stop() a2.stop() a3.stop()
def test_sendmsg_counts(self): comm = infrastructure.communication.InProcessCommunicationLayer() a1 = infrastructure.Agent('a1', comm) a2 = infrastructure.Agent('a2', comm) comm.register(a1.name, a1) comm.register(a2.name, a2) self.assertEqual(a2._num_received, 0) self.assertEqual(a1._num_sent, 0) a1.send_msg('a1', 'a2', 'pouet') self.assertEqual(a2._num_received, 1) self.assertEqual(a2._num_sent, 0) self.assertEqual(a1._num_sent, 1) self.assertEqual(a1._num_received, 0) received = a2.q.get_nowait() self.assertEqual(received, ('a1', 'a2', 'pouet'))
def maxsum_smartlights_multiplecomputationagent_costvariable(): """ This sample use variable with integrated cost. * 3 lights l1, l2 & l3 each light can have a luminosity level in the 0-9 range The energy cost is a linear function of the luminosity level, with l1 more efficient than l2 and l3 * one scene action y1, the room luminosity level y1 = (l1 + l2 + l3 )/3 y1 domain is also between 0-9 * one rule : l3 must be off AND y1 Must be 5 """ # building the Factor Graph # Lights : l1 = VariableWithCostFunc('l1', list(range(10)), lambda x: x * 0.5) l2 = VariableWithCostFunc('l2', list(range(10)), lambda x: x) l3 = VariableWithCostFunc('l3', list(range(10)), lambda x: x) # Scene action y1 = Variable('y1', list(range(10))) @relations.AsNAryFunctionRelation(l1, l2, l3, y1) def scene_rel(l1, l2, l3, y1): if y1 == round(l1 / 3.0 + l2 / 3.0 + l3 / 3.0): return 0 return INFNT # Rule @relations.AsNAryFunctionRelation(l3, y1) def rule_rel(l3, y1): """ This rule means : target luminosity if 5, and x3 is off. :param x3: :param y1: :return: """ return 10 * (abs(y1 - 5) + l3) # Create computation for factors and variables # Light 1 algo_l1 = amaxsum.VariableAlgo(l1, [scene_rel.name]) # Light 2 algo_l2 = amaxsum.VariableAlgo(l2, [scene_rel.name]) # Light 3 algo_l3 = amaxsum.VariableAlgo(l3, [scene_rel.name, rule_rel.name]) # Scene algo_y1 = amaxsum.VariableAlgo(y1, [rule_rel.name, scene_rel.name]) algo_scene = amaxsum.FactorAlgo(scene_rel) # Rule algo_rule = amaxsum.FactorAlgo(rule_rel) # Distribution of the computation on the three physical light-bulb nodes. # We have 9 computations to distribute on 3 agents, mapping the 3 light # bulbs. comm = infrastructure.communication.InProcessCommunicationLayer() a1 = infrastructure.Agent('Bulb1', comm) a1.add_computation(algo_l1) a1.add_computation(algo_scene) a1.add_computation(algo_y1) a2 = infrastructure.Agent('Bulb2', comm) a2.add_computation(algo_l2) a3 = infrastructure.Agent('Bulb3', comm) a3.add_computation(algo_l3) a3.add_computation(algo_rule) dcop_agents = [a1, a2, a3] results, _, _ = infrastructure.synchronous_single_run(dcop_agents, 5) print(results) if results == {'l1': 9, 'y1': 5, 'l3': 0, 'l2': 5}: logging.info('SUCCESS !! ') return 0 else: logging.info('invalid result found, needs some debugging ...' + str(results)) return 1