From 21193bba6fa5073a4ea6d49755ef9ccb8d51617b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 14 Nov 2017 22:53:54 +0000 Subject: Add benchmark python script --- benchmark.py | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 benchmark.py diff --git a/benchmark.py b/benchmark.py new file mode 100644 index 0000000..28c6b4b --- /dev/null +++ b/benchmark.py @@ -0,0 +1,117 @@ +import json +import math +import os +import sys +from subprocess import run, TimeoutExpired, CalledProcessError, DEVNULL +from abc import ABCMeta, abstractmethod + +tempRulesFilename = "temp_rules.json" +timeoutsLimit = 4 +timeoutSeconds = 1 + +class SolitaireRulesGen(metaclass=ABCMeta): + + def __init__(self, name): + self.name = name + self.baseJson = json.load(open('resources/' + name + '.json')) + + def genRules(self, level): + self.alterFieldsToChange(level) + with open(tempRulesFilename, "w") as tempOut: + json.dump(self.baseJson, tempOut) + + @abstractmethod + def alterFieldsToChange(self, level): + pass + + def __str__(self): + return json.dumps(self.baseJson, indent=4) + +class BlackHoleRulesGen(SolitaireRulesGen): + + def __init__(self): + super().__init__("black-hole") + + def alterFieldsToChange(self, level): + self.baseJson["max rank"] = level + self.baseJson["tableau piles"]["count"] = math.ceil((4*level - 1)/3) + +class SpanishPatienceRulesGen(SolitaireRulesGen): + + def __init__(self): + super().__init__("spanish-patience") + + def alterFieldsToChange(self, level): + self.baseJson["max rank"] = level + self.baseJson["tableau piles"]["count"] = level + +class FreeCellRulesGen(SolitaireRulesGen): + + def __init__(self): + super().__init__("free-cell") + + def alterFieldsToChange(self, level): + self.baseJson["max rank"] = level + self.baseJson["tableau piles"]["count"] = math.ceil(level * 0.61) + self.baseJson["cells"] = math.ceil(level * 0.3) + +class CanfieldRulesGen(SolitaireRulesGen): + + def __init__(self): + super().__init__("canfield") + + def alterFieldsToChange(self, level): + self.baseJson["max rank"] = level + cardsNotInTableau = (level - 1) * 4 + reserveSize = math.ceil(cardsNotInTableau * 0.7) + stockSize = (level - 1) * 4 - reserveSize + self.baseJson["reserve size"] = reserveSize + self.baseJson["stock size"] = stockSize + +# Loops through each canonical solitaire +for rulesGen in [BlackHoleRulesGen(), SpanishPatienceRulesGen(), + FreeCellRulesGen(), CanfieldRulesGen()]: + + # Loops through the levels + for level in range(1, 13): + + # Calls the solitaire rules generator function with the level + rulesGen.genRules(level) + + # Runs the solver with the rules and 20 random seeds. If more than 4 + # don't return in 5 seconds, output that this is the max level + timeouts = 0 + for attempt in range(20): + try: + run("./cmake-build-release/bin/solvitaire --rules " + + tempRulesFilename + " --random " + str(attempt + 1), + shell=True, + check=True, + stdout=DEVNULL, + stderr=DEVNULL, + timeout=timeoutSeconds) + except TimeoutExpired: + timeouts += 1 + except CalledProcessError: + print("Error running " + rulesGen.name + " at level " + + str(level) + "!") + sys.exit() + except: + print("Unknown error in script") + sys.exit() + + if timeouts > timeoutsLimit: + break; + + if timeouts > timeoutsLimit: + print("Could not solve " + rulesGen.name + " at level " + str(level) + + " within time constraint\n") + print("Game description at level " + str(level) + ":") + print(str(rulesGen) + "\n") + break + else: + print("Solved " + rulesGen.name + " at level " + str(level) + + " ...") + +# Cleans up +os.remove(tempRulesFilename) -- cgit v1.2.1 From 21a816bd52298ba2b152449472d3be7c7709d296 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 14 Nov 2017 22:56:52 +0000 Subject: Fix deal bug when no cards in starting tableau --- src/main/game/game_state.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 3e8d129..1814a41 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -82,20 +82,19 @@ game_state::game_state(int seed, const sol_rules& s_rules) : } } + // Creates the tableau piles + for (unsigned int i = 0; i < rules.tableau_pile_count; i++) { + tableau_piles.push_back(pile::tableau_factory(rules.build_ord)); + } + // Deal to the tableau piles (row-by-row) unsigned int t = 0; while (!deck.empty()) { card c = deck.back(); deck.pop_back(); - // For the first row we must create all the tableau pile vectors - if (t / rules.tableau_pile_count == 0) { - tableau_piles.push_back(pile::tableau_factory(rules.build_ord)); - } - // Add the randomly generated card to the tableau piles - tableau_piles[t % rules.tableau_pile_count].place(c); - t++; + tableau_piles[t++ % rules.tableau_pile_count].place(c); } // If there are foundation piles, create the relevant pile vectors -- cgit v1.2.1 From 3b3376f4a7736f3bab9209b836e2149655774e11 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 14 Nov 2017 23:06:36 +0000 Subject: Add cleanup logic to benchmark script --- benchmark.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/benchmark.py b/benchmark.py index 28c6b4b..cc8af20 100644 --- a/benchmark.py +++ b/benchmark.py @@ -2,6 +2,7 @@ import json import math import os import sys +import atexit from subprocess import run, TimeoutExpired, CalledProcessError, DEVNULL from abc import ABCMeta, abstractmethod @@ -68,6 +69,12 @@ class CanfieldRulesGen(SolitaireRulesGen): self.baseJson["reserve size"] = reserveSize self.baseJson["stock size"] = stockSize +def cleanup(): + os.remove(tempRulesFilename) + run("pkill solvitaire", shell=True) + +atexit.register(cleanup) + # Loops through each canonical solitaire for rulesGen in [BlackHoleRulesGen(), SpanishPatienceRulesGen(), FreeCellRulesGen(), CanfieldRulesGen()]: @@ -112,6 +119,3 @@ for rulesGen in [BlackHoleRulesGen(), SpanishPatienceRulesGen(), else: print("Solved " + rulesGen.name + " at level " + str(level) + " ...") - -# Cleans up -os.remove(tempRulesFilename) -- cgit v1.2.1