Example #1
0
 def test_is_satisfiable_bigger_problem(self):
     actors_400 = [[1,2] for i in range(200)]
     actors_400.extend([[-1,-2,-3] for i in range(200)])
     result = do_solve(
         variables=actors_400,
         reference_value=0,
         csp_solver_config=csp_solver_config
     )
     assert result['satisfiable_bool'] == True
     assert result['solution_list'] == [-1, -1, -1, -1, -3, -1, -1, -3, -1,
        -3, -1, -1, -3, -1, -1, -3, -1, 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 1, 2, 2,
        1, 2, 1, 2, 2, 1, 2, 2, -3, -1, -1, -3, -1, -1, -1, -1, -3, -1, -3, -1,
        -3, -1, -1, -3, -1, -3, -1, -1, -1, -3, -1, -1, -3, -1, -1, -3, -3, -1,
        -3, -3, 2, 1, 2, 1, 1, 2, 1, 2, 2, 2, 2, -1, -1, 2, 2, 2, 2, -1, 2, 2,
        -1, -1, 1, 2, 2, 2, 2, 1, -1, -1, -1, -1, -3, -1, -1, -1, -1, 2, 2, 2,
        1, 1, 2, 2, 2, 2, 1, -1, -3, -3, -1, -1, -1, -1, -1, -3, -1, -1, -3,
        -1, -1, -3, -1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 1, 1, 2, -3, -1,
        -1, -1, -1, -1, -1, -3, -3, -1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, -3,
        -1, -1, -3, -1, -3, -3, -1, -3, -1, -1, -1, -1, -3, 1, 2, 2, 2, 2, 2,
        2, 1, 2, 1, -1, -3, -3, -1, -1, -1, -1, -3, -1, -1, 1, 2, 2, 2, 2, 1,
        1, 2, -1, -1, -1, -3, -1, -3, 2, 1, -3, -1, 1, 2, 2, 2, 2, 1, 2, 2, 1,
        2, -3, -3, -1, -1, -1, -1, 1, 2, 2, 2, 1, 2, 2, 1, 2, 1, 2, 2, 2, 1,
        1, 2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 2, 2, 2, 1, -1, -1, 2, -3, 2, 1,
        1, 2, -1, -3, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, -1, -1, -3, -1, -1,
        -1, -1, -3, -1, -3, -3, -1, -3, -1, -1, -1, 2, 2, 1, 2, 2, 1, 2, 2, 1,
        2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 1, 2, -1, -3, -3, -1, -1, -1, -1, -3,
        -3, -1, -3, -3, -1, -1, -1, -3, -1, -1, -3, -1, -1, -1, -3, -3, -1,
        -3, -1, -1, -1, -1, -1, -3, -1, -1, -3, 1, -3, -1, -1, -1, -1, 2, -3,
        2, 2, 1, 2, 2, 1, 2, 2, -1, 1, -1, -1, -1, -3, -1, -3, -3, -1, 1, 2,
        2, 1, 2, 2, 1, 2, 2, 1, -1, -3], result['solution_list']
Example #2
0
    def set_value(self, new_value):
        set_value = self.validate(new_value)

        all_actor_ranges = list(
            self._parallel_apply_for_actors_n_wait(get_actor_vr))

        csp_result = csp_solver.do_solve(
            variables=all_actor_ranges,
            reference_value=set_value,
            csp_solver_config=self._csp_solver_config)

        if ('satisfiable_bool' in csp_result
                and csp_result['satisfiable_bool'] == True):

            # Using a subprocess will pickle the used objects
            # --> the "set" values are lost.
            # RemoteActor manipulates outside of
            # context actors --> ok.
            if self.actors_cls in [ControllerActor, RemoteActor]:
                iterable = [
                    (self._actors[index], assigned_value)
                    for index, assigned_value \
                        in enumerate(csp_result['solution_list'])
                ]
                _query_results = self._parallel_apply_for_actors_n_wait(
                    set_actor_value, iterable)
            else:
                for index, assigned_value in enumerate(
                        csp_result['solution_list']):
                    set_actor_value((self._actors[index], assigned_value))

            return set_value
        else:
            raise NotSolvable('{0} is not an satisfiable'.format(set_value))
Example #3
0
 def test_is_satisfiable2(self):
     result = do_solve(
         variables=[[1,2], [1,2]],
         reference_value=4,
         csp_solver_config=csp_solver_config
     )
     assert result['satisfiable_bool'] == True
     assert result['solution_list'] == [2, 2]
Example #4
0
 def test_is_satisfiable1(self):
     result = do_solve(
         variables=[[1,2]],
         reference_value=1,
         csp_solver_config=csp_solver_config
     )
     assert ('satisfiable_bool' in result and
             result['satisfiable_bool'] == True), result
     assert result['solution_list'] == [1], result
Example #5
0
 def test_is_satisfiable3(self):
     result = do_solve(
         variables=[
             [-1, 0, 1], [-1,-2,-3], [-1, 0, 1], [-1,-2,-3]
         ],
         reference_value=0,
         csp_solver_config=csp_solver_config
     )
     assert result['satisfiable_bool'] == True
     assert result['solution_list'] == [1, -1, 1, -1]
