new_game = new_problem.set_assignment(unassigned_var, domain)
            solution = solve_constraint_dfs(new_game)
            count = count + solution[1]
            if solution[0] != None:
                winner = solution
                break
        winner = winner[0], count
        return winner


# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with DFS?
# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.

ANSWER_1 = solve_constraint_dfs(get_pokemon_problem())[1]

#### Part 3: Forward Checking ##################################################


def eliminate_from_neighbors(csp, var):
    """
    Eliminates incompatible values from var's neighbors' domains, modifying
    the original csp.  Returns an alphabetically sorted list of the neighboring
    variables whose domains were reduced, with each variable appearing at most
    once.  If no domains were reduced, returns empty list.
    If a domain is reduced to size 0, quits immediately and returns None.
    """
    v_domain = csp.get_domain(var)
    neighbors = csp.get_neighbors(var)
Esempio n. 2
0
                        new_problem.set_assignment(var, value)
                        new_problems.append(new_problem)
                    queue = new_problems + queue
                else:
                    return (curr.assignments, extensions)

    return (None, extensions)


# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with DFS?

# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.

print(solve_constraint_dfs(get_pokemon_problem()))
ANSWER_1 = 20

#### Part 3: Forward Checking ##################################################


def eliminate_from_neighbors(csp, var):
    """
    Eliminates incompatible values from var's neighbors' domains, modifying
    the original csp.  Returns an alphabetically sorted list of the neighboring
    variables whose domains were reduced, with each variable appearing at most
    once.  If no domains were reduced, returns empty list.
    If a domain is reduced to size 0, quits immediately and returns None.
    """

    # see which of if w and v of W and V are incompatible
Esempio n. 3
0
                    if enqueue_condition:
                        propagate(enqueue_condition,new_prob)
                    new_problems.append(new_prob)
                new_problems.reverse()
                for problem in new_problems:
                    forward_check(problem, var)
                    agenda.insert(0,problem)
            else:
                solution = current.assignments
                break
    return (solution, extensions)

# QUESTION 5: How many extensions does it take to solve the Pokemon problem
#    with forward checking and propagation through singleton domains? (Don't
#    use domain reduction before solving it.)
poke = get_pokemon_problem()
print(solve_constraint_generic(poke,condition_forward_checking))
ANSWER_5 = 7


#### Part 6: Defining Custom Constraints #######################################

def constraint_adjacent(m, n) :
    """Returns True if m and n are adjacent, otherwise False.
    Assume m and n are ints."""
    if abs(m-n) == 1:
        return True
    return False

def constraint_not_adjacent(m, n) :
    """Returns True if m and n are NOT adjacent, otherwise False.
Esempio n. 4
0
File: lab4.py Progetto: corcillo/ai
                        continue
                if anyNonViolators==False:
                    csp.eliminate(var2, domainVal2)
                    if len(csp.get_domain(var2))==0:
                        return None
                    if var2 not in queue:
                        queue.append(var2)
    return dequeued
            
            
# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DON'T use domain reduction before solving it?

# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.
csp = get_pokemon_problem()
ANSWER_1 = solve_constraint_dfs(csp)[1]
# QUESTION 2: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DO use domain reduction before solving it?
csp = get_pokemon_problem()
domain_reduction(csp,None)
ANSWER_2 = solve_constraint_dfs(csp)[1]


#### PART 3: PROPAGATION THROUGH REDUCED DOMAINS

def solve_constraint_propagate_reduced_domains(problem) :
    """Solves the problem using depth-first search with forward checking and
    propagation through all reduced domains.  Same return type as
    solve_constraint_dfs."""
    q = [problem]
Esempio n. 5
0
            queue.append(element)
    return answer


def solve_constraint_dfs_domain_reduction(problem):
    domain_reduction(problem)
    return solve_constraint_dfs(problem)


# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DON'T use domain reduction before solving it?

# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.

ANSWER_1 = solve_constraint_dfs(get_pokemon_problem())[1]

# QUESTION 2: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DO use domain reduction before solving it?

ANSWER_2 = solve_constraint_dfs_domain_reduction(get_pokemon_problem())[1]

#### PART 3: PROPAGATION THROUGH REDUCED DOMAINS


def solve_constraint_propagate_reduced_domains(problem):
    """Solves the problem using depth-first search with forward checking and
    propagation through all reduced domains.  Same return type as
    solve_constraint_dfs."""

    stack = [problem]
Esempio n. 6
0
def search_with_dfs():
    ans=solve_constraint_dfs(get_pokemon_problem())
Esempio n. 7
0
def domain_before_search():
    omain_reduction(get_pokemon_problem(),queue=None)
Esempio n. 8
0
        liste = eliminate_from_neighbors(csp,var)
        if liste:
            for neighbor in liste:
                if neighbor not in q:
                    q.append(neighbor)
        if liste is None:
            return None
        
    return dequeued

# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DON'T use domain reduction before solving it?

# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.
print solve_constraint_dfs(get_pokemon_problem())
ANSWER_1 = 20

# QUESTION 2: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DO use domain reduction before solving it?
d =  (get_pokemon_problem())
#domain_reduction(d)
print solve_constraint_dfs(d)
ANSWER_2 = 6


#### PART 3: PROPAGATION THROUGH REDUCED DOMAINS

def solve_constraint_propagate_reduced_domains(problem) :
    """Solves the problem using depth-first search with forward checking and
    propagation through all reduced domains.  Same return type as
