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:
    print('No solution')
Exemple #2
0
                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])]
    print('cost:', final_cost, sep='')
else:
    print('No Solution')