Skip to content

Commit

Permalink
Merge pull request #2 from sverrejb/argparser
Browse files Browse the repository at this point in the history
Argparser
  • Loading branch information
Sverre Johann Bjørke authored Apr 5, 2017
2 parents 5e7b367 + 4b6a852 commit c5f0862
Show file tree
Hide file tree
Showing 42 changed files with 1,210 additions and 12 deletions.
7 changes: 7 additions & 0 deletions EA/BGw-sealed-opponent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This was BGw-sealed-opponent.dckplaying versus:
GB-sealed-opponent.dck: 0
UWg-sealed-opponent.dck: 1
RG-sealed-opponent.dck: 0
BGw-sealed-opponent.dck: 0
UR-sealed-opponent.dck: 0
RW-sealed-opponent.dck: 1
7 changes: 7 additions & 0 deletions EA/GB-sealed-opponent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This was GB-sealed-opponent.dckplaying versus:
GB-sealed-opponent.dck: 0
UWg-sealed-opponent.dck: 0
RG-sealed-opponent.dck: 0
BGw-sealed-opponent.dck: 1
UR-sealed-opponent.dck: 2
RW-sealed-opponent.dck: 0
7 changes: 7 additions & 0 deletions EA/RG-sealed-opponent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This was RG-sealed-opponent.dckplaying versus:
GB-sealed-opponent.dck: 2
UWg-sealed-opponent.dck: 2
RG-sealed-opponent.dck: 0
BGw-sealed-opponent.dck: 1
UR-sealed-opponent.dck: 0
RW-sealed-opponent.dck: 1
6 changes: 6 additions & 0 deletions EA/RG-sealed-opponent.txtBGw-sealed-opponent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This was RG-sealed-opponent.dckBGw-sealed-opponent.dckplaying versus:
GB-sealed-opponent.dck: 0
UWg-sealed-opponent.dck: 0
RG-sealed-opponent.dckBGw-sealed-opponent.dck: 0
UR-sealed-opponent.dck: 0
RW-sealed-opponent.dck: 0
7 changes: 7 additions & 0 deletions EA/RW-sealed-opponent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This was RW-sealed-opponent.dckplaying versus:
GB-sealed-opponent.dck: 2
UWg-sealed-opponent.dck: 1
RG-sealed-opponent.dck: 0
BGw-sealed-opponent.dck: 2
UR-sealed-opponent.dck: 2
RW-sealed-opponent.dck: 1
7 changes: 7 additions & 0 deletions EA/UR-sealed-opponent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This was UR-sealed-opponent.dckplaying versus:
GB-sealed-opponent.dck: 1
UWg-sealed-opponent.dck: 1
RG-sealed-opponent.dck: 2
BGw-sealed-opponent.dck: 2
UR-sealed-opponent.dck: 2
RW-sealed-opponent.dck: 1
7 changes: 7 additions & 0 deletions EA/UWg-sealed-opponent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This was UWg-sealed-opponent.dckplaying versus:
GB-sealed-opponent.dck: 1
UWg-sealed-opponent.dck: 1
RG-sealed-opponent.dck: 1
BGw-sealed-opponent.dck: 2
UR-sealed-opponent.dck: 2
RW-sealed-opponent.dck: 2
10 changes: 5 additions & 5 deletions EA/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

