forked from onp/gmcr-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
146 lines (119 loc) · 6.04 KB
/
tests.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import unittest
import data_01_conflictModel
import data_02_conflictSolvers
import data_03_gmcrUtilities as util
import data_04_spSolvers
import numpy
files = ["Garrison",
"MilkRiver",
"Prisoners",
"SyriaIraq"]
class TestStateSetMath(unittest.TestCase):
"""Tests on math to combine/subtract state sets."""
def test_reduce(self):
"""Test ability to reduce sets of states."""
# non-overlapping sets should not change.
a1 = util.reducePatterns(["Y---", "---Y"])
self.assertEqual(a1, ["Y---", "---Y"])
# subsets should be removed.
a2 = util.reducePatterns(["Y---", "YY--"])
self.assertEqual(a2, ["Y---"])
# larger example
a3 = util.reducePatterns(["-Y---", "YY---", "---NY", "NNNY-"])
self.assertEqual(a3, ["-Y---", "---NY", "NNNY-"])
# If elements have different lengths, an exception should be raised.
with self.assertRaises(ValueError):
a4 = util.reducePatterns(["N-NN", "Y-N"])
# If something other than a list is provided, an exception should be raised.
with self.assertRaises(TypeError):
a5 = util.reducePatterns("N--,Y-N")
def test_subtract(self):
# basic subtraction example.
a1 = util._subtractPattern("---", "NYN")
self.assertEqual(a1, ['Y--', 'NN-', 'NYY'])
# subtracting itself should give empty set.
a2 = util._subtractPattern("Y--", "Y--")
self.assertEqual(a2, [])
# subtracting a superset should give an empty set.
a3 = util._subtractPattern("-Y-", "---")
self.assertEqual(a3, [])
# If elements have different lengths, an exception should be raised.
with self.assertRaises(ValueError):
a4 = util._subtractPattern("-Y-", "Y---")
with self.assertRaises(ValueError):
a5 = util._subtractPattern("-Y--", "Y--")
def test_subtractSingleFromGroup(self):
# basic examples
a1 = util.rmvSt(['-N-Y-', '-N-NN'], 'NNNY-')
self.assertEqual(a1[0], ["YN-Y-", 'NNYY-', '-N-NN'])
self.assertEqual(a1[1], 2)
# Results must be presented in minimal form.
a2 = util.rmvSt(['N----', 'YN---'], '-Y---')
self.assertEqual(a2[0], ['-N---'])
self.assertEqual(a2[1], 8)
def test_groupSubtract(self):
# basic examples
a1 = util.subtractStateSets(['-----'], ["-Y---", "YY---", "---NY", "NNNY-"])
self.assertEqual(a1, ["YN-Y-", 'NNYY-', '-N-NN'])
a2 = util.subtractStateSets(['N----', 'YN---'], ["-Y---", "---NY", "NNNY-"])
class TestSolvers(unittest.TestCase):
def setUp(self):
self.conf = data_01_conflictModel.ConflictModel()
def test_fileLoading(self):
self.assertTrue(len(self.conf.decisionMakers) == 0)
self.assertTrue(len(self.conf.options) == 0)
for file in files:
self.conf.load_from_file("Examples/" + file + ".gmcr")
self.assertTrue(len(self.conf.decisionMakers) > 0)
self.assertTrue(len(self.conf.options) > 0)
self.conf.__init__()
self.assertTrue(len(self.conf.decisionMakers) == 0)
self.assertTrue(len(self.conf.options) == 0)
def test_logSol(self):
for file in files:
self.conf.load_from_file("Examples/" + file + ".gmcr")
solver = data_02_conflictSolvers.LogicalSolver(self.conf)
solver.findEquilibria()
expected = numpy.loadtxt("test_data/" + file + "_logSol.txt")
numpy.testing.assert_array_equal(expected, solver.allEquilibria, "Incorrect logical solution for " + file)
def test_splogSol(self):
for file in files:
self.conf.load_from_file("Examples/" + file + ".gmcr")
solver = data_04_spSolvers.LogicalSolver(self.conf)
solver.findEquilibria()
expected = numpy.loadtxt("test_data/" + file + "_logSol.txt")
numpy.testing.assert_array_equal(expected, solver.allEquilibria, "Incorrect logical solution for " + file)
# def test_narration(self):
# for file in files:
# self.conf.load_from_file("Examples/" + file + ".gmcr")
# solver = data_02_conflictSolvers.LogicalSolver(self.conf)
# narrOut = ""
# for dm in self.conf.decisionMakers:
# for state in self.conf.feasibles:
# narrOut += "\n#DM %s state %s NASH\n"%(dm.name, state)
# narrOut += solver.nash(dm, state)[1]
# narrOut += "\n#DM %s state %s SEQ\n"%(dm.name, state)
# narrOut += solver.seq(dm, state)[1]
# narrOut += "\n#DM %s state %s SIM\n"%(dm.name, state)
# narrOut += solver.sim(dm, state)[1]
# narrOut += "\n#DM %s state %s GMR\n"%(dm.name, state)
# narrOut += solver.gmr(dm, state)[1]
# narrOut += "\n#DM %s state %s SMR\n"%(dm.name, state)
# narrOut += solver.smr(dm, state)[1]
# with open("test_data/" + file + "_narr.txt", "r") as f:
# expected = f.read().splitlines()
# generated = narrOut.splitlines()
# self.assertEqual(expected, generated) # used for testing
# # f.write(narrOut) #used to update expected test results
def test_inverseSol(self):
for file in files:
self.conf.load_from_file("Examples/" + file + ".gmcr")
varyRanges = [([0, min(len(dm.preferenceRanking) + 1, 4)] if len(dm.preferenceRanking) > 1 else [0, 0]) for dm in self.conf.decisionMakers]
desEqs = range(min(5, len(self.conf.feasibles)))
for desEq in desEqs:
solver = data_02_conflictSolvers.InverseSolver(self.conf, varyRanges, desEq)
solver.findEquilibria()
expected = numpy.loadtxt("test_data/%s_%s_invSol.txt"%(file, desEq))
numpy.testing.assert_array_equal(expected, solver.equilibriums, "Incorrect inverse results for %s_%s"%(file, desEq))
if __name__ == "__main__":
unittest.main()