예제 #1
0
def hearthstone_tester1():
    createConfig("experiments", "best_deck", "random", 1)
    opponents = 50
    population = init(opponents, "Mage")
    print(population[0])
    wins = 0
    for opponent in population:
        pytohs("random", opponent.get_class(), opponent.get_deck())
        system("gradlew runSim")  #run simulation
        #if getResults("config")["P0"] == 1:
        #    wins += 1
        fp = open("experiments/experiments.hsres")
        text = fp.read()
        # print("run")
        while text.find('winner":') == -1:
            system(
                "gradlew runSim --configure-on-demand --no-rebuild --offline")
            fp = open("experiments/experiments.hsres")
            text = fp.read()
        if text[text.find('winner":') + 8] == "0":
            wins += 1
    fp.close()

    fit = wins / opponents
    print(fit)
예제 #2
0
def hearthstone_GA_single_class():
    createConfig("experiments", "protagonist", "antagonist", 1)
    pop_size = 6

    population = init(pop_size, "Mage")
    print(population[0])
    fitness_all(population)  #give each member of the population a fitness

    # population = initialization(pop_size)
    # best_fitness = 0
    # best_model = None
    # fitness_evals = 0

    generation = 0
    #fitnesses = []
    #each iteration of this loop is a generation
    while generation < 10:
        print(generation)
        # save(population, generation)
        generation += 1
        population = selection(population)

        #mutate population
        for element in population:
            if random.random() < 0.8:
                mutate(element)
    population.sort(key=attrgetter('fitness'), reverse=True)
    print(population[0])

    print("Generation:", generation)
예제 #3
0
def hearthstone_tester2():
    createConfig("experiments", "best_deck", "random", 1)

    #    population.append(init(pop_size, "Warlock"))
    #    population.append(init(pop_size, "Mage"))
    #    population.append(init(pop_size, "Druid"))
    #    population.append(init(pop_size, "Hunter"))
    #    population.append(init(pop_size, "Paladin"))
    #    population.append(init(pop_size, "Priest"))
    #    population.append(init(pop_size, "Rogue"))
    #    population.append(init(pop_size, "Shaman"))
    #    population.append(init(pop_size, "Warrior"))

    opponents = 40
    types = [
        "Warlock", "Druid", "Hunter", "Paladin", "Priest", "Rogue", "Shaman",
        "Warrior"
    ]  #8 types
    population = []
    for my_type in types:
        sub_pop = init(5, my_type)
        for model in sub_pop:
            population.append(model)
    print(population[0])
    wins = 0
    for opponent in population:
        pytohs("random", opponent.get_class(), opponent.get_deck())
        system("gradlew runSim")  #run simulation
        #if getResults("config")["P0"] == 1:
        #    wins += 1
        fp = open("experiments/experiments.hsres")
        text = fp.read()
        # print("run")
        while text.find('winner":') == -1:
            system(
                "gradlew runSim --configure-on-demand --no-rebuild --offline")
            fp = open("experiments/experiments.hsres")
            text = fp.read()
        if text[text.find('winner":') + 8] == "0":
            wins += 1
    fp.close()

    fit = wins / opponents
    print(fit)
예제 #4
0
def hearthstone_GA():
    createConfig("experiments", "protagonist", "antagonist", 5)
    pop_size = 10
    population = init(pop_size, "Mage")

    generation = 0
    print("Generation: " + str(generation))

    calculateFitness(
        population)  # give each member of the population a fitness
    print(str(population[0]))
    # population = initialization(pop_size)
    # best_fitness = 0
    # best_model = None
    # fitness_evals = 0

    fitnesses = []
    # each iteration of this loop is a generation
    while generation < 10:
        print("Generation: " + str(generation))
        # save(population, generation)
        generation += 1
        # fitness_evals += 100#each generation performs 100 fitness evaluations

        # cross over population
        population = crossoverPopulation(population)

        # mutate population
        for element in population:
            if random.random() < 0.8:
                mutate(element)

    population.sort(key=attrgetter('fitness'), reverse=True)
    bestIndividual = population[0]
    pytohs(bestIndividual.get_class(), bestIndividual.get_class(),
           bestIndividual.get_deck())

    print(
        str(bestIndividual.get_class()) +
        "'s best individual has a fitness of: " +
        str(bestIndividual.get_fitness()))

    print("Generation:", generation)