POPSIZE = 10
DECKSIZE = 40
CROSSOVER_RATE = 0.1
CROSSOVER_RATE = 0.5
MUTATION_RATE = 0.2
NUMBER_OF_GENERATIONS = 500
MATCHES_PER_OPPONENT = '1' # must be string!
NUMBER_OF_GENERATIONS = 200
MATCHES_PER_OPPONENT = '50' # must be string!
CARD_POOL = read_card_pool('../AER-POOL-1.txt')
CARD_POOL_SIZE = len(CARD_POOL)
CARD_DIRECTORY = config.CARD_DIR
FORGE_PATH = config.FORGE_DIR
DECKLIST_HEADER = '[metadata]\nName=candidate\n[Main]\n'
# OPPONENTS = ["GB-sealed-opponent.dck"]
OPPONENTS = ["GB-sealed-opponent.dck", "UWg-sealed-opponent.dck", "UW-sealed-opponent.dck", "BGw-sealed-opponent.dck"]
OPPONENTS = ["GB-sealed-opponent.dck", "UWg-sealed-opponent.dck", "RG-sealed-opponent1.dck",
"BGw-sealed-opponent.dck", "UR-sealed-opponent.dck", "RW-sealed-opponent.dck"]
EXPERIMENT_TIMESTAMP = datetime.datetime.now().strftime("%d%m%H%M")
EXPERIMENT_FOLDER = "results/" + EXPERIMENT_TIMESTAMP
TERMINATION_TRESHOLD = 65
Expand Down
9 changes: 6 additions & 3 deletions EA/fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ def build_cmd(candidate_name, opponent_name, nr_matches):
'-n', nr_matches, '-f', 'sealed']


def evaluate_deck_by_wins(individual):
def evaluate_deck_by_wins(data):

number_of_matches = len(ct.OPPONENTS) * ct.MATCHES_PER_OPPONENT
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
1 change: 1 addition & 0 deletions EA/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from email.utils import COMMASPACE, formatdate



def send_mail(send_to, text, files=[], isTls=True):
print('Sending mail')
msg = MIMEMultipart()
Expand Down
39 changes: 35 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,11 @@ 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
population = toolbox.select(offspring, k=len(population))
Expand All @@ -108,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 @@ -147,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))

45 changes: 45 additions & 0 deletions EA/test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import subprocess

def build_cmd(candidate_name, opponent_name, nr_matches):
return ['java', '-Xmx1024m', '-jar',
'forge-gui-desktop-1.5.61-SNAPSHOT-jar-with-dependencies.jar', 'sim',
'-d', candidate_name, opponent_name,
'-n', nr_matches, '-f', 'sealed']



#total_damage = 0
wins = 0
OPPONENTS = ["GB-sealed-opponent.dck", "UWg-sealed-opponent.dck", "RG-sealed-opponent.dck",
"BGw-sealed-opponent.dck", "UR-sealed-opponent.dck", "RW-sealed-opponent.dck"]

for challenger in OPPONENTS:
print(challenger)
with open("results\\tournament\\" + challenger.replace(".dck", ".txt"),"w") as file:
file.write("This was " + challenger + "playing versus:\n\n")
for opponent in OPPONENTS:
print(opponent)
wins = 0
for i in range(25):
print(i)
cmd = build_cmd(challenger, opponent, str(50))
p = subprocess.Popen(cmd, cwd="C:\\Users\\Public\\IC\\forge-gui-desktop-1.5.60", stdout=subprocess.PIPE)
for line in p.stdout:
line = line.decode("utf-8").strip()
#if challenger=="RG-sealed-opponent.dck":
# print(line)
# if 'combat damage to Ai(2' in line:
# hit_event = line.split(' ')
# # print(hit_event) #For debugging
# damage_index = hit_event.index('deals') + 1
# damage = int(hit_event[damage_index])
# total_damage += damage
if 'Match result' in line:
result = line.split(' ')
wins += int(result[4])
p.wait()

with open("results\\tournament\\" + challenger.replace(".dck", ".txt"), "a") as file:
file.write(opponent + ": {}\n \n".format(wins))


37 changes: 37 additions & 0 deletions EA/tournament.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import subprocess

import constants as ct
from decks import genome_to_decklist, write_decklist

def build_cmd(candidate_name, opponent_name, nr_matches):
return ['java', '-Xmx1024m', '-jar',
'forge-gui-desktop-1.5.61-SNAPSHOT-jar-with-dependencies.jar', 'sim',
'-d', candidate_name, opponent_name,
'-n', nr_matches, '-f', 'sealed']



total_damage = 0
wins = 0
# colors,lands = colorsymbols_in_deck(CARDS, decklist)

