source: geniuswebcore/test/geniusweb/opponentmodel/FrequencyOppModelTest.py@ 94

Last change on this file since 94 was 90, checked in by Bart Vastenhouw, 3 years ago

Refactor to help reusing partiesserver.

File size: 7.1 KB
Line 
1from decimal import Decimal
2from typing import Dict, List
3import unittest
4from unittest.mock import Mock
5
6from tudelft.utilities.immutablelist.Range import Range
7from unitpy.GeneralTests import GeneralTests
8
9from geniusweb.actions.Offer import Offer
10from geniusweb.actions.Offer import Offer
11from geniusweb.actions.PartyId import PartyId
12from geniusweb.issuevalue.Bid import Bid
13from geniusweb.issuevalue.DiscreteValue import DiscreteValue
14from geniusweb.issuevalue.DiscreteValueSet import DiscreteValueSet
15from geniusweb.issuevalue.Domain import Domain
16from geniusweb.issuevalue.NumberValue import NumberValue
17from geniusweb.issuevalue.NumberValueSet import NumberValueSet
18from geniusweb.issuevalue.Value import Value
19from geniusweb.issuevalue.ValueSet import ValueSet
20from geniusweb.opponentmodel.FrequencyOpponentModel import FrequencyOpponentModel
21from geniusweb.progress.Progress import Progress
22
23
24class FrequencyOppModelTest (unittest.TestCase, GeneralTests[FrequencyOpponentModel]):
25 ISS1 = "issue1"
26 ISS2 = "issue2";
27 I1V1 = DiscreteValue("i1v1")
28 I1V2 = DiscreteValue("i1v2");
29 I2V1 = DiscreteValue("i2v1")
30 I2V2 = DiscreteValue("i2v2")
31 I1V2b = DiscreteValue("i1v2b");
32 progress = Mock(Progress);
33 other = PartyId("other")
34 HALF = Decimal("0.5")
35 # list:List[List[Bid]] = []
36
37 def setUp(self):
38 issues:Dict[str, ValueSet] = {}
39 discretevalues1:List[DiscreteValue] = []
40 discretevalues1.append(self.I1V1);
41 discretevalues1.append(self.I1V2);
42 values1 = DiscreteValueSet(discretevalues1)
43 issues[self.ISS1] = values1
44 values2 = NumberValueSet(Range(Decimal(0), Decimal(10), Decimal("0.3")))
45 issues[self.ISS2] = values2
46 self.domain = Domain("test", issues)
47 self.domain2 = Domain("test2", issues)
48
49 # slightly different issue1
50 issues = {}
51 discretevalues1 = []
52 discretevalues1.append(self.I1V1)
53 discretevalues1.append(self.I1V2b)
54 values1 = DiscreteValueSet(discretevalues1)
55 issues[self.ISS1] = values1
56 values2 = NumberValueSet(Range(Decimal(0), Decimal(10), Decimal("0.3")))
57 issues[self.ISS2] = values2
58 self.domain3 = Domain("test", issues)
59
60 # all bids are for domain
61 issuevalues:Dict[str, Value] = {}
62 issuevalues[self.ISS1] = self.I1V1
63 issuevalues[self.ISS2] = NumberValue(Decimal("1.2"))
64 self.bid1 = Bid(issuevalues)
65
66 issuevalues[self.ISS1] = self.I1V1
67 issuevalues[self.ISS2] = NumberValue(Decimal("1.5"))
68 self.bid2 = Bid(issuevalues)
69
70 issuevalues[self.ISS1] = self.I1V2
71 issuevalues[self.ISS2] = NumberValue(Decimal("1.5"))
72 self.bid3 = Bid(issuevalues)
73
74 self.oppModel1 = FrequencyOpponentModel.create().With(self.domain, None)
75 self.oppModel1b = FrequencyOpponentModel.create().With(self.domain, None)
76 self.oppModel2 = FrequencyOpponentModel.create().With(self.domain2, None)
77 self.oppModel3 = FrequencyOpponentModel.create().With(self.domain3, None)
78 self.oppModel4 = self.oppModel3.WithAction(Offer(self.other, self.bid1), self.progress)
79
80 # Override
81 def getGeneralTestData(self) -> List[List[FrequencyOpponentModel]]:
82 return [[self.oppModel1, self.oppModel1b],
83 [self.oppModel2], [self.oppModel3], [self.oppModel4]]
84
85 # Override
86 def getGeneralTestStrings(self) -> List[str]:
87 return ["FrequencyOpponentModel\\[0,\\{issue.=\\{\\}, issue.=\\{\\}\\}\\]",
88 "FrequencyOpponentModel\\[0,\\{issue.=\\{\\}, issue.=\\{\\}\\}\\]",
89 "FrequencyOpponentModel\\[0,\\{issue.=\\{\\}, issue.=\\{\\}\\}\\]",
90 "FrequencyOpponentModel\\[1,\\{issue.*, issue.*"]
91
92 def testsmokeTestNull(self):
93 self.assertRaises(ValueError, lambda:FrequencyOpponentModel.create().With(None, None))
94
95 def testsmokeTest(self):
96 FrequencyOpponentModel.create().With(self.domain, None)
97
98 def testEmptyModel(self):
99 oppModel = FrequencyOpponentModel.create().With(self.domain, None)
100 self.assertEqual(1, oppModel.getUtility(self.bid1))
101 self.assertEqual(1, oppModel.getUtility(self.bid2))
102
103 def testEmptyModelPartialBid(self):
104 oppModel = FrequencyOpponentModel.create().With(self.domain, None)
105 bid = Bid({})
106 self.assertEqual(1, oppModel.getUtility(bid))
107
108 def testPartialUtility(self):
109 freqs:Dict[str, Dict[Value, int]] = {}
110 freqs1:Dict[Value, int] = {}
111 freqs2:Dict[Value, int] = {}
112
113 freqs1[self.I1V1] = 2
114 freqs1[self.I1V2] = 0
115 freqs2[self.I2V1] = 1
116 freqs2[self.I2V2] = 1
117
118 freqs[self.ISS1] = freqs1
119 freqs[self.ISS2] = freqs2
120 total = Decimal(2)
121
122 oppModel = FrequencyOpponentModel(self.domain, freqs, total, None)
123
124 E = 0.0000001
125 self.assertAlmostEqual(0.5, oppModel.getUtility(Bid({self.ISS1: self.I1V1})), None, E)
126 self.assertAlmostEqual(0, oppModel.getUtility(Bid({self.ISS1: self.I1V2})))
127 self.assertAlmostEqual(0.25,
128 oppModel.getUtility(Bid({self.ISS2:self.I2V1})), None, E)
129 self.assertAlmostEqual(0.25, oppModel.getUtility(Bid({self.ISS2: self.I2V2})))
130
131 def testUpdate(self):
132 oppModel = self.oppModel1.WithAction(Offer(self.other, self.bid1),
133 self.progress);
134 self.assertEqual(1, oppModel.getUtility(self.bid1))
135 self.assertEqual(0, oppModel.getUtility(self.bid3))
136 # bid2 has 1 of 2 issue values same as bid1.
137 self.assertTrue(0.5, oppModel.getUtility(self.bid2))
138
139 def testUpdate2(self):
140 # bid1 and bid2 both want I1V1. They differ on the number value.
141 # bid3 wants I1V2 but does have the number value from bid2
142 oppModel = self.oppModel1\
143 .WithAction(Offer(self.other, self.bid1), self.progress)\
144 .WithAction(Offer(self.other, self.bid2), self.progress)
145 self.assertEqual(0.75, oppModel.getUtility(self.bid1))
146 self.assertEqual(0.75, oppModel.getUtility(self.bid2))
147 self.assertEqual(0.25, oppModel.getUtility(self.bid3))
148
149 def testPartialBidUpdate(self):
150 oppModel = self.oppModel1.WithAction(Offer(self.other, self.bid1),
151 self.progress)
152 partialbid = Bid({self.ISS1: self.I1V1})
153 oppModel.WithAction(Offer(self.other, partialbid), self.progress)
154
155 def testGetCounts(self):
156 self.assertEqual({}, self.oppModel1.getCounts(self.ISS1))
157 self.assertEquals({}, self.oppModel1.getCounts(self.ISS2))
158
159 values1:Dict[str, Value] = {}
160 values1[self.ISS1] = self.I1V1
161 values1[self.ISS2] = self.I2V1
162 offer1 = Offer(self.other, Bid(values1))
163
164 oppmod = self.oppModel1.WithAction(offer1, self.progress)
165
166 self.assertEqual(1, oppmod.getCounts(self.ISS1)[self.I1V1])
167 self.assertFalse(self.I1V2 in oppmod.getCounts(self.ISS1))
168 self.assertEquals(1, oppmod.getCounts(self.ISS2)[self.I2V1])
169 self.assertFalse(self.I2V2 in oppmod.getCounts(self.ISS2))
170
171 # same identical bid. Counts should go to 2
172 oppmod = oppmod.WithAction(offer1, self.progress)
173
174 self.assertEquals(2, oppmod.getCounts(self.ISS1)[self.I1V1])
175 self.assertEquals(2, oppmod.getCounts(self.ISS2)[self.I2V1])
176
177 values2:Dict[str, Value] = {}
178 values2[self.ISS1] = self.I1V2
179 values2[self.ISS2] = self.I2V2
180 offer2 = Offer(self.other, Bid(values2))
181
182 # Other bid. with value2 for both issues
183 oppmod = oppmod.WithAction(offer2, self.progress)
184
185 self.assertEqual(2, oppmod.getCounts(self.ISS1)[self.I1V1])
186 self.assertEqual(2, oppmod.getCounts(self.ISS2)[self.I2V1])
187 self.assertEqual(1, oppmod.getCounts(self.ISS1)[self.I1V2])
188 self.assertEqual(1, oppmod.getCounts(self.ISS2)[self.I2V2])
189
190 def testStableName(self):
191 name = self.oppModel1.getName()
192 self.assertNotEqual(None, name)
193 self.assertEqual(name, self.oppModel1.getName())
194
Note: See TracBrowser for help on using the repository browser.