예제 #5
0
def hearthstone_GA():
    createConfig("experiments", "protagonist", "antagonist", 1)
    pop_size = 5

    # warlordPop = init(pop_size, "warlord")
    # magePop = init(pop_size, "mage")
    # druidPop = init(pop_size, "druid")
    # hunterPop = init(pop_size, "hunter")
    # paladinPop = init(pop_size, "paladin")
    # priestPop = init(pop_size, "priest")
    # roguePop = init(pop_size, "rogue")
    # shamanPop = init(pop_size, "shaman")
    # warriorPop = init(pop_size, "warrior")
    #
    # population = {
    #     "warlord": warlordPop,
    #     "druid": druidPop,
    #     "mage": magePop,
    #     "hunter": hunterPop,
    #     "paladin": paladinPop,
    #     "priest": priestPop,
    #     "rogue": roguePop,
    #     "shaman": shamanPop,
    #     "warrior": warriorPop
    # }
    population = []
    population.append(init(pop_size, "Warlock"))
    population.append(init(pop_size, "Mage"))
    population.append(init(pop_size, "Druid"))
    population.append(init(pop_size, "Hunter"))
    population.append(init(pop_size, "Paladin"))
    population.append(init(pop_size, "Priest"))
    population.append(init(pop_size, "Rogue"))
    population.append(init(pop_size, "Shaman"))
    population.append(init(pop_size, "Warrior"))

    # warlordPop = init(pop_size, "warlord")
    # magePop = init(pop_size, "mage")
    # druidPop = init(pop_size, "druid")
    # hunterPop = init(pop_size, "hunter")
    # paladinPop = init(pop_size, "paladin")
    # priestPop = init(pop_size, "priest")
    # roguePop = init(pop_size, "rogue")
    # shamanPop = init(pop_size, "shaman")
    # warriorPop = init(pop_size, "warrior")

    calculateFitness(population)  #give each member of the population a fitness
    print(str(population[0][0]))
    # population = initialization(pop_size)
    # best_fitness = 0
    # best_model = None
    # fitness_evals = 0

    generation = 0
    fitnesses = []
    #each iteration of this loop is a generation
    while generation < 10:
        print(generation)
        # save(population, generation)
        generation += 1
        # fitness_evals += 100#each generation performs 100 fitness evaluations

        #cross over population
        population = crossoverPopulation(population)

        #mutate population
        for subPopulation in population:
            for element in subPopulation:
                if random.random() < 0.8:
                    mutate(element)

        #
        # index = 0
        # while index < pop_size:
        #     model = population[index]
        #     roll = random.random()
        #     #80% chance of mutation for each member of the population
        #     if roll < 0.80:
        #         mutate(model)
        #     index += 1
        #evaluate and find best model
        """
        models_fitness = []
        for model in population:
            models_fitness.append((model,fitness(model)))
        models_fitness.sort(key=itemgetter(1), reverse=True)
        fitnesses.append(models_fitness[0][1])
        if models_fitness[0][1] > best_fitness:
            best_fitness = models_fitness[0][1]
            best_model = models_fitness[0][0]"""
    #after termination condition, display best model, best fitness of said model, how many generations ran, and how many fitness evaluations
    # print(best_model)
    # print(best_fitness)

    for subPopulation in population:
        subPopulation.sort(key=attrgetter('fitness'), reverse=True)
        bestIndividual = subPopulation[0]

        for element in subPopulation:
            print(str(element.get_deck()) + str(element.get_fitness()))
        print(str(bestIndividual))
        pytohs(
            bestIndividual.get_class(), bestIndividual.get_class(),
            bestIndividual.get_deck()
        )  #outputs the best performing individual to heroclass.hsdeck to analyse
        print(
            str(bestIndividual.get_class()) +
            "'s best individual has a fitness of: " +
            str(bestIndividual.get_fitness()))

    print("Generation:", generation)