# Path: simpleai/search/utils.py
# def argmin(iterable, function):
#     return _generic_arg(iterable, function, min)


import random
from copy import deepcopy, copy
from itertools import product
from simpleai.search.utils import argmin
    from simpleai.search.arc import arc_consistency_3
    variable = variable_chooser(problem, pending, domains)

    values = values_sorter(problem, assignment, variable, domains)

    for value in values:
        new_assignment = deepcopy(assignment)
        new_assignment[variable] = value

        if not _count_conflicts(problem, new_assignment):  # TODO on aima also checks if using fc
            new_domains = deepcopy(domains)
            new_domains[variable] = [value]

            if not inference or arc_consistency_3(new_domains, problem.constraints):
                result = _backtracking(problem,
                                       new_assignment,
                                       new_domains,
                                       variable_chooser,
                                       values_sorter,
                                       inference=inference)
                if result:
                    return result

    return None


def _min_conflicts_value(problem, assignment, variable):
    '''
    Return the value generate the less number of conflicts.
    In case of tie, a random value is selected among this values subset.
    '''

# Path: simpleai/search/utils.py
# def argmin(iterable, function):
#     return _generic_arg(iterable, function, min)


# Path: simpleai/search/csp.py
import random
from copy import deepcopy, copy
from itertools import product
from simpleai.search.utils import argmin
    from simpleai.search.arc import arc_consistency_3

    variable = variable_chooser(problem, pending, domains)

    values = values_sorter(problem, assignment, variable, domains)

    for value in values:
        new_assignment = deepcopy(assignment)
        new_assignment[variable] = value

        if not _count_conflicts(problem, new_assignment):  # TODO on aima also checks if using fc
            new_domains = deepcopy(domains)
            new_domains[variable] = [value]

            if not inference or arc_consistency_3(new_domains, problem.constraints):
                result = _backtracking(problem,
                                       new_assignment,
                                       new_domains,
                                       variable_chooser,
                                       values_sorter,
                                       inference=inference)
                if result:
                    return result

    return None


def _min_conflicts_value(problem, assignment, variable):
    '''
    Return the value generate the less number of conflicts.
    In case of tie, a random value is selected among this values subset.
    '''

    return argmin(problem.domains[variable], lambda x: _count_conflicts(problem, assignment, variable, x))