public double Error(double[][] x, double[][] y, IList <IRule> ruleBase) { if (x.Length != y.Length) { throw new Exception("Input and desired output lengths not match"); } if (ruleBase == null || ruleBase.Count == 0) { throw new Exception("Incorrect rulebase"); } int outputDim = ruleBase[0].Z.Length; int numOfRules = ruleBase.Count; double globalError = 0.0; for (int sample = 0; sample < x.Length; sample++) { double[] o = ANFIS.Inference(x[sample], ruleBase); for (int C = 0; C < outputDim; C++) { globalError += Math.Abs(o[C] - y[sample][C]); } } return(globalError / x.Length); }
private static void subtestLogisticsMap <T>(double[][] x, double[][] y, double[][] tx, double[][] ty, ITraining bprop) where T : IRule, new() { KMEANSExtractorIO extractor = new KMEANSExtractorIO(10); var timer = Stopwatch.StartNew(); ANFIS fis = ANFISBuilder <T> .Build(x, y, extractor, bprop, 1000); timer.Stop(); double err = bprop.Error(tx, ty, fis.RuleBase); Trace.WriteLine(string.Format("[{1} - {4}]\tLogistic map Error {0}\tElapsed {2}\tRuleBase {3}", err, bprop.GetType().Name, timer.Elapsed, fis.RuleBase.Length, typeof(T).Name), "training"); Assert.IsFalse(err > 1e-2); }
public void ANFIS_OutputFromDataSet() { var sampleSize = RobotArmDataSet.Input.Length - 1; //StochasticBatch sprop = new StochasticBatch(sampleSize, 1e-5); //sprop.UnknownCaseFaced += AddRule<GaussianRule2>; //var sprop = new Backprop(1e-1); var sprop = new StochasticQprop(sampleSize); var extractor = new KMEANSExtractorIO(25); //ANFIS fis = ANFISBuilder<GaussianRule2>.Build(RobotArmDataSet.Input, RobotArmDataSet.OutputTheta1, extractor, sprop, 150); ANFIS fis = ANFISBuilder <GaussianRule> .Build(RobotArmDataSet.Input, RobotArmDataSet.OutputTheta1, extractor, sprop, 150); var output1 = fis.Inference(new[] { 1.10413546487088, 2.81104319371924 }).FirstOrDefault(); // 1.1 var output2 = fis.Inference(new[] { 2.31665592712393, 1.9375717475909 }).FirstOrDefault(); // 0.6 var output3 = fis.Inference(new[] { 2.88944142930409, 16.7526454098038 }).FirstOrDefault(); // 1.4 }
private static void Main(string[] args) { const int numberOfRules = 10; var anfis = new ANFIS.ANFIS(numberOfRules, 10_000); var gradSus = anfis.Run(useBatch: false, eta: 0.001, etaZ: 0.01, verbose: false); // var anfisBatch = new ANFIS(numberOfRules, 10_000); // var gradSus = anfisBatch.Run(useBatch: true, batchSize: 4, eta: 0.0001, etaZ: 0.01, verbose: false); // var samples = ANFIS.GenerateSamples(); // const string root = "C:/Faks/NENR/NENR6/NENR6/Data/"; // var testRoot = root + $"{numberOfRules}.txt"; // var testRoot = root + "sampled.txt"; // FileHelper.WriteToFile(samples, testRoot); // WriteDifferencesToFile(); }
public static void WriteDifferencesToFile() { const int numberOfRules = 10; var anfis = new ANFIS.ANFIS(numberOfRules, 10_000); var gradSus = anfis.Run(useBatch: false, eta: 0.001, etaZ: 0.01, verbose: false); // var anfisBatch = new ANFIS(numberOfRules, 10_000); // var gradSus = anfisBatch.Run(useBatch: true, batchSize: 4, eta: 0.0001, etaZ: 0.01, verbose: false); var samples = ANFIS.ANFIS.GenerateSamples(); const string root = "C:/Faks/NENR/NENR6/NENR6/Data/SGD/Differences/"; var testRoot = root + $"{numberOfRules}.txt"; var sampleDifferences = samples; for (var i = 0; i < samples.Count; i++) { sampleDifferences[i] = new Sample(samples[i].X, samples[i].Y, samples[i].Z - gradSus[i].Z); } FileHelper.WriteToFile(sampleDifferences, testRoot); }
void Awake() { //Check if there is already an instance of SoundManager if (instance == null) { //if not, set it to this. instance = this; } //If instance already exists: else if (instance != this) { //Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager. Destroy(gameObject); } //Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene. DontDestroyOnLoad(gameObject); Backprop bprop = new Backprop(1e-2); KMEANSExtractorIO extractor = new KMEANSExtractorIO(5); fis = ANFISBuilder <GaussianRule> .Build(gameParams, musicParams, extractor, bprop, 5); }
private static void ReportTwo(List <UserData> userRawData, Dictionary <int, string> juxtaposition) { var ruleset = new List <IRule> { new SimpleLinearRule( new double[] { -0.1, -1 }, new double[] { 0.1, 150 }, 0), new SimpleLinearRule( new double[] { 0, -1 }, new double[] { 3, 2 }, 1), new SimpleLinearRule( new double[] { 1, -1 }, new double[] { 6, 3 }, 2), new SimpleLinearRule( new double[] { 1, -1 }, new double[] { 11, 4 }, 3), new SimpleLinearRule( new double[] { 0, 0 }, new double[] { 150, 150 }, 4), new SimpleLinearRule( new double[] { 2, -1 }, new double[] { 150, 4 }, 5), new SimpleLinearRule( new double[] { 2, -1 }, new double[] { 21, 3 }, 6), }; var fis = new ANFIS(ruleset); var groopy = userRawData.ToLookup(z => z.Id, z => z.GroupId).Where(z => z.Count() < 150); var sb = new StringBuilder(); sb.AppendLine($"ID, {string.Join(", ", Enumerable.Range(0, 7).Select(z => $"Стадия {z}"))}"); var count = groopy.Count(); foreach (var user in groopy) { var codes = user.Where(z => juxtaposition.ContainsKey(z)).Select(z => juxtaposition[z]); if (codes.Any()) { var line = $"{user.Key}, "; var input = new double[] { codes.Count(z => z.Equals("s6-2")), codes.Count(z => z.Equals("s6-3")) }; var res = fis.Inference(input); if (res.Any(z => double.IsNaN(z))) { res = new double[] { 1.0, 0, 0, 0, 0, 0, 0 } } ; line += string.Join(", ", res.Select(z => z.ToString("0.00", CultureInfo.InvariantCulture))); sb.AppendLine(line); } else { sb.AppendLine($"{user.Key}, {string.Join(", ", Enumerable.Range(0, 7).Select(z => "0.00"))}"); } Console.Write($"\r Remains {count--} "); } File.WriteAllText("report_two.csv", sb.ToString()); } }
private static void ReportOne(List <UserData> userRawData, Dictionary <int, string> juxtaposition) { var ruleset = new List <IRule> { new SimpleLinearRule( new double[] { 0.25, 0.01, 0.30, 0.0, 0.001, 0 }, new double[] { 0.80, 0.02, 0.80, 0.8, 0.020, 0.05 }, 1), new SimpleLinearRule( new double[] { 0.25, 0.5, 0.30, 0.0, 0.05, 0 }, new double[] { 0.60, 0.1, 0.60, 0.6, 0.10, 0.05 }, 2), new SimpleLinearRule( new double[] { 0.00, 0.00, 0.30, 0.0, 0.50, 0 }, new double[] { 0.40, 0.25, 0.60, 0.3, 0.30, 0.15 }, 3), new SimpleLinearRule( new double[] { 0.00, 0.00, 0.00, 0.0, 0.10, 0.10 }, new double[] { 0.30, 0.20, 0.30, 0.6, 0.40, 0.50 }, 4), new SimpleLinearRule( new double[] { 0.00, 0.00, 0.00, 0.00, 0.10, 0.00 }, new double[] { 0.20, 0.20, 0.20, 0.15, 0.50, 0.30 }, 5), new SimpleLinearRule( new double[] { 0.00, 0.00, 0.00, 0.0, 0.10, 0.00 }, new double[] { 0.90, 0.20, 0.30, 0.4, 0.50, 0.05 }, 6) }; var fis = new ANFIS(ruleset); var groopy = userRawData.ToLookup(z => z.Id, z => z.GroupId).Where(z => z.Count() < 150); var sb = new StringBuilder(); sb.AppendLine($"ID, {string.Join(", ", Enumerable.Range(0, 7).Select(z => $"Стадия {z}"))}"); var count = groopy.Count(); foreach (var user in groopy) { var codes = user.Where(z => juxtaposition.ContainsKey(z)).Select(z => juxtaposition[z]); if (codes.Any()) { var overall = (double)codes.Count(); var line = $"{user.Key}, "; var input = new double[] { codes.Count(z => z.Equals("s6-1a")) / overall, codes.Count(z => z.Equals("s6-1b")) / overall, codes.Count(z => z.Equals("s6-1c")) / overall, codes.Count(z => z.Equals("s6-1d")) / overall, codes.Count(z => z.Equals("s6-2")) / overall, codes.Count(z => z.Equals("s6-3")) / overall }; var res = fis.Inference(input); if (res.Any(z => double.IsNaN(z))) { res = new double[] { 1.0, 0, 0, 0, 0, 0, 0 } } ; line += string.Join(", ", res.Select(z => z.ToString("0.00", CultureInfo.InvariantCulture))); sb.AppendLine(line); } else { sb.AppendLine($"{user.Key}, {string.Join(", ", Enumerable.Range(0, 7).Select(z => "0.00"))}"); } Console.Write($"\r Remains {count--} "); } File.WriteAllText("report_one.csv", sb.ToString()); }
private static void ReportThree(List <UserData> userRawData, SpecialPublicData[] opposition) { var ruleset = new List <IRule> { new SimpleLinearRule( new double[] { 0, 0, 0 }, new double[] { 4, 5, 6 }, 1), new SimpleLinearRule( new double[] { 2, 1, 1 }, new double[] { 8, 8, 9 }, 2), new SimpleLinearRule( new double[] { 6, 3, 7 }, new double[] { 11, 11, 11 }, 3), new SimpleLinearRule( new double[] { 0, 5, 0 }, new double[] { 7, 11, 11 }, 4), new SimpleLinearRule( new double[] { 2, 6, 5 }, new double[] { 9, 11, 11 }, 5), new SimpleLinearRule( new double[] { 8, 2, 6 }, new double[] { 11, 9, 11 }, 6) }; var fis = new ANFIS(ruleset); var groopy = userRawData.ToLookup(z => z.Id, z => z.GroupId).Where(z => z.Count() < 150); var sb = new StringBuilder(); sb.AppendLine($"ID, {string.Join(", ", Enumerable.Range(0, 7).Select(z => $"Стадия {z}"))}"); var count = groopy.Count(); var dic = opposition.ToDictionary(z => z.Id, z => z); foreach (var user in groopy) { var line = $"{user.Key}, "; var input = new double[] { 0, 0, 0 }; foreach (var pub in user) { if (dic.ContainsKey(pub)) { input[0] = Math.Max(input[0], dic[pub].s7_1); input[1] = Math.Max(input[1], dic[pub].s7_2); input[2] = Math.Max(input[2], dic[pub].s7_3); } } var res = fis.Inference(input); if (res.Any(z => double.IsNaN(z))) { res = new double[] { 1.0, 0, 0, 0, 0, 0, 0 } } ; line += string.Join(", ", res.Select(z => z.ToString("0.00", CultureInfo.InvariantCulture))); sb.AppendLine(line); Console.Write($"\r Remains {count--} "); } File.WriteAllText("report_three.csv", sb.ToString()); }
private static void ReportCommon(List <UserData> userRawData, Dictionary <int, string> juxtaposition, SpecialPublicData[] opposition) { var ruleset = new List <IRule> { new SimpleLinearRule( new double[] { 0.25, 0.01, 0.30, 0.0, 0.001, 0.00, 0, -1, 0, 0, 0 }, new double[] { 0.80, 0.02, 0.80, 0.8, 0.020, 0.05, 3, 2, 4, 5, 6 }, 1), new SimpleLinearRule( new double[] { 0.25, 0.5, 0.30, 0.0, 0.05, 0.00, 1, -1, 2, 1, 1 }, new double[] { 0.60, 0.1, 0.60, 0.6, 0.10, 0.05, 6, 3, 8, 8, 9 }, 2), new SimpleLinearRule( new double[] { 0.00, 0.00, 0.30, 0.0, 0.50, 0.00, 1, -1, 6, 3, 7 }, new double[] { 0.40, 0.25, 0.60, 0.3, 0.30, 0.15, 11, 4, 11, 11, 11 }, 3), new SimpleLinearRule( new double[] { 0.00, 0.00, 0.00, 0.0, 0.10, 0.10, 0, 0, 0, 5, 0 }, new double[] { 0.30, 0.20, 0.30, 0.6, 0.40, 0.50, 150, 150, 7, 11, 11 }, 4), new SimpleLinearRule( new double[] { 0.00, 0.00, 0.00, 0.00, 0.10, 0.00, 2, -1, 2, 6, 5 }, new double[] { 0.20, 0.20, 0.20, 0.15, 0.50, 0.30, 150, 4, 9, 11, 11 }, 5), new SimpleLinearRule( new double[] { 0.00, 0.00, 0.00, 0.0, 0.10, 0.00, 2, -1, 8, 2, 6 }, new double[] { 0.90, 0.20, 0.30, 0.4, 0.50, 0.05, 21, 3, 11, 9, 11 }, 6) }; var fis = new ANFIS(ruleset); var groopy = userRawData.ToLookup(z => z.Id, z => z.GroupId).Where(z => z.Count() < 150); var sb = new StringBuilder(); sb.AppendLine($"ID, {string.Join(", ", Enumerable.Range(0, 7).Select(z => $"Стадия {z}"))}"); var trainset = new StringBuilder(); trainset.AppendLine("ID, %s6-1a, %s6-1b, %s6-1c, %s6-1d, %s6-2, %s6-3, s6-2, s6-3, s7-1, s7-2, s7-3"); var count = groopy.Count(); var dic = opposition.ToDictionary(z => z.Id, z => z); foreach (var user in groopy) { var codes = user.Where(z => juxtaposition.ContainsKey(z)).Select(z => juxtaposition[z]); if (codes.Any()) { var overall = (double)codes.Count(); var line = $"{user.Key}, "; var opp = new double[3]; foreach (var pub in user) { if (dic.ContainsKey(pub)) { opp[0] = Math.Max(opp[0], dic[pub].s7_1); opp[1] = Math.Max(opp[1], dic[pub].s7_2); opp[2] = Math.Max(opp[2], dic[pub].s7_3); } } var input = new double[] { codes.Count(z => z.Equals("s6-1a")) / overall, codes.Count(z => z.Equals("s6-1b")) / overall, codes.Count(z => z.Equals("s6-1c")) / overall, codes.Count(z => z.Equals("s6-1d")) / overall, codes.Count(z => z.Equals("s6-2")) / overall, codes.Count(z => z.Equals("s6-3")) / overall, codes.Count(z => z.Equals("s6-2")), codes.Count(z => z.Equals("s6-3")), opp[0], opp[1], opp[2] }; var res = fis.Inference(input); if (res.Any(z => double.IsNaN(z))) { res = new double[] { 1.0, 0, 0, 0, 0, 0, 0 } } ; line += string.Join(", ", res.Select(z => z.ToString("0.00", CultureInfo.InvariantCulture))); sb.AppendLine(line); trainset.AppendLine($"{user.Key},{string.Join(", ", input.Select(z => z.ToString("0.0000", CultureInfo.InvariantCulture)))}"); } else { sb.AppendLine($"{user.Key}, {string.Join(", ", Enumerable.Range(0, 7).Select(z => "0.00"))}"); } Console.Write($"\r Remains {count--} "); } File.WriteAllText("report_common.csv", sb.ToString()); File.WriteAllText("origin.csv", trainset.ToString()); }