public static ANFIS Build(double[][] input, double[][] output, IRuleExtractor RuleExtractor, ITraining trainer, int MaxIterations) { InMemoryLogger.PrintMessage("Start..."); InMemoryLogger.PrintMessage($"Constructing initial rule set with [{RuleExtractor.GetType().Name}]"); var ruleBase = RuleSetFactory <R> .Build(input, output, RuleExtractor).Select(z => z as IRule).ToList(); InMemoryLogger.PrintMessage($"Get {ruleBase.Count} initial rules."); int epoch = 0; double trnError = 0.0; Console.WriteLine(); Console.WriteLine(); do { trnError = trainer.Iteration(input, output, ruleBase); InMemoryLogger.PrintMessage($"Epoch {epoch}, training error {trnError}"); if (double.IsNaN(trnError)) { InMemoryLogger.PrintMessage("Failure! Training error is NAN."); throw new Exception("Failure! Bad system design."); } } while (!trainer.isTrainingstoped() && epoch++ < MaxIterations); ANFIS fis = new ANFIS(ruleBase); InMemoryLogger.PrintMessage("Done"); return(fis); }
public void TestRulesetGeneration() { int trainingSamples = 100; double[][] x = new double[trainingSamples][]; double[][] y = new double[trainingSamples][]; Random rnd = new Random(); for (int i = 0; i < trainingSamples; i++) { bool isRigth = i % 2 == 0; double valx = (isRigth ? 1 : -1) + (0.5 - rnd.NextDouble()); x[i] = new double[] { valx, valx }; y[i] = new double[] { isRigth ? 1 : 0, isRigth ? 0 : 1 }; } KMEANSExtractorIO extractor = new KMEANSExtractorIO(2); List <GaussianRule> ruleBase = RuleSetFactory <GaussianRule> .Build(x, y, extractor); if (ruleBase[0].Z[0] > 0.5) { Assert.AreEqual(ruleBase[0].Z[0], 1, 1e-2); Assert.AreEqual(ruleBase[0].Z[1], 0, 1e-2); Assert.AreEqual(ruleBase[1].Z[1], 1, 1e-2); Assert.AreEqual(ruleBase[1].Z[0], 0, 1e-2); Assert.AreEqual(ruleBase[0].Parameters[0], 1, 1e-1); Assert.AreEqual(ruleBase[0].Parameters[1], 1, 1e-1); Assert.AreEqual(ruleBase[1].Parameters[0], -1, 1e-1); Assert.AreEqual(ruleBase[1].Parameters[1], -1, 1e-1); } else { Assert.AreEqual(ruleBase[0].Z[1], 1, 1e-2); Assert.AreEqual(ruleBase[0].Z[0], 0, 1e-2); Assert.AreEqual(ruleBase[1].Z[0], 1, 1e-2); Assert.AreEqual(ruleBase[1].Z[1], 0, 1e-2); Assert.AreEqual(ruleBase[1].Parameters[0], 1, 1e-1); Assert.AreEqual(ruleBase[1].Parameters[1], 1, 1e-1); Assert.AreEqual(ruleBase[0].Parameters[0], -1, 1e-1); Assert.AreEqual(ruleBase[0].Parameters[1], -1, 1e-1); } }
public static ANFIS Build(double[][] input, double[][] output, IRuleExtractor RuleExtractor, ITraining trainer, int MaxIterations) { _log.Info("Start..."); _log.Info($"Constructing initial rule set with [{RuleExtractor.GetType().Name}]"); var ruleBase = RuleSetFactory <R> .Build(input, output, RuleExtractor).Select(z => z as IRule).ToList(); _log.Info($"Get {ruleBase.Count} initial rules."); int epoch = 0; double trnError = 0.0; Console.WriteLine(); Console.WriteLine("Старт обучения"); do { trnError = trainer.Iteration(input, output, ruleBase); _log.Info($"Epoch {epoch}, training error {trnError}"); if (double.IsNaN(trnError)) { _log.Info("Failure! Training error is NAN."); throw new Exception("Failure! Bad system design."); } if (epoch % 100 == 0) { Console.WriteLine("Обучение: " + epoch.ToString() + " итерация. Ошибка: " + trnError); } } while (!trainer.isTrainingstoped() && epoch++ < MaxIterations); ANFIS fis = new ANFIS(ruleBase); Console.WriteLine("Завершение обучения"); _log.Info("Done"); return(fis); }