Changeset 15414 for branches/MCTS-SymbReg-2796
- Timestamp:
- 10/09/17 18:53:34 (7 years ago)
- Location:
- branches/MCTS-SymbReg-2796
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis.MCTSSymbReg.csproj
r15410 r15414 20 20 <DebugType>full</DebugType> 21 21 <Optimize>false</Optimize> 22 <OutputPath> ..\..\..\..\trunk\sources\bin\</OutputPath>22 <OutputPath>bin\</OutputPath> 23 23 <DefineConstants>DEBUG;TRACE</DefineConstants> 24 24 <ErrorReport>prompt</ErrorReport> … … 98 98 </ItemGroup> 99 99 <ItemGroup> 100 <Compile Include="MctsSymbolicRegression\ApproximateDoubleEqualityComparer.cs" /> 100 101 <Compile Include="MctsSymbolicRegression\IConstraintHandler.cs" /> 101 102 <Compile Include="MctsSymbolicRegression\Automaton.cs" /> -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs
r15410 r15414 398 398 v == StateLogTFEnd || 399 399 v == StateInvTFEnd || 400 v == StateExpFEnd; 400 v == StateExpFEnd 401 ; 401 402 } 402 403 -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Disassembler.cs
r15410 r15414 58 58 switch (op) { 59 59 case (byte)OpCodes.Add: sb.Append(" + "); break; 60 case (byte)OpCodes.Mul: sb.Append(" *"); break;61 case (byte)OpCodes.LoadConst1: sb.Append(" 1 ");break;62 case (byte)OpCodes.LoadConst0: sb.Append(" 0 ");break;63 case (byte)OpCodes.LoadParamN: sb.AppendFormat(" c ");break;60 case (byte)OpCodes.Mul: sb.Append(""); break; 61 case (byte)OpCodes.LoadConst1: break; 62 case (byte)OpCodes.LoadConst0: break; 63 case (byte)OpCodes.LoadParamN: break; 64 64 case (byte)OpCodes.LoadVar: { 65 65 short arg = (short)((code[pc] << 8) | code[pc + 1]); 66 66 pc += 2; 67 sb.AppendFormat(" var{0} ", arg); break;67 sb.AppendFormat("{0}", (char)('a'+arg)); break; 68 68 } 69 69 case (byte)OpCodes.Exp: sb.Append(" exp "); break; -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/ExprHash.cs
r15404 r15414 20 20 #endregion 21 21 using System; 22 using System.Collections.Generic; 22 23 using System.Diagnostics.Contracts; 23 24 using HeuristicLab.Random; 25 using System.Linq; 24 26 25 27 namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression { … … 40 42 // We only need to identify equivalent structures. The numeric constants are irrelevant. 41 43 // Therefore, equivalent structures with different numeric constants map to the same hash code. 42 44 43 45 public static class ExprHash { 44 46 const int MaxStackSize = 100; 45 47 const int MaxVariables = 1000; 46 private static double[] varSymbValues; 48 private static double[] varSymbValues; 47 49 static ExprHash() { 48 50 var rand = new MersenneTwister(); 49 51 varSymbValues = new double[MaxVariables]; 50 for (int i = 0;i < MaxVariables;i++) {52 for (int i = 0; i < MaxVariables; i++) { 51 53 varSymbValues[i] = rand.NextDouble(); 52 54 } … … 54 56 55 57 public static ulong GetHash(byte[] code, int nParams) { 56 var bits = (ulong)BitConverter.DoubleToInt64Bits(E xec(code, nParams));58 var bits = (ulong)BitConverter.DoubleToInt64Bits(Eval(code, nParams)); 57 59 // clear last five bits (insignificant?) 58 60 bits = bits & 0xFFFFFFFFFFFFFFE0; … … 60 62 } 61 63 62 private static double Exec(byte[] code, int nParams) { 63 var stack = new double[MaxStackSize]; 64 private static double Eval(byte[] code, int nParams) { 65 // The hash code calculation already preserves commutativity, associativity and distributivity of operations. 66 // However, we also need to hash c1*x1 + c2*x1 to the same value as c3*x1. 67 // Similarly for x1*x2 + x1*x2 or log(x1) + log(x1)! 68 69 // Calculate sums lazily. Keep all terms and only when the actual sum is necessary remove duplicate terms and calculate sum 70 // think about speed later (TODO) 71 72 var stack = new ISet<double>[MaxStackSize]; 73 var terms = new HashSet<double>(new ApproximateDoubleEqualityComparer()); // the set of arguments for the current operator (+, *) 64 74 int topOfStack = -1; 65 75 int pc = 0; … … 73 83 case OpCodes.LoadConst0: { 74 84 ++topOfStack; 75 stack[topOfStack] = 0.0; 85 stack[topOfStack] = new HashSet<double>( new[] { 0.0 }); 86 87 // terms.Add(0.0); // ignore numeric constants in expr-hash 88 76 89 break; 77 90 } 78 91 case OpCodes.LoadConst1: { 79 92 ++topOfStack; 80 stack[topOfStack] = 1.0; 93 stack[topOfStack] = new HashSet<double>(new[] { 1.0 }); 94 95 // args.Add(1.0); ignore numeric constants in expr-hash 96 81 97 break; 82 98 } 83 99 case OpCodes.LoadParamN: { 84 100 ++topOfStack; 85 stack[topOfStack] = 1.0;101 stack[topOfStack] = new HashSet<double>(new[] { 1.0 }); 86 102 break; 87 103 } 88 104 case OpCodes.LoadVar: { 89 105 ++topOfStack; 90 stack[topOfStack] = varSymbValues[arg]; 106 stack[topOfStack] = new HashSet<double>(new[] { varSymbValues[arg] }); 107 108 // args.Add(varSymbValues[arg]); 109 91 110 break; 92 111 } 93 112 case OpCodes.Add: { 94 var t1 = stack[topOfStack - 1]; 95 var t2 = stack[topOfStack]; 113 // take arguments from stack and put both terms into the set of terms 114 // for every other operation we need to evaluate the sum of terms first and put it onto the stack (lazy eval of sums) 115 116 stack[topOfStack - 1].UnionWith(stack[topOfStack]); 96 117 topOfStack--; 97 stack[topOfStack] = t1 + t2; 118 119 // stack[topOfStack] = t1 + t2; (later) 98 120 break; 99 121 } … … 102 124 var t2 = stack[topOfStack]; 103 125 topOfStack--; 104 stack[topOfStack] = t1 * t2;126 stack[topOfStack] = new HashSet<double>(new double[] { t1.Sum() * t2.Sum() }); 105 127 break; 106 128 } 107 129 case OpCodes.Log: { 108 130 var v1 = stack[topOfStack]; 109 stack[topOfStack] = Math.Log(v1);131 stack[topOfStack] = new HashSet<double>(new double[] { Math.Log( v1.Sum()) }); 110 132 break; 111 133 } 112 134 case OpCodes.Exp: { 113 135 var v1 = stack[topOfStack]; 114 stack[topOfStack] = Math.Exp(v1);136 stack[topOfStack] = new HashSet<double>(new double[] { Math.Exp(v1.Sum()) }); 115 137 break; 116 138 } 117 139 case OpCodes.Inv: { 118 140 var v1 = stack[topOfStack]; 119 stack[topOfStack] = 1.0 / v1;141 stack[topOfStack] = new HashSet<double>(new double[] { 1.0 / v1.Sum() }); 120 142 break; 121 143 } 122 144 case OpCodes.Exit: 123 145 Contract.Assert(topOfStack == 0); 124 return stack[topOfStack] ;146 return stack[topOfStack].Sum(); 125 147 } 126 148 } 149 } 150 151 private static void EvalTerms(HashSet<double> terms, double[] stack, ref int topOfStack) { 152 ++topOfStack; 153 stack[topOfStack] = terms.Sum(); 154 terms.Clear(); 127 155 } 128 156 -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs
r15410 r15414 25 25 using System.Diagnostics.Contracts; 26 26 using System.Linq; 27 using System.Text; 27 28 using HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression.Policies; 28 29 using HeuristicLab.Core; … … 58 59 // weight more than an improvement from 0.98 to 0.99. Also, we are more interested in the best value of a 59 60 // branch and less in the expected value. (--> Review "Extreme Bandit" literature again) 60 // TODO: Constraint handling is too restrictive! E.g. for Poly-10, if MCTS identifies the term x3*x4 first it is 61 // not possible to add the term x1*x2 later on. The same is true for individual terms after x2 it is not 62 // possible to multiply x1. It is easy to get stuck. Why do we actually need the current way of constraint handling? 63 // It would probably be easier to use some kind of hashing to identify equivalent expressions in the tree. 64 // TODO: State unification (using hashing) is partially done. The hashcode calculation should be improved to also detect that 65 // c*x1 + c*x1*x1 + c*x1 is the same as c*x1 + c*x1*x1 61 // TODO: Solve Poly-10 66 62 // TODO: After state unification the recursive backpropagation of results takes a lot of time. How can this be improved? 63 // TODO: unit tests for benchmark problems which contain log / exp / x^-1 but without numeric constants 67 64 // TODO: check if transformation of y is correct and works (Obj 2) 68 65 // TODO: The algorithm is not invariant to location and scale of variables. … … 71 68 // TODO: support e(-x) and possibly (1/-x) (Obj 1) 72 69 // TODO: is it OK to initialize all constants to 1 (Obj 2)? 70 // TODO: improve memory usage 73 71 #region static API 74 72 … … 179 177 this.testEvaluator = new ExpressionEvaluator(testY.Length, lowerEstimationLimit, upperEstimationLimit); 180 178 181 this.automaton = new Automaton(x, new SimpleConstraintHandler( 100), allowProdOfVars, allowExp, allowLog, allowInv, allowMultipleTerms);179 this.automaton = new Automaton(x, new SimpleConstraintHandler(maxVariables), allowProdOfVars, allowExp, allowLog, allowInv, allowMultipleTerms); 182 180 this.treePolicy = treePolicy ?? new Ucb(); 183 181 this.tree = new Tree() { 184 182 state = automaton.CurrentState, 185 183 actionStatistics = treePolicy.CreateActionStatistics(), 186 expr = "" 184 expr = "", 185 level = 0 187 186 }; 188 187 … … 497 496 mctsState.effectiveRollouts++; 498 497 499 if (mctsState.effectiveRollouts % 10 == 1) Console.WriteLine(WriteTree(tree)); 498 if (mctsState.effectiveRollouts % 10 == 1) { 499 //Console.WriteLine(WriteTree(tree)); 500 //Console.WriteLine(TraceTree(tree)); 501 } 500 502 return q; 501 503 } … … 520 522 // We do this until we reach a complete expression (final state) 521 523 522 // Loops in the graph are p ossible! (Problem?)524 // Loops in the graph are prevented by checking that the level of a child must be larger than the level of the parent 523 525 // Sub-graphs which have been completely searched are marked as done. 524 526 // Roll-out could lead to a state where all follow-states are done. In this case we call the rollout ineffective. … … 526 528 while (!automaton.IsFinalState(automaton.CurrentState)) { 527 529 if (children.ContainsKey(tree)) { 530 if (children[tree].All(ch => ch.Done)) { 531 tree.Done = true; 532 break; 533 } 528 534 // ROLLOUT INSIDE TREE 529 535 // UCT selection within tree … … 540 546 int nFs; 541 547 automaton.FollowStates(automaton.CurrentState, out possibleFollowStates, out nFs); 542 while (nFs == 1 && !automaton.IsEvalState(possibleFollowStates[0]) ) {548 while (nFs == 1 && !automaton.IsEvalState(possibleFollowStates[0]) && !automaton.IsFinalState(possibleFollowStates[0])) { 543 549 automaton.Goto(possibleFollowStates[0]); 544 550 automaton.FollowStates(automaton.CurrentState, out possibleFollowStates, out nFs); … … 551 557 int nFs; 552 558 automaton.FollowStates(automaton.CurrentState, out possibleFollowStates, out nFs); 553 while (nFs == 1 && !automaton.IsEvalState(possibleFollowStates[0]) ) {559 while (nFs == 1 && !automaton.IsEvalState(possibleFollowStates[0]) && !automaton.IsFinalState(possibleFollowStates[0])) { 554 560 // no alternatives -> just go to the next state 555 561 automaton.Goto(possibleFollowStates[0]); … … 565 571 for (int i = 0; i < nFs; i++) { 566 572 Tree child = null; 567 // for selected states we introduce state unification (detection of equivalent states)573 // for selected states (EvalStates) we introduce state unification (detection of equivalent states) 568 574 if (automaton.IsEvalState(possibleFollowStates[i])) { 569 575 var hc = Hashcode(automaton); … … 573 579 state = possibleFollowStates[i], 574 580 actionStatistics = treePolicy.CreateActionStatistics(), 575 expr = ExprStr(automaton) 581 expr = string.Empty, // ExprStr(automaton), 582 level = tree.level + 1 576 583 }; 577 584 nodes.Add(hc, child); 578 } else { 585 } 586 // only allow forward edges (don't add the child if we would go back in the graph) 587 else if (child.level > tree.level) { 579 588 // whenever we join paths we need to propagate back the statistics of the existing node through the newly created link 580 589 // to all parents 581 590 BackpropagateStatistics(child.actionStatistics, tree); 591 } else { 592 // prevent cycles 593 Debug.Assert(child.level <= tree.level); 594 child = null; 582 595 } 583 596 } else { … … 586 599 state = possibleFollowStates[i], 587 600 actionStatistics = treePolicy.CreateActionStatistics(), 588 expr = ExprStr(automaton) 601 expr = string.Empty, // ExprStr(automaton), 602 level = tree.level + 1 589 603 }; 590 604 } 591 newChildren.Add(child); 605 if (child != null) 606 newChildren.Add(child); 607 } 608 609 if (!newChildren.Any()) { 610 // stuck in a dead end (no final state and no allowed follow states) 611 tree.Done = true; 612 break; 592 613 } 593 614 … … 599 620 } 600 621 622 601 623 // follow one of the children 602 624 tree = SelectFinalOrRandom2(automaton, tree, rand); … … 610 632 if (automaton.IsFinalState(automaton.CurrentState)) { 611 633 tree.Done = true; 634 tree.expr = ExprStr(automaton); 612 635 byte[] code; int nParams; 613 636 automaton.GetCode(out code, out nParams); … … 636 659 // EXPERIMENTAL! 637 660 // optimal result: q = 1 -> return huge value 638 if (q >= 1.0) return 1E16;639 // return number of 9s in R²640 return -Math.Log10(1 - q);661 // if (q >= 1.0) return 1E16; 662 // // return number of 9s in R² 663 // return -Math.Log10(1 - q); 641 664 } 642 665 … … 852 875 return sb.ToString(); 853 876 } 877 878 private static string TraceTree(Tree tree) { 879 var sb = new StringBuilder(); 880 sb.Append( 881 @"digraph { 882 ratio = fill; 883 node [style=filled]; 884 "); 885 int nodeId = 0; 886 887 TraceTreeRec(tree, 0, sb, ref nodeId); 888 sb.Append("}"); 889 return sb.ToString(); 890 } 891 892 private static void TraceTreeRec(Tree tree, int parentId, StringBuilder sb, ref int nextId) { 893 var avgNodeQ = tree.actionStatistics.AverageQuality; 894 var tries = tree.actionStatistics.Tries; 895 if (double.IsNaN(avgNodeQ)) avgNodeQ = 0.0; 896 var hue = (1 - avgNodeQ) / 360.0 * 240.0; // 0 equals red, 240 equals blue 897 898 sb.AppendFormat("{0} [label=\"{1:N3} {2}\" color=\"{3:N3} 0.999 0.999\"]; ", parentId, avgNodeQ, tries, hue).AppendLine(); 899 900 var list = new List<Tuple<int, int, Tree>>(); 901 if (children.ContainsKey(tree)) { 902 foreach (var ch in children[tree]) { 903 nextId++; 904 avgNodeQ = ch.actionStatistics.AverageQuality; 905 tries = ch.actionStatistics.Tries; 906 if (double.IsNaN(avgNodeQ)) avgNodeQ = 0.0; 907 hue = (1 - avgNodeQ) / 360.0 * 240.0; // 0 equals red, 240 equals blue 908 sb.AppendFormat("{0} [label=\"{1:N3} {2}\" color=\"{3:N3} 0.999 0.999\"]; ", nextId, avgNodeQ, tries, hue).AppendLine(); 909 sb.AppendFormat("{0} -> {1}", parentId, nextId, avgNodeQ).AppendLine(); 910 list.Add(Tuple.Create(tries, nextId, ch)); 911 } 912 foreach (var tup in list.OrderByDescending(t => t.Item1).Take(1)) { 913 TraceTreeRec(tup.Item3, tup.Item2, sb, ref nextId); 914 } 915 } 916 } 917 854 918 private static string WriteTree(Tree tree) { 855 919 var sb = new System.IO.StringWriter(System.Globalization.CultureInfo.InvariantCulture); … … 860 924 node [style=filled]; 861 925 "); 862 foreach(var kvp in children) { 926 int threshold = nodes.Count > 500 ? 10 : 0; 927 foreach (var kvp in children) { 863 928 var parent = kvp.Key; 864 929 int parentId; 865 if (!nodeIds.TryGetValue(parent, out parentId)) {930 if (!nodeIds.TryGetValue(parent, out parentId)) { 866 931 parentId = nodeIds.Count + 1; 867 var avgNodeQ = parent.actionStatistics.AverageQuality; 932 var avgNodeQ = parent.actionStatistics.AverageQuality; 868 933 var tries = parent.actionStatistics.Tries; 869 934 if (double.IsNaN(avgNodeQ)) avgNodeQ = 0.0; 870 var hue = (1 - avgNodeQ) / 255.0 * 240.0; // 0 equals red, 240 equals blue 871 sb.Write("{0} [label=\"{1:N3} {2}\" color=\"{3:N3} 0.999 0.999\"]; ", parentId, avgNodeQ, tries, hue); 935 var hue = (1 - avgNodeQ) / 360.0 * 240.0; // 0 equals red, 240 equals blue 936 if (parent.actionStatistics.Tries > threshold) 937 sb.Write("{0} [label=\"{1:N3} {2}\" color=\"{3:N3} 0.999 0.999\"]; ", parentId, avgNodeQ, tries, hue); 872 938 nodeIds.Add(parent, parentId); 873 939 } 874 foreach (var child in kvp.Value) {940 foreach (var child in kvp.Value) { 875 941 int childId; 876 if (!nodeIds.TryGetValue(child, out childId)) {942 if (!nodeIds.TryGetValue(child, out childId)) { 877 943 childId = nodeIds.Count + 1; 878 944 nodeIds.Add(child, childId); … … 882 948 if (tries < 1) continue; 883 949 if (double.IsNaN(avgNodeQ)) avgNodeQ = 0.0; 884 var hue = (1 - avgNodeQ) / 255.0 * 240.0; // 0 equals red, 240 equals blue 885 sb.Write("{0} [label=\"{1:N3} {2}\" color=\"{3:N3} 0.999 0.999\"]; ", childId, avgNodeQ, tries, hue); 886 var edgeLabel = child.expr; 887 if (parent.expr.Length > 0) edgeLabel = edgeLabel.Replace(parent.expr, ""); 888 sb.Write("{0} -> {1} [label=\"{3}\"]", parentId, childId, avgNodeQ, edgeLabel); 950 var hue = (1 - avgNodeQ) / 360.0 * 240.0; // 0 equals red, 240 equals blue 951 if (tries > threshold) { 952 sb.Write("{0} [label=\"{1:N3} {2}\" color=\"{3:N3} 0.999 0.999\"]; ", childId, avgNodeQ, tries, hue); 953 var edgeLabel = child.expr; 954 // if (parent.expr.Length > 0) edgeLabel = edgeLabel.Replace(parent.expr, ""); 955 sb.Write("{0} -> {1} [label=\"{3}\"]", parentId, childId, avgNodeQ, edgeLabel); 956 } 889 957 } 890 958 } -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Policies/Ucb.cs
r15410 r15414 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Diagnostics; 3 4 using System.Diagnostics.Contracts; 4 5 using System.Linq; … … 81 82 return buf[rand.Next(buf.Count)]; 82 83 } 83 Contract.Assert(totalTries > 0);84 Debug.Assert(totalTries > 0); 84 85 double logTotalTries = Math.Log(totalTries); 85 86 var bestQ = double.NegativeInfinity; -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/SimpleConstraintHandler.cs
r15410 r15414 31 31 32 32 public bool IsAllowedFollowState(int currentState, int followState) { 33 return numVariables < maxVariables || ( 34 followState != Automaton.StateExpFactorStart && 35 followState != Automaton.StateFactorStart && 36 followState != Automaton.StateTermStart && 37 followState != Automaton.StateLogTStart && 38 followState != Automaton.StateLogTFStart && 39 followState != Automaton.StateInvTStart && 40 followState != Automaton.StateInvTFStart); 33 return numVariables < maxVariables || 34 // going to the final state is always allowed (smaller states are closer to the final state) 35 currentState > followState; 36 // ( 37 // followState != Automaton.StateExpFactorStart && 38 // followState != Automaton.StateFactorStart && 39 // followState != Automaton.StateTermStart && 40 // followState != Automaton.StateLogTStart && 41 // followState != Automaton.StateLogTFStart && 42 // followState != Automaton.StateInvTStart && 43 // followState != Automaton.StateInvTFStart); 41 44 } 42 45 -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Tree.cs
r15410 r15414 26 26 internal class Tree { 27 27 public int state; 28 public int level; 28 29 public string expr; 29 30 public bool Done { -
branches/MCTS-SymbReg-2796/Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs
r15410 r15414 152 152 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2); 153 153 codeGen.Emit1(OpCodes.Log); 154 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 155 codeGen.Emit1(OpCodes.Exit); 156 codeGen.GetCode(out code, out nParams); 157 var h2 = ExprHash.GetHash(code, nParams); 158 159 Assert.AreEqual(h1, h2); 160 } 161 162 { 163 // x1 + x1 is equivalent to x1 164 var codeGen = new CodeGenerator(); 165 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 166 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 167 codeGen.Emit1(OpCodes.Add); 168 codeGen.Emit1(OpCodes.Exit); 169 codeGen.GetCode(out code, out nParams); 170 var h1 = ExprHash.GetHash(code, nParams); 171 172 codeGen = new CodeGenerator(); 173 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 174 codeGen.Emit1(OpCodes.Exit); 175 codeGen.GetCode(out code, out nParams); 176 var h2 = ExprHash.GetHash(code, nParams); 177 178 Assert.AreEqual(h1, h2); 179 } 180 { 181 // c1*x1 + c2*x1 is equivalent to c3*x1 182 var codeGen = new CodeGenerator(); 183 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 184 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 185 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 186 187 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 188 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 189 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 190 191 codeGen.Emit1(OpCodes.Add); 192 codeGen.Emit1(OpCodes.Exit); 193 codeGen.GetCode(out code, out nParams); 194 var h1 = ExprHash.GetHash(code, nParams); 195 196 codeGen = new CodeGenerator(); 197 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 198 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 199 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 200 codeGen.Emit1(OpCodes.Exit); 201 codeGen.GetCode(out code, out nParams); 202 var h2 = ExprHash.GetHash(code, nParams); 203 204 Assert.AreEqual(h1, h2); 205 } 206 207 { 208 var codeGen = new CodeGenerator(); 209 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 210 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 211 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 212 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 213 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 214 215 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 216 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 217 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 218 219 codeGen.Emit1(OpCodes.Add); 220 221 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 222 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 223 224 codeGen.Emit1(OpCodes.Exit); 225 codeGen.GetCode(out code, out nParams); 226 var h1 = ExprHash.GetHash(code, nParams); 227 228 codeGen = new CodeGenerator(); 229 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 230 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 231 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 232 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 233 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 234 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 154 235 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 155 236 codeGen.Emit1(OpCodes.Exit); … … 432 513 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234); 433 514 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 "))); 434 TestMctsWithoutConstants(regProblem, iterations: 1000, allowExp: false, allowLog: false, allowInv: false);515 TestMctsWithoutConstants(regProblem, nVarRefs: 10, allowExp: false, allowLog: false, allowInv: false); 435 516 } 436 517 [TestMethod] … … 450 531 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234); 451 532 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F3 "))); 452 TestMctsWithoutConstants(regProblem, allowExp: false, allowLog: false, allowInv: false);533 TestMctsWithoutConstants(regProblem, nVarRefs: 15, iterations: 1000000, allowExp: false, allowLog: false, allowInv: false); 453 534 } 454 535 [TestMethod] … … 459 540 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234); 460 541 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F4 "))); 461 TestMctsWithoutConstants(regProblem, allowExp: false, allowLog: false, allowInv: false); 542 TestMctsWithoutConstants(regProblem, nVarRefs: 25, iterations: 1000000, allowExp: false, allowLog: false, allowInv: false); 543 } 544 545 [TestMethod] 546 [TestCategory("Algorithms.DataAnalysis")] 547 [TestProperty("Time", "short")] 548 public void MctsSymbReg_NoConstants_Nguyen7() { 549 // log(x + 1) + log(x² + 1) 550 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234); 551 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 "))); 552 TestMctsWithoutConstants(regProblem, nVarRefs: 10, iterations: 1000000, allowExp: false, allowLog: true, allowInv: false); 462 553 } 463 554 … … 468 559 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234); 469 560 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10"))); 470 TestMctsWithoutConstants(regProblem, iterations: 100, allowExp: false, allowLog: false, allowInv: false);561 TestMctsWithoutConstants(regProblem, nVarRefs: 15, iterations: 1000000, allowExp: false, allowLog: false, allowInv: false); 471 562 } 472 563 #endregion … … 705 796 706 797 707 private void TestMctsWithoutConstants(IRegressionProblemData problemData, int iterations = 200000, double successThreshold = 0.99999, 798 private void TestMctsWithoutConstants(IRegressionProblemData problemData, 799 int nVarRefs = 10, 800 int iterations = 200000, double successThreshold = 0.99999, 708 801 bool allowExp = true, 709 802 bool allowLog = true, … … 717 810 mctsSymbReg.Problem = regProblem; 718 811 mctsSymbReg.Iterations = iterations; 719 mctsSymbReg.MaxVariableReferences = 25;812 mctsSymbReg.MaxVariableReferences = nVarRefs; 720 813 mctsSymbReg.SetSeedRandomly = false; 721 814 mctsSymbReg.Seed = 1234;
Note: See TracChangeset
for help on using the changeset viewer.