def test_go_accept_with_postponed_value_message(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable("x2", list(range(2)))
        x3 = Variable("x3", list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi, psi]),
                AlgorithmDef.build_with_default_param("mgm2"),
            ))
        computation.message_sender = DummySender()

        computation._neighbors_values = {"x2": 1, "x3": 1}
        computation.__value__ = 1
        computation.__cost__ = 9
        computation._state = "go?"
        computation._postponed_msg["value"] = [("x2", Mgm2ValueMessage(1), 1)]
        # from Response message or accepted offer
        computation._potential_gain = 10
        computation._potential_value = 0

        computation.on_go_msg("x3", Mgm2GoMessage(True), 1)
        # Common tests
        self.assertEqual(computation._state, "value")
        self.assertEqual(computation._potential_gain, 0)
        self.assertIsNone(computation._potential_value)
        self.assertEqual(computation._neighbors_values, {"x2": 1})
        self.assertEqual(computation._neighbors_gains, dict())
        self.assertEqual(computation._offers, [])
        self.assertIsNone(computation._partner)
        self.assertEqual(computation.__nb_received_offers__, 0)
        self.assertFalse(computation._committed)
        self.assertFalse(computation._is_offerer)
        self.assertFalse(computation._can_move)

        # If cannot move
        self.assertEqual(computation.current_value, 1)
        # If can move
        computation._can_move = True
        computation._state = "go?"
        computation._potential_value = 0
        computation.on_go_msg("x3", Mgm2GoMessage(True), 1)
        self.assertEqual(computation.current_value, 0)
    def test_go_accept_with_postponed_value_message(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable('x2', list(range(2)))
        x3 = Variable('x3', list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(x1, [phi, psi],
                                      msg_sender=DummySender(),
                                      comp_def=MagicMock())
        computation._neighbors_values = {'x2': 1, 'x3': 1}
        computation.__value__ = 1
        computation.__cost__ = 9
        computation._state = 'go?'
        computation._postponed_msg['value'] = [('x2', Mgm2ValueMessage(1))]
        # from Response message or accepted offer
        computation._potential_gain = 10
        computation._potential_value = 0

        computation._handle_go_message('x3', Mgm2GoMessage(True))
        # Common tests
        self.assertEqual(computation._state, 'value')
        self.assertEqual(computation._potential_gain, 0)
        self.assertIsNone(computation._potential_value)
        self.assertEqual(computation._neighbors_values, {'x2': 1})
        self.assertEqual(computation._neighbors_gains, dict())
        self.assertEqual(computation._offers, [])
        self.assertIsNone(computation._partner)
        self.assertEqual(computation.__nb_received_offers__, 0)
        self.assertFalse(computation._committed)
        self.assertFalse(computation._is_offerer)
        self.assertFalse(computation._can_move)

        #If cannot move
        self.assertEqual(computation.current_value, 1)
        #If can move
        computation._can_move = True
        computation._state = 'go?'
        computation._potential_value = 0
        computation._handle_go_message('x3', Mgm2GoMessage(True))
        self.assertEqual(computation.current_value, 0)
    def test_go_reject_no_postponed_value_message(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable("x2", list(range(2)))
        x3 = Variable("x3", list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi, psi]),
                AlgorithmDef.build_with_default_param("mgm2"),
            ))
        computation.message_sender = DummySender()

        computation._neighbors_values = {"x2": 1, "x3": 1}
        computation.__value__ = 1
        computation._potential_value = 0
        computation._state = "go?"
        # from Response message or accepted offer

        computation._handle_go_message("x3", Mgm2GoMessage(False))

        self.assertEqual(computation._state, "value")
        self.assertEqual(computation.__value__, 1)
        self.test_clear_agent()
    def test_go_reject_no_postponed_value_message(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable('x2', list(range(2)))
        x3 = Variable('x3', list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(x1, [phi, psi],
                                      msg_sender=DummySender(),
                                      comp_def=MagicMock())
        computation._neighbors_values = {'x2': 1, 'x3': 1}
        computation.__value__ = 1
        computation._potential_value = 0
        computation._state = 'go?'
        # from Response message or accepted offer

        computation._handle_go_message('x3', Mgm2GoMessage(False))

        self.assertEqual(computation._state, 'value')
        self.assertEqual(computation.__value__, 1)
        self.test_clear_agent()
    def test_go_accept_no_postponed_value_message(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable("x2", list(range(2)))
        x3 = Variable("x3", list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi, psi]),
                AlgorithmDef.build_with_default_param("mgm2"),
            ))
        computation.message_sender = DummySender()
        computation._neighbors_values = {"x2": 1, "x3": 1}
        computation.__value__ = 1
        computation.__cost__ = 9
        computation._state = "go?"
        # from Response message or accepted offer
        computation._potential_gain = 10
        computation._potential_value = 0
        # Common behavior: clear agent view
        computation.on_go_msg("x3", Mgm2GoMessage(True), 1)
        self.assertEqual(computation._state, "value")
        self.test_clear_agent()

        # If cannot move
        self.assertEqual(computation.current_value, 1)
        # If can move
        computation._state = "go?"
        computation._can_move = True
        computation._potential_value = 0
        computation.on_go_msg("x3", Mgm2GoMessage(True), 1)
        self.assertEqual(computation.current_value, 0)
    def test_go_accept_no_postponed_value_message(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable('x2', list(range(2)))
        x3 = Variable('x3', list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(x1, [phi, psi],
                                      msg_sender=DummySender(),
                                      comp_def=MagicMock())
        computation._neighbors_values = {'x2': 1, 'x3': 1}
        computation.__value__ = 1
        computation.__cost__ = 9
        computation._state = 'go?'
        # from Response message or accepted offer
        computation._potential_gain = 10
        computation._potential_value = 0
        # Common behavior: clear agent view
        computation._handle_go_message('x3', Mgm2GoMessage(True))
        self.assertEqual(computation._state, 'value')
        self.test_clear_agent()

        # If cannot move
        self.assertEqual(computation.current_value, 1)
        # If can move
        computation._state = 'go?'
        computation._can_move = True
        computation._potential_value = 0
        computation._handle_go_message('x3', Mgm2GoMessage(True))
        self.assertEqual(computation.current_value, 0)