예제 #1
0
def run(problem,name):
    print("\n\n*******",name)

    print("\nA*:")
    asearcher = AStarSearcher(problem)
    print("Path found:",asearcher.search(),"  cost=",asearcher.solution.cost)
    print("there are",asearcher.frontier.count(asearcher.solution.cost),
          "elements remaining on the queue with f-value=",asearcher.solution.cost)

    print("\nA* with MPP:"),
    msearcher = SearcherMPP(problem)
    print("Path found:",msearcher.search(),"  cost=",msearcher.solution.cost)
    print("there are",msearcher.frontier.count(msearcher.solution.cost),
          "elements remaining on the queue with f-value=",msearcher.solution.cost)

    bound = asearcher.solution.cost+0.01
    print("\nBranch and bound (with too-good initial bound of", bound,")")
    tbb = DF_branch_and_bound(problem,bound)  # cheating!!!!
    print("Path found:",tbb.search(),"  cost=",tbb.solution.cost)
    print("Rerunning B&B")
    print("Path found:",tbb.search())

    bbound = asearcher.solution.cost*2+10
    print("\nBranch and bound (with not-very-good initial bound of", bbound, ")")
    tbb2 = DF_branch_and_bound(problem,bbound)  # cheating!!!!
    print("Path found:",tbb2.search(),"  cost=",tbb2.solution.cost)
    print("Rerunning B&B")
    print("Path found:",tbb2.search())

    print("\nDepth-first search: (Use ^C if it goes on forever)")
    tsearcher = Searcher(problem)
    print("Path found:",tsearcher.search(),"  cost=",tsearcher.solution.cost)
예제 #2
0
def main():
    prob = Tile_problem(4, [1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0])
    #prob = Tile_problem(3, [1,1,0, 1,0,0, 1,0,1])
    s = AStarSearcher(prob)
    path = s.search()
    if path:
        print(f"Path found (cost = {path.cost})\n{path}")
        return path
예제 #3
0
def main():
    smoke_test()
    prob = River_problem()
    s = AStarSearcher(prob)
    path = s.search()
    if path:
        print("Path found (cost=%s)\n%s" % (path.cost, path))
        return path

    if __name__ == '__main__':
        main()
예제 #4
0
파일: driver.py 프로젝트: catxmai/cs-210
def searchTest(algo, board):
    bp0 = BloxorzProblem(board)
    if algo == "BFSMPP":
        searcher = BFSMultiPruneSearcher(bp0)
        result = searcher.search()
    elif algo == "A*":
        searcher = AStarSearcher(bp0)
        result = searcher.search(True)
    elif algo == "A*MPP":
        searcher = AStarMultiPruneSearcher(bp0)
        result = searcher.search(True)
    else:
        raise ValueError("invalid search algo")
    expansion = searcher.num_expanded
    return (result, expansion)
예제 #5
0
            line = line.strip()
            line = line.replace(',', '')
            line = line.split(' ')
            tasks = line[1]
            day = week_to_num[line[3]]
            time = time_to_num[line[4]]
            soft_cost[tasks] = int(line[-1])
            soft_constraint[tasks] = day * 10 + time

# print(soft_constraint)
# print(hard_constraint)
# print(task_domain)

csp = New_CSP(task_domain, hard_constraint, soft_constraint, soft_cost)
problem = Search_with_AC_from_Cost_CSP(csp)
solution = AStarSearcher(problem).search()

# print(solution)

if solution:
    solution = solution.end()
    for task in solution:
        for item in week_to_num:
            if week_to_num[item] == list(solution[task])[0][0] // 10:
                day = item
        for item in time_to_num:
            if time_to_num[item] == list(solution[task])[0][0] % 10:
                time = item
        print(f'{task}:{day} {time}')
    print(f'cost:{problem.heuristic(solution)}')
else:
예제 #6
0
                soft_constraint(task, t, line[1].split()[1:])
        # hard constraints
        else:
            domains[task] = domains[task] & set(
                domain_num(' '.join(line[-1].split()[1:])))

fp.close()

for k in domains.keys():
    domains[k] = domains[k] & set(domain_duration(k, domains[k]))
# calling CSP class with domains, constraints and cost
csp = CSP(domains, constraints, cost)
# calling Search_with_AC_from_Cost_CSP class with csp
search_from_cost_csp = Search_with_AC_from_Cost_CSP(csp)
# calling search funstion of AStarSearcher class with search_from_cost_csp
path = AStarSearcher(search_from_cost_csp).search()

