コード例 #1
0
def choose_goals(data,
                 agents,
                 quests_per_agent=3,
                 attempts_per_agent=12,
                 verbose=True):
    """ Chooses goals based on preferences, by creating goals stochastically and
    rating them according to action costs """

    #data = "world/bge/"
    domain = "domain.pddl"

    for _ in range(attempts_per_agent):
        if attempts_per_agent < 2:
            goals[:] = []
        random_goals(agents)
    if attempts_per_agent < 2:
        return

    good_goals = []

    scores = []

    for agent in agents:

        all_plans = []

        while len(all_plans) < attempts_per_agent:
            #print(goals[0])
            #agent = agents[0]
            create(data, [agent])
            if verbose:
                print("Wait")
            #time.sleep(2)

            calculating = [questPlanning.plan_quest(agent)]
            too_long = 180  # 3 minutes
            thinking_time = time.perf_counter()
            thinking_timelast = thinking_time
            while not finished_thinking(calculating):
                time.sleep(0.5)
                thinking_timelast += 0.5
                if thinking_time + too_long < thinking_timelast:
                    calculating[0].kill()
                    print("Took too long!")
                    break

            translation, quest, NPCNames = questTranslation.interpret(data)
            if quest == []:
                quest = ['Cluster']
            score = rate_plan(quest)
            if verbose:
                print(quest)
                print(score)
            all_plans.append((score, goals[0], quest))
            goals.pop(0)
            # Maybe make it choose the longest quest in case of equal scores?
            if len(all_plans) == attempts_per_agent:
                all_plans = sorted(all_plans)
                good_goals.append(all_plans[0][1])
                scores.append(all_plans[0][0])
                if verbose:
                    print(good_goals[-1], all_plans[0][0])

    if verbose:
        print("Should be empty now:")
        print(goals)
    goals.extend(good_goals)
    if verbose:
        print("---------------------------------")
        print("Quests chosen by the agents :")
        print(goals)
        print(scores)
def main():
    data = "world"

    domain = "domain.pddl"

    #agents = sorted(["baker", "king", "lumberjack", "blacksmith", "merchant", "guard", "daughter"])
    agents = sorted(["Jafar", "Aladdin", "Jasmine", "Genie", "Dragon"])

    motivation_count = dict()
    motivations = [
        "Knowledge", "Comfort", "Reputation", "Serenity", "Protection",
        "Conquest", "Wealth", "Ability", "Equipment"
    ]
    for motiv in motivations:
        motivation_count[motiv] = 0
    motivation_count["Not found"] = 0

    print(motivation_count)

    emptyOrComplete = 0

    quest_log = open("quest_log.txt", "w")

    too_long = 600  # 10 minutes
    run = 1000

    while run > 0:

        write_domains(data, domain, agents,
                      worldManagementAladdin2.preferences)

        worldManagementAladdin2.create(data,
                                       agents,
                                       attempts_per_agent=4,
                                       genesis=True,
                                       verbose=False)

        calculating = []
        opened_files = []

        for agent in agents:
            calculating.append(questPlanning.plan_quest(agent))
            #opened_files.append(open(os.path.join(data,agent+".soln"),"w"))
            #calculating.append(subprocess.Popen([os.path.join("metricff","Metric-FF-v2.1","ff"), '-o', os.path.join(data,"domain"+agent+".pddl"), '-f', os.path.join(data,agent+".pddl"), '-s', '3'],stdout=opened_files[-1]))

        thinking_time = time.clock()
        thinking_timelast = thinking_time
        print('Thinking')
        while not finished_thinking(calculating):
            time.sleep(0.5)
            print(".", end=".")
            sys.stdout.flush()
            thinking_timelast += 0.5
            #print(thinking_timelast)
            if thinking_time + too_long < thinking_timelast:
                print(thinking_time)
                print("Took too long!")
                for stillcalculating in calculating:
                    if stillcalculating.poll() == None:
                        stillcalculating.kill()
                break

        for opened_file in opened_files:
            opened_file.close()

        print("Done")
        translations, formalplans = questTranslation.interpret(data)

        #print(formalplans)

        motivations = [
            questClassification.classify_fuzzy(formalplan)
            for formalplan in formalplans
        ]
        for formalplan, motivation in zip(formalplans, motivations):
            #print(translation,motivation)
            if len(formalplan) < 1:
                emptyOrComplete += 1
            else:
                print(" ".join(formalplan) + " " + motivation[0])
                quest_log.write(" ".join(formalplan))
                quest_log.write(" m:" + motivation[0])
                quest_log.write("\n\n")
            motivation_count[motivation[0]] += 1
        """
        for formalplan in formalplans:
            #print(formalplan)
            if len(formalplan) < 1:
                emptyOrComplete += 1
            else:
                print(" ".join(formalplan))
                quest_log.write(" ".join(formalplan))
                quest_log.write("\n\n")
        """

        print(motivation_count)
        print(emptyOrComplete)
        run -= 1
    quest_log.close()