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 subtestIris(double[][] x, double[][] y, double[][] tx, double[][] ty, ITraining bprop) { KMEANSExtractorI extractor = new KMEANSExtractorI(15); var timer = Stopwatch.StartNew(); ANFIS fis = ANFISBuilder <GaussianRule2> .Build(x, y, extractor, bprop, 1000); timer.Stop(); double err = bprop.Error(tx, ty, fis.RuleBase); double correctClass = 0; for (int i = 0; i < tx.Length; i++) { double[] o = fis.Inference(tx[i]); for (int j = 0; j < ty[i].Length; j++) { if (ty[i][j] == 1.0 && o[j] == o.Max()) { correctClass++; } } } Trace.WriteLine(string.Format("[{1}]\tIris Dataset Error {0} Classification Error {4}\tElapsed {2}\tRuleBase {3}", err, bprop.GetType().Name, timer.Elapsed, fis.RuleBase.Length, 1.0 - correctClass / ty.Length), "training"); Assert.IsFalse(ty.Length - correctClass > 2); }
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); }
public void getMusic(double[] levelParam) { double[] levelMusicParam = fis.Inference(levelParam); for (int i = 0; i < levelMusicParam.Length; i++) { Debug.Log(levelMusicParam[i]); } int step = (int)(levelMusicParam[3] / 60) * 30; string url = "https://www.wolframcloud.com/objects/user-a13d29f3-43bf-4b00-8e9b-e55639ecde19/NKMMusicDownload" + "?id=NKM-G-10-" + (int)levelMusicParam[0] + "-" + (int)levelMusicParam[1] + "-1-" + (int)levelMusicParam[2] + "-" + step + "-" + (int)levelMusicParam[3] + "-4-2773-" + (int)levelMusicParam[4] + "-0-1-" + getRole((int)levelMusicParam[5], roles) + "-1-" + getRole((int)levelMusicParam[6], roles) + "-1-" + getRole((int)levelMusicParam[7], roles) + "-0-0-0-0-0&form=WAV"; WWW www = new WWW(url); //yield return www //musicSource = GetComponent<AudioSource>(); musicSource.clip = www.GetAudioClip(false, false, AudioType.WAV); }
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()); }