Changeset 12014
- Timestamp:
- 02/16/15 09:14:38 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/ExpressionExtender.cs
r11981 r12014 19 19 // supports the grammar 20 20 // G(E): 21 // E -> V | V+E | V-E | V*E | V%E | (E)21 // E -> V | V+E | V-E | V*E | V%E | V/E | (E) 22 22 // V -> <variables> 23 23 // "; 24 24 25 // transformations: 26 // - transform c*b*a to a*b*c (lexicographic ordering for factors, cached) 27 // - transfrom - to + (assumed optimized constants for terms) 28 25 // might produce expressions of the form |/x 26 // the pipe symbol | is used for the constant one (in comparison to other constants) 29 27 private string sentence; 30 28 private int syIdx; 31 29 32 30 public string CanonicalRepresentation(string phrase) { 33 throw new NotImplementedException();34 31 InitLex(phrase); 35 32 var e = CanonicalExpr(); 36 return e.ToString(); 33 return e.ToString(); 37 34 } 38 35 … … 97 94 NewSy(); 98 95 f = CanonicalFact(); 99 f.Invert(); 100 if (f != null) factors.Add(f); 96 // if there is only one term we can add multiple inverted simple factors instead of the whole inverted expression 97 if (!f.IsSimpleFactor && f.Expr.Terms.Count == 1) { 98 foreach (var invF in f.Expr.Terms.First().Factors) { 99 if (invF.ToString() == "|") continue; 100 invF.Invert(); 101 factors.Add(invF); 102 } 103 } else { 104 f.Invert(); 105 if (f != null) factors.Add(f); 106 } 101 107 } 102 108 curSy = CurSy(); 103 109 } 110 111 factors = CancelFactors(factors).ToList(); 104 112 return ExpandFactors(factors); 105 113 } 106 114 107 /*108 private List<Factor> CancelFactors(Term t) {109 var nonInvF = t.Factors.Where(f => !f.IsInverse).ToArray();110 var invF = t.Factors.Where(f => f.IsInverse).ToArray();111 112 var result = new List<Factor>();113 foreach (var f in nonInvF) {114 if (!invF.Contains(f)) {115 result.Add(f);116 }117 }118 if (result.Count == 0) result.Add(new Factor('1'));119 return result;120 }121 */122 115 // canonical fact returns a factor (either a singe variable, or a set of terms) 123 116 private Factor CanonicalFact() { … … 144 137 145 138 // a list of factors (symbols, or expressions, and possibly inverses are read 146 // a lis to factors symbols or expressions and possibly inverses are produced139 // a list to factors symbols or expressions and possibly inverses are produced 147 140 // all non-inverse expression factors are expanded 148 141 private Expr ExpandFactors(IEnumerable<Factor> factors) { … … 154 147 Expr currentFact = null; 155 148 var firstFactor = factors.First(); 156 if (firstFactor.IsSimpleFactor ) currentFact = new Expr(new Term(firstFactor));149 if (firstFactor.IsSimpleFactor || firstFactor.IsInverse) currentFact = new Expr(new Term(firstFactor)); 157 150 else currentFact = firstFactor.Expr; 158 151 … … 180 173 var results = new List<Factor>(factors); 181 174 foreach (var f in factorsArr) { 182 if (f.ToString() == "1") results.Remove(f); 175 if (f.ToString() == "|") results.Remove(f); 176 if (f.ToString() == "|/(|)") results.Remove(f); 183 177 if (f.IsInverse) { 184 178 // find matching 185 179 Factor match; 186 if (f.IsSimpleFactor) match = factorsArr.FirstOrDefault(other => !other.IsInverse && f.Symbol.Equals(other.Symbol)); 187 else match = factorsArr.FirstOrDefault(other => !other.IsInverse && f.Expr.Equals(other.Expr)); 180 match = factorsArr.FirstOrDefault(other => !other.IsInverse && f.Cancels(other)); 188 181 if (match != null) { 182 results.Remove(f); 183 var idx = results.IndexOf(match); 184 189 185 results.Remove(match); 190 results.Remove(f); 186 if (!results.Any()) 187 results.Insert(idx, new Factor('|')); // when the factor is the last one then insert a one 188 189 // also mark as cancelled in the factorsArr 190 idx = Array.IndexOf(factorsArr, match); 191 factorsArr[idx] = new Factor('|'); 191 192 } 192 193 } 193 194 } 194 if (results.Count == 0) results.Add(new Factor('1')); 195 // remove all unnecessary "1* factors" 196 197 if (results.Count == 0) results.Add(new Factor('|')); 195 198 return results; 196 199 } … … 273 276 this.inv = !inv; 274 277 } 278 public bool Cancels(Factor other) { 279 if (this.inv == other.inv) return false; 280 if (this.Expr != null && other.Expr == null) return false; 281 if (this.Expr == null && other.Expr != null) return false; 282 if (Expr == null) return this.Symbol.Equals(other.symbol); 283 else return this.Expr.CompareTo(other.Expr) == 0; 284 } 275 285 276 286 public int CompareTo(Factor other) { … … 294 304 var s = Expr == null ? symbol.ToString() : "(" + expr.ToString() + ")"; 295 305 if (IsInverse) { 296 return " 1/" + s;306 return "|/" + s; 297 307 } else return s; 298 308 } … … 356 366 var other = obj as Expr; 357 367 if (other == null) return false; 358 //if (this.Inverse != other.Inverse) return false;359 368 if (this.Terms.Count() != other.Terms.Count()) return false; 360 369 return this.Terms.Intersect(other.Terms).Count() == this.Terms.Count; 361 370 } 371 362 372 public override int GetHashCode() { 363 373 var h = 31415; … … 388 398 return ContainsNonTerminal(term.Factors); 389 399 } 390 391 392 /*393 394 internal class FactorComparer : IComparer<char> {395 public int Compare(char x, char y) {396 if (IsNonTerminal(x) && !IsNonTerminal(y)) {397 return 1;398 } else if (!IsNonTerminal(x) && IsNonTerminal(y)) {399 return -1;400 } else if (IsNonTerminal(x) && IsNonTerminal(y)) {401 return x.CompareTo(y);402 } else {403 return x.CompareTo(y);404 }405 }406 }*/407 400 } 408 401 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.SymbReg/ExpressionCompiler.cs
r11972 r12014 17 17 // does not compile to IL it is only necessary to calculate gradients for parameter optimization 18 18 // Expr -> Term { ('+' | '-' | '^' ) Term } 19 // Term -> Fact { ('*' | '%' ) Fact }20 // Fact -> '!' Expr | '(' Expr ')' | Var | const 19 // Term -> Fact { ('*' | '%' | '/') Fact } 20 // Fact -> '!' Expr | '(' Expr ')' | Var | const | one 21 21 // Var -> 'a'..'z' 22 22 // const -> '0' .. '9' 23 // one -> | // pipe symbol for constant one instead of ERC 1 23 24 24 25 // constants are completely ignored, instead we introduce a multiplicative constant factor for each term and an additive constant term for each expression … … 94 95 if (f != null) factors.Add(f); 95 96 var curSy = CurSy(); 96 while (curSy == '*' || curSy == '%' ) {97 while (curSy == '*' || curSy == '%' || curSy == '/') { // division and protected division symbols are handled in the same way 97 98 if (curSy == '*') { 98 99 NewSy(); … … 134 135 r = variables[varIdx]; 135 136 NewSy(); 137 } else if (curSy == '|') { 138 // pipe symbol is used in the expressionextender to represent constant one (|/x). 139 // this is necessary because we also use symbols 0..9 as indices for ERCs 140 r = 1.0; 141 NewSy(); 136 142 } else if (curSy >= '0' && curSy <= '9') { 137 143 int o = (byte)curSy - (byte)'0'; -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.SymbReg/SymbolicRegressionProblem.cs
r11981 r12014 9 9 using System.Text; 10 10 using AutoDiff; 11 using HeuristicLab.Algorithms.Bandits; 11 12 using HeuristicLab.Common; 12 13 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 128 129 return OptimizeConstantsAndEvaluate(sentence); 129 130 else { 131 var extender = new ExpressionExtender(); 132 133 Debug.Assert(SimpleEvaluate(sentence) == SimpleEvaluate(extender.CanonicalRepresentation(sentence))); 130 134 return SimpleEvaluate(sentence); 131 135 } … … 143 147 144 148 public string CanonicalRepresentation(string phrase) { 145 return phrase; 149 var extender = new ExpressionExtender(); 150 return extender.CanonicalRepresentation(phrase); 146 151 } 147 152 148 153 public IEnumerable<Feature> GetFeatures(string phrase) { 149 // first generate canonical expression (which must not contain ()) 150 // recursively split into expressions 151 // for each expression split into terms 152 // for each term order factors to canonical // ../E = * 1/E 154 phrase = CanonicalRepresentation(phrase); 153 155 return new Feature[] { new Feature(phrase, 1.0) }; 154 156 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/ExpressionInterpreter.cs
r11981 r12014 92 92 r = Fact(d, erc); 93 93 var curSy = CurSy(); 94 while (curSy == '*' || curSy == '%' ) {94 while (curSy == '*' || curSy == '%' || curSy == '/') { // division and protected division symbols are handled in the same way 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). 128 // this is necessary because we also use symbols 0..9 as indices for ERCs 129 r = 1.0; 130 NewSy(); 126 131 } else /* if (curSy >= '0' && curSy <= '9') */ { 127 132 int o = (byte)curSy - (byte)'0'; -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Grammar.cs
r11980 r12014 184 184 Debug.Assert(maxLenOfReplacement > 0); 185 185 186 var alts = Get TerminalAlternatives(nt).Where(alt => MinPhraseLength(alt) <= maxLenOfReplacement);186 var alts = GetAlternatives(nt).Where(alt => MinPhraseLength(alt) <= maxLenOfReplacement); 187 187 Debug.Assert(alts.Any()); 188 188 -
branches/HeuristicLab.Problems.GrammaticalOptimization/Test/RunDemo.cs
r11981 r12014 7 7 using HeuristicLab.Algorithms.Bandits.BanditPolicies; 8 8 using HeuristicLab.Algorithms.Bandits.GrammarPolicies; 9 using HeuristicLab.Algorithms.Bandits.Models; 9 10 using HeuristicLab.Algorithms.GrammaticalOptimization; 11 using Microsoft.VisualStudio.TestTools.UnitTesting; 12 using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy; 10 13 11 14 namespace HeuristicLab.Problems.GrammaticalOptimization.Test { 12 class RunDemo { 13 private static void RunGridTest() { 14 int maxIterations = 200000; // for poly-10 with 50000 evaluations no successful try with hl yet 15 [TestClass] 16 public class RunDemo { 17 [TestMethod] 18 public void RunGridTest() { 19 int maxIterations = 20000; // for poly-10 with 50000 evaluations no successful try with hl yet 15 20 //var globalRandom = new Random(31415); 16 21 var localRandSeed = new Random().Next(); … … 19 24 var policyFactories = new Func<IBanditPolicy>[] 20 25 { 21 //() => new RandomPolicy(),22 //() => new ActiveLearningPolicy(),23 //() => new EpsGreedyPolicy(0.01, (aInfo)=> aInfo.MaxReward, "max"),24 //() => new EpsGreedyPolicy(0.05, (aInfo)=> aInfo.MaxReward, "max"),25 //() => new EpsGreedyPolicy(0.1, (aInfo)=> aInfo.MaxReward, "max"),26 //() => new EpsGreedyPolicy(0.2, (aInfo)=> aInfo.MaxReward, "max"),27 // //() => new GaussianThompsonSamplingPolicy(),28 //() => new GaussianThompsonSamplingPolicy(true),29 //() => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)),30 //() => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)),31 // //() => new BernoulliThompsonSamplingPolicy(),32 //() => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)),33 //() => new EpsGreedyPolicy(0.01),34 //() => new EpsGreedyPolicy(0.05),35 //() => new EpsGreedyPolicy(0.1),36 //() => new EpsGreedyPolicy(0.2),37 //() => new EpsGreedyPolicy(0.5),38 //() => new UCTPolicy(0.01),39 //() => new UCTPolicy(0.05),40 //() => new UCTPolicy(0.1),41 //() => new UCTPolicy(0.5),42 //() => new UCTPolicy(1),43 //() => new UCTPolicy(2),44 //() => new UCTPolicy( 5),45 //() => new UCTPolicy( 10),46 //() => new ModifiedUCTPolicy(0.01),47 //() => new ModifiedUCTPolicy(0.05),48 //() => new ModifiedUCTPolicy(0.1),49 //() => new ModifiedUCTPolicy(0.5),50 //() => new ModifiedUCTPolicy(1),51 //() => new ModifiedUCTPolicy(2),52 //() => new ModifiedUCTPolicy( 5),53 //() => new ModifiedUCTPolicy( 10),54 //() => new UCB1Policy(),55 //() => new UCB1TunedPolicy(),56 //() => new UCBNormalPolicy(),57 //() => new BoltzmannExplorationPolicy(1),58 //() => new BoltzmannExplorationPolicy(10),59 //() => new BoltzmannExplorationPolicy(20),60 //() => new BoltzmannExplorationPolicy(100),61 //() => new BoltzmannExplorationPolicy(200),62 //() => new BoltzmannExplorationPolicy(500),63 //() => new ChernoffIntervalEstimationPolicy( 0.01),64 //() => new ChernoffIntervalEstimationPolicy( 0.05),65 //() => new ChernoffIntervalEstimationPolicy( 0.1),66 //() => new ChernoffIntervalEstimationPolicy( 0.2),67 //() => new ThresholdAscentPolicy(5, 0.01),68 //() => new ThresholdAscentPolicy(5, 0.05),69 //() => new ThresholdAscentPolicy(5, 0.1),70 //() => new ThresholdAscentPolicy(5, 0.2),71 //() => new ThresholdAscentPolicy(10, 0.01),72 //() => new ThresholdAscentPolicy(10, 0.05),73 //() => new ThresholdAscentPolicy(10, 0.1),74 //() => new ThresholdAscentPolicy(10, 0.2),75 //() => new ThresholdAscentPolicy(50, 0.01),76 //() => new ThresholdAscentPolicy(50, 0.05),77 //() => new ThresholdAscentPolicy(50, 0.1),78 //() => new ThresholdAscentPolicy(50, 0.2),79 //() => new ThresholdAscentPolicy(100, 0.01),26 () => new RandomPolicy(), 27 () => new ActiveLearningPolicy(), 28 () => new EpsGreedyPolicy(0.01, (aInfo)=> aInfo.MaxReward, "max"), 29 () => new EpsGreedyPolicy(0.05, (aInfo)=> aInfo.MaxReward, "max"), 30 () => new EpsGreedyPolicy(0.1, (aInfo)=> aInfo.MaxReward, "max"), 31 () => new EpsGreedyPolicy(0.2, (aInfo)=> aInfo.MaxReward, "max"), 32 //() => new GaussianThompsonSamplingPolicy(), 33 () => new GaussianThompsonSamplingPolicy(true), 34 () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)), 35 () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)), 36 //() => new BernoulliThompsonSamplingPolicy(), 37 () => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)), 38 () => new EpsGreedyPolicy(0.01), 39 () => new EpsGreedyPolicy(0.05), 40 () => new EpsGreedyPolicy(0.1), 41 () => new EpsGreedyPolicy(0.2), 42 () => new EpsGreedyPolicy(0.5), 43 () => new UCTPolicy(0.01), 44 () => new UCTPolicy(0.05), 45 () => new UCTPolicy(0.1), 46 () => new UCTPolicy(0.5), 47 () => new UCTPolicy(1), 48 () => new UCTPolicy(2), 49 () => new UCTPolicy( 5), 50 () => new UCTPolicy( 10), 51 () => new ModifiedUCTPolicy(0.01), 52 () => new ModifiedUCTPolicy(0.05), 53 () => new ModifiedUCTPolicy(0.1), 54 () => new ModifiedUCTPolicy(0.5), 55 () => new ModifiedUCTPolicy(1), 56 () => new ModifiedUCTPolicy(2), 57 () => new ModifiedUCTPolicy( 5), 58 () => new ModifiedUCTPolicy( 10), 59 () => new UCB1Policy(), 60 () => new UCB1TunedPolicy(), 61 () => new UCBNormalPolicy(), 62 () => new BoltzmannExplorationPolicy(1), 63 () => new BoltzmannExplorationPolicy(10), 64 () => new BoltzmannExplorationPolicy(20), 65 () => new BoltzmannExplorationPolicy(100), 66 () => new BoltzmannExplorationPolicy(200), 67 () => new BoltzmannExplorationPolicy(500), 68 () => new ChernoffIntervalEstimationPolicy( 0.01), 69 () => new ChernoffIntervalEstimationPolicy( 0.05), 70 () => new ChernoffIntervalEstimationPolicy( 0.1), 71 () => new ChernoffIntervalEstimationPolicy( 0.2), 72 () => new ThresholdAscentPolicy(5, 0.01), 73 () => new ThresholdAscentPolicy(5, 0.05), 74 () => new ThresholdAscentPolicy(5, 0.1), 75 () => new ThresholdAscentPolicy(5, 0.2), 76 () => new ThresholdAscentPolicy(10, 0.01), 77 () => new ThresholdAscentPolicy(10, 0.05), 78 () => new ThresholdAscentPolicy(10, 0.1), 79 () => new ThresholdAscentPolicy(10, 0.2), 80 () => new ThresholdAscentPolicy(50, 0.01), 81 () => new ThresholdAscentPolicy(50, 0.05), 82 () => new ThresholdAscentPolicy(50, 0.1), 83 () => new ThresholdAscentPolicy(50, 0.2), 84 () => new ThresholdAscentPolicy(100, 0.01), 80 85 () => new ThresholdAscentPolicy(100, 0.05), 81 //() => new ThresholdAscentPolicy(100, 0.1),82 //() => new ThresholdAscentPolicy(100, 0.2),83 //() => new ThresholdAscentPolicy(500, 0.01),84 //() => new ThresholdAscentPolicy(500, 0.05),85 //() => new ThresholdAscentPolicy(500, 0.1),86 //() => new ThresholdAscentPolicy(500, 0.2),87 //() => new ThresholdAscentPolicy(5000, 0.01),88 //() => new ThresholdAscentPolicy(10000, 0.01),86 () => new ThresholdAscentPolicy(100, 0.1), 87 () => new ThresholdAscentPolicy(100, 0.2), 88 () => new ThresholdAscentPolicy(500, 0.01), 89 () => new ThresholdAscentPolicy(500, 0.05), 90 () => new ThresholdAscentPolicy(500, 0.1), 91 () => new ThresholdAscentPolicy(500, 0.2), 92 () => new ThresholdAscentPolicy(5000, 0.01), 93 () => new ThresholdAscentPolicy(10000, 0.01), 89 94 }; 90 95 … … 96 101 //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:false), 15), 97 102 //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:true), 15), 98 (rand) => Tuple.Create((IProblem)new SymbolicRegressionPoly10Problem(), 23) 103 //(rand) => Tuple.Create((IProblem)new SymbolicRegressionPoly10Problem(), 23) 104 (rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17) 99 105 }; 100 106 101 107 foreach (var instanceFactory in instanceFactories) { 102 108 foreach (var useCanonical in new bool[] { true /*, false */ }) { 103 foreach (var randomTries in new int[] { 1/*, 1, 10 /*, /* 5, 100 /*, 500, 1000 */}) {109 foreach (var randomTries in new int[] { 0 /*, 1, 10 /*, /* 5, 100 /*, 500, 1000 */}) { 104 110 foreach (var policyFactory in policyFactories) { 105 111 var myRandomTries = randomTries; -
branches/HeuristicLab.Problems.GrammaticalOptimization/Test/Test.csproj
r11981 r12014 8 8 <AppDesignerFolder>Properties</AppDesignerFolder> 9 9 <RootNamespace>HeuristicLab.Problems.GrammaticalOptimization.Test</RootNamespace> 10 <AssemblyName> HeuristicLab.Problems.GrammaticalOptimization.Test</AssemblyName>10 <AssemblyName>Test</AssemblyName> 11 11 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> 12 12 <FileAlignment>512</FileAlignment> … … 68 68 <Compile Include="Properties\AssemblyInfo.cs" /> 69 69 <Compile Include="TestSolvers.cs" /> 70 <Compile Include="TestTunedSettings.cs" /> 70 71 </ItemGroup> 71 72 <ItemGroup> -
branches/HeuristicLab.Problems.GrammaticalOptimization/Test/TestCanonicalExpressions.cs
r11972 r12014 5 5 using System.Threading.Tasks; 6 6 using HeuristicLab.Algorithms.Bandits; 7 using HeuristicLab.Algorithms.Bandits.GrammarPolicies; 8 using HeuristicLab.Algorithms.GrammaticalOptimization; 9 using HeuristicLab.Problems.GrammaticalOptimization.SymbReg; 7 10 using Microsoft.VisualStudio.TestTools.UnitTesting; 8 11 … … 64 67 Assert.AreEqual("a*b+b*c", extender.CanonicalRepresentation("b*(c-a)")); 65 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)")); 66 70 } 67 71 [TestMethod] 68 72 public void TestDivisionExpansion() { 69 73 var extender = new ExpressionExtender(); 70 Assert.AreEqual("a* 1/c+b*1/c", extender.CanonicalRepresentation("(b+a)%c"));71 Assert.AreEqual("a* 1/c*1/d+b*1/c*1/d", extender.CanonicalRepresentation("(b+a)%(d*c)"));72 Assert.AreEqual("a* 1/(c+d)+b*1/(c+d)", extender.CanonicalRepresentation("(b-a)%(d-c)"));73 Assert.AreEqual("a*b* 1/(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)")); 74 78 75 Assert.AreEqual("a*b* 1/(a+b)*1/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)%(a+b)"));76 Assert.AreEqual("a*b* 1/(a*1/e+b*1/e)*1/(c+d)", 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)")); 77 81 // a*b*e%(c+d)%(a+b) 78 82 } … … 80 84 public void TestDivisionCancellation() { 81 85 var extender = new ExpressionExtender(); 82 Assert.AreEqual(" 1", extender.CanonicalRepresentation("a%a"));86 Assert.AreEqual("|", extender.CanonicalRepresentation("a%a")); 83 87 Assert.AreEqual("a", extender.CanonicalRepresentation("a*a%a")); 84 Assert.AreEqual(" 1/a", extender.CanonicalRepresentation("(a%a)%a"));85 Assert.AreEqual(" 1/a", extender.CanonicalRepresentation("a%a%a"));88 Assert.AreEqual("|/a", extender.CanonicalRepresentation("(a%a)%a")); 89 Assert.AreEqual("|/a", extender.CanonicalRepresentation("a%a%a")); 86 90 Assert.AreEqual("a", extender.CanonicalRepresentation("a%(a%a)")); 87 Assert.AreEqual("1", extender.CanonicalRepresentation("(a+b)%(b+a)")); 88 Assert.AreEqual("1/a+1/b", extender.CanonicalRepresentation("(a+b)%(a*b)")); 89 Assert.AreEqual("a*1/(a*c*1/b+e*1/d*1/f)+b*1/(a*c*1/b+e*1/d*1/f)", extender.CanonicalRepresentation("(a+b)%(a%b*c+e%f%d)")); 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)")); 96 } 97 98 [TestMethod] 99 public void TestRandomExpressions() { 100 // samples sentences for the Tower dataset with the random MCTS policy 101 // and evaluates the original and the extended expression 102 // the results must be the same 103 104 var problem = new SymbolicRegressionProblem(new Random(), "Tower"); 105 var random = new Random(31415); 106 var solver = new SequentialSearch(problem, 30, random, 0, new RandomPolicy(problem, true)); 107 108 var extender = new ExpressionExtender(); 109 110 solver.SolutionEvaluated += (sentence, quality) => { 111 var canonicalSentence = extender.CanonicalRepresentation(sentence); 112 113 Assert.AreEqual(problem.SimpleEvaluate(sentence), problem.SimpleEvaluate(canonicalSentence), 1E-4, string.Format("{0} <> {1}", sentence, canonicalSentence)); 114 }; 115 116 solver.Run(10000); 90 117 } 91 118 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/Test/TestSymbRegInstances.cs
r11895 r12014 1 1 using System; 2 2 using System.Linq; 3 using HeuristicLab.Algorithms.Bandits.BanditPolicies; 4 using HeuristicLab.Algorithms.Bandits.GrammarPolicies; 3 5 using HeuristicLab.Algorithms.GeneticProgramming; 4 6 using HeuristicLab.Algorithms.GrammaticalOptimization; … … 12 14 [TestMethod] 13 15 public void TestGetDataDescriptors() { 16 var problem = new SymbolicRegressionProblem(new Random(), "Tower"); 17 Assert.IsNotNull(problem); 18 } 14 19 20 [TestMethod] 21 public void TestConstantOptimization() { 22 double r2; 15 23 var problem = new SymbolicRegressionProblem(new Random(), "Tower"); 16 double r2;17 24 Assert.AreEqual(problem.Evaluate("a*b"), problem.OptimizeConstantsAndEvaluate("a*b")); 18 25 Assert.AreEqual(problem.OptimizeConstantsAndEvaluate("a*b"), problem.Evaluate("a*b")); … … 23 30 Assert.IsTrue(problem.OptimizeConstantsAndEvaluate("0*a+1*b") >= problem.Evaluate("a+b")); 24 31 } 32 33 [TestMethod] 34 public void TestSequentialSolverForTower() { 35 var problem = new SymbolicRegressionProblem(new Random(), "Tower"); 36 var random = new Random(31415); 37 var solver = new SequentialSearch(problem, 30, random, 0, new GenericGrammarPolicy(problem, new UCB1TunedPolicy(), true)); 38 solver.FoundNewBestSolution += (s, d) => { 39 Console.WriteLine("{0:F3} {1}", d, s); 40 }; 41 solver.Run(100); 42 } 43 25 44 } 26 45 }
Note: See TracChangeset
for help on using the changeset viewer.