Changeset 12024 for branches/HeuristicLab.Problems.GrammaticalOptimization
- Timestamp:
- 02/17/15 16:03:49 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/ExpressionExtender.cs
r12014 r12024 11 11 namespace HeuristicLab.Algorithms.Bandits { 12 12 // helper to create canonical forms of expressions 13 // NOTE: Not implemented yet (see unit test for divisions)14 13 // TODO: change symbolicregressionpoly10problem to use this class 15 14 // this does not support expressions with constants (in transformations we assume constant opt is used) … … 19 18 // supports the grammar 20 19 // G(E): 21 // E -> V | V+E | V-E | V*E | V%E | V/E |(E)20 // E -> V | V+E | V-E | V*E | V%E | (E) 22 21 // V -> <variables> 23 22 // "; 24 23 25 // might produce expressions of the form |/x24 // might produce expressions of the form /x (= 1/x) 26 25 // the pipe symbol | is used for the constant one (in comparison to other constants) 27 26 private string sentence; … … 31 30 InitLex(phrase); 32 31 var e = CanonicalExpr(); 33 return e.ToString(); 32 return e.ToString(); 34 33 } 35 34 … … 97 96 if (!f.IsSimpleFactor && f.Expr.Terms.Count == 1) { 98 97 foreach (var invF in f.Expr.Terms.First().Factors) { 99 if (invF.ToString() == " |") continue;98 if (invF.ToString() == "1") continue; 100 99 invF.Invert(); 101 100 factors.Add(invF); … … 173 172 var results = new List<Factor>(factors); 174 173 foreach (var f in factorsArr) { 175 if (f.ToString() == " |") results.Remove(f);176 if (f.ToString() == " |/(|)") results.Remove(f);174 if (f.ToString() == "1") results.Remove(f); 175 if (f.ToString() == "1/(1)") results.Remove(f); 177 176 if (f.IsInverse) { 178 177 // find matching … … 185 184 results.Remove(match); 186 185 if (!results.Any()) 187 results.Insert(idx, new Factor(' |')); // when the factor is the last one then insert a one186 results.Insert(idx, new Factor('1')); // when the factor is the last one then insert a one 188 187 189 188 // also mark as cancelled in the factorsArr 190 189 idx = Array.IndexOf(factorsArr, match); 191 factorsArr[idx] = new Factor(' |');190 factorsArr[idx] = new Factor('1'); 192 191 } 193 192 } 194 193 } 195 // remove all unnecessary "1* factors" 196 197 if (results.Count == 0) results.Add(new Factor('|')); 194 if (results.Count == 0) results.Add(new Factor('1')); 198 195 return results; 199 196 } … … 230 227 var countComp = Factors.Count().CompareTo(other.Factors.Count()); 231 228 if (countComp != 0) return countComp; 232 return string.Join("", Factors).CompareTo(string.Join("", other.Factors)); 229 foreach (var pair in Factors.Zip(other.Factors, Tuple.Create)) { 230 var fComp = pair.Item1.CompareTo(pair.Item2); 231 if (fComp != 0) return fComp; 232 } 233 return 0; 233 234 } 234 235 } … … 304 305 var s = Expr == null ? symbol.ToString() : "(" + expr.ToString() + ")"; 305 306 if (IsInverse) { 306 return " |/" + s;307 return "/" + s; 307 308 } else return s; 308 309 } … … 356 357 357 358 public override string ToString() { 358 // if (Inverse && Terms.Count > 1)359 // return "(" + string.Join("+", Terms) + ")";360 // else if (Inverse && Terms.Count == 1) {361 // return Terms.First().ToString().Replace("%", "#").Replace("*", "%").Replace("#", "*");362 // } else363 359 return string.Join("+", Terms); 364 360 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.SymbReg/ExpressionCompiler.cs
r12014 r12024 95 95 if (f != null) factors.Add(f); 96 96 var curSy = CurSy(); 97 while (curSy == '*' || curSy == '%' || curSy == '/') { // division and protected division symbols are handled in the same way97 while (curSy == '*' || curSy == '%') { // division and protected division symbols are handled in the same way 98 98 if (curSy == '*') { 99 99 NewSy(); … … 135 135 r = variables[varIdx]; 136 136 NewSy(); 137 } else if (curSy == ' |') {138 // pipe symbol is used in the expressionextender to represent constant one (|/x).137 } else if (curSy == '/') { 138 // /-symbol used in the expressionextender to represent inverse (1/x). 139 139 // this is necessary because we also use symbols 0..9 as indices for ERCs 140 r = 1.0;141 140 NewSy(); 141 r = 1.0 / Fact(variables, constants); 142 142 } else if (curSy >= '0' && curSy <= '9') { 143 143 int o = (byte)curSy - (byte)'0'; -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.SymbReg/SymbolicRegressionProblem.cs
r12014 r12024 126 126 127 127 public double Evaluate(string sentence) { 128 var extender = new ExpressionExtender(); 129 sentence = extender.CanonicalRepresentation(sentence); 128 130 if (useConstantOpt) 129 131 return OptimizeConstantsAndEvaluate(sentence); 130 132 else { 131 var extender = new ExpressionExtender();132 133 133 134 Debug.Assert(SimpleEvaluate(sentence) == SimpleEvaluate(extender.CanonicalRepresentation(sentence))); … … 152 153 153 154 public IEnumerable<Feature> GetFeatures(string phrase) { 155 // throw new NotImplementedException(); 154 156 phrase = CanonicalRepresentation(phrase); 155 return new Feature[] { new Feature(phrase, 1.0) }; 157 return phrase.Split('+').Distinct().Select(t => new Feature(t, 1.0)); 158 // return new Feature[] { new Feature(phrase, 1.0) }; 156 159 } 157 160 -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/ExpressionInterpreter.cs
r12014 r12024 92 92 r = Fact(d, erc); 93 93 var curSy = CurSy(); 94 while (curSy == '*' || curSy == '%' || curSy == '/') { // division and protected division symbols are handled in the same way94 while (curSy == '*' || curSy == '%') { 95 95 if (curSy == '*') { 96 96 NewSy(); … … 124 124 r = d[o]; 125 125 NewSy(); 126 } else if (curSy == ' |') {127 // pipe symbol is used in the expressionextender to represent constant one (|/x).126 } else if (curSy == '/') { 127 // /-symbol is used in the expressionextender to represent inverse (1/x). 128 128 // this is necessary because we also use symbols 0..9 as indices for ERCs 129 r = 1.0;130 129 NewSy(); 130 r = 1.0 / Fact(d, erc); 131 131 } else /* if (curSy >= '0' && curSy <= '9') */ { 132 132 int o = (byte)curSy - (byte)'0'; -
branches/HeuristicLab.Problems.GrammaticalOptimization/Test/TestCanonicalExpressions.cs
r12014 r12024 67 67 Assert.AreEqual("a*b+b*c", extender.CanonicalRepresentation("b*(c-a)")); 68 68 Assert.AreEqual("a*b+a*d+b*c+c*d", extender.CanonicalRepresentation("(b-d)*(c-a)")); 69 Assert.AreEqual(" |/(a+b)", extender.CanonicalRepresentation("c%c%(a+b)"));69 Assert.AreEqual("/(a+b)", extender.CanonicalRepresentation("c%c%(a+b)")); 70 70 } 71 71 [TestMethod] 72 72 public void TestDivisionExpansion() { 73 73 var extender = new ExpressionExtender(); 74 Assert.AreEqual("a* |/c+b*|/c", extender.CanonicalRepresentation("(b+a)%c"));75 Assert.AreEqual("a* |/c*|/d+b*|/c*|/d", extender.CanonicalRepresentation("(b+a)%(d*c)"));76 Assert.AreEqual("a* |/(c+d)+b*|/(c+d)", extender.CanonicalRepresentation("(b-a)%(d-c)"));77 Assert.AreEqual("a*b* |/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)"));74 Assert.AreEqual("a*/c+b*/c", extender.CanonicalRepresentation("(b+a)%c")); 75 Assert.AreEqual("a*/c*/d+b*/c*/d", extender.CanonicalRepresentation("(b+a)%(d*c)")); 76 Assert.AreEqual("a*/(c+d)+b*/(c+d)", extender.CanonicalRepresentation("(b-a)%(d-c)")); 77 Assert.AreEqual("a*b*/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)")); 78 78 79 Assert.AreEqual("a*b* |/(a+b)*|/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)%(a+b)"));80 Assert.AreEqual("a*b* |/(c+d)*|/(a*|/e+b*|/e)", extender.CanonicalRepresentation("((b*a)%(d-c))%((a+b)%e)"));79 Assert.AreEqual("a*b*/(a+b)*/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)%(a+b)")); 80 Assert.AreEqual("a*b*/(c+d)*/(a*/e+b*/e)", extender.CanonicalRepresentation("((b*a)%(d-c))%((a+b)%e)")); 81 81 // a*b*e%(c+d)%(a+b) 82 82 } … … 84 84 public void TestDivisionCancellation() { 85 85 var extender = new ExpressionExtender(); 86 Assert.AreEqual(" |", extender.CanonicalRepresentation("a%a"));86 Assert.AreEqual("1", extender.CanonicalRepresentation("a%a")); 87 87 Assert.AreEqual("a", extender.CanonicalRepresentation("a*a%a")); 88 Assert.AreEqual(" |/a", extender.CanonicalRepresentation("(a%a)%a"));89 Assert.AreEqual(" |/a", extender.CanonicalRepresentation("a%a%a"));88 Assert.AreEqual("/a", extender.CanonicalRepresentation("(a%a)%a")); 89 Assert.AreEqual("/a", extender.CanonicalRepresentation("a%a%a")); 90 90 Assert.AreEqual("a", extender.CanonicalRepresentation("a%(a%a)")); 91 Assert.AreEqual(" |", extender.CanonicalRepresentation("(a+b)%(b+a)"));92 Assert.AreEqual(" |/a+|/b", extender.CanonicalRepresentation("(a+b)%(a*b)"));93 Assert.AreEqual("a* |/(a*c*|/b+e*|/d*|/f)+b*|/(a*c*|/b+e*|/d*|/f)", extender.CanonicalRepresentation("(a+b)%(a%b*c+e%f%d)"));94 Assert.AreEqual(" |", extender.CanonicalRepresentation("(a%a%a+b%b%b)%(a%a*a%a%a+b%b*b%b%b)"));95 Assert.AreEqual(" |", extender.CanonicalRepresentation("(a%(a%a)+b%(b%b))%(a+b)"));91 Assert.AreEqual("1", extender.CanonicalRepresentation("(a+b)%(b+a)")); 92 Assert.AreEqual("/a+/b", extender.CanonicalRepresentation("(a+b)%(a*b)")); 93 Assert.AreEqual("a*/(a*c*/b+e*/d*/f)+b*/(a*c*/b+e*/d*/f)", extender.CanonicalRepresentation("(a+b)%(a%b*c+e%f%d)")); 94 Assert.AreEqual("1", extender.CanonicalRepresentation("(a%a%a+b%b%b)%(a%a*a%a%a+b%b*b%b%b)")); 95 Assert.AreEqual("1", extender.CanonicalRepresentation("(a%(a%a)+b%(b%b))%(a+b)")); 96 96 } 97 97
Note: See TracChangeset
for help on using the changeset viewer.