aboutsummaryrefslogtreecommitdiff
path: root/optimisation-test.py
blob: 60ca39c247d601cc629fecca4f2f8e19f8ec492d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
from subprocess import run, CalledProcessError, DEVNULL, PIPE, Popen

runCount = 500

# Loops through each canonical solitaire

for optimisation in [
#    "-no-reduced-state",
#    "-no-pile-symmetry",
#    "-no-auto-foundations",
    "-no-symmetry"
]:

    for preset in [
        "simple-alpha-star",
        "simple-bakers-dozen",
        "simple-black-hole",
        "simple-canfield",
        "simple-flower-garden",
        "simple-fortunes-favor",
        "simple-free-cell",
        "simple-somerset",
        "simple-spanish-patience"
    ]:

        differs = []

        for attempt in range(runCount):
            try:
                op_p = Popen("./cmake-build-release/bin/solvitaire" + optimisation
                    + " --type " + preset + " --random " + str(attempt)
                    + " --classify",
                    shell=True,
                    stdout=PIPE,
                    stderr=DEVNULL)
                out_str = op_p.stdout.readline()
                if out_str == b'Solved\n':
                    op_res = True
                elif out_str == b'No Possible Solution\n':
                    op_res = False
                else:
                    raise Exception('Invalid return value!')

                no_op_p = Popen("./cmake-build-release/bin/solvitaire"
                    + " --type " + preset + " --random " + str(attempt)
                    + " --classify",
                    shell=True,
                    stdout=PIPE,
                    stderr=DEVNULL)
                out_str = no_op_p.stdout.readline()
                if out_str == b'Solved\n':
                    no_op_res = True
                elif out_str == b'No Possible Solution\n':
                    no_op_res = False
                else:
                    raise Exception('Invalid return value!')

                if op_res != no_op_res:
                    differs.append(attempt)

            except CalledProcessError:
                print("Error running " + preset + " with seed " + str(attempt) + "!")
                sys.exit()
            except Exception as e:
                print("Unknown error in script: ", e)
                sys.exit()

            run("pkill solvitaire", shell=True)

        if not differs :
            print(preset + ": No difference in solutions for " + optimisation)
        else :
            print(preset + ": Optimisation " + optimisation + " differs for the following seeds:")
            print(differs)