Repository navigation
Expand file tree
/
Copy pathGisModel.cs
More file actions
308 lines (281 loc) · 9.77 KB
/
Copy pathGisModel.cs
File metadata and controls
308 lines (281 loc) · 9.77 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//Copyright (C) 2005 Richard J. Northedge
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//This file is based on the GISModel.java source file found in the
//original java implementation of MaxEnt. That source file contains the following header:
// Copyright (C) 2001 Jason Baldridge and Gann Bierner
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Models
{
/// <summary>
/// A maximum entropy model which has been trained using the Generalized
/// Iterative Scaling procedure.
/// </summary>
/// <author>
/// Tom Morton and Jason Baldridge
/// </author>
/// <author>
/// Richard J. Northedge
/// </author>
/// <version>
/// based on GISModel.java, $Revision: 1.13 $, $Date: 2004/06/11 20:51:44 $
/// </version>
public sealed class GisModel : IMaximumEntropyModel
{
private readonly IO.IGisModelReader _reader;
private readonly string[] _outcomeNames;
private readonly int _outcomeCount;
private readonly double _initialProbability;
private readonly double _correctionConstantInverse;
private readonly int[] _featureCounts;
/// <summary>
/// Constructor for a maximum entropy model trained using the
/// Generalized Iterative Scaling procedure.
/// </summary>
/// <param name="reader">
/// A reader providing the data for the model.
/// </param>
public GisModel(IO.IGisModelReader reader)
{
this._reader = reader;
_outcomeNames = reader.GetOutcomeLabels();
CorrectionConstant = reader.CorrectionConstant;
CorrectionParameter = reader.CorrectionParameter;
_outcomeCount = _outcomeNames.Length;
_initialProbability = Math.Log(1.0 / _outcomeCount);
_correctionConstantInverse = 1.0 / CorrectionConstant;
_featureCounts = new int[_outcomeCount];
}
// implementation of IMaxentModel -------
/// <summary>
/// Returns the number of outcomes for this model.
/// </summary>
/// <returns>
/// The number of outcomes.
/// </returns>
public int OutcomeCount
{
get
{
return (_outcomeCount);
}
}
/// <summary>
/// Evaluates a context.
/// </summary>
/// <param name="context">
/// A list of string names of the contextual predicates
/// which are to be evaluated together.
/// </param>
/// <returns>
/// An array of the probabilities for each of the different
/// outcomes, all of which sum to 1.
/// </returns>
public double[] Evaluate(string[] context)
{
return Evaluate(context, new double[_outcomeCount]);
}
/// <summary>
/// Use this model to evaluate a context and return an array of the
/// likelihood of each outcome given that context.
/// </summary>
/// <param name="context">
/// The names of the predicates which have been observed at
/// the present decision point.
/// </param>
/// <param name="outcomeSums">
/// This is where the distribution is stored.
/// </param>
/// <returns>
/// The normalized probabilities for the outcomes given the
/// context. The indexes of the double[] are the outcome
/// ids, and the actual string representation of the
/// outcomes can be obtained from the method
/// GetOutcome(int outcomeIndex).
/// </returns>
public double[] Evaluate(string[] context, double[] outcomeSums)
{
for (int outcomeIndex = 0; outcomeIndex < _outcomeCount; outcomeIndex++)
{
outcomeSums[outcomeIndex] = _initialProbability;
_featureCounts[outcomeIndex] = 0;
}
foreach (string con in context)
{
_reader.GetPredicateData(con, _featureCounts, outcomeSums);
}
double normal = 0.0;
for (int outcomeIndex = 0;outcomeIndex < _outcomeCount; outcomeIndex++)
{
outcomeSums[outcomeIndex] = Math.Exp((outcomeSums[outcomeIndex] * _correctionConstantInverse) + ((1.0 - (_featureCounts[outcomeIndex] / CorrectionConstant)) * CorrectionParameter));
normal += outcomeSums[outcomeIndex];
}
for (int outcomeIndex = 0; outcomeIndex < _outcomeCount;outcomeIndex++)
{
outcomeSums[outcomeIndex] /= normal;
}
return outcomeSums;
}
/// <summary>
/// Return the name of the outcome corresponding to the highest likelihood
/// in the parameter outcomes.
/// </summary>
/// <param name="outcomes">
/// A double[] as returned by the Evaluate(string[] context)
/// method.
/// </param>
/// <returns>
/// The name of the most likely outcome.
/// </returns>
public string GetBestOutcome(double[] outcomes)
{
int bestOutcomeIndex = 0;
for (int currentOutcome = 1; currentOutcome < outcomes.Length; currentOutcome++)
if (outcomes[currentOutcome] > outcomes[bestOutcomeIndex])
{
bestOutcomeIndex = currentOutcome;
}
return _outcomeNames[bestOutcomeIndex];
}
/// <summary>
/// Return a string matching all the outcome names with all the
/// probabilities produced by the <code>Evaluate(string[] context)</code>
/// method.
/// </summary>
/// <param name="outcomes">
/// A <code>double[]</code> as returned by the
/// <code>eval(string[] context)</code>
/// method.
/// </param>
/// <returns>
/// string containing outcome names paired with the normalized
/// probability (contained in the <code>double[] outcomes</code>)
/// for each one.
/// </returns>
public string GetAllOutcomes(double[] outcomes)
{
if (outcomes.Length != _outcomeNames.Length)
{
throw new ArgumentException("The double array sent as a parameter to GisModel.GetAllOutcomes() must not have been produced by this model.");
}
else
{
var outcomeInfo = new StringBuilder(outcomes.Length * 2);
outcomeInfo.Append(_outcomeNames[0]).Append("[").Append(outcomes[0].ToString("0.0000", System.Globalization.CultureInfo.CurrentCulture)).Append("]");
for (int currentOutcome = 1; currentOutcome < outcomes.Length; currentOutcome++)
{
outcomeInfo.Append(" ").Append(_outcomeNames[currentOutcome]).Append("[").Append(outcomes[currentOutcome].ToString("0.0000", System.Globalization.CultureInfo.CurrentCulture)).Append("]");
}
return outcomeInfo.ToString();
}
}
/// <summary>
/// Return the name of an outcome corresponding to an integer ID value.
/// </summary>
/// <param name="outcomeIndex">
/// An outcome ID.
/// </param>
/// <returns>
/// The name of the outcome associated with that ID.
/// </returns>
public string GetOutcomeName(int outcomeIndex)
{
return _outcomeNames[outcomeIndex];
}
/// <summary>
/// Gets the index associated with the string name of the given outcome.
/// </summary>
/// <param name="outcome">
/// the string name of the outcome for which the
/// index is desired
/// </param>
/// <returns>
/// the index if the given outcome label exists for this
/// model, -1 if it does not.
/// </returns>
public int GetOutcomeIndex(string outcome)
{
for (int iCurrentOutcomeName = 0; iCurrentOutcomeName < _outcomeNames.Length; iCurrentOutcomeName++)
{
if (_outcomeNames[iCurrentOutcomeName] == outcome)
{
return iCurrentOutcomeName;
}
}
return - 1;
}
/// <summary>
/// Provides the predicates data structure which is part of the encoding of the maxent model
/// information. This method will usually only be needed by
/// GisModelWriters.
/// </summary>
/// <returns>
/// Dictionary containing PatternedPredicate objects.
/// </returns>
public Dictionary<string, PatternedPredicate> GetPredicates()
{
return _reader.GetPredicates();
}
/// <summary>
/// Provides the list of outcome patterns used by the predicates. This method will usually
/// only be needed by GisModelWriters.
/// </summary>
/// <returns>
/// Array of outcome patterns.
/// </returns>
public int[][] GetOutcomePatterns()
{
return _reader.GetOutcomePatterns();
}
/// <summary>
/// Provides the outcome names data structure which is part of the encoding of the maxent model
/// information. This method will usually only be needed by
/// GisModelWriters.
/// </summary>
/// <returns>
/// Array containing the outcome names.
/// </returns>
public string[] GetOutcomeNames()
{
return _outcomeNames;
}
/// <summary>
/// Provides the model's correction constant.
/// This property will usually only be needed by GisModelWriters.
/// </summary>
public int CorrectionConstant { get; private set; }
/// <summary>
/// Provides the model's correction parameter.
/// This property will usually only be needed by GisModelWriters.
/// </summary>
public double CorrectionParameter { get; private set; }
}
}