Example #1
0
def problem(verbose):
    domain = Domain((
        Action(
            'oppoint',
            preconditions=(('at', 'home'), ),
            effects=(('loverget', 'present'), ),
        ),
        Action(
            'dating',
            preconditions=(('loverget', 'present'), ),
            effects=(('feel', 'happy'), ),
        ),
    ))
    problem = Problem(domain, {
        'storelist': ('restaurant', 'bookstore'),
        'buylist': ('book', 'food'),
        'decidelist': ('outside', 'playground'),
    },
                      init=(('at', 'home'), ),
                      goal=(
                          ('loverget', 'present'),
                          ('feel', 'happy'),
                      ))

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
Example #2
0
def problem(distancemat, agentcount, verbose=True):
    agents = [i for i in range(agentcount)]
    trashcount = len(distancemat) - agentcount
    trash_cans = [i + agentcount for i in range(trashcount)]
    agentDict = dict([(i,0) for i in agents])
    domain = Domain((
        Action(
            "Check",
            # Send agent A1 from T1 to pick up trash at T2
            parameters=(
                ("agent", "A1"),
                ("trash_can", "T1"),
                ("trash_can", "T2"),
            ),
            preconditions=(
                ("at", "A1", "T1"),
                ("unchecked", "T2"),
            ),
            effects=(
                # A1 is no longer at T1
                neg(("at", "A1", "T1")),
                # A1 is now at T2
                ("at", "A1", "T2"),
                # T2 is checked.
                ("checked", "T2"),
                neg(("unchecked", "T2")),
            )
        ),
    ))
    problem = Problem(
        domain,
        {
            # List of all agents
            "agent":  agents,
            # list of trash cans. Note: Starting positions are
            # treated as trash cans.
            "trash_can":  [i for i in range(len(distancemat))],
        },
        init=[("at", i, i) for i in agents] + \
                [("unchecked", i) for i in trash_cans],
        goal=[("checked", i) for i in trash_cans]
    )
    # Heuristics based on the agent that has travelled the farthest
    def heuristic(state):
        # copy prepared table
        agent2cost = agentDict.copy()
        # calculate cost
        for a in [action.sig for action in state.plan()]:
            agent2cost[a[1]] += distancemat[a[2]][a[3]]
        cost = max(agent2cost.values())
        #heur = ?
        return cost

    return planner(problem,
                   heuristic=heuristic,
                   verbose=verbose)
Example #3
0
def problem(verbose):
    domain = Domain((
        Action(
            'sell',
            parameters=(
                ('product', 'p'),
            ),
            preconditions=(
                ('>', ('quantity', 'p'), 0),
                ('>=', ('customer-money',), ('price', 'p')),
            ),
            effects=(
                ('-=', ('quantity', 'p'), 1),
                ('+=', ('account',), ('price', 'p')),
                ('-=', ('customer-money',), ('price', 'p')),
            ),
        ),
    ))
    problem = Problem(
        domain,
        {
            'product': ('apples', 'oranges',),
        },
        init=(
            ('=', ('account',), 0),
            ('=', ('customer-money',), 15),
            ('=', ('quantity', 'apples'), 10),
            ('=', ('quantity', 'oranges'), 10),
            ('=', ('price', 'apples'), 3),
            ('=', ('price', 'oranges'), 5),
        ),
        goal=(
            ('=', ('account',), 13),
        )
    )

    plan = planner(problem, verbose=True)
    if plan is None:
        print('No Plan!')
    else:
        state = problem.initial_state
        for action in plan:
            print(action)
            state = state.apply(action)
            print(state.f_dict)
Example #4
0
def problem2(world):
    domain = Domain((
        Action(
            'askPhone',
            preconditions=(
                ('t', 'start'),
                ('f', 'askPhoneNO'),
            ),
            effects=(('t', 'askPhoneNO'), ),
        ),
        Action(
            'start',
            preconditions=(('f', 'start'), ),
            effects=(('t', 'start'), ),
        ),
        Action(
            'close2',
            preconditions=(
                ('t', 'start'),
                ('t', 'askPhoneNO'),
                ('f', 'close'),
            ),
            effects=(('t', 'close'), ),
        ),
    ))
    init = [('t', 'start'), ('f', 'close')]
    goal = (('t', 'close'), )
    init.append(
        ('t', 'askPhoneNO')) if world.userProfile.phone > 0 else init.append(
            ('f', 'askPhoneNO'))

    init = tuple(init)
    # print(init)
    problem = Problem(domain, {}, init=init, goal=goal)

    plan = planner(problem)
    return plan
