def test_message_factory_serialization(): MyMessage = message_type("my_msg", ["foo", "bar"]) msg = MyMessage(42, 21) r = simple_repr(msg) print(r) obtained = from_repr(r) assert msg == obtained
def test_message_factory_serialization(): MyMessage = message_type('my_msg', ['foo', 'bar']) msg = MyMessage(42, 21) r = simple_repr(msg) print(r) obtained = from_repr(r) assert msg == obtained
def test_message_factory_kwargs(): MyMessage = message_type("my_msg", ["foo", "bar"]) msg = MyMessage(bar=42, foo=21) assert msg.type == "my_msg" assert msg.foo == 21 assert msg.bar == 42 with pytest.raises(ValueError): msg = MyMessage(bar=42, pouet=21)
def test_message_factory(): MyMessage = message_type("my_msg", ["foo", "bar"]) msg = MyMessage(42, 21) assert msg.type == "my_msg" assert msg.foo == 42 assert msg.bar == 21 with pytest.raises(ValueError): MyMessage(1, 2, 3)
def test_message_factory_kwargs(): MyMessage = message_type('my_msg', ['foo', 'bar']) msg = MyMessage(bar=42, foo=21) assert msg.type == 'my_msg' assert msg.foo == 21 assert msg.bar == 42 with pytest.raises(ValueError): msg = MyMessage(bar=42, pouet=21)
def test_message_factory(): MyMessage = message_type('my_msg', ['foo', 'bar']) msg = MyMessage(42, 21) assert msg.type == 'my_msg' assert msg.foo == 42 assert msg.bar == 21 with pytest.raises(ValueError): MyMessage(1, 2, 3)
""" from typing import Any, Tuple from numpy import random from pydcop.algorithms import ComputationDef from pydcop.dcop.relations import assignment_cost from pydcop.infrastructure.computations import VariableComputation, \ message_type, register # Type of computations graph that must be used with dsa GRAPH_TYPE = 'constraints_hypergraph' DsaMessage = message_type("dsa_value", ["value"]) class DsaTutoComputation(VariableComputation): """ A very simple DSA implementation. Parameters ---------- variable: Variable an instance of Variable, whose this computation is responsible for constraints: an iterable of constraints objects The constraints the variables depends on computation_definition: ComputationDef the definition of the computation, given as a ComputationDef instance.
agt1 = Agent('agt1', InProcessCommunicationLayer()) agt1.discovery.use_directory('agt_dir', agt_dir.address) agt1.start() agt2 = Agent('agt2', InProcessCommunicationLayer()) agt2.discovery.use_directory('agt_dir', agt_dir.address) agt2.start() yield agt_dir, agt1, agt2 agt1.stop() agt2.stop() agt_dir.stop() PingMessage = message_type('ping', ['count']) class PingComputation(MessagePassingComputation): def __init__(self, name: str, target: str = None): super().__init__(name) self.target = target self.ping_count = 0 self._msg_handlers = {'ping': self._on_ping} def on_start(self): if self.target is not None: ping_msg = PingMessage(1) self.post_msg(self.target, ping_msg) def _on_ping(self, var_name, msg, t):
self.messaging.post_msg( ORCHESTRATOR_MGT, ORCHESTRATOR_MGT, Message(method, arg), msg_type=5) def _on_timeout(self): """Run timeout callback""" self.status = "TIMEOUT" self.logger.info("Timeout, requesting agents to stop") self.stop_agents(5) self.mgt.ready_to_run.set() ################################################################################ # Orchestration messages definition SetMetricsModeMessage = message_type('metrics_mode', ['mode', 'period']) DeployMessage = message_type('deploy', ['comp_def']) RunAgentMessage = message_type('run_computations', ['computations']) ReplicateComputationsMessage = message_type('replication', ['k']) ComputationReplicatedMessage = message_type('replicated', ['agent', 'replica_hosts', 'metrics']) PauseMessage = message_type('pause_computations', ['computations']) ResumeMessage = message_type('resume_computations', ['computations'])
register, message_type, ) GRAPH_TYPE = "ordered_graph" INFINITY = float("inf") # Some types definition for the content of messages VarName = str VarVal = Any Cost = float PathElement = Tuple[VarName, VarVal, Cost] Path = List[PathElement] SyncBBForwardMessage = message_type("forward", ["current_path", "ub"]) SyncBBBackwardMessage = message_type("backward", ["current_path", "ub"]) SyncBBTerminateMessage = message_type("terminate", ["current_path", "ub"]) class SyncBBComputation(VariableComputation): """ Computation for the SyncBB algorithm. """ def __init__(self, computation_definition: ComputationDef): super().__init__(computation_definition.node.variable, computation_definition) assert computation_definition.algo.algo == "syncbb" self.constraints = computation_definition.node.constraints
import pytest from pydcop.algorithms import ComputationDef, AlgorithmDef from pydcop.computations_graph.objects import ComputationNode from pydcop.infrastructure.computations import ( MessagePassingComputation, register, message_type, SynchronousComputationMixin, SynchronizationMsg, ComputationException, DcopComputation, ) FooMsg = message_type("FooMsg", ["data"]) class SynchC(SynchronousComputationMixin, MessagePassingComputation): def __init__(self, name, neighbors): super().__init__(name) self._neighbors = neighbors self._msg_sender = MagicMock() self.started = False @property def neighbors(self): return self._neighbors @register("FooMsg") def on_foo(self, sender, msg, t):
def computation_memory(*args): raise NotImplementedError( "DPOP has no computation memory implementation (yet)") def communication_load(*args): raise NotImplementedError( "DPOP has no communication_load implementation (yet)") def build_computation(comp_def: ComputationDef): return NcbbAlgo(comp_def) ValueMessage = message_type("value", ["value"]) CostMessage = message_type("cost", ["cost"]) SearchMessage = message_type("search", ["upper_bound"]) SearchValueMessage = message_type("search_value", ["value"]) SearchCostMessage = message_type("search_cost", ["lower_bound"]) StopMessage = message_type("stop", ["stop"]) PHASES = {"INIT", "SEARCH"} class NcbbAlgo(SynchronousComputationMixin, VariableComputation): """ Computation implementation for the NCBB algorithm. Parameters ----------