|
10 | 10 | using KnowledgeProduction;
|
11 | 11 | using IdManagement;
|
12 | 12 | using RLDT;
|
| 13 | +using System.Diagnostics; |
| 14 | +using System.Runtime.CompilerServices; |
13 | 15 |
|
14 | 16 | namespace KnowProdContBlackBox.Experiments
|
15 | 17 | {
|
@@ -193,5 +195,200 @@ public void LogicOperations(int iterations)
|
193 | 195 |
|
194 | 196 | return;
|
195 | 197 | }
|
| 198 | + |
| 199 | + [Theory] |
| 200 | + //[InlineData(5)] |
| 201 | + //[InlineData(10)] |
| 202 | + //[InlineData(15)] |
| 203 | + //[InlineData(20)] |
| 204 | + //[InlineData(25)] |
| 205 | + //[InlineData(30)] |
| 206 | + //[InlineData(35)] |
| 207 | + [InlineData(40)] |
| 208 | + public void TrigFunctions(int passes) |
| 209 | + { |
| 210 | + double angleInterval = 1; |
| 211 | + |
| 212 | + List<double> angles = new List<double> {}; |
| 213 | + for (double r = 0; r < 180; r += angleInterval) |
| 214 | + angles.Add(r); |
| 215 | + IdManager idManager = new IdManager(); |
| 216 | + BlackBox trigBlackBox = new BlackBoxModeling.Samples.TrigFunctions() { TimeInterval_ms = 10 }; |
| 217 | + DiscreteBlackBox discBlackBox = new DiscreteBlackBox(trigBlackBox, idManager); |
| 218 | + ProducerBlackBox prodBlackBox = new ProducerBlackBox(discBlackBox, idManager); |
| 219 | + Interpreter interpreter = new Interpreter(prodBlackBox) { MemorySize = passes * 2 }; |
| 220 | + PolicyLearner policyLearner = new PolicyLearner(interpreter, idManager); |
| 221 | + Random rand = new Random(); |
| 222 | + trigBlackBox.Start(); |
| 223 | + |
| 224 | + #region Train |
| 225 | + for (int i = 0; i < passes; i++) |
| 226 | + { |
| 227 | + //Change input |
| 228 | + List<double> angles_noisy = GenerateNoisyData(angles, 0.001, 1).OrderBy(p => rand.NextDouble()).ToList(); |
| 229 | + foreach(double angle in angles_noisy) |
| 230 | + { |
| 231 | + for (int j = 0; j < 1; j++) |
| 232 | + { |
| 233 | + trigBlackBox.Input["angle"] = angle; |
| 234 | + |
| 235 | + //Wait until next sample time |
| 236 | + Thread.Sleep(trigBlackBox.TimeInterval_ms); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + #endregion |
| 241 | + |
| 242 | + //Get Discretizers and Producers |
| 243 | + var discAngle = discBlackBox.Discretizers["angle"]; |
| 244 | + var discSin = discBlackBox.Discretizers["sin"]; |
| 245 | + var discCos = discBlackBox.Discretizers["cos"]; |
| 246 | + var discTan = discBlackBox.Discretizers["tan"]; |
| 247 | + var prodAngle = prodBlackBox.Producers["angle"]; |
| 248 | + var prodSin = prodBlackBox.Producers["sin"]; |
| 249 | + var prodCos = prodBlackBox.Producers["cos"]; |
| 250 | + var prodTan = prodBlackBox.Producers["tan"]; |
| 251 | + Policy policySin = policyLearner.Policies["sin"]; |
| 252 | + Policy policyCos = policyLearner.Policies["cos"]; |
| 253 | + Policy policyTan = policyLearner.Policies["tan"]; |
| 254 | + |
| 255 | + //Check if training was sucessfull |
| 256 | + //Assert.Equal(4, disc1.Bins.Count); |
| 257 | + //Assert.Equal(4, disc2.Bins.Count); |
| 258 | + //Assert.Equal(4, discand.Bins.Count); |
| 259 | + //Assert.Equal(4, discor.Bins.Count); |
| 260 | + //Assert.Equal(4, discxor.Bins.Count); |
| 261 | + //Assert.Equal(4, discxor.Bins.Count); |
| 262 | + //Assert.InRange(prod1.KnowInstances.Count, 8, 10); |
| 263 | + //Assert.InRange(prod2.KnowInstances.Count, 8, 10); |
| 264 | + //Assert.InRange(prodand.KnowInstances.Count, 8, 10); |
| 265 | + //Assert.InRange(prodor.KnowInstances.Count, 8, 10); |
| 266 | + //Assert.InRange(prodxor.KnowInstances.Count, 8, 10); |
| 267 | + |
| 268 | + //Name Entities by their average |
| 269 | + foreach(Bin theBin in discAngle.Bins) |
| 270 | + idManager.SetName(theBin.BinID, theBin.Average.ToString("N2")); |
| 271 | + foreach (Bin theBin in discSin.Bins) |
| 272 | + idManager.SetName(theBin.BinID, theBin.Average.ToString("N2")); |
| 273 | + foreach (Bin theBin in discCos.Bins) |
| 274 | + idManager.SetName(theBin.BinID, theBin.Average.ToString("N2")); |
| 275 | + foreach (Bin theBin in discTan.Bins) |
| 276 | + idManager.SetName(theBin.BinID, theBin.Average.ToString("N2")); |
| 277 | + |
| 278 | + //Calculate predicted values and error |
| 279 | + Dictionary<double, Dictionary<string, double>> compResults = new Dictionary<double, Dictionary<string, double>>(); |
| 280 | + Dictionary<string, double> trigEntryTemplate = new Dictionary<string, double>(); |
| 281 | + trigEntryTemplate.Add("sin", double.NaN); |
| 282 | + trigEntryTemplate.Add("cos", double.NaN); |
| 283 | + trigEntryTemplate.Add("tan", double.NaN); |
| 284 | + trigEntryTemplate.Add("sinPred", double.NaN); |
| 285 | + trigEntryTemplate.Add("cosPred", double.NaN); |
| 286 | + trigEntryTemplate.Add("tanPred", double.NaN); |
| 287 | + trigEntryTemplate.Add("sinError", double.NaN); |
| 288 | + trigEntryTemplate.Add("cosError", double.NaN); |
| 289 | + trigEntryTemplate.Add("tanError", double.NaN); |
| 290 | + for (double angle=0; angle <= 180; angle += angleInterval) |
| 291 | + { |
| 292 | + //Create entry in dictionaries |
| 293 | + compResults.Add(angle, new Dictionary<string, double>(trigEntryTemplate)); |
| 294 | + double radians = angle / 180 * Math.PI; |
| 295 | + |
| 296 | + //Get real values |
| 297 | + compResults[angle]["sin"] = Math.Sin(radians); |
| 298 | + compResults[angle]["cos"] = Math.Cos(radians); |
| 299 | + compResults[angle]["tan"] = Math.Tan(radians); |
| 300 | + |
| 301 | + //Convert angle to knowledge instance |
| 302 | + Bin binAngle = discAngle.GetBin(angle); |
| 303 | + KnowInstance ki = prodAngle.Get(binAngle.BinID); |
| 304 | + |
| 305 | + #region Get predicted values |
| 306 | + DataVector dv = new DataVector(new string[] {"angle"}, new object[] {ki}); |
| 307 | + //Sin |
| 308 | + try |
| 309 | + { |
| 310 | + KnowInstanceValue predKi_sin = (KnowInstanceValue) ((KnowInstanceWithMetaData)policySin.Classify_ByPolicy(dv, false)).InnerKnowInstance; |
| 311 | + Bin predBinSin = (Bin)predKi_sin.Content; |
| 312 | + compResults[angle]["sinPred"] = predBinSin.Average; |
| 313 | + } |
| 314 | + catch { } |
| 315 | + //Cos |
| 316 | + try |
| 317 | + { |
| 318 | + KnowInstanceValue predKi_cos = (KnowInstanceValue)((KnowInstanceWithMetaData)policyCos.Classify_ByPolicy(dv, false)).InnerKnowInstance; |
| 319 | + Bin predBinCos = (Bin)predKi_cos.Content; |
| 320 | + compResults[angle]["cosPred"] = predBinCos.Average; |
| 321 | + } |
| 322 | + catch { } |
| 323 | + //Tan |
| 324 | + try |
| 325 | + { |
| 326 | + KnowInstanceValue predKi_tan = (KnowInstanceValue)((KnowInstanceWithMetaData)policyTan.Classify_ByPolicy(dv, false)).InnerKnowInstance; |
| 327 | + Bin predBinTan = (Bin)predKi_tan.Content; |
| 328 | + compResults[angle]["tanPred"] = predBinTan.Average; |
| 329 | + } |
| 330 | + catch { } |
| 331 | + #endregion |
| 332 | + |
| 333 | + //Calculate error |
| 334 | + compResults[angle]["sinError"] = compResults[angle]["sin"] - compResults[angle]["sinPred"]; |
| 335 | + compResults[angle]["cosError"] = compResults[angle]["cos"] - compResults[angle]["cosPred"]; |
| 336 | + compResults[angle]["tanError"] = compResults[angle]["tan"] - compResults[angle]["tanPred"]; |
| 337 | + } |
| 338 | + |
| 339 | + //Save error results to csv |
| 340 | + List<string> errorResults = new List<string>(); |
| 341 | + errorResults.Add("angle,sin,cos,tan,Predicted Sin,Predicted Cos,Predicted Tan, Error Sin, Error Cos, Error Tan"); |
| 342 | + foreach(var prediction in compResults) |
| 343 | + { |
| 344 | + double angle = prediction.Key; |
| 345 | + string row = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", |
| 346 | + angle, |
| 347 | + compResults[angle]["sin"], |
| 348 | + compResults[angle]["cos"], |
| 349 | + compResults[angle]["tan"], |
| 350 | + |
| 351 | + compResults[angle]["sinPred"], |
| 352 | + compResults[angle]["cosPred"], |
| 353 | + compResults[angle]["tanPred"], |
| 354 | + |
| 355 | + compResults[angle]["sinError"], |
| 356 | + compResults[angle]["cosError"], |
| 357 | + compResults[angle]["tanError"] |
| 358 | + ); |
| 359 | + errorResults.Add(row); |
| 360 | + } |
| 361 | + File.WriteAllLines(SavePath(passes + "_ErrorResults.csv"), errorResults.ToArray()); |
| 362 | + |
| 363 | + //Save training results to csv |
| 364 | + File.WriteAllLines(SavePath(passes + "_angle.csv"), ToStringArray(prodAngle.KnowInstances.Values.ToList(), idManager)); |
| 365 | + File.WriteAllLines(SavePath(passes + "_sin.csv"), ToStringArray(prodSin.KnowInstances.Values.ToList(), idManager)); |
| 366 | + File.WriteAllLines(SavePath(passes + "_cos.csv"), ToStringArray(prodCos.KnowInstances.Values.ToList(), idManager)); |
| 367 | + File.WriteAllLines(SavePath(passes + "_tan.csv"), ToStringArray(prodTan.KnowInstances.Values.ToList(), idManager)); |
| 368 | + |
| 369 | + //Convert policies to html and save to file |
| 370 | + string htmlTree = HtmlTools.ToHtml(policyLearner, idManager); |
| 371 | + File.WriteAllText(SavePath(passes + "_decision_tree.html"), htmlTree); |
| 372 | + |
| 373 | + return; |
| 374 | + } |
| 375 | + |
| 376 | + public string[] ToStringArray(List<KnowInstance> knowInstances, IdManager idManager) |
| 377 | + { |
| 378 | + List<string> csv = new List<string>(); |
| 379 | + csv.Add("ID,Name,Content"); |
| 380 | + foreach(KnowInstance ki in knowInstances) |
| 381 | + { |
| 382 | + KnowInstanceWithMetaData kim = new KnowInstanceWithMetaData(ki, idManager); |
| 383 | + csv.Add(string.Format("{0},\"{1}\",\"{2}\"", kim.ID, kim.Name, ki.ContentToString())); |
| 384 | + } |
| 385 | + |
| 386 | + return csv.ToArray(); |
| 387 | + } |
| 388 | + private string SavePath(string fileName) |
| 389 | + { |
| 390 | + return Path.Combine(this.ResultsDir, fileName); |
| 391 | + } |
| 392 | + |
196 | 393 | }
|
197 | 394 | }
|
0 commit comments