if path != None:
    final_cost = 0
    # sorting the path dictionary in increasing order of the tasknames
    path = {k: path.end().to_node[k] for k in sorted(path.end().to_node)}
    # printing out the output
    for v in path:
        print(v,
              ':',
              working_days[path[v] // 8],
              ' ',
              working_hours[path[v] % 8],
              sep='')
        # calculating the final_cost
        final_cost += cost[(v, path[v])]
예제 #7
0
        return newconst

    def possible(self,pair,constraint):
        (x,y) = pair
        return (y,x) not in constraint

from searchBranchAndBound import DF_branch_and_bound
from searchGeneric import AStarSearcher
from searchMPP import SearcherMPP 
from stripsProblem import problem0, problem1, problem2 

rplanning0 = POP_search_from_STRIPS(problem0)
rplanning1 = POP_search_from_STRIPS(problem1)
rplanning2 = POP_search_from_STRIPS(problem2)
searcher0 = DF_branch_and_bound(rplanning0,5)
searcher0a = AStarSearcher(rplanning0)
searcher1 = DF_branch_and_bound(rplanning1,10)
searcher1a = AStarSearcher(rplanning1)
searcher2 = DF_branch_and_bound(rplanning2,10)
searcher2a = AStarSearcher(rplanning2)
# Try one of the following searchers
# a = searcher0.search()
# a = searcher0a.search()
# a.end().extract_plan()  # print a plan found
# a.end().constraints     # print the constraints
# AStarSearcher.max_display_level = 0  # less detailed display
# DF_branch_and_bound.max_display_level = 0  # less detailed display
# a = searcher1.search()
# a = searcher1a.search()
# a = searcher2.search()
# a = searcher2a.search()
예제 #8
0
    def effect(self, act, state_asst):
        """returns the state that is the effect of doing act given state_asst"""
        new_state_asst = self.prob_domain.strips_map[act].effects.copy()
        for prop in state_asst:
            if prop not in new_state_asst:
                new_state_asst[prop] = state_asst[prop]
        return State(new_state_asst)

    def heuristic(self, state):
        """in the forward planner a node is a state.
        the heuristic is an (under)estimate of the cost
        of going from the state to the top-level goal.
        """
        return self.heur(state.assignment, self.goal)


from searchBranchAndBound import DF_branch_and_bound
from searchGeneric import AStarSearcher
from searchMPP import SearcherMPP
from stripsProblem import problem0, problem1

# AStarSearcher(Forward_STRIPS(problem0)).search()  #A*
# SearcherMPP(Forward_STRIPS(problem0)).search()  #A* with MPP
# DF_branch_and_bound(Forward_STRIPS(problem0),10).search() #B&B
# # To find more than one plan:
# s1 = SearcherMPP(Forward_STRIPS(problem0))  #A*
# s1.search()  #find another plan
print(AStarSearcher(Forward_STRIPS(problem0)).search())
print(AStarSearcher(Forward_STRIPS(problem1)).search())
예제 #9
0
                                         min_cost)
                    else:
                        list_Cost.append(0)
                if len(list_Cost) != 0:
                    minCost.append(min(list_Cost))
        return sum(minCost)


# displaying to stdout as required by assignment spec
def print_output(Solver, searchProblem):
    if Solver is not None:
        s_best = Solver.end()
        for task in Solver.end():
            day = str(list(s_best[task])[0][0])[0]
            time = str(list(s_best[task])[0][0])[1]
            for day_key in days:
                if days[day_key] == day:
                    day = day_key
            for time_key in times:
                if times[time_key] == time:
                    time = time_key
            print(f'{task}:{day} {time}')
        print(f'cost:{searchProblem.heuristic(s_best)}')
    else:
        print('No solution')


CSP_Cost_1 = CSP_cost(task_vals, constraints_h, constraints_s, deadline_costs)
searchProblem = Search_with_AC_from_Cost_CSP(CSP_Cost_1)
Solver = AStarSearcher(searchProblem).search()
print_output(Solver, searchProblem)
예제 #10
0
                if x == x1 and (x0, y) not in newconst:
                    todo.append((x0, y))
                if y == x0 and (x, x1) not in newconst:
                    todo.append((x, x1))
        return newconst

    def possible(self, pair, constraint):
        (x, y) = pair
        return (y, x) not in constraint


rplanning1 = POP_search_from_STRIPS(strips_delivery1)
rplanning2 = POP_search_from_STRIPS(strips_delivery2)
rplanning3 = POP_search_from_STRIPS(strips_delivery3)
searcher1 = DF_branch_and_bound(rplanning1, 5)
searcher1a = AStarSearcher(rplanning1)
searcher2 = DF_branch_and_bound(rplanning2, 10)
searcher2a = AStarSearcher(rplanning2)
searcher3 = DF_branch_and_bound(rplanning3, 10)
searcher3a = AStarSearcher(rplanning4)
# Try one of the following searchers
# a = searcher1.search()
# a = searcher1a.search()
# a.end().extract_plan()  # print a plan found
# a.end().constraints     # print the constraints
# AStarSearcher.max_display_level = 0  # less detailed display
# DF_branch_and_bound.max_display_level = 0  # less detailed display
# a = searcher2.search()
# a = searcher2a.search()
# a = searcher3.search()
# a = searcher3a.search()
예제 #11
0
        # get domains for each task
        for var in variables:
            task = var[0]
            duration = var[1]
            domains[task] = sorted({(i * 10 + j, i * 10 + j + duration) for i in range(1, 6) for j in range(1, 10 - duration)})

    # print("variables = ", variables)
    # print("domains = ", domains)
    # print("constraints", constraints)
    # print("soft_constraints", soft_constraints)
    # print("soft_cost", soft_cost)

    # which adds soft_constraints and soft_cost compared with origin
    my_csp = My_CSP(domains, constraints, soft_constraints, soft_cost)
    my_problem = Search_with_AC_from_Cost_CSP(my_csp)
    my_searcher = AStarSearcher(my_problem)

    my_solution = my_searcher.search()
    # print(my_solution)
    if my_solution is not None:  # there is a solution if finding a path
        final_schedule = my_solution.end()  # find the end node(result) in our solution path
        if final_schedule is not None:
            my_cost = my_problem.heuristic(final_schedule)
            for task in final_schedule:
                start_time = list(final_schedule[task])[0][0]
                day = start_time // 10
                time = start_time % 10
                output_day = ""
                output_time = ""
                for key in day2num:
                    if day2num[key] == day: