private static List <Regulation> GetProduction(SyntaxTree syntaxTree) { // <Production> ::= <Vn> "::=" <Canditate> <RightPartList> ; List <Regulation> result = new List <Regulation>(); if (syntaxTree.NodeType.Type == ContextfreeGrammarTreeNodeType.__Production) { Vn left = GetVn(syntaxTree.Children[0]); TreeNodeType leftNode = left.GetTreeNodeType(); { Candidate candidate = GetCandidate(syntaxTree.Children[2]); var regulation = new Regulation(leftNode, (from item in candidate select item.GetTreeNodeType()).ToArray()); result.Add(regulation); } RightPartList rightPartList = GetRightPartList(syntaxTree.Children[3]); foreach (var candidate in rightPartList) { var regulation = new Regulation(leftNode, (from item in candidate select item.GetTreeNodeType()).ToArray()); result.Add(regulation); } } return(result); }
/// <summary> /// The "kernel" of the Goertzel recursive calculation. /// </summary> /// <param name="sample">Array of samples to pass through the filter</param> /// <param name="k"> The Goertzel coefficient </param> /// <param name="vn"> Previous (two) results</param> /// <returns>The new value of "previous two results" </returns> public static Vn kernel(double[] sample, double k, Vn vn) { foreach (double x in sample) { double t = k * vn._1 - vn._2 + x; vn._2 = vn._1; vn._1 = t; } return(vn); }
/// <summary> /// Calculate the power of the signal that was passed through /// the filter. /// </summary> /// <param name="koef"> The Goertzel coefficient </param> /// <param name="vn"> Previous (two) results</param> /// <param name="n"> The number of samples that have passed through the /// filter </param> /// <returns> The calculated power </returns> static double power(double koef, Vn vn, int n) { double rslt = vn._1 * vn._1 + vn._2 * vn._2 - koef * vn._1 * vn._2; if (rslt < Double.Epsilon) { rslt = Double.Epsilon; } return(rslt / (n * n)); }
public void Build(string[] Input) { try { Start = Input[0][0]; foreach (string s in Input) { if (s == string.Empty) { continue; } string[] tp = s.Split(new String[] { "→" }, StringSplitOptions.None); if (tp.Length != 2) { throw new Exception("产生式应该有且仅有一个→"); } if (tp[0].Length != 1 || !isVn(tp[0][0])) { throw new Exception("产生式左边应该是一个非终结符"); } char left = tp[0][0]; char empty = System.Configuration.ConfigurationManager.AppSettings["Empty"][0]; Vn.Add(left); string[] right = tp[1].Split('|'); for (int i = 0; i < right.Count(); ++i) { string part = right[i]; foreach (char c in part) { if (isVn(c)) { Vn.Add(c); } else { Vt.Add(c); } } Production p = new Production(left, part); if (!ProductionsMap.ContainsKey(p)) { Productions.Add(p); ProductionsMap.Add(p, ProductionsMap.Count() + 1); _ProductionsMap.Add(_ProductionsMap.Count() + 1, p); } } } } catch (Exception) { throw new Exception("输入不合法\n"); } }
private static Vn GetVn(SyntaxTree syntaxTree) { // <Vn> ::= "<" identifier ">" ; Vn vn = null; if (syntaxTree.NodeType.Type == ContextfreeGrammarTreeNodeType.__Vn) { string identifierContent = syntaxTree.Children[1].NodeType.Content; vn = new Vn(identifierContent); } return(vn); }
/// <summary> /// Process the given array of samples on this filter. /// </summary> /// <returns> The current power of the signal passed through the /// filter (from the start). /// </returns> public double Process(double[] samples) { vn_ = kernel(samples, koef_, vn_); return(power(koef_, vn_, samples.Length)); }
public async Task Vndb(params string[] args) { Utilities.CheckAvailability(Context.Guild.Id, Program.Module.Vn); await p.DoAction(Context.User, Context.Guild.Id, Program.Module.Vn); var result = await Vn.SearchVn(args, !((ITextChannel)Context.Channel).IsNsfw); switch (result.error) { case Error.Vn.Help: await ReplyAsync(Sentences.VndbHelp(Context.Guild.Id)); break; case Error.Vn.NotFound: await ReplyAsync(Sentences.VndbNotFound(Context.Guild.Id)); break; case Error.Vn.None: EmbedBuilder embed = new EmbedBuilder() { Title = result.answer.originalTitle == null ? result.answer.title : result.answer.originalTitle + " (" + result.answer.title + ")", Url = result.answer.vnUrl, ImageUrl = result.answer.imageUrl, Description = result.answer.description, Color = Color.Blue }; embed.AddField(Sentences.AvailableEnglish(Context.Guild.Id), result.answer.isAvailableEnglish ? Base.Sentences.YesStr(Context.Guild.Id) : Base.Sentences.NoStr(Context.Guild.Id), true); embed.AddField(Sentences.AvailableWindows(Context.Guild.Id), result.answer.isAvailableWindows ? Base.Sentences.YesStr(Context.Guild.Id) : Base.Sentences.NoStr(Context.Guild.Id), true); string length = Sentences.Unknown(Context.Guild.Id); switch (result.answer.length) { case VisualNovelLength.VeryShort: length = Sentences.Hours(Context.Guild.Id, "< 2 "); break; case VisualNovelLength.Short: length = Sentences.Hours(Context.Guild.Id, "2 - 10 "); break; case VisualNovelLength.Medium: length = Sentences.Hours(Context.Guild.Id, "10 - 30 "); break; case VisualNovelLength.Long: length = Sentences.Hours(Context.Guild.Id, "30 - 50 "); break; case VisualNovelLength.VeryLong: length = Sentences.Hours(Context.Guild.Id, "> 50 "); break; } embed.AddField(Sentences.Length(Context.Guild.Id), length, true); embed.AddField(Sentences.VndbRating(Context.Guild.Id), result.answer.rating + " / 10", true); string releaseDate; if (result.answer.releaseYear == null) { releaseDate = Sentences.Tba(Context.Guild.Id); } else { releaseDate = result.answer.releaseYear.ToString(); if (result.answer.releaseMonth != null) { releaseDate = Utilities.AddZero(result.answer.releaseMonth.ToString()) + "/" + releaseDate; } if (result.answer.releaseDay != null) { releaseDate = Utilities.AddZero(result.answer.releaseDay.ToString()) + "/" + releaseDate; } } embed.AddField(Sentences.ReleaseDate(Context.Guild.Id), releaseDate, true); await ReplyAsync("", false, embed.Build()); break; default: throw new NotImplementedException(); } }
private void startBtn_Click(object sender, EventArgs e) { double a = Convert.ToDouble(aBox.Text); double b = Convert.ToDouble(bBox.Text); double d = Convert.ToDouble(dBox.Text); int T = Convert.ToInt32(Tbox.Text); Random generator = new Random(); int round = 0; int l = (int)Math.Ceiling(Math.Log(((b - a) * (1 / d)) + 1, 2)); double pom = d; while (pom < 1) { round++; pom *= 10; } List <Individual> individuals = new List <Individual>(); List <List <Individual> > ListofListVc = new List <List <Individual> >(); List <Individual> ListVcBest = new List <Individual>(); Individual Vc; Individual VBest = null; Individual Vn; List <Individual> listVc; for (int i = 0; i < T; i++) { listVc = new List <Individual>(); Vc = HC.MakeFirstInd(a, b, d, l, generator); listVc.Add(Vc.Clone()); if (i == 0) { VBest = Vc.Clone(); } while (true) { Vn = HC.MakeVn(Vc, a, b, l, round); if (Vn.Fx > Vc.Fx) { Vc = Vn.Clone(); listVc.Add(Vc.Clone()); } else { break; } } ListofListVc.Add(listVc); if (VBest.Fx < Vc.Fx) { VBest = Vc.Clone(); } ListVcBest.Add(VBest.Clone()); if (VBest.Xreal == 10.999) { break; } } individuals.Add(VBest); var bindingList = new BindingList <Individual>(individuals); var source = new BindingSource(bindingList, null); table.DataSource = source; ToTxt.WriteToFile(ListofListVc, T, d); MakeChart(ListofListVc, ListVcBest); }
private void startTestBtn_Click(object sender, EventArgs e) { double a = Convert.ToDouble(aBox.Text); double b = Convert.ToDouble(bBox.Text); double d = Convert.ToDouble(dBox.Text); Random generator = new Random(); int round = 0; int l = (int)Math.Ceiling(Math.Log(((b - a) * (1 / d)) + 1, 2)); double pom = d; while (pom < 1) { round++; pom *= 10; } List <List <Individual> > ListofListVc = new List <List <Individual> >(); List <Individual> ListVcBest = new List <Individual>(); Individual Vc; Individual VBest = null; Individual Vn; List <Individual> listVc; List <Generation> listGen = new List <Generation>(); for (int i = 0; i < 3000; i++) { Generation generation = new Generation { Iteration = i + 1 }; listGen.Add(generation); } for (int j = 0; j < 10000; j++) { for (int i = 0; i < 3000; i++) { listVc = new List <Individual>(); Vc = HC.MakeFirstInd(a, b, d, l, generator); listVc.Add(Vc.Clone()); if (i == 0) { VBest = Vc.Clone(); } while (true) { Vn = HC.MakeVn(Vc, a, b, l, round); if (Vn.Fx > Vc.Fx) { Vc = Vn.Clone(); listVc.Add(Vc.Clone()); } else { break; } } ListofListVc.Add(listVc); if (VBest.Fx < Vc.Fx) { VBest = Vc.Clone(); } ListVcBest.Add(VBest.Clone()); if (VBest.Xreal == 10.999) { listGen[i].SolveCount += 1; break; } } } for (int i = 1; i < listGen.Count; i++) { listGen[i].Cumulation += listGen[i - 1].SolveCount + listGen[i - 1].Cumulation; } MakeTestChart(listGen); var bindingList = new BindingList <Generation>(listGen); var source = new BindingSource(bindingList, null); testTable.DataSource = source; }