for challenger in ct.OPPONENTS:
total_wins=0
for i in range(20):
for opponent in ct.OPPONENTS:
cmd = build_cmd(challenger, opponent, ct.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()
#print(line)
# if 'combat damage to Ai(2' in line:
# hit_event = line.split(' ')
# # print(hit_event) #For debugging
# damage_index = hit_event.index('deals') + 1
# damage = int(hit_event[damage_index])
# total_damage += damage
if 'Match result' in line:
result = line.split(' ')
wins += int(result[3])
p.wait()
total_wins+=wins
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions decks/RG-sealed-opponent.dck
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[metadata]
Name=RG aggro 2
[Main]
8 Mountain
1 Sky Skiff
1 Harnessed Lightning
2 Spireside Infiltrator
1 Chandra's Pyrohelix
1 Highspire Artisan
1 Wayward Giant
1 Blossoming Defense
1 Wild Wanderer
1 Whirlermaker
8 Forest
2 Welding Sparks
1 Fairgrounds Trumpeter
1 Fateful Showdown
1 Elegant Edgecrafters
1 Brazen Scourge
1 Attune with Aether
1 Thriving Rhino
1 Cultivator of Blades
1 Fleetwheel Cruiser
1 Oviya Pashiri, Sage Lifecrafter
2 Voltaic Brawler
1 Renegade Freighter
25 changes: 25 additions & 0 deletions decks/RW-sealed-opponent.dck
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[metadata]
Name=RW aggro
[Main]
1 Bomat Bazaar Barge
2 Brazen Scourge
1 Chief of the Foundry
1 Combustible Gearhulk
1 Consul's Shieldguard
1 Fleetwheel Cruiser
1 Fragmentize
3 Gearshift Ace
1 Glint-Sleeve Artisan
1 Inventor's Apprentice
9 Mountain
8 Plains
1 Propeller Pioneer
1 Renegade Freighter
1 Revoke Privileges
1 Scrapheap Scrounger
1 Sky Skiff
1 Skyship Stalker
1 Thriving Grubs
1 Veteran Motorist
1 Visionary Augmenter
1 Weldfast Monitor
71 changes: 71 additions & 0 deletions decks/Sveinung_2_RG.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
8 Mountain
1 Sky Skiff
1 Harnessed Lightning
2 Spireside Infiltrator
1 Chandra's Pyrohelix
1 Highspire Artisan
1 Wayward Giant
1 Blossoming Defense
1 Wild Wanderer
1 Whirlermaker
8 Forest
2 Welding Sparks
1 Fairgrounds Trumpeter
1 Fateful Showdown
1 Elegant Edgecrafters
1 Brazen Scourge
1 Attune with Aether
1 Thriving Rhino
1 Cultivator of Blades
1 Fleetwheel Cruiser
1 Oviya Pashiri, Sage Lifecrafter
2 Voltaic Brawler
1 Renegade Freighter

1 Inspired Charge
1 Refurbish
1 Propeller Pioneer
1 Skywhaler's Shot
1 Herald of the Fair
1 Acrobatic Maneuver
1 Ninth Bridge Patrol
1 Eddytrail Hawk
3 Aviary Mechanic
1 Tasseled Dromedary
1 Failed Inspection
1 Weldfast Wingsmith
2 Disappearing Act
2 Dramatic Reversal
1 Curio Vendor
1 Minister of Inquiries
1 Demon of Dark Schemes
1 Prakhata Club Security
2 Maulfist Squad
1 Aetherborn Marauder
1 Essence Extraction
1 Mind Rot
1 Make Obsolete
1 Live Fast
2 Lawless Broker
1 Foundry Screecher
2 Fortuitous Find
1 Syndicate Trafficker
1 Subtle Strike
2 Rush of Vitality
1 Die Young
1 Harsh Scrutiny
1 Salivating Gremlins
1 Spark of Creativity
1 Renegade Tactics
2 Cowl Prowler
2 Larger Than Life
1 Take Down
2 Ornamental Courage
1 Aradara Express
1 Dynavolt Tower
1 Woodweaver's Puzzleknot
3 Torch Gauntlet
1 Sky Skiff
1 Metalspinner's Puzzleknot
1 Fireforger's Puzzleknot
1 Sequestered Stash
Loading

0 comments on commit c5f0862

Please sign in to comment.