Skip to content

Commit

Permalink
added arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
sverrejb committed Mar 28, 2017
1 parent 037de38 commit 4b6a852
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
11 changes: 8 additions & 3 deletions EA/fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ def build_cmd(candidate_name, opponent_name, nr_matches):
'-n', nr_matches, '-f', 'sealed']


def evaluate_deck_by_wins(individual):
number_of_matches = len(ct.OPPONENTS) * int(ct.MATCHES_PER_OPPONENT)
def evaluate_deck_by_wins(data):

individual = data[0]
matches_per_opponent = data[1]

number_of_matches = len(ct.OPPONENTS) * int(matches_per_opponent)

decklist = genome_to_decklist(individual)
filename = "candidate.dck"
write_decklist(ct.CARD_DIRECTORY + filename, decklist)
wins = 0
for opponent in ct.OPPONENTS:
cmd = build_cmd(filename, opponent, ct.MATCHES_PER_OPPONENT)
cmd = build_cmd(filename, opponent, matches_per_opponent)
p = subprocess.Popen(cmd, cwd=ct.FORGE_PATH, stdout=subprocess.PIPE)
for line in p.stdout:
line = line.decode("utf-8").strip()
Expand Down
38 changes: 34 additions & 4 deletions EA/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argparse
import copy
import os
import time
import sys
from random import randint
from statistics import median

Expand Down Expand Up @@ -68,9 +70,30 @@ def mate_individuals(ind1, ind2):
return ind1, ind2


def parse_arguments():
ap = argparse.ArgumentParser()
ap.add_argument('-g', '--gens', type=str, help='number of generations')
ap.add_argument('-m', '--matches', type=float, help='matches per opponent')
args = vars(ap.parse_args())

if args['gens'] is None:
print('Number of generations not specified with -g, using default of 200')
else:
ct.NUMBER_OF_GENERATIONS = int(args['gens'])
print(ct.NUMBER_OF_GENERATIONS)

if args['matches'] is None:
print("Number of matches per opponent not specified with -m, using default of 50")
else:
ct.MATCHES_PER_OPPONENT = str(int(args['matches']))
print(ct.MATCHES_PER_OPPONENT)


def main():
# TODO: VELG BREEDING OG MUTASJONSSTRATEGI

parse_arguments()
number_of_matches = int(ct.MATCHES_PER_OPPONENT) * len(ct.OPPONENTS) * ct.NUMBER_OF_GENERATIONS * ct.POPSIZE

print('Starting experiment {}'.format(ct.EXPERIMENT_TIMESTAMP))
print('Doing {} matches'.format(number_of_matches))

Expand All @@ -97,8 +120,10 @@ def main():

for gen in range(ct.NUMBER_OF_GENERATIONS):
offspring = algorithms.varAnd(population, toolbox, cxpb=ct.CROSSOVER_RATE, mutpb=ct.MUTATION_RATE)
fits = list(futures.map(toolbox.evaluate, offspring))
print("Generation {}, {}".format(gen, fits))

offspring_and_matches = [(x, ct.MATCHES_PER_OPPONENT) for x in offspring]

fits = list(futures.map(toolbox.evaluate, offspring_and_matches))

for fit, ind in zip(fits, offspring):
ind.fitness.values = fit
Expand All @@ -109,11 +134,17 @@ def main():
for i, solution in enumerate(population):
write_decklist(card_location + "/" + str(i) + '.dck', genome_to_decklist(solution))
fitness_list = [x[0] for x in fits]

maximum = max(fitness_list)

strongest_individual = tools.selBest(population, k=1)

median_score = median(fitness_list)

minimum = min(fitness_list)

print("Generation {}, max: {} median: {} min: ".format(gen, median_score, median_score, minimum))

if median_score > best_median:
best_median = median_score
last_improvement = gen
Expand Down Expand Up @@ -148,4 +179,3 @@ def main():
except Exception as e:
print("Unexpected error:\n{}".format(e))
send_mail(['[email protected]', '[email protected]'], "The program has crashed:\n{}".format(e))

0 comments on commit 4b6a852

Please sign in to comment.