Changeset 12298 for branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization
- Timestamp:
- 04/10/15 16:12:08 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/PartialExpressionInterpreter.cs
r12291 r12298 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 using System.Security.Policy; 4 5 using HeuristicLab.Common; 5 6 … … 9 10 private string sentence; 10 11 private int syIdx; 12 private HashSet<double> intermediateValues = new HashSet<double>(); 11 13 private Stack<double> stack = new Stack<double>(); 12 private Stack<char> opStack = new Stack<char>();13 14 // interprets sentences from L(G(Expr)): 14 15 // Expr -> Term { ('+' | '-' | '^' ) Term } … … 22 23 // The constant symbols '0' .. '9' are treated as ERC indices 23 24 24 public Stack<double> Interpret(string sentence, double[] vars) {25 public IEnumerable<double> Interpret(string sentence, double[] vars) { 25 26 return Interpret(sentence, vars, emptyErc); 26 27 } 27 28 28 public Stack<double> Interpret(string sentence, double[] vars, double[] erc) {29 public IEnumerable<double> Interpret(string sentence, double[] vars, double[] erc) { 29 30 InitLex(sentence); 30 stack.Clear(); opStack.Clear(); 31 intermediateValues.Clear(); 32 stack.Clear(); 31 33 Expr(vars, erc); 32 return new Stack<double>(stack);34 return intermediateValues; 33 35 } 34 36 … … 58 60 if (curSy == '+') { 59 61 NewSy(); 60 if (!Term(d, erc)) { stack.Push(-1.0);return false; }62 if (!Term(d, erc)) { return false; } 61 63 stack.Push(stack.Pop() + stack.Pop()); 64 intermediateValues.Add(stack.Peek()); 62 65 } else if (curSy == '-') { 63 66 NewSy(); 64 if (!Term(d, erc)) { stack.Push(-2.0); return false;return false; }67 if (!Term(d, erc)) { return false; } 65 68 stack.Push(-stack.Pop() + stack.Pop()); 69 intermediateValues.Add(stack.Peek()); 66 70 } else { 67 71 NewSy(); 68 if (!Term(d, erc)) { stack.Push(-3.0);return false; }72 if (!Term(d, erc)) { return false; } 69 73 var e = stack.Pop(); 70 74 var r = stack.Pop(); 71 75 stack.Push(Not(r) * e + r * Not(e)); // xor = (!x AND y) OR (x AND !y) 76 intermediateValues.Add(stack.Peek()); 72 77 } 73 78 curSy = CurSy(); … … 82 87 if (curSy == '*') { 83 88 NewSy(); 84 if (!Fact(d, erc)) { stack.Push(-4.0);return false; }89 if (!Fact(d, erc)) { return false; } 85 90 stack.Push(stack.Pop() * stack.Pop()); 91 intermediateValues.Add(stack.Peek()); 86 92 } else { 87 93 NewSy(); 88 if (!Fact(d, erc)) { stack.Push(-5.0);return false; }94 if (!Fact(d, erc)) { return false; } 89 95 var nom = stack.Pop(); 90 96 var r = stack.Pop(); 91 97 if (HeuristicLab.Common.Extensions.IsAlmost(nom, 0.0)) nom = 1.0; 92 98 stack.Push(r / nom); 99 intermediateValues.Add(stack.Peek()); 93 100 } 94 101 curSy = CurSy(); … … 100 107 var curSy = CurSy(); 101 108 if (curSy == '!') { 102 NewSy();103 if (!Expr(d, erc)) { stack.Push(-7.0); return false; }104 stack.Push(Not(stack.Pop()));109 //NewSy(); 110 //if (!Expr(d, erc)) { stack.Push(-7.0); return false; } 111 //stack.Push(Not(stack.Pop())); 105 112 } else if (curSy == '(') { 106 NewSy();107 if (!Expr(d, erc)) { stack.Push(-8.0); return false; }108 if (CurSy() != ')') throw new ArgumentException();109 NewSy();113 //NewSy(); 114 //if (!Expr(d, erc)) { stack.Push(-8.0); return false; } 115 //if (CurSy() != ')') throw new ArgumentException(); 116 //NewSy(); 110 117 } else if (curSy >= 'a' && curSy <= 'z') { 111 118 int o = (byte)curSy - (byte)'a'; … … 113 120 if (o < 0 || o >= d.Length) throw new ArgumentException(); 114 121 stack.Push(d[o]); 122 intermediateValues.Add(stack.Peek()); 115 123 NewSy(); 116 124 } else if (curSy == '/') { 117 125 // /-symbol is used in the expressionextender to represent inverse (1/x). 118 126 // this is necessary because we also use symbols 0..9 as indices for ERCs 119 NewSy();120 if (!Fact(d, erc)) { stack.Push(-9.0); return false; }121 stack.Push(1.0 / stack.Pop());127 //NewSy(); 128 //if (!Fact(d, erc)) { stack.Push(-9.0); return false; } 129 //stack.Push(1.0 / stack.Pop()); 122 130 } else if (curSy >= '0' && curSy <= '9') { 123 131 int o = (byte)curSy - (byte)'0'; … … 125 133 if (o < 0 || o >= 10) throw new ArgumentException(); 126 134 stack.Push(erc[o]); 135 intermediateValues.Add(stack.Peek()); 127 136 NewSy(); 128 137 } else { -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs
r12295 r12298 156 156 // splits the phrase into terms and creates (sparse) term-occurrance features 157 157 public IEnumerable<Feature> GetFeatures(string phrase) { 158 // var canonicalTerms = new HashSet<string>(); 159 // foreach (string t in phrase.Split('+')) { 160 // canonicalTerms.Add(CanonicalTerm(t)); 158 //if (phrase.EndsWith("E")) phrase = phrase.TrimEnd('*', '+', 'E'); 159 //yield return new Feature("$$$", 1.0); // const 160 //var canonicalTerms = new HashSet<string>(); 161 //foreach (string t in phrase.Split('+')) { 162 // canonicalTerms.Add(CanonicalTerm(t)); 163 //} 164 //return canonicalTerms.Select(entry => new Feature(entry, 1.0)); 165 //.Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) }); 166 167 168 if (phrase.EndsWith("E")) phrase = phrase.TrimEnd('*', '+', 'E'); 169 //var len = 5; 170 //var start = Math.Max(0, phrase.Length - len); 171 //var end = Math.Min(phrase.Length, start + len); 172 //string f = phrase.Substring(start, end - start); 173 //yield return new Feature(f, 1.0); 174 // 175 176 var terms = phrase.Split('+'); 177 foreach (var t in terms.Distinct()) yield return new Feature(t, 1.0); 178 179 for (int i = 0; i < terms.Length; i++) { 180 for (int j = i + 1; j < terms.Length; j++) { 181 yield return new Feature(terms[i] + " " + terms[j], 1.0); 182 } 183 } 184 185 // var substrings = new HashSet<string>(); 186 // for (int start = 0; start <= phrase.Length - 2; start += 2) { 187 // var s = phrase.Substring(start, 3); 188 // substrings.Add(s); 161 189 // } 162 // return canonicalTerms.Select(entry => new Feature(entry, 1.0)) 163 // .Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) }); 164 165 return new Feature[] { new Feature(phrase, 1.0), }; 166 167 // var partialInterpreter = new PartialExpressionInterpreter(); 168 // var vars = new double[] { 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, }; 169 // var s = partialInterpreter.Interpret(phrase, vars); 170 // //if (s.Any()) 171 // // return new Feature[] { new Feature(s.Pop().ToString(), 1.0), }; 172 // //else 173 // // return new Feature[] { new Feature("$", 1.0), }; 174 // return new Feature[] { new Feature(string.Join(",", s), 1.0) }; 190 // 191 // var list = new List<string>(substrings); 192 // 193 // for (int i = 0; i < list.Count; i++) { 194 // yield return new Feature(list[i], 1.0); 195 // //for (int j = i+1; j < list.Count; j++) { 196 // // yield return new Feature(list[i] + " " + list[j], 1.0); 197 // //} 198 // } 199 200 // 201 // for (int len = 1; len <= phrase.Length; len += 2) { 202 // var start = Math.Max(0, phrase.Length - len); 203 // var end = Math.Min(phrase.Length, start + len); 204 // string f = phrase.Substring(start, end - start); 205 // yield return new Feature(f, 1.0); 206 // 207 // } 208 209 //var partialInterpreter = new PartialExpressionInterpreter(); 210 //var vars = new double[] { 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, }; 211 //var s = partialInterpreter.Interpret(phrase, vars); 212 ////if (s.Any()) 213 //// return new Feature[] { new Feature(s.Pop().ToString(), 1.0), }; 214 ////else 215 //// return new Feature[] { new Feature("$", 1.0), }; 216 //return s.Select(f => new Feature(f.ToString(), 1.0)); 175 217 } 176 218 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/SentenceSetStatistics.cs
r11865 r12298 50 50 public override string ToString() { 51 51 return 52 string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5,30} first {6,7:F5} {7,20} last {8,7:F5} {9,20}",52 string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5,30} last {6,7:F5} {7,20}", 53 53 NumberOfSentences, AverageQuality, 54 54 BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, bestKnownQuality) ? 1.0 : 0.0, 55 55 BestSentenceIndex, TrimToSize(BestSentence, 30), 56 FirstSentenceQuality, TrimToSize(FirstSentence, 20),57 56 LastSentenceQuality, TrimToSize(LastSentence, 20) 57 //LastSentenceQuality, TrimToSize(LastSentence, 20) 58 58 ); 59 59 }
Note: See TracChangeset
for help on using the changeset viewer.