Changeset 12024 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/ExpressionExtender.cs
- Timestamp:
- 02/17/15 16:03:49 (10 years ago)
- File:
-
- 1 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 }
Note: See TracChangeset
for help on using the changeset viewer.