Example #5
0
def problem(verbose):
    domain = Domain((
        Action(
            #학교에 갈 짐을 쌉니다. 교재와 펜을 챙겨야 합니다.
            'pack',
            parameters=(
                ('things', 'th'),
            ),
            preconditions=(
                ('at', 'home'),
            ),
            effects=(
                ('packed', 'th'),
            ),
        ),
        
        Action(
            #교재와 펜을 챙기면 밖으로 나올 수 있습니다. 피시방을 가거나 학교에 갑니다.
            'walk',
            parameters=(
                ('locations', 'loc'),
            ),
            preconditions=(
                ('packed', 'textbook'),
                ('packed', 'pen'),
            ),
            effects=(
                ('at', 'loc'),
            ),
        ),
        
        Action(
            #맞는 교실을 갑니다. 지금은 인공지능 수업을 가야할 때 입니다.
            'goclass',
            parameters=(
                ('buildings', 'bui'),
            ),
            preconditions=(
                ('at', 'waytoschool'),
            ),
            effects=(
                ('at', 'bui'),
                neg(('at','waytoschool')),
            ),
        ),
        
        Action(
            #가지고 온 것들을 엽니다. 인공지능 수업에 있고 교재가 필요합니다.
            'seetextbook',
            parameters=(
                ('things', 'th'),
            ),
            preconditions=(
                ('at', 'aiclass'),
                ('packed','textbook')
            ),
            effects=(
                neg(('packed', 'textbook')),
                ('open','th')
            ),
        ),
        
        Action(
            #수업을 듣습니다. 교재를 핀 상태여야 합니다.
            'takeclass',
            parameters=(
                ('things', 'th'),
            ),
            preconditions=(
                ('open', 'textbook'),
            ),
            effects=(
                ('study', 'th'),
            ),
        ),
        
        Action(
            #수업이 끝나면 물건들을 닫고 마무리합니다.
            'endclass',
            parameters=(
                ('things', 'th'),
            ),
            preconditions=(
                ('study', 'textbook'),
            ),
            effects=(
                ('closed', 'th'),
            ),
        ),
        
    ))
    problem = Problem(
        domain,
        {
            'things': ('textbook', 'comicbook','pen'),
            'locations': ('PCroom', 'waytoschool'),
            'buildings': ('aiclass', 'algorithmclass'),
            
        },
        init=(
            ('at', 'home'),
        ),
        goal=(
            ('closed', 'textbook'),
        )
    )

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
def problem(verbose):
    domain = Domain((
        Action(
            'move-up',
            parameters=(
                ('tile', 't'),
                ('position', 'px'),
                ('position', 'py'),
                ('position', 'by'),
            ),
            preconditions=(
                ('dec', 'by', 'py'),
                ('blank', 'px', 'by'),
                ('at', 't', 'px', 'py'),
            ),
            effects=(
                neg(('blank', 'px', 'by')),
                neg(('at', 't', 'px', 'py')),
                ('blank', 'px', 'py'),
                ('at', 't', 'px', 'by'),
            ),
        ),
        Action(
            'move-down',
            parameters=(
                ('tile', 't'),
                ('position', 'px'),
                ('position', 'py'),
                ('position', 'by'),
            ),
            preconditions=(
                ('inc', 'by', 'py'),
                ('blank', 'px', 'by'),
                ('at', 't', 'px', 'py'),
            ),
            effects=(
                neg(('blank', 'px', 'by')),
                neg(('at', 't', 'px', 'py')),
                ('blank', 'px', 'py'),
                ('at', 't', 'px', 'by'),
            ),
        ),
        Action(
            'move-left',
            parameters=(
                ('tile', 't'),
                ('position', 'px'),
                ('position', 'py'),
                ('position', 'bx'),
            ),
            preconditions=(
                ('dec', 'bx', 'px'),
                ('blank', 'bx', 'py'),
                ('at', 't', 'px', 'py'),
            ),
            effects=(
                neg(('blank', 'bx', 'py')),
                neg(('at', 't', 'px', 'py')),
                ('blank', 'px', 'py'),
                ('at', 't', 'bx', 'py'),
            ),
        ),
        Action(
            'move-right',
            parameters=(
                ('tile', 't'),
                ('position', 'px'),
                ('position', 'py'),
                ('position', 'bx'),
            ),
            preconditions=(
                ('inc', 'bx', 'px'),
                ('blank', 'bx', 'py'),
                ('at', 't', 'px', 'py'),
            ),
            effects=(
                neg(('blank', 'bx', 'py')),
                neg(('at', 't', 'px', 'py')),
                ('blank', 'px', 'py'),
                ('at', 't', 'bx', 'py'),
            ),
        ),
    ))
    problem = Problem(
        domain,
        {
            'tile': (1, 2, 3, 4, 5, 6, 7, 8),
            'position': (1, 2, 3),
        },
        init=(
            ('inc', 1, 2),
            ('inc', 2, 3),
            ('dec', 3, 2),
            ('dec', 2, 1),
            ('at', 8, 1, 1),
            ('at', 7, 2, 1),
            ('at', 6, 3, 1),
            ('blank', 1, 2),
            ('at', 4, 2, 2),
            ('at', 1, 3, 2),
            ('at', 2, 1, 3),
            ('at', 5, 2, 3),
            ('at', 3, 3, 3),
        ),
        goal=(
            ('blank', 1, 1),
            ('at', 1, 2, 1),
            ('at', 2, 3, 1),
            ('at', 3, 1, 2),
            ('at', 4, 2, 2),
            ('at', 5, 3, 2),
            ('at', 6, 1, 3),
            ('at', 7, 2, 3),
            ('at', 8, 3, 3),
        )
    )

    def to_coordinates(state):
        grid = {}
        for p in state:
            if p[0] == 'at':
                grid[p[1]] = (p[2], p[3])
        return grid

    goal_coords = to_coordinates(problem.goals)

    def manhattan_distance_heuristic(state):
        state_coords = to_coordinates(state.predicates)
        dist = 0
        for k in goal_coords.keys():
            c1, r1 = goal_coords[k]
            c2, r2 = state_coords[k]
            dist += (abs(c1 - c2) + abs(r1 - r2))
        return dist

    plan = planner(problem, heuristic=manhattan_distance_heuristic, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
Example #7
0
def problem3(world):
    domain = Domain((
        Action(
            'suggestLong',
            preconditions=(('f', 'start'), ),
            effects=(('t', 'start'), ),
        ),
        Action(
            'askWant',
            preconditions=(
                ('t', 'start'),
                ('f', 'want'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'want'),
                ('t', 'want', 'short_invest'),
                ('t', 'want', 'invest'),
                ('t', 'want', 'insurance'),
                ('t', 'want', 'fund'),
                ('t', 'want', 'plan'),
            ),
        ),
        Action(
            'askTerm',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'need_term'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'need_term'),
                ('t', 'need_term', 'short'),
                ('t', 'need_term', 'long'),
            ),
        ),
        Action(
            'askMoney',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'need_money'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'need_money'),
                ('t', 'need_money', 'small'),
                ('t', 'need_money', 'large'),
            ),
        ),
        Action(
            'askRequirement',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'need_term'),
                ('t', 'need_money'),
                ('t', 'want', 'plan'),
                ('f', 'requirement'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'requirement'),
                ('t', 'requirement', 'house'),
                ('t', 'requirement', 'education'),
            ),
        ),
        Action(
            'suggestPlan',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'want', 'plan'),
                ('t', 'need_term', 'long'),
                ('t', 'need_money', 'large'),
                ('f', 'close'),
            ),
            effects=(('t', 'want', 'plan'), ),
        ),
        Action(
            'suggestPlanResult',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'want', 'plan'),
                ('t', 'need_term'),
                ('t', 'need_money'),
                ('t', 'requirement'),
                ('f', 'suggest'),
                ('f', 'close'),
            ),
            effects=(('t', 'suggest'), ),
        ),
        Action(
            'suggestShort',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'want', 'plan'),
                ('f', 'want', 'short_invest'),
                ('t', 'need_term', 'short'),
                ('f', 'close'),
            ),
            effects=(('t', 'want', 'short_invest'), ),
        ),
        Action(
            'executeShortInvest',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'want', 'short_invest'),
                ('t', 'need_term', 'short'),
                ('f', 'suggest'),
                ('f', 'close'),
            ),
            effects=(('t', 'suggest'), ),
        ),
        Action(
            'executeInvest',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'want', 'invest'),
                ('t', 'need_term', 'long'),
                ('t', 'need_money', 'small'),
                ('f', 'suggest'),
                ('f', 'close'),
            ),
            effects=(('t', 'suggest'), ),
        ),
        Action(
            'suggestOther',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'want', 'invest'),
                ('f', 'want', 'plan'),
                ('t', 'need_term', 'long'),
                ('t', 'need_money', 'small'),
                ('f', 'close'),
            ),
            effects=(('t', 'want', 'invest'), ),
        ),
        Action(
            'close',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'suggest'),
                ('f', 'close'),
            ),
            effects=(('t', 'close'), ),
        ),
    ))
    init = [
        ('f', 'start'),
        # ('f', 'want'),
        # ('f', 'want', 'invest'),
        # ('f', 'want', 'short_invest'),
        # ('f', 'want', 'fund'),
        # ('f', 'want', 'insurance'),
        # ('f', 'want', 'plan'),
        # ('f', 'need_term'),
        # ('f', 'need_term', 'short'),
        # ('f', 'need_term', 'long'),
        # ('f', 'need_money'),
        # ('f', 'need_money', 'small'),
        # ('f', 'need_money', 'large'),
        # ('f', 'requirement'),
        # ('f', 'requirement', 'house'),
        # ('f', 'requirement', 'education'),
        ('f', 'close')
    ]
    goal = (('t', 'close'), )
    init.append(
        ('t', 'want')) if world.salesProfile.status >= 1 else init.append(
            ('f', 'want'))
    init.append(
        ('t', 'want',
         'invest')) if world.salesProfile.userGoal == 1 else init.append(
             ('f', 'want', 'invest'))
    init.append(
        ('t', 'want',
         'short_invest')) if world.salesProfile.userGoal == 2 else init.append(
             ('f', 'want', 'short_invest'))
    init.append(('t', 'want',
                 'fund')) if world.salesProfile.userGoal == 3 else init.append(
                     ('f', 'want', 'fund'))
    init.append(
        ('t', 'want',
         'insurance')) if world.salesProfile.userGoal == 4 else init.append(
             ('f', 'want', 'insurance'))
    init.append(('t', 'want',
                 'plan')) if world.salesProfile.userGoal == 5 else init.append(
                     ('f', 'want', 'plan'))

    init.append(
        ('t', 'need_term')) if world.userProfile.term > 0 else init.append(
            ('f', 'need_term'))
    init.append(('t', 'need_term',
                 'short')) if world.userProfile.term == 1 else init.append(
                     ('f', 'need_term', 'short'))
    init.append(('t', 'need_term',
                 'long')) if world.userProfile.term == 2 else init.append(
                     ('f', 'need_term', 'long'))
    init.append(
        ('t', 'need_money')) if world.userProfile.money > 0 else init.append(
            ('f', 'need_money'))
    init.append(('t', 'need_money',
                 'small')) if world.userProfile.money == 1 else init.append(
                     ('f', 'need_money', 'small'))
    init.append(('t', 'need_money',
                 'large')) if world.userProfile.money == 2 else init.append(
                     ('f', 'need_mongey', 'large'))

    init.append(
        ('t',
         'requirement')) if world.userProfile.purpose > 0 else init.append(
             ('f', 'requirement'))
    init.append(('t', 'requirement',
                 'house')) if world.userProfile.purpose == 1 else init.append(
                     ('f', 'requirement', 'house'))
    init.append(
        ('t', 'requirement',
         'education')) if world.userProfile.purpose == 4 else init.append(
             ('f', 'requirement', 'education'))

    init.append(
        ('t', 'suggest')
    ) if world.salesProfile.currentType == 2 and world.salesProfile.yesorno == 1 else init.append(
        ('f', 'suggest'))

    init = tuple(init)
    # print(init)
    problem = Problem(domain, {}, init=init, goal=goal)

    plan = planner(problem)
    return plan