Example #6
0
 def test_not_satisfiable(self):
     result = do_solve(
         variables=[[1,2], [1,2]],
         reference_value=5,
         csp_solver_config=csp_solver_config
     )
     assert result['satisfiable_bool'] == False, \
         result['satisfiable_bool']
     # is not a minisat result, sugar returns that!
     assert result['solution_list'] == None, result['solution_list']
Example #7
0
def test_minisat_is_deterministic():
    for _ in range(20):
        result = do_solve(
            variables=[
                [-1, 0, 1], [-1, 0, 1], [-1, 0, 1], [-1, 0, 1],
            ],
            reference_value=0,
            quiet=False,
            csp_solver_config=csp_solver_config
        )

        # possible solutions:
        #[0,0,0,0], [-1,-1,1,1]...
        assert result['satisfiable_bool'] == True
        assert result['solution_list'] == [0, -1, 0, 1], \
            result['solution_list']
Example #8
0
    def get_value_range(self):
        # the alternative algorithm (try every possible answer) is
        # not an option here as there might be an infinite
        # amount of possibilities
        # --> calculate a few small problems by trying out every
        # possibility is solvable and faster

        range_min = 0
        range_max = 0
        all_actor_ranges = []

        query_results = self._parallel_apply_for_actors_n_wait(
            get_actor_vr
        )

        for actor_value_range in query_results:
            all_actor_ranges.append(actor_value_range)
            actor_min = min(actor_value_range)
            range_min += actor_min
            actor_max = max(actor_value_range)
            range_max += actor_max

        range_theo_max = range(range_min, range_max+1)
        range_theo_max_length = len(range_theo_max)

        if range_theo_max_length > 500:
            self.log('warning: big interval to check: {0}'.format(
                range_theo_max_length))
            raise ValueError(
                'please improve algorithm for large numbers')

        own_value_range = set()

        for possibly_a_value_range_value in range_theo_max:
            csp_result = csp_solver.do_solve(
                variables=all_actor_ranges,
                reference_value=possibly_a_value_range_value,
                csp_solver_config=self._csp_solver_config
            )
            if ('satisfiable_bool' in csp_result
                and csp_result['satisfiable_bool'] == True):
                own_value_range.add(possibly_a_value_range_value)
            else:
                self.log('not a solution: {0}'.format(
                    possibly_a_value_range_value))

        return own_value_range
Example #9
0
    def set_value(self, new_value):
        set_value = self.validate(new_value)

        all_actor_ranges = list(self._parallel_apply_for_actors_n_wait(
            get_actor_vr
        ))

        csp_result = csp_solver.do_solve(
            variables=all_actor_ranges,
            reference_value=set_value,
            csp_solver_config=self._csp_solver_config
        )

        if ('satisfiable_bool' in csp_result
            and csp_result['satisfiable_bool'] == True):

            # Using a subprocess will pickle the used objects
            # --> the "set" values are lost.
            # RemoteActor manipulates outside of
            # context actors --> ok.
            if self.actors_cls in [
                    ControllerActor, RemoteActor
                    ]:
                iterable = [
                    (self._actors[index], assigned_value)
                    for index, assigned_value \
                        in enumerate(csp_result['solution_list'])
                ]
                _query_results = self._parallel_apply_for_actors_n_wait(
                    set_actor_value,
                    iterable
                )
            else:
                for index, assigned_value in enumerate(
                        csp_result['solution_list']):
                    set_actor_value(
                        (self._actors[index], assigned_value)
                    )

            return set_value
        else:
            raise NotSolvable(
                '{0} is not an satisfiable'.format(set_value)
            )
Example #10
0
    def get_value_range(self):
        # the alternative algorithm (try every possible answer) is
        # not an option here as there might be an infinite
        # amount of possibilities
        # --> calculate a few small problems by trying out every
        # possibility is solvable and faster

        range_min = 0
        range_max = 0
        all_actor_ranges = []

        query_results = self._parallel_apply_for_actors_n_wait(get_actor_vr)

        for actor_value_range in query_results:
            all_actor_ranges.append(actor_value_range)
            actor_min = min(actor_value_range)
            range_min += actor_min
            actor_max = max(actor_value_range)
            range_max += actor_max

        range_theo_max = range(range_min, range_max + 1)
        range_theo_max_length = len(range_theo_max)

        if range_theo_max_length > 500:
            self.log('warning: big interval to check: {0}'.format(
                range_theo_max_length))
            raise ValueError('please improve algorithm for large numbers')

        own_value_range = set()

        for possibly_a_value_range_value in range_theo_max:
            csp_result = csp_solver.do_solve(
                variables=all_actor_ranges,
                reference_value=possibly_a_value_range_value,
                csp_solver_config=self._csp_solver_config)
            if ('satisfiable_bool' in csp_result
                    and csp_result['satisfiable_bool'] == True):
                own_value_range.add(possibly_a_value_range_value)
            else:
                self.log(
                    'not a solution: {0}'.format(possibly_a_value_range_value))

        return own_value_range