Revize 6dc585b4
Přidáno uživatelem Roman Kalivoda před téměř 4 roky(ů)
Server/ServerApp/Predictor/PredictionController.cs | ||
---|---|---|
43 | 43 |
/// </summary> |
44 | 44 |
private IPredictor[] Predictors; |
45 | 45 |
|
46 |
/// <summary> |
|
47 |
/// A mutual exclusive lock to pro |
|
48 |
/// </summary> |
|
49 |
private readonly object predictorsLock = new Object(); |
|
50 |
|
|
46 | 51 |
/// <summary> |
47 | 52 |
/// A reference to a data parser. |
48 | 53 |
/// </summary> |
... | ... | |
64 | 69 |
/// <param name="dataParser">A data parser used to get training data.</param> |
65 | 70 |
public PredictionController(IJsonParser weatherService, IDataParser dataParser, string pathToConfig = null) |
66 | 71 |
{ |
67 |
// TODO look for existing predictors |
|
68 | 72 |
_log.Info("Constructing a new PredictionController instance."); |
69 | 73 |
this.weatherService = weatherService; |
70 | 74 |
// load config or get the default one |
... | ... | |
85 | 89 |
} |
86 | 90 |
|
87 | 91 |
this.DataParser = dataParser; |
88 |
this.Predictors = new IPredictor[this.Configuration.PredictorCount];
|
|
92 |
var predictors = new IPredictor[this.Configuration.PredictorCount];
|
|
89 | 93 |
this.FeatureExtractor = new FeatureExtractor(this.DataParser, this.Configuration); |
90 | 94 |
|
91 | 95 |
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath); |
... | ... | |
100 | 104 |
_log.Info("No predictors found, creating new ones"); |
101 | 105 |
for (int i = 0; i < this.Configuration.PredictorCount; i++) |
102 | 106 |
{ |
103 |
Predictors[i] = new NaiveBayesClassifier();
|
|
107 |
predictors[i] = new NaiveBayesClassifier();
|
|
104 | 108 |
} |
105 | 109 |
} |
110 |
lock (predictorsLock) |
|
111 |
{ |
|
112 |
this.Predictors = predictors; |
|
113 |
} |
|
106 | 114 |
PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration); |
107 | 115 |
} |
108 | 116 |
public List<string> GetPredictors() |
... | ... | |
122 | 130 |
{ |
123 | 131 |
newPredictors[i] = new NaiveBayesClassifier(Array.Find(files, f => Regex.IsMatch(f.Name, $"{predictorID}_{i}.zip")).FullName); |
124 | 132 |
} |
125 |
this.Predictors = newPredictors; |
|
126 | 133 |
files = di.GetFiles($"{predictorID}.txt"); |
127 |
this.DataFilenames = File.ReadLines(files[0].FullName); |
|
128 |
this.PredictorID = predictorID; |
|
134 |
var dataFilenames = File.ReadLines(files[0].FullName); |
|
135 |
lock (predictorsLock) |
|
136 |
{ |
|
137 |
this.Predictors = newPredictors; |
|
138 |
this.DataFilenames = dataFilenames; |
|
139 |
this.PredictorID = predictorID; |
|
140 |
} |
|
129 | 141 |
} catch (FileNotFoundException e) |
130 | 142 |
{ |
131 | 143 |
_log.Error(e.ToString()); |
... | ... | |
133 | 145 |
} |
134 | 146 |
} else |
135 | 147 |
{ |
136 |
// TODO indicate exception |
|
137 | 148 |
_log.Debug("Could not find predictor with given predictorID"); |
138 | 149 |
return 1; |
139 | 150 |
} |
... | ... | |
144 | 155 |
{ |
145 | 156 |
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath); |
146 | 157 |
|
147 |
for (int i = 0; i < this.Configuration.PredictorCount; i++)
|
|
158 |
lock (predictorsLock)
|
|
148 | 159 |
{ |
149 |
Predictors[i].Save(Path.Combine(di.FullName, $"{PredictorID}_{i}.zip")); |
|
160 |
for (int i = 0; i < this.Configuration.PredictorCount; i++) |
|
161 |
{ |
|
162 |
Predictors[i].Save(Path.Combine(di.FullName, $"{PredictorID}_{i}.zip")); |
|
163 |
} |
|
164 |
File.WriteAllLinesAsync(Path.Combine(di.FullName, $"{PredictorID}.txt"), this.DataFilenames); |
|
150 | 165 |
} |
151 |
File.WriteAllLinesAsync(Path.Combine(di.FullName, $"{PredictorID}.txt"), this.DataFilenames); |
|
152 | 166 |
} |
153 | 167 |
|
154 | 168 |
public int Rollback() |
... | ... | |
221 | 235 |
if (request.useWeather) |
222 | 236 |
{ |
223 | 237 |
_log.Debug("Predicting for requested weather."); |
224 |
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput
|
|
238 |
lock (predictorsLock)
|
|
225 | 239 |
{ |
226 |
Rain = (float)request.rain, |
|
227 |
Temp = (float)request.temperature, |
|
228 |
Wind = (float)request.wind, |
|
229 |
Hour = predictionTime.Hour, |
|
230 |
Time = predictionTime |
|
231 |
}); |
|
240 |
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput |
|
241 |
{ |
|
242 |
Rain = (float)request.rain, |
|
243 |
Temp = (float)request.temperature, |
|
244 |
Wind = (float)request.wind, |
|
245 |
Hour = predictionTime.Hour, |
|
246 |
Time = predictionTime |
|
247 |
}); |
|
248 |
} |
|
232 | 249 |
} |
233 | 250 |
else |
234 | 251 |
{ |
... | ... | |
241 | 258 |
} |
242 | 259 |
else |
243 | 260 |
{ |
244 |
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput
|
|
261 |
lock (predictorsLock)
|
|
245 | 262 |
{ |
246 |
Rain = weatherInfo.rain, |
|
247 |
Temp = (float)weatherInfo.temp, |
|
248 |
Wind = (float)weatherInfo.wind, |
|
249 |
Hour = predictionTime.Hour, |
|
250 |
Time = predictionTime |
|
251 |
}); |
|
263 |
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput |
|
264 |
{ |
|
265 |
Rain = weatherInfo.rain, |
|
266 |
Temp = (float)weatherInfo.temp, |
|
267 |
Wind = (float)weatherInfo.wind, |
|
268 |
Hour = predictionTime.Hour, |
|
269 |
Time = predictionTime |
|
270 |
}); |
|
271 |
} |
|
252 | 272 |
} |
253 | 273 |
} |
254 | 274 |
} |
... | ... | |
273 | 293 |
public void Train() |
274 | 294 |
{ |
275 | 295 |
DataParser.Parse(DateTime.MinValue, DateTime.MaxValue, this.Configuration.TimeResolution, wholeDay: false); |
276 |
for (int i = 0; i < this.Predictors.Length; i++)
|
|
296 |
for (int i = 0; i < this.Configuration.PredictorCount; i++)
|
|
277 | 297 |
{ |
278 | 298 |
// train on all available data |
279 | 299 |
List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i); |
280 | 300 |
Console.WriteLine("Training predictor with {0} samples.", data.Count); |
281 |
this.Predictors[i].Fit(data); |
|
301 |
lock (predictorsLock) |
|
302 |
{ |
|
303 |
this.Predictors[i].Fit(data); |
|
304 |
} |
|
305 |
} |
|
306 |
lock (predictorsLock) |
|
307 |
{ |
|
308 |
this.DataFilenames = this.DataParser.WeatherDataUsed.Concat(this.DataParser.ActivityDataUsed); |
|
309 |
this.PredictorID = DateTime.Now.ToBinary().ToString(); |
|
282 | 310 |
} |
283 |
this.DataFilenames = this.DataParser.WeatherDataUsed.Concat(this.DataParser.ActivityDataUsed); |
|
284 |
this.PredictorID = DateTime.Now.ToBinary().ToString(); |
|
285 | 311 |
this.Save(); |
286 | 312 |
} |
287 | 313 |
|
Také k dispozici: Unified diff
Re #9049 thread-safety implementation