Example #8
0
def problem1(world):
    domain = Domain((
        Action(
            'start',
            preconditions=(
                ('f', 'start'),
            ),
            effects=(
                ('t', 'start'),
            ),
        ),
        Action(
            'opening',
            preconditions=(
                ('t', 'start'),
                ('f', 'sizeConfirmation'),
                ('f', 'timeConfirmation'),
                ('t', 'doneWaiting'),
                ('t', 'reason'),
                ('f', 'bar'),
                ('f', 'confirm'),
                ('f', 'sayThanks'),
                ('f', 'doubleConfirm')
            ),
            effects=(
                ('t', 'sizeConfirmation'),
                ('t', 'timeConfirmation'),
                ('t', 'sayThanks'),
            ),
        ),

        Action(
            'tableFor2',
            preconditions=(
                ('t', 'start'),
                ('f', 'sizeConfirmation'),
                ('t', 'timeConfirmation'),
                ('f', 'tableFor2')
            ),
            effects=(
                ('t', 'tableFor2'),
                ('t', 'sayThanks'),
            ),
        ),

        Action(
            'combineTables',
            preconditions=(
                ('t', 'start'),
                ('t', 'tableFor2'),
                ('f', 'sizeConfirmation'),
                ('f', 'confirm'),
                ('t', 'timeConfirmation'),
                ('f', 'bar'),
                ('f', 'biggerTable'),
            ),
            effects=(
                ('t', 'sizeConfirmation'),
                ('t', 'sayThanks'),
            ),
        ),

        Action(
            'changeTime',
            preconditions=(
                ('t', 'start'),
                ('f', 'timeConfirmation'),
                ('t', 'sizeConfirmation'),
                ('f', 'askForAlternative'),
                ('f', 'confirm'),
            ),
            effects=(
                ('t', 'timeConfirmation'),
                ('t', 'sayThanks'),
            ),
        ),

        Action(
            'changeRestaurant',
            preconditions=(
                ('t', 'start'),
                ('t', 'changeRestaurant'),
            ),
            effects=(
                ('t', 'confirm'),
            ),
        ),

        Action(
            'tentativelyReserve',
            preconditions=(
                ('t', 'start'),
                ('t', 'tentativelyReserve'),
                ('t', 'timeConfirmation'),
                ('t', 'sizeConfirmation'),
            ),
            effects=(
                ('t', 'callBackLater'),
            ),
        ),

        Action(
            'askForAlternatives',
            preconditions=(
                ('t', 'start'),
                ('t', 'askForAlternative'),
            ),
            effects=(
                ('t', 'confirm'),
            ),
        ),

        Action(
            'askForReason',
            preconditions=(
                ('t', 'start'),
                ('f', 'sizeConfirmation'),
                ('f', 'timeConfirmation'),
                ('f', 'confirm'),
                ('f', 'reason')
            ),
            effects=(
                ('t', 'reason'),
                ('t', 'sizeConfirmation'),
                ('t', 'timeConfirmation'),
                ('t', 'sayThanks'),
            ),
        ),

        Action(
            'sitAtBar',
            preconditions=(
                ('t', 'start'),
                ('t', 'timeConfirmation'),
                ('f', 'sizeConfirmation'),
                ('f', 'confirm'),
                ('t', 'bar'),
            ),
            effects=(
                ('t', 'sizeConfirmation'),
                ('t', 'sayThanks'),
            ),
        ),

        Action(
            'callBackLater',
            preconditions=(
                ('t', 'start'),
                ('t', 'callBackLater'),
            ),
            effects=(
                ('t', 'confirm'),
            ),
        ),

        Action(
            'wait',
            preconditions=(
                ('t', 'start'),
                ('t', 'timeConfirmation'),
                ('t', 'sizeConfirmation'),
                ('f', 'confirm'),
                ('f', 'doneWaiting')
            ),
            effects=(
                ('t', 'doneWaiting'),
                ('t', 'sayThanks'),
            ),
        ),

        Action(
            'askForBiggerTable',
            preconditions=(
                ('t', 'start'),
                ('t', 'biggerTable'),
                ('t', 'timeConfirmation'),
                ('f', 'sizeConfirmation'),
            ),
            effects=(
                ('t', 'sizeConfirmation'),
                ('t', 'sayThanks'),
            ),
        ),

        Action(
            'sayName',
            preconditions=(
                ('t', 'start'),
                ('t', 'name')
            ),
            effects=(
                ('t', 'confirm'),
            ),
        ),

        Action(
            'doubleConfirm',
            preconditions=(
                ('t', 'start'),
                ('t', 'timeConfirmation'),
                ('t', 'sizeConfirmation'),
                ('f', 'tentativelyReserve'),
                ('f', 'confirm'),
                ('t', 'doubleConfirm'),
                ('t', 'doneWaiting'),
            ),
            effects=(
                ('t', 'confirm'),
            ),
        ),


        Action(
            'sayThanks',
            preconditions=(
                ('t', 'sayThanks'),
                ('t', 'start'),
                ('t', 'timeConfirmation'),
                ('t', 'sizeConfirmation'),
                ('f', 'tentativelyReserve'),
                ('f', 'confirm'),
                ('t', 'doneWaiting'),
            ),
            effects=(
                ('t', 'confirm'),
            ),
        ),

        Action(
            'close',
            preconditions=(
                ('t', 'confirm'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'close'),
            ),
        ),
    ))

    init = [
        ('f', 'close'),
    ]
    goal = (
        ('t', 'timeConfirmation'),
        ('t', 'sizeConfirmation'),
        ('t', 'close'),
    )
    init.append(('t', 'start')) if world.dialog.status >= 1 else init.append(('f', 'start'))

    init.append(('t', 'size_user'))
    init.append(('t', 'time_user'))

    if world.dialog.status == 6.1:
        init.append(('t', 'askForAlternative'))
    else:
        init.append(('f', 'askForAlternative'))

    if world.dialog.status == 6:
        init.append(('t', 'changeRestaurant'))
    else:
        init.append(('f', 'changeRestaurant'))

    if world.humanSlot.size > 0 and world.humanSlot.size == world.robotSlot.size:
        init.append(('t', 'sizeConfirmation'))
    else:
        init.append(('f', 'sizeConfirmation'))

    if len(world.humanSlot.time) > 0 and world.humanSlot.time == world.robotSlot.time:
        init.append(('t', 'timeConfirmation'))
    else:
        init.append(('f', 'timeConfirmation'))

    if world.dialog.status == 3:
        init.append(('f', 'doneWaiting'))
    else:
        init.append(('t', 'doneWaiting'))

    if world.dialog.status == 4.9:
        init.append(('t', 'name'))
    else:
        init.append(('f', 'name'))

    if world.dialog.status == 7:
        init.append(('t', 'callBackLater'))
    else:
        init.append(('f', 'callBackLater'))

    if world.dialog.status == 5:
        init.append(('t', 'sayThanks'))
    else:
        init.append(('f', 'sayThanks'))

    if world.dialog.status == 8:
        init.append(('t', 'confirm'))
    else:
        init.append(('f', 'confirm'))

    if world.dialog.status == 1.1:
        init.append(('f', 'reason'))
    else:
        init.append(('t', 'reason'))

    if world.dialog.status == 4.1:
        init.append(('t', 'biggerTable'))
    else:
        init.append(('f', 'biggerTable'))

    if world.dialog.status == 9:
        init.append(('t', 'askForAlternative'))
    else:
        init.append(('f', 'askForAlternative'))

    if world.dialog.status == 4.12:
        init.append(('t', 'bar'))
    else:
        init.append(('f', 'bar'))

    if world.dialog.status == 4.2:
        init.append(('f', 'tableFor2'))
    else:
        init.append(('t', 'tableFor2'))

    if world.dialog.status == 5.1:
        init.append(('t', 'tentativelyReserve'))
    else:
        init.append(('f', 'tentativelyReserve'))

    if world.dialog.status == 5.2:
        init.append(('t', 'doubleConfirm'))
    else:
        init.append(('f', 'doubleConfirm'))

    init = tuple(init)

    problem = Problem(
        domain,
        {},
        init=init,
        goal=goal
    )

    plan = planner(problem)
    return plan, init
