-
Notifications
You must be signed in to change notification settings - Fork 1
/
RewardModel.cs
220 lines (184 loc) · 6.22 KB
/
RewardModel.cs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// This class stores the model of the rewarding system
// The rewarding system is coded following the MVC design pattern.
// This is the Model part of the MVC.
namespace com.glups.Reward
{
public class RewardModel
{
internal int _currentLevel = 0;
internal int _score = 0;
internal int _rank = 0;
internal int _iScore = 0;
internal float _incentive = 0.001f;
internal int _positives = 0;
internal int _negatives = 0;
internal int _reward = 0;
internal int _previousThreshold = 0;
internal int _nextThreshold = 0;
// display
internal virtual void trace(int date, int playerID, string strategy)
{
Console.WriteLine("\t" + date + ", " + playerID + ", " + strategy + ", " + "<level " + _currentLevel + "> " + "<Score " + _score + "> <rank " + _rank + "> " + "<iScore " + _iScore + ", " + string.Format("{0:F04}", _incentive) + ", " + _nextThreshold + "> <reward " + _reward + "> " + "<positives " + _positives + "> <negatives " + _negatives + ">");
}
// Accessors
internal virtual int Score
{
get
{
return _score;
}
}
internal virtual int RewardScore
{
get
{
return _iScore;
}
}
// Methods
public virtual void openLevel(int level)
{
_currentLevel = level;
_positives = 0;
_negatives = 0;
_iScore = _score;
_incentive = 0.001f;
_previousThreshold = _iScore;
_nextThreshold = _iScore;
}
internal virtual int Positives
{
get
{
return _positives;
}
}
internal virtual int Negatives
{
get
{
return _negatives;
}
}
internal virtual int addPositive(int count, int points)
{
// returns the score used to compute rewards
if (count > 0)
{
_positives += count;
_score += points;
_iScore += points;
}
return _iScore;
}
internal virtual int addNegative(int errors, float incentive)
{
// returns the score used to compute rewards
// incentive is accumulated until a value higher than 1 is accumulated. We add this value to iScore then.
if (errors > 0)
{
_negatives += errors;
}
_incentive += incentive;
// this is to check equality between 2 floats.
if ((_incentive - 1.0f) >= 0.0001f)
{
_iScore += (int)((long)Math.Round(Math.Floor(_incentive), MidpointRounding.AwayFromZero));
_incentive = 0.001f;
}
return _iScore;
}
public virtual int updateRank(int[] steps)
{
// Return the new computed rank. If the rank stays the same, returns 0.
// ranks start at 0 (below the ranks), first rank is 1 and finishes at rankTable.length. Example {10; 100, 200; 300, 400}.
// rankTable: each value is the minimum value required in that position.
// When _score becomes higher than thin minimum, increment the rank and return the new one.
int l = steps.Length;
if (_rank < 0)
{
_rank = 0;
}
if (_rank > l)
{
Console.Error.WriteLine("Error: updateRank - _rank > vector length:" + _rank + " > " + l);
return 0;
}
// Score is higher then the last step of the vector.
if (_score >= steps[l - 1])
{
if (_rank == l)
{
return 0;
}
else
{
return _rank = l;
}
}
int oldRank = _rank;
for (int i = _rank; i < l && steps[i] <= _score; i++)
{
_rank++;
}
// correct the _rank if it is higher then what is should be.
while (_rank > 0 && _score < steps[_rank - 1])
{
_rank--;
}
if (_rank == oldRank)
{
return 0;
}
else
{
return _rank;
}
}
internal virtual int distance2Reward(int points)
{
// check if the additional points can trigger a reward
// returns > 0 if the reward is triggered; and returns the number of rewards above threshold
// return <0 indicating number of values needed to get a reward
if (_iScore + points < _nextThreshold)
{
return _iScore + points - _nextThreshold;
}
else
{
return _iScore + points - _nextThreshold + 1;
}
}
internal virtual int updateReward(int gap)
{
// Return 0 if no update applicable, otherwise the current counter of rewards.
// update the reward counter. Side effect, update the nextThreshold
if (gap == 0)
{
Console.Error.WriteLine("Error: updateReward - gap == 0");
return 0;
}
if (_nextThreshold == _previousThreshold)
{
_nextThreshold = _previousThreshold + gap;
}
int distance = distance2Reward(0);
if (distance > 0)
{
_previousThreshold = _nextThreshold;
_nextThreshold += gap;
/* This is another alternative. In this case, just 1 reward is awarded even if the score is way after the threshold.
* do {
* _nextThreshold += gap;
* } while(_nextThreshold <= _iScore);
*/
_reward++;
return _reward;
}
else
{
return 0;
}
}
}
}