Esempio n. 9
0
        agenda = append_to_front + agenda[1:]

    if agenda:
        return (agenda[0].assignments, extensions)
    else:
        return (None, extensions)


# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with DFS?

# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.

ANSWER_1 = solve_constraint_dfs(get_pokemon_problem())[1]

#### Part 3: Forward Checking ##################################################


def eliminate_from_neighbors(csp, var):
    """
    Eliminates incompatible values from var's neighbors' domains, modifying
    the original csp.  Returns an alphabetically sorted list of the neighboring
    variables whose domains were reduced, with each variable appearing at most
    once.  If no domains were reduced, returns empty list.
    If a domain is reduced to size 0, quits immediately and returns None.
    """
    # print ('Var: {}'.format(var))
    # print ('Domain: {}'.format(csp.get_domain(var)))
    # print (csp)
Esempio n. 10
0
        new_queue = eliminate_from_neighbors(csp, var)

        if new_queue is None:
            return None

        queue.extend((n for n in new_queue if n not in queue and (not singletons_only or len(csp.get_domain(n)) == 1)))
    return dequeued


# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DON'T use domain reduction before solving it?

# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.

ANSWER_1 = solve_constraint_dfs(get_pokemon_problem())[1]

# QUESTION 2: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DO use domain reduction before solving it?

q2 = get_pokemon_problem()
domain_reduction(q2)
ANSWER_2 = solve_constraint_dfs(q2)[1]


#### PART 3: PROPAGATION THROUGH REDUCED DOMAINS


def solve_constraint_propagate_reduced_domains(problem):
    """Solves the problem using depth-first search with forward checking and
    propagation through all reduced domains.  Same return type as
Esempio n. 11
0
    return set


def check_no_repeat(queue, j, reduced):
    queue_set = from_list_to_set(queue[j:], set())
    for i in reduced:
        if i not in queue_set:
            queue.append(i)


# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DON'T use domain reduction before solving it?

# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.
solution, ext = solve_constraint_dfs(get_pokemon_problem())
ANSWER_1 = ext

# QUESTION 2: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DO use domain reduction before solving it?
problem = get_pokemon_problem()
domain_reduction(problem)
solution, ext = solve_constraint_dfs(problem)
ANSWER_2 = ext

#### PART 3: PROPAGATION THROUGH REDUCED DOMAINS


def solve_constraint_propagate_reduced_domains(problem):
    """Solves the problem using depth-first search with forward checking and
    propagation through all reduced domains.  Same return type as
Esempio n. 12
0
from constraint_api import *
from test_problems import get_pokemon_problem
from lab4 import solve_constraint_dfs


# test 16
solve_constraint_dfs_2_expected = (
    {"Q1": "B", "Q3": "D", "Q2": "B", "Q5": "C", "Q4": "C"},
    20,
)

result = solve_constraint_dfs(get_pokemon_problem())
print(result)
Esempio n. 13
0
        agenda.pop(0)
        extensions += 1
        if check_all_constraints(curr_problem) == True and has_empty_domains(curr_problem) == False:
            next_unassigned_var = curr_problem.pop_next_unassigned_var()
            if next_unassigned_var == None:
                return curr_problem.assignments, extensions
            else:
                list_of_extensions = create_extensions(curr_problem, next_unassigned_var)
                agenda = list_of_extensions + agenda
    if agenda == []:
        return None, extensions
    

# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with DFS?
pokemon_problem = get_pokemon_problem()
solution, extensions = solve_constraint_dfs(pokemon_problem)
# Hint: Use get_pokemon_problem() to get a new copy of the Pokemon problem
#    each time you want to solve it with a different search method.
ANSWER_1 = extensions


#### Part 3: Forward Checking ##################################################

def helper_checking_neighbors(csp, constraints, val_neighbor, values_for_var):
    flag = [True] * len(values_for_var)
    for i, val in enumerate(values_for_var):
        for constraint in constraints:
            if constraint.check(val, val_neighbor) == False:
                flag[i] = False
    if True in flag:
Esempio n. 14
0
        ##
        if nextval == None:
            return None

        for j in nextval:
            if j not in queue:
                queue.append(j)

    return q2


# QUESTION 1: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DON'T use domain reduction before solving it?

ANSWER_1 = solve_constraint_dfs(get_pokemon_problem())[1]

# QUESTION 2: How many extensions does it take to solve the Pokemon problem
#    with dfs if you DO use domain reduction before solving it?
extensionproblem = get_pokemon_problem()
domain_reduction(extensionproblem)

ANSWER_2 = solve_constraint_dfs(extensionproblem)[1]

#### PART 3: PROPAGATION THROUGH REDUCED DOMAINS


def solve_constraint_propagate_reduced_domains(problem):

    counter = 0
    queue = [problem]