def problem(verbose):
    domain = Domain((
        # 일반 공격 : me와 enemy 모두 가능한 액션, 현재 턴(now_turn)인 공격자가 상대방(prev_turn)의 hp를 20 깎음
        Action(
            'normal-attack',
            parameters=(
                ('turn', 't'),
                ('turn', 'prev_t'),
            ),
            preconditions=(
                ('now_turn', 't'),
                ('prev_turn', 'prev_t'),
            ),
            effects=(
                neg(('now_turn', 't')),
                neg(('prev_turn', 'prev_t')),
                ('now_turn', 'prev_t'),
                ('prev_turn', 't'),
                ('+=', ('hp', 'prev_t'), -20),
                ('-=', ('damaged', 'prev_t'), ('damaged', 'prev_t')),
                ('+=', ('damaged', 'prev_t'), -20),
            ),
        ),
        # 마법 공격 : me와 enemy 모두 가능한 액션, 현재 턴(now_turn)인 공격자가 상대방(prev_turn)의 hp를 35 깎고, mp를 40 소모
        # mp가 40 이상일 때만 사용 가능
        Action(
            'magic-attack',
            parameters=(
                ('turn', 't'),
                ('turn', 'prev_t'),
            ),
            preconditions=(
                ('now_turn', 't'),
                ('prev_turn', 'prev_t'),
                ('>=', ('mp', 't'), 40),
            ),
            effects=(
                neg(('now_turn', 't')),
                neg(('prev_turn', 'prev_t')),
                ('now_turn', 'prev_t'),
                ('prev_turn', 't'),
                ('+=', ('hp', 'prev_t'), -35),
                ('+=', ('mp', 't'), -40),
                ('-=', ('damaged', 'prev_t'), ('damaged', 'prev_t')),
                ('+=', ('damaged', 'prev_t'), -35),
            ),
        ),
        # hp 회복 : me 만 사용 가능한 스킬, me의 mp를 60 소모하여, hp를 55 회복 함
        # mp가 60 이상일 때만 사용 가능, 현재 hp가 0 보다 클 때만 사용 가능(부활 불가)
        Action(
            'heal-me',
            preconditions=(
                ('now_turn', 'me'),
                ('prev_turn', 'enemy'),
                ('>', ('hp', 'me'), 0),
                ('>=', ('mp', 'me'), 60),
            ),
            effects=(
                neg(('now_turn', 'me')),
                neg(('prev_turn', 'enemy')),
                ('now_turn', 'enemy'),
                ('prev_turn', 'me'),
                ('+=', ('hp', 'me'), 55),
                ('+=', ('mp', 'me'), -60),
            ),
        ),
        # mp 회복 : me 만 사용 가능한 스킬, me의 mp를 40 회복 함
        Action(
            'charge-my-magicka',
            preconditions=(
                ('now_turn', 'me'),
                ('prev_turn', 'enemy'),
            ),
            effects=(
                neg(('now_turn', 'me')),
                neg(('prev_turn', 'enemy')),
                ('now_turn', 'enemy'),
                ('prev_turn', 'me'),
                ('+=', ('mp', 'me'), 40),
            ),
        ),
        # 반격 : me 만 사용 가능한 스킬, me의 mp를 60 소모하여, 바로 직전 턴에 enemy 에게 받은 데미지 만큼 hp를 회복함
        # mp가 60 이상일 때만 사용 가능, 직전 턴에 받은 데미지를 회복한 hp가 0 보다 커야만 사용 가능
        Action(
            'reflect-enemy-attack',
            preconditions=(
                ('now_turn', 'me'),
                ('prev_turn', 'enemy'),
                ('>', ('hp', 'me'), ('damaged', 'me')),
                ('>=', ('mp', 'me'), 60),
            ),
            effects=(
                neg(('now_turn', 'me')),
                neg(('prev_turn', 'enemy')),
                ('now_turn', 'enemy'),
                ('prev_turn', 'me'),
                ('-=', ('hp', 'me'), ('damaged', 'me')),
                ('+=', ('hp', 'enemy'), ('damaged', 'me')),
                ('+=', ('mp', 'me'), -60),
                ('-=', ('damaged', 'me'), ('damaged', 'me')),
            ),
        ),
    ))
    problem = Problem(
        domain,
        {
            'turn': ('me', 'enemy'),  # 가능한 turn Domain
        },
        # hp:100, mp:80 인 enemy와 hp:50, mp:100인 me 중 enemy의 선공으로 게임을 시작하는 초기 state
        init=(
            ('now_turn', 'enemy'),  # enemy 선공
            ('prev_turn', 'me'),  # prev_turn 이지만 init 단계이므로 다음 턴 공격자인 me
            ('=', ('hp', 'me'), 50),  # me의 시작 hp
            ('=', ('mp', 'me'), 100),  # me의 시작 mp
            ('=', ('damaged', 'me'), 0),  # me가 입은 데미지 초기 값, reflect-enemy-attack 액션을 통해 받은 데미지를 회복하기 위해 필요
            ('=', ('hp', 'enemy'), 100),  # enemy의 시작 hp
            ('=', ('mp', 'enemy'), 80),  # enemy의 시작 mp
            ('=', ('damaged', 'enemy'), 0),  # enemy가 입은 데미지 초기 값, enemy는 reflect-enemy-attack 액션을 사용할 수 없지만, domain parameter로 인한 런타임 에러를 막기위해 필요
        ),
        # goal test 직전인 마지막 액션의 turn이 me이고, 생존 상태의 me가 enemy를 쓰러트린 state
        # me가 생존한 상태에서, enemy를 쓰러트리는 state
        goal=(
            ('prev_turn', 'me'),
            ('>', ('hp', 'me'), 0),
            ('<=', ('hp', 'enemy'), 0),
        )
    )

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
Example #10
0
def problem(verbose):
    domain = Domain((

        #ready-for-battle & defencable -> enemy is defenced
        Action(
            'enemy-defenced',
            parameters=(
                ('state', '_state'),
                ('state', '_prev_state'),
            ),
            preconditions=(
                ('defencable', '_state'),
                ('ready-for-battle', ''),
                ('character_state', '_state'),
                ('character_prev_state', '_prev_state'),
            ),
            effects=(
                neg(('character_prev_state', '_prev_state')),
                neg(('character_state', '_state')),
                ('character_prev_state', '_state'),
                ('character_state', 'defenced'),
                ('use-defence', ''),
            ),
        ),

        #ready-for-battle & exhaustable -> enemy is exhausted
        Action(
            'enemy-exhausted',
            parameters=(
                ('state', '_state'),
                ('state', '_prev_state'),
            ),
            preconditions=(
                ('exhaustable', '_state'),
                ('ready-for-battle', ''),
                ('character_state', '_state'),
                ('character_prev_state', '_prev_state'),
            ),
            effects=(
                neg(('character_prev_state', '_prev_state')),
                neg(('character_state', '_state')),
                ('character_state', 'exhausted'),
                ('character_prev_state', '_state'),
            ),
        ),

        #ready-for-battle & ignitable -> enemy is ignited
        Action(
            'enemy-ignited',
            parameters=(
                ('state', '_state'),
                ('state', '_prev_state'),
            ),
            preconditions=(
                ('ignitable', '_state'),
                ('ready-for-battle', ''),
                ('character_state', '_state'),
                ('character_prev_state', '_prev_state'),
            ),
            effects=(
                neg(('character_prev_state', '_prev_state')),
                neg(('character_state', '_state')),
                ('character_prev_state', '_state'),
                ('character_state', 'ignited'),
                ('use-ignite', ''),
            ),
        ),

        # ready-for-battle & electric ready & electric shockable -> enemy is electric shocked
        Action(
            'electric-shock',
            parameters=(
                ('state', '_state'),
                ('state', '_prev_state'),
            ),
            preconditions=(
                ('electric-shockable', '_state', '_prev_state'),
                ('electric-ready', ''),
                ('ready-for-battle', ''),
                ('character_state', '_state'),
                ('character_prev_state', '_prev_state'),
            ),
            effects=(
                neg(('character_prev_state', '_prev_state')),
                neg(('character_state', '_state')),
                ('character_prev_state', '_state'),
                ('character_state', 'electric-shocked'),
            ),
        ),

        # ready-for-battle & electric preparable -> electric is prepared
        Action(
            'electric-ready-perfectly',
            parameters=(
                ('state', '_state'),
                ('state', '_prev_state'),
            ),
            preconditions=(
                ('electric-preparable', '_state'),
                ('ready-for-battle', ''),
                ('character_state', '_state'),
                ('character_prev_state', '_prev_state'),
            ),
            effects=(('electric-ready', ''), ),
        ),

        # idle -> ready for battle
        Action(
            'ready-for-battle',
            parameters=(
                ('state', '_state'),
                ('state', '_prev_state'),
            ),
            preconditions=(
                ('character_state', 'idle'),
                ('character_state', '_state'),
                ('character_prev_state', '_prev_state'),
            ),
            effects=(('ready-for-battle', ''), ),
        ),
    ))
    problem = Problem(
        domain,
        {
            'state': ('electric-shocked', 'idle', 'defenced', 'ignited',
                      'exhausted', 'NONE'),
        },
        init=(
            ('electric-shockable', 'exhausted', 'defenced'
             ),  #defenced -> exghausted -> now it can use electric shock
            ('electric-shockable', 'exhausted', 'ignited'
             ),  #ignited -> exghausted -> now it can use electric shock
            ('electric-preparable',
             'ignited'),  #prepare electric when enemy was ignited
            ('electric-preparable',
             'defenced'),  #prepare electric when enemy was exhausted
            ('defencable', 'idle'),
            ('ignitable', 'idle'),
            ('exhaustable', 'ignited'),
            ('exhaustable', 'defenced'),
            ('character_state', 'idle'),
            ('character_prev_state', 'NONE'),
        ),
        goal=(
            ('character_state', 'electric-shocked'),

            # case 1 use ignite
            ('use-ignite', ''),

            # 1. ready for battle
            # 2. ignite to enemy
            # 3. ready electric
            # 4. enemy was exhausted
            # 5. electric shock to enemy

            ################
            # case 2 use defence
            # ('use-defence',''),

            # 1. ready for battle
            # 2. enemy was defenced
            # 3. ready electric
            # 4. enemy was exhausted
            # 5. electric shock to enemy
        ))

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
Example #11
0
def problem(verbose):
    domain = Domain((
        Action(
            'move-up',
            parameters=(
                ('tile', 't'),
                ('position', 'px'),
                ('position', 'py'),
                ('position', 'by'),
            ),
            preconditions=(
                ('dec', 'by', 'py'),
                ('blank', 'px', 'by'),
                ('at', 't', 'px', 'py'),
            ),
            effects=(
                neg(('blank', 'px', 'by')),
                neg(('at', 't', 'px', 'py')),
                ('blank', 'px', 'py'),
                ('at', 't', 'px', 'by'),
            ),
        ),
        Action(
            'move-down',
            parameters=(
                ('tile', 't'),
                ('position', 'px'),
                ('position', 'py'),
                ('position', 'by'),
            ),
            preconditions=(
                ('inc', 'by', 'py'),
                ('blank', 'px', 'by'),
                ('at', 't', 'px', 'py'),
            ),
            effects=(
                neg(('blank', 'px', 'by')),
                neg(('at', 't', 'px', 'py')),
                ('blank', 'px', 'py'),
                ('at', 't', 'px', 'by'),
            ),
        ),
        Action(
            'move-left',
            parameters=(
                ('tile', 't'),
                ('position', 'px'),
                ('position', 'py'),
                ('position', 'bx'),
            ),
            preconditions=(
                ('dec', 'bx', 'px'),
                ('blank', 'bx', 'py'),
                ('at', 't', 'px', 'py'),
            ),
            effects=(
                neg(('blank', 'bx', 'py')),
                neg(('at', 't', 'px', 'py')),
                ('blank', 'px', 'py'),
                ('at', 't', 'bx', 'py'),
            ),
        ),
        Action(
            'move-right',
            parameters=(
                ('tile', 't'),
                ('position', 'px'),
                ('position', 'py'),
                ('position', 'bx'),
            ),
            preconditions=(
                ('inc', 'bx', 'px'),
                ('blank', 'bx', 'py'),
                ('at', 't', 'px', 'py'),
            ),
            effects=(
                neg(('blank', 'bx', 'py')),
                neg(('at', 't', 'px', 'py')),
                ('blank', 'px', 'py'),
                ('at', 't', 'bx', 'py'),
            ),
        ),
    ))
    problem = Problem(domain, {
        'tile': (1, 2, 3, 4, 5, 6, 7, 8),
        'position': (1, 2, 3),
    },
                      init=(
                          ('inc', 1, 2),
                          ('inc', 2, 3),
                          ('dec', 3, 2),
                          ('dec', 2, 1),
                          ('at', 8, 1, 1),
                          ('at', 7, 2, 1),
                          ('at', 6, 3, 1),
                          ('blank', 1, 2),
                          ('at', 4, 2, 2),
                          ('at', 1, 3, 2),
                          ('at', 2, 1, 3),
                          ('at', 5, 2, 3),
                          ('at', 3, 3, 3),
                      ),
                      goal=(
                          ('blank', 1, 1),
                          ('at', 1, 2, 1),
                          ('at', 2, 3, 1),
                          ('at', 3, 1, 2),
                          ('at', 4, 2, 2),
                          ('at', 5, 3, 2),
                          ('at', 6, 1, 3),
                          ('at', 7, 2, 3),
                          ('at', 8, 3, 3),
                      ))

    def to_coordinates(state):
        grid = {}
        for p in state:
            if p[0] == 'at':
                grid[p[1]] = (p[2], p[3])
        return grid

    goal_coords = to_coordinates(problem.goals)

    def manhattan_distance_heuristic(state):
        state_coords = to_coordinates(state.predicates)
        dist = 0
        for k in goal_coords.keys():
            c1, r1 = goal_coords[k]
            c2, r2 = state_coords[k]
            dist += (abs(c1 - c2) + abs(r1 - r2))
        return dist

    plan = planner(problem,
                   heuristic=manhattan_distance_heuristic,
                   verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
Example #12
0
def problem(verbose):
    domain = Domain((
        Action(
            'walk',
            parameters=(
                ('person', 'p'),
                ('location', 'l1'),
                ('location', 'l2'),
            ),
            preconditions=(('at', 'p', 'l1'), ),
            effects=(
                ('at', 'p', 'l2'),
                neg(('at', 'p', 'l1')),
            ),
        ),
        Action(
            'pick_from',
            parameters=(
                ('person', 'p'),
                ('object', 'o1'),
                ('object', 'o2'),
                ('location', 'l'),
            ),
            preconditions=(
                ('at', 'o1', 'l'),
                ('contains', 'o1', 'o2'),
                ('at', 'p', 'l'),
                ('found', 'p', 'o2'),
            ),
            effects=(
                ('has', 'p', 'o2'),
                neg(('contains', 'o1', 'o2')),
            ),
        ),
        Action(
            'look_n_found',
            parameters=(
                ('person', 'p'),
                ('object', 'o1'),
                ('object', 'o2'),
                ('location', 'l'),
            ),
            preconditions=(
                ('at', 'p', 'l'),
                ('at', 'o1', 'l'),
                ('contains', 'o1', 'o2'),
                ('>=', ('brave', 'p'), 10),
            ),
            effects=(('found', 'p', 'o2'), ),
        ),
        Action(
            'sing_for',
            parameters=(
                ('person', 'p1'),
                ('person', 'p2'),
                ('location', 'l'),
            ),
            preconditions=(
                ('at', 'p1', 'l'),
                ('at', 'p2', 'l'),
                ('>=', ('like', 'p1', 'p2'), 10),
                ('>=', ('brave', 'p1'), 10),
            ),
            effects=(
                ('+=', ('delighted', 'p2'), 10),
                ('+=', ('jealousy', 'Jack'), 5),
            ),
        ),
        Action(
            'propose',
            parameters=(
                ('person', 'p1'),
                ('person', 'p2'),
                ('location', 'l'),
            ),
            preconditions=(
                ('at', 'p1', 'l'),
                ('at', 'p2', 'l'),
                ('>=', ('like', 'p1', 'p2'), 10),
                ('>=', ('brave', 'p1'), 10),
            ),
            effects=(
                ('+=', ('like', 'p2', 'p1'), 5),
                ('+=', ('jealousy', 'Jack'), 5),
            ),
        ),
        Action(
            'steal',
            parameters=(
                ('person', 'p1'),
                ('person', 'p2'),
                ('object', 'o'),
                ('location', 'l'),
            ),
            preconditions=(
                ('at', 'p1', 'l'),
                ('at', 'p2', 'l'),
                ('has', 'p2', 'ring'),
                ('>=', ('jealousy', 'p1'), 10),
            ),
            effects=(
                neg(('has', 'p2', 'ring')),
                ('has', 'p1', 'ring'),
                ('+=', ('upset', 'p2'), 5),
            ),
        ),
        Action(
            'give_ring',
            parameters=(
                ('person', 'p1'),
                ('person', 'p2'),
                ('location', 'l'),
            ),
            preconditions=(
                ('has', 'p1', 'ring'),
                ('at', 'p1', 'l'),
                ('at', 'p2', 'l'),
                ('>=', ('like', 'p1', 'p2'), 10),
            ),
            effects=(
                ('wait_accept', 'p1', 'p2', 'l'),
                ('+=', ('happy', 'p2'), 5),
                ('+=', ('jealousy', 'Jack'), 5),
            ),
        ),
        Action(
            'accept_ring',
            parameters=(
                ('person', 'p1'),
                ('person', 'p2'),
                ('location', 'l'),
            ),
            preconditions=(
                ('wait_accept', 'p2', 'p1', 'l'),
                ('has', 'p2', 'ring'),
                ('at', 'p1', 'l'),
                ('at', 'p2', 'l'),
                ('>=', ('like', 'p1', 'p2'), 10),
                ('>=', ('happy', 'p1'), 10),
                ('>=', ('delighted', 'p1'), 10),
            ),
            effects=(
                neg(('has', 'p2', 'ring')),
                ('has', 'p1', 'ring'),
                ('+=', ('happy', 'p2'), 5),
                ('propose_accepted', 'p1', 'p2'),
                ('+=', ('jealousy', 'Jack'), 5),
            ),
        ),
        Action(
            'wisper',
            parameters=(
                ('person', 'p1'),
                ('person', 'p2'),
                ('location', 'l'),
            ),
            preconditions=(
                ('at', 'p1', 'l'),
                ('at', 'p2', 'l'),
                ('>=', ('like', 'p1', 'p2'), 10),
            ),
            effects=(('-=', ('upset', 'p2'), 5), ),
        ),
        Action(
            'accept_propose',
            parameters=(
                ('person', 'p1'),
                ('person', 'p2'),
                ('location', 'l'),
            ),
            preconditions=(
                ('at', 'p1', 'l'),
                ('at', 'p2', 'l'),
                ('>=', ('like', 'p1', 'p2'), 10),
                ('>=', ('happy', 'p1'), 10),
                ('>=', ('delighted', 'p1'), 10),
                ('<=', ('upset', 'p1'), 1),
                ('<=', ('jealousy', 'p1'), 1),
            ),
            effects=(
                ('propose_accepted', 'p1', 'p2'),
                ('+=', ('happy', 'p2'), 5),
                ('+=', ('desparate', 'Jack'), 10),
            ),
        ),
    ))
    problem = Problem(
        domain,
        {
            'location': ('loc1', 'loc2', 'loc3', 'loc4'),
            'person': ('Bob', 'Marry', 'Jack'),
            'object': ('ring', 'box', 'bench'),
        },
        init=(
            ('at', 'Bob', 'loc1'),
            ('at', 'Marry', 'loc2'),
            ('at', 'Jack', 'loc3'),
            ('at', 'bench', 'loc2'),
            ('at', 'box', 'loc4'),
            ('contains', 'box', 'ring'),
            ('sit_on', 'Marry', 'bench'),
            ('=', ('like', 'Marry', 'Bob'), 5),
            ('=', ('like', 'Marry', 'Jack'), 1),
            ('=', ('like', 'Bob', 'Marry'), 10),
            ('=', ('like', 'Bob', 'Jack'), 1),
            ('=', ('like', 'Jack', 'Marry'), 10),
            ('=', ('like', 'Jack', 'Bob'), 1),
            ('=', ('like', 'Bob', 'Bob'), -1),
            ('=', ('like', 'Marry', 'Marry'), -1),
            ('=', ('like', 'Jack', 'Jack'), -1),
            ('=', ('happy', 'Bob'), 5),
            ('=', ('happy', 'Marry'), 5),
            ('=', ('happy', 'Jack'), 5),
            ('=', ('brave', 'Bob'), 10),
            ('=', ('brave', 'Marry'), 1),
            ('=', ('brave', 'Jack'), 1),
            ('=', ('delighted', 'Bob'), 5),
            ('=', ('delighted', 'Marry'), 5),
            ('=', ('delighted', 'Jack'), 5),
            ('=', ('jealousy', 'Bob'), 1),
            ('=', ('jealousy', 'Marry'), 1),
            ('=', ('jealousy', 'Jack'), 5),
            ('=', ('desparate', 'Bob'), 1),
            ('=', ('desparate', 'Marry'), 1),
            ('=', ('desparate', 'Jack'), 1),
            ('=', ('upset', 'Bob'), 1),
            ('=', ('upset', 'Marry'), 1),
            ('=', ('upset', 'Jack'), 1),
        ),
        goal=(
            #            ('propose_accepted', 'Marry', 'Bob'),
            #            ('>=', ('desparate', 'Jack'), 5),
            #            ('>=', ('happy', 'Marry'), 10),
            #            ('>=', ('happy', 'Bob'), 10),
            #            ('has', 'Jack', 'ring'),
            ('has', 'Jack', 'ring'),
            ('>=', ('jealousy', 'Jack'), 10),
            ('>=', ('happy', 'Marry'), 10),
            ('>=', ('happy', 'Bob'), 10),
        ))

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
gripper_name = "R_gripper"

# put all objects into one dictionary with types
scene_obj = {type_block: block_names, type_gripper: (gripper_name, )}

# get simple action from all controllers
domain = Domain(actions)

# goal is for now numerical order of block placed on each other, unless specified otherwise
goal = [(b_on_b, scene_obj[type_block][i], scene_obj[type_block][i + 1])\
        for i in range(len(scene_obj[type_block]) - 1)]

# also append free hand
goal.append((hand_empty, scene_obj[type_gripper][0]))

print(goal)

# normal initial conditions
init_free_hand = (hand_empty, scene_obj[type_gripper][0])
init_free_blocks = [(block_free, block) for block in scene_obj[type_block]]

# extend all initial conditions
init = [init_free_hand]
init.extend(init_free_blocks)

# define problem here
prob = Problem(domain, scene_obj, init=init, goal=goal)

G = planner(prob, monotone=False)

visualize_search.draw_search_graph([], [], G, "simple_path.png")
Example #14
0
def problem(verbose):
    # problem domain
    domain = Domain((
        # Function to move between tiles.
        Action(
            "move",
            # P1 and P2 are the parameters the solver will tweak.
            parameters=(
                ("agent", "A"),
                ("position", "P1"),
                ("position", "P2"),
            ),
            # Agent(a), Adjacent(P1, P2), At(a, P1), Blank(P2)
            preconditions=(
                ("adjacent", "P1", "P2"),
                ("at", "A", "P1"),
                ("blank", "P2"),
            ),
            # At(a, P2), Blank(P1), not(At(a, P1)), not(Blank(P2))
            effects=(
                ("at", "A", "P2"),
                ("blank", "P1"),
                neg(("at", "A", "P1")),
                neg(("blank", "P2")),
            )), ))
    problem = Problem(
        domain,
        {
            # type          values
            "agent": ("a", "b"),
            "position": (1, 2, 3, 4, 5, 6, 7, 8, 9),
        },
        init=(
            # initial KB for our problem
            ("at", "a", 1),
            ("at", "b", 9),
            ("blank", 2),
            ("blank", 3),
            ("blank", 4),
            ("blank", 5),
            ("blank", 6),
            ("blank", 7),
            ("blank", 8),
            ("adjacent", 1, 2),
            ("adjacent", 1, 4),
            ("adjacent", 2, 1),
            ("adjacent", 2, 5),
            ("adjacent", 2, 3),
            ("adjacent", 3, 2),
            ("adjacent", 3, 6),
            ("adjacent", 4, 1),
            ("adjacent", 4, 5),
            ("adjacent", 4, 7),
            ("adjacent", 5, 2),
            ("adjacent", 5, 4),
            ("adjacent", 5, 6),
            ("adjacent", 5, 8),
            ("adjacent", 6, 3),
            ("adjacent", 6, 5),
            ("adjacent", 6, 9),
            ("adjacent", 7, 4),
            ("adjacent", 7, 8),
            ("adjacent", 8, 7),
            ("adjacent", 8, 5),
            ("adjacent", 8, 9),
            ("adjacent", 9, 6),
            ("adjacent", 9, 8),
        ),
        goal=(
            ("at", "a", 9),
            ("at", "b", 1),
        ))

    postable = {
        1: (1, 1),
        2: (1, 2),
        3: (1, 3),
        4: (2, 1),
        5: (2, 2),
        6: (2, 3),
        7: (3, 1),
        8: (3, 2),
        9: (3, 3)
    }

    def to_coordinates(state):
        poslist = {}
        for p in state:
            if p[0] == "at":
                poslist[p[1]] = postable[p[2]]
        return poslist

    goal_coords = to_coordinates(problem.goals)

    def manhattan_distance_heuristic(state):
        state_coords = to_coordinates(state.predicates)
        dist = 0
        for k in goal_coords.keys():
            c1, r1 = goal_coords[k]
            c2, r2 = state_coords[k]
            dist += (abs(c1 - c2) + abs(r1 - r2))
        return dist

    plan = planner(problem,
                   heuristic=manhattan_distance_heuristic,
                   verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
Example #15
0
            domain,
            {
                'position': tuple(positions),
            },
            init=tuple(init),
            goal=tuple(goal),
        )

        return problem


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Map file need to be specified!")
        print("Example: python3 " + sys.argv[0] + " world1.txt")
        sys.exit(1)
    w = world()
    w.load(sys.argv[1])
    problem = w.getProblem()
    plan = pyddl.planner(problem, verbose=True)
    if plan is None:
        print('Hunter is not able to solve this world!')
    else:
        actions = [action.name for action in plan]
        print(", ".join(actions))
        f = open(sys.argv[1] + ".solution", "w")
        f.write("\n".join(actions))
        f.close()
        input()
        simulator.simulate(sys.argv[1], sys.argv[1] + ".solution")
def problem(verbose):
    domain = Domain(
        (
            Action(
                "cross-right",
                preconditions=(("at", "left-bank"), (">", ("occupants",), 0)),
                effects=(neg(("at", "left-bank")), ("at", "right-bank")),
            ),
            Action(
                "cross-left",
                preconditions=(("at", "right-bank"), (">", ("occupants",), 0)),
                effects=(neg(("at", "right-bank")), ("at", "left-bank")),
            ),
            Action(
                "onboard-cannibal",
                parameters=(("location", "l"),),
                preconditions=(("at", "l"), (">", ("cannibals", "l"), 0), ("<", ("occupants",), 2)),
                effects=(("-=", ("cannibals", "l"), 1), ("+=", ("cannibals", "boat"), 1), ("+=", ("occupants",), 1)),
            ),
            Action(
                "onboard-missionary",
                parameters=(("location", "l"),),
                preconditions=(
                    ("at", "l"),
                    (">", ("missionaries", "l"), 0),
                    (">", ("missionaries", "l"), ("cannibals", "l")),
                    ("<", ("occupants",), 2),
                ),
                effects=(
                    ("-=", ("missionaries", "l"), 1),
                    ("+=", ("missionaries", "boat"), 1),
                    ("+=", ("occupants",), 1),
                ),
            ),
            Action(
                "offboard-cannibal",
                parameters=(("location", "l"),),
                preconditions=(
                    ("at", "l"),
                    (">", ("cannibals", "boat"), 0),
                    (">", ("missionaries", "l"), ("cannibals", "l")),
                ),
                effects=(("-=", ("cannibals", "boat"), 1), ("-=", ("occupants",), 1), ("+=", ("cannibals", "l"), 1)),
            ),
            Action(
                "offboard-missionary",
                parameters=(("location", "l"),),
                preconditions=(("at", "l"), (">", ("missionaries", "boat"), 0)),
                effects=(
                    ("-=", ("missionaries", "boat"), 1),
                    ("-=", ("occupants",), 1),
                    ("+=", ("missionaries", "l"), 1),
                ),
            ),
        )
    )
    problem = Problem(
        domain,
        {"location": ("left-bank", "right-bank")},
        init=(
            ("at", "left-bank"),
            ("=", ("missionaries", "boat"), 0),
            ("=", ("cannibals", "boat"), 0),
            ("=", ("occupants",), 0),
            ("=", ("missionaries", "left-bank"), 3),
            ("=", ("cannibals", "left-bank"), 3),
            ("=", ("missionaries", "right-bank"), 0),
            ("=", ("cannibals", "right-bank"), 0),
        ),
        goal=(("=", ("missionaries", "right-bank"), 3), ("=", ("cannibals", "right-bank"), 3)),
    )

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print("No Plan!")
    else:
        for action in plan:
            print(action)
def brain_surgery_heuristic(state):
    r = 0.0
    for p in state.predicates:
        if (p[0] == 'open'):
            r += 0.0  #0.1
        elif (p[0] == 'on_ice'):
            r += 0.4
        elif (p[0] == 'empty'):
            r += 0.4
    return r


# In[18]:

#tbs_plan = pyddl.planner(two_brain_swap, heuristic=brain_surgery_heuristic)
tbs_plan = pyddl.planner(two_brain_swap)

# In[19]:

for action in tbs_plan:
    print(action)

# In[20]:

g = Grammar({
    'open':
    '#body_id.possessive# cranium is open and the brain is exposed',
    'closed':
    '#body_id.possessive# cranium is either intact, or closed and repaired',
    'brain_in':
    '#brain_id.possessive# brain is in #body_id.possessive# head',
def problem(verbose):
    domain = Domain((
        Action(
            'oppoint',
            parameters=(
                ('location', 'loc'),
                
            ),
            preconditions=(
                ('energy', 'high'),
                ('emotion', 'bad'),
            ),
            effects=(
                ('at','loc'),
                ('emotion', 'good'),
                neg(('emotion', 'bad')),
            ),
        ),
        Action(
            'dating',
            parameters=(
                ('storelist', 'store'),
                ('decidelist', 'dec'),
            ),
            preconditions=(
                neg(('at', 'home')),
            ),
            effects=(
                ('at', 'store'),
                neg(('at','dec')),
            ),
        ),
        Action(
            'buy',
            parameters=(
                ('buylist', 'buy'),
                ('storelist','store'),
            ),
            preconditions=(
                ('at', 'store'),

            ),
            effects=(
                ('have','buy'),
            ),
        ),
        Action(
            'pack',
            parameters=(
                ('buylist', 'buy'),
            ),
            preconditions=(
                ('have','buy'),

            ),
            effects=(
                ('packed','buy'),
            ),
        ),
        Action(
            'give',
            parameters=(
                ('buylist', 'buy'),
            ),
            preconditions=(
                ('packed', 'buy'),

            ),
            effects=(
                ('loverget','present'),
            ),
        ),
    ))
    problem = Problem(
        domain,
        {
            'storelist': ('restaurant', 'bookstore'),
            'buylist': ('book', 'food'),
            'decidelist': ('outside', 'playground'),
            
        },
        init=(
            ('at', 'home'),
        ),
        goal=(
            ('loverget', 'present'),
        )
    )

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
Example #19
0
def problem(verbose):
    domain = Domain((
        Action(
            'askPhone',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'askPhoneNO'),
            ),
            effects=(('t', 'askPhoneNO'), ),
        ),
        Action(
            'start',
            preconditions=(('f', 'start'), ),
            effects=(('t', 'start'), ),
        ),
        Action(
            'askWant',
            preconditions=(
                ('t', 'start'),
                ('f', 'want'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'want'),
                ('t', 'want', 'short_invest'),
                ('t', 'want', 'invest'),
                ('t', 'want', 'insurance'),
                ('t', 'want', 'fund'),
                ('t', 'want', 'plan'),
            ),
        ),
        Action(
            'askTerm',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'need_term'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'need_term'),
                ('t', 'need_term', 'short'),
                ('t', 'need_term', 'long'),
            ),
        ),
        Action(
            'askMoney',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'need_money'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'need_money'),
                ('t', 'need_money', 'small'),
                ('t', 'need_money', 'large'),
            ),
        ),
        Action(
            'askRequirement',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'need_term'),
                ('t', 'need_money'),
                ('t', 'want', 'plan'),
                ('f', 'requirement'),
                ('f', 'close'),
            ),
            effects=(
                ('t', 'requirement'),
                ('t', 'requirement', 'house'),
                ('t', 'requirement', 'education'),
            ),
        ),
        Action(
            'suggestPlan',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'want', 'plan'),
                ('t', 'need_term', 'long'),
                ('t', 'need_money', 'large'),
                ('f', 'close'),
            ),
            effects=(('t', 'want', 'plan'), ),
        ),
        Action(
            'suggestPlanResult',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'want', 'plan'),
                ('t', 'need_term'),
                ('t', 'need_money'),
                ('t', 'requirement'),
                ('f', 'suggest'),
                ('f', 'close'),
            ),
            effects=(('t', 'suggest'), ),
        ),
        Action(
            'suggestShort',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'want', 'plan'),
                ('f', 'want', 'short_invest'),
                ('t', 'need_term', 'short'),
                ('f', 'close'),
            ),
            effects=(('t', 'want', 'short_invest'), ),
        ),
        Action(
            'executeShortInvest',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'want', 'short_invest'),
                ('t', 'need_term', 'short'),
                ('f', 'suggest'),
                ('f', 'close'),
            ),
            effects=(('t', 'suggest'), ),
        ),
        Action(
            'executeInvest',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'want', 'invest'),
                ('t', 'need_term', 'long'),
                ('t', 'need_money', 'small'),
                ('f', 'suggest'),
                ('f', 'close'),
            ),
            effects=(('t', 'suggest'), ),
        ),
        Action(
            'suggestOther',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('f', 'want', 'invest'),
                ('f', 'want', 'plan'),
                ('t', 'need_term', 'long'),
                ('t', 'need_money', 'small'),
                ('f', 'close'),
            ),
            effects=(('t', 'want', 'invest'), ),
        ),
        Action(
            'close',
            preconditions=(
                ('t', 'start'),
                ('t', 'want'),
                ('t', 'askPhoneNO'),
                ('f', 'close'),
            ),
            effects=(('t', 'close'), ),
        ),
    ))
    init = (
        ('t', 'start'),
        ('f', 'suggest'),
        ('f', 'close'),
        ('t', 'want'),
        ('f', 'want', 'fund'),
        ('f', 'want', 'insurance'),
        ('t', 'want', 'invest'),
        ('f', 'want', 'short_invest'),
        ('f', 'want', 'plan'),
        ('t', 'need_term'),
        ('f', 'need_term', 'short'),
        ('t', 'need_term', 'long'),
        ('t', 'need_money'),
        ('f', 'need_money', 'small'),
        ('t', 'need_money', 'large'),
        ('t', 'requirement'),
        ('t', 'requirement', 'house'),
        ('f', 'requirement', 'education'),
        ('t', 'replan'),
        ('f', 'askPhoneNO'),
    )

    problem = Problem(
        domain,
        {
            # 'brick': ('want', 'need_term', 'need_money', 'requirement'),
            # 'state': ('fund', 'insurance', 'invest', 'short_invest', 'plan', 'short', 'long', 'small', 'large', 'house', 'pension', 'education', 'marriage'),
            # 'product': ('short_small', 'short_large', 'long_small', 'long_large', 'insurance', 'fund1', 'fund2'),
        },
        init=init,
        goal=(
            # ('t', 'start'),
            ('t', 'close'), ))

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
Example #20
0
def find_a_path(hand_start_node_id,
                object_start_node_id,
                graph,
                goal_node_labels,
                verbose=False):
    locations = ['l' + str(i) for i in range(len(graph.nodes))]

    connections = [('connected', 'l' + str(e.node_id_start),
                    'l' + str(e.node_id_end)) for e in graph.edges]
    grasping_locations = [('is_grasping_location', 'l' + str(i))
                          for i, n in enumerate(graph.nodes)
                          if n.label in goal_node_labels or n.label + '_' +
                          str(i) in goal_node_labels]

    # define possible actions
    domain = pyddl.Domain((
        pyddl.Action(
            'move_hand',
            parameters=(
                ('location', 'from'),
                ('location', 'to'),
            ),
            preconditions=(
                ('hand_at', 'from'),
                ('connected', 'from', 'to'),
            ),
            effects=(
                pyddl.neg(('hand_at', 'from')),
                ('hand_at', 'to'),
            ),
        ),
        pyddl.Action(
            'move_object',
            parameters=(
                ('location', 'from'),
                ('location', 'to'),
            ),
            preconditions=(
                ('hand_at', 'from'),
                ('object_at', 'from'),
                ('connected', 'from', 'to'),
            ),
            effects=(
                pyddl.neg(('hand_at', 'from')),
                pyddl.neg(('object_at', 'from')),
                ('hand_at', 'to'),
                ('object_at', 'to'),
            ),
        ),
        pyddl.Action(
            'grasp_object',
            parameters=(('location', 'l'), ),
            preconditions=(('hand_at', 'l'), ('object_at', 'l'),
                           ('is_grasping_location', 'l')),
            effects=(('grasped', 'object'), ),
        ),
    ))

    # each node in the graph is a location
    problem = pyddl.Problem(domain, {
        'location': locations,
    },
                            init=[
                                ('hand_at', 'l' + str(hand_start_node_id)),
                                ('object_at', 'l' + str(object_start_node_id)),
                            ] + connections + grasping_locations,
                            goal=(('grasped', 'object'), ))

    plan = pyddl.planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)

    return plan
def problem(verbose):
    domain = Domain((
        Action(
            'cross-right',
            preconditions=(
                ('at', 'left-bank'),
                ('>', ('occupants', ), 0),
            ),
            effects=(
                neg(('at', 'left-bank')),
                ('at', 'right-bank'),
            ),
        ),
        Action(
            'cross-left',
            preconditions=(
                ('at', 'right-bank'),
                ('>', ('occupants', ), 0),
            ),
            effects=(
                neg(('at', 'right-bank')),
                ('at', 'left-bank'),
            ),
        ),
        Action(
            'onboard-cannibal',
            parameters=(('location', 'l'), ),
            preconditions=(
                ('at', 'l'),
                ('>', ('cannibals', 'l'), 0),
                ('<', ('occupants', ), 2),
            ),
            effects=(
                ('-=', ('cannibals', 'l'), 1),
                ('+=', ('cannibals', 'boat'), 1),
                ('+=', ('occupants', ), 1),
            ),
        ),
        Action(
            'onboard-missionary',
            parameters=(('location', 'l'), ),
            preconditions=(
                ('at', 'l'),
                ('>', ('missionaries', 'l'), 0),
                ('>', ('missionaries', 'l'), ('cannibals', 'l')),
                ('<', ('occupants', ), 2),
            ),
            effects=(
                ('-=', ('missionaries', 'l'), 1),
                ('+=', ('missionaries', 'boat'), 1),
                ('+=', ('occupants', ), 1),
            ),
        ),
        Action(
            'offboard-cannibal',
            parameters=(('location', 'l'), ),
            preconditions=(
                ('at', 'l'),
                ('>', ('cannibals', 'boat'), 0),
                ('>', ('missionaries', 'l'), ('cannibals', 'l')),
            ),
            effects=(
                ('-=', ('cannibals', 'boat'), 1),
                ('-=', ('occupants', ), 1),
                ('+=', ('cannibals', 'l'), 1),
            ),
        ),
        Action(
            'offboard-missionary',
            parameters=(('location', 'l'), ),
            preconditions=(
                ('at', 'l'),
                ('>', ('missionaries', 'boat'), 0),
            ),
            effects=(
                ('-=', ('missionaries', 'boat'), 1),
                ('-=', ('occupants', ), 1),
                ('+=', ('missionaries', 'l'), 1),
            ),
        ),
    ))
    problem = Problem(domain, {
        'location': ('left-bank', 'right-bank'),
    },
                      init=(
                          ('at', 'left-bank'),
                          ('=', ('missionaries', 'boat'), 0),
                          ('=', ('cannibals', 'boat'), 0),
                          ('=', ('occupants', ), 0),
                          ('=', ('missionaries', 'left-bank'), 3),
                          ('=', ('cannibals', 'left-bank'), 3),
                          ('=', ('missionaries', 'right-bank'), 0),
                          ('=', ('cannibals', 'right-bank'), 0),
                      ),
                      goal=(
                          ('=', ('missionaries', 'right-bank'), 3),
                          ('=', ('cannibals', 'right-bank'), 3),
                      ))

    plan = planner(problem, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)
def problem(verbose):
    domain = Domain((
        Action(
            'move',
            parameters=(
                ('position', 'wp_prec'),
                ('position', 'wp_effe'),
            ),
            preconditions=(
                ('connect', 'wp_prec', 'wp_effe'),
                ('base', 'wp_prec'),
                ('arm', 'unreach'),
            ),
            effects=(
                neg(('base', 'wp_prec')),
                ('base', 'wp_effe'),
            ),
        ),
        Action(
            'reach',
            parameters=(
                ('position', 'wp'),
                ('location', 'loc'),
            ),
            preconditions=(
                ('reachable', 'wp', 'loc'),
                ('base', 'wp'),
                ('arm', 'unreach'),
            ),
            effects=(
                neg(('arm', 'unreach')),
                ('arm', 'reach'),
            ),
        ),
        Action(
            'grip',
            parameters=(
                ('position', 'wp'),
                ('object', 'obj'),
                ('location', 'loc'),
            ),
            preconditions=(
                ('locate', 'obj', 'loc'),
                ('ungripped', 'obj'),
                ('reachable', 'wp', 'loc'),
                ('base', 'wp'),
                ('arm', 'reach'),
            ),
            effects=(
                neg(('ungripped', 'obj')),
                ('gripped', 'obj'),
            ),
        ),
        Action(
            'unreach',
            parameters=(),
            preconditions=(('arm', 'reach'), ),
            effects=(
                ('arm', 'unreach'),
                neg(('arm', 'reach')),
            ),
        ),
        Action(
            'reach-when-gripping',
            parameters=(
                ('position', 'wp'),
                ('location', 'loc'),
                ('object', 'obj'),
            ),
            preconditions=(
                ('reachable', 'wp', 'loc'),
                ('base', 'wp'),
                ('gripped', 'obj'),
                ('arm', 'unreach'),
            ),
            effects=(
                neg(('arm', 'unreach')),
                ('arm', 'reach'),
                ('locate', 'obj', 'loc'),
            ),
        ),
        Action(
            'ungrip',
            parameters=(('object', 'obj'), ),
            preconditions=(('gripped', 'obj'), ),
            effects=(
                neg(('gripped', 'obj')),
                ('ungripped', 'obj'),
            ),
        ),
    ))
    problem = Problem(
        domain,
        {
            'position': ('wp1', 'wp2', 'wp3', 'wp4'),
            'location': ('loc1', 'loc2', 'loc3'),
            'object': ('obj1', 'obj2'),
        },
        init=(
            ('connect', 'wp4', 'wp3'),
            ('connect', 'wp3', 'wp2'),
            ('connect', 'wp2', 'wp1'),
            ('connect', 'wp1', 'wp3'),
            ('reachable', 'wp1', 'loc1'),
            ('reachable', 'wp3', 'loc3'),
            ('base', 'wp4'),
            ('locate', 'obj1', 'loc1'),
            ('ungripped', 'obj1'),
            ('arm', 'unreach'),
            #('locate','obj2','loc2'),
            #('ungripped','obj2'),
        ),
        goal=(
            #('base','wp1'),
            #('gripped','obj1'),
            ('locate', 'obj1', 'loc3'),
            ('ungripped', 'obj1'),
        ))

    plan = planner(problem, verbose=verbose)
    #action_string = []

    rank = 1
    rank_return = rank
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            str_arr = StringArray()
            i = 1
            str_arr.strings.append(str(rank))
            for arg in action.sig:
                str_arr.strings.append(str(arg))

            print(str_arr)

            flag = 'unfinish'
            while (flag != 'finish' or rank_return != rank):  #finish and rank
                pub.publish(str_arr)
                #print(str_arr,i)
                i = i + 1
                #print(listening.action_state.strings,str_arr)

                if listening.action_state.strings:  #empty check
                    flag = listening.action_state.strings[1]
                    rank_return = int(listening.action_state.strings[0])
                    #while listening.action_state.strings[1] == 'finish':{} #may get previous 'finish', wait for a new unfinish
                if rospy.is_shutdown():
                    return 0

                #r.sleep()
            print('finish', i)
            rank = rank + 1
    return 0
def problem(verbose):
    with open("all_data.csv", "r") as f:
        data = f.readlines()
        data = [x.strip().strip(punctuation) for x in data]
    data = data[:40]
    medicines = []
    symptoms = []
    domain = Domain((Action(
        'take',
        parameters=(
            ('medicine', 'med'),
            ('symptom', 'sym1'),
            ('symptom', 'sym2'),
            ('symptom', 'sym3'),
        ),
        preconditions=(
            ('cures', 'med', 'sym1'),
            ('cures', 'med', 'sym2'),
            ('cures', 'med', 'sym3'),
            ('have_med', 'med'),
            ('has', 'sym1'),
            ('has', 'sym2'),
            ('has', 'sym3'),
        ),
        effects=(
            ('had', 'sym1'),
            ('had', 'sym2'),
            ('had', 'sym3'),
            neg(('have_med', 'med')),
        ),
    ), ))
    user_init = []
    user_goal = []
    user_symptoms = []

    for x in data:
        y = x.split(',')
        y = [z.strip(punctuation) for z in y]
        if len(y) < 2 or len(y) > 4:
            continue
        medicines.append(y[0])
        user_init.append(('have_med', str(y[0])))
        symptoms += y[1:]
        for sym in y[1:]:
            user_init.append(('cures', str(y[0]), str(sym)))

    with open("user_symptoms.txt", "r") as f:
        syms = f.readlines()
        syms = [s.strip().strip(punctuation) for s in syms]
        for sym in syms:
            user_init.append(('has', str(sym)))
            user_symptoms.append(str(sym))
            user_goal.append(('had', str(sym)))

    total_symptoms = len(user_init)
    user_symptoms = list(set(user_symptoms))

    problem = Problem(
        domain,
        {
            'medicine': list(set(medicines)),
            'symptom': list(set(symptoms)),
        },
        init=user_init,
        goal=user_goal,
    )

    def distance_heuristic(state):
        num_satisfied = len([
            p for p in state.predicates
            if p[0] == "had" and p[1] in user_symptoms
        ])
        return total_symptoms - num_satisfied

    plan = planner(problem, heuristic=distance_heuristic, verbose=verbose)
    if plan is None:
        print('No Plan!')
    else:
        for action in plan:
            print(action)