Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/05/17 10:03:32 (7 years ago)
Author:
gkronber
Message:

#2796 worked on MCTS (preparation of hashing for expressions)

Location:
branches/MCTS-SymbReg-2796
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis.MCTSSymbReg.csproj

    r15360 r15404  
    112112    <Compile Include="MctsSymbolicRegression\Policies\Ucb.cs" />
    113113    <Compile Include="MctsSymbolicRegression\Policies\UcbTuned.cs" />
     114    <Compile Include="MctsSymbolicRegression\ExprHash.cs" />
    114115    <Compile Include="MctsSymbolicRegression\SymbolicExpressionGenerator.cs" />
    115116    <Compile Include="MctsSymbolicRegression\Tree.cs" />
  • branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/CodeGenerator.cs

    r14185 r15404  
    2323
    2424namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
    25   internal class CodeGenerator {
     25  public class CodeGenerator {
    2626
    2727    public const int MaxCodeLength = 10000;
  • branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/ConstraintHandler.cs

    r15403 r15404  
    324324
    325325
     326      /* DISABLED FOR TESTING CONSTRAINTS
    326327      // starting a new term is only allowed if we can add a term with at least the number of variables of the prev term
    327328      if (followState == Automaton.StateTermStart && !IsNewTermAllowed()) return false;
     
    329330      if (currentState == Automaton.StateFactorStart && !IsAllowedAsNextFactorType(followState)) return false;
    330331      if (followState == Automaton.StateTermEnd && prevTerm != null && CompareTerms(prevTerm, curTerm) > 0) return false;
     332      */
    331333
    332334      // all of these states add at least one variable
     
    341343      }
    342344
     345      /* DISABLED FOR TESTING CONSTRAINTS
    343346      if (currentState == Automaton.StateVariableFactorStart && !IsAllowedAsNextVariableFactor(followState)) return false;
    344347      else if (currentState == Automaton.StateExpFStart && !IsAllowedAsNextInExp(followState)) return false;
     
    353356      else if (followState == Automaton.StateLogTEnd && !IsTermCompleteInPoly()) return false;
    354357
    355       else if (nVars > maxVariables) return false;
     358      else
     359      */
     360      if (nVars > maxVariables) return false;
    356361      else return true;
    357362    }
  • branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs

    r15403 r15404  
    5454    //   
    5555
    56     // TODO NEXT: check if transformation of y is correct and works
    57     // TODO: transform target y to zero-mean and remove linear scaling parameters
    58     // TODO: include offset for variables as parameter
    59     // TODO: why does LM optimization converge so slowly with exp(x), log(x), and 1/x allowed?
    60     // TODO: support e(-x) and possibly (1/-x)
    61     // TODO: is it OK to initialize all constants to 1?
     56    // TODO: Constraint handling is too restrictive!  E.g. for Poly-10, if MCTS identifies the term x3*x4 first it is
     57    //       not possible to add the term x1*x2 later on. The same is true for individual terms after x2 it is not
     58    //       possible to multiply x1. It is easy to get stuck. Why do we actually need the current way of constraint handling?
     59    //       It would probably be easier to use some kind of hashing to identify equivalent expressions in the tree.
     60    // TODO: check if transformation of y is correct and works (Obj 2)
     61    // TODO: The algorithm is not invariant to location and scale of variables.
     62    //       Include offset for variables as parameter (for Objective 2)
     63    // TODO: why does LM optimization converge so slowly with exp(x), log(x), and 1/x allowed (Obj 2)?
     64    // TODO: support e(-x) and possibly (1/-x) (Obj 1)
     65    // TODO: is it OK to initialize all constants to 1 (Obj 2)?
    6266    #region static API
    6367
  • branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/OpCodes.cs

    r14185 r15404  
    2121
    2222namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
    23   internal enum OpCodes : byte {
     23  public enum OpCodes : byte {
    2424    Nop,
    2525    LoadConst0,
  • branches/MCTS-SymbReg-2796/Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs

    r15403 r15404  
    22using System.Linq;
    33using System.Threading;
    4 using HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression;
    54using HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression.Policies;
    65using HeuristicLab.Data;
     
    109using Microsoft.VisualStudio.TestTools.UnitTesting;
    1110
    12 namespace HeuristicLab.Algorithms.DataAnalysis {
     11namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
    1312  [TestClass()]
    1413  public class MctsSymbolicRegressionTest {
     14    #region expression hashing
     15    [TestMethod]
     16    [TestCategory("Algorithms.DataAnalysis")]
     17    [TestProperty("Time", "short")]
     18    public void ExprHashTest() {
     19      int nParams;
     20      byte[] code;
     21
     22      {
     23        // addition of variables
     24        var codeGen = new CodeGenerator();
     25        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     26        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     27        codeGen.Emit1(OpCodes.Add);
     28        codeGen.Emit1(OpCodes.Exit);
     29        codeGen.GetCode(out code, out nParams);
     30        var h1 = ExprHash.GetHash(code, nParams);
     31
     32        codeGen = new CodeGenerator();
     33        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     34        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     35        codeGen.Emit1(OpCodes.Add);
     36        codeGen.Emit1(OpCodes.Exit);
     37        codeGen.GetCode(out code, out nParams);
     38        var h2 = ExprHash.GetHash(code, nParams);
     39
     40        Assert.AreEqual(h1, h2);
     41      }
     42
     43      {
     44        // multiplication of variables
     45        var codeGen = new CodeGenerator();
     46        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     47        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     48        codeGen.Emit1(OpCodes.Mul);
     49        codeGen.Emit1(OpCodes.Exit);
     50        codeGen.GetCode(out code, out nParams);
     51        var h1 = ExprHash.GetHash(code, nParams);
     52
     53        codeGen = new CodeGenerator();
     54        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     55        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     56        codeGen.Emit1(OpCodes.Mul);
     57        codeGen.Emit1(OpCodes.Exit);
     58        codeGen.GetCode(out code, out nParams);
     59        var h2 = ExprHash.GetHash(code, nParams);
     60
     61        Assert.AreEqual(h1, h2);
     62      }
     63
     64      {
     65        // distributivity
     66        var codeGen = new CodeGenerator();
     67        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     68        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     69        codeGen.Emit1(OpCodes.Add);
     70        codeGen.Emit2(OpCodes.LoadVar, 3);
     71        codeGen.Emit1(OpCodes.Mul);
     72        codeGen.Emit1(OpCodes.Exit);
     73        codeGen.GetCode(out code, out nParams);
     74        var h1 = ExprHash.GetHash(code, nParams);
     75
     76        codeGen = new CodeGenerator();
     77        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     78        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 3);
     79        codeGen.Emit1(OpCodes.Mul);
     80        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     81        codeGen.Emit2(OpCodes.LoadVar, 3);
     82        codeGen.Emit1(OpCodes.Mul);
     83        codeGen.Emit1(OpCodes.Add);
     84        codeGen.Emit1(OpCodes.Exit);
     85        codeGen.GetCode(out code, out nParams);
     86        var h2 = ExprHash.GetHash(code, nParams);
     87
     88        Assert.AreEqual(h1, h2);
     89      }
     90
     91
     92      {
     93        // div
     94        var codeGen = new CodeGenerator();
     95        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     96        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     97        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
     98        codeGen.Emit1(OpCodes.Inv);
     99        codeGen.Emit1(OpCodes.Exit);
     100        codeGen.GetCode(out code, out nParams);
     101        var h1 = ExprHash.GetHash(code, nParams);
     102
     103        codeGen = new CodeGenerator();
     104        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     105        codeGen.Emit1(OpCodes.Inv);
     106        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     107        codeGen.Emit1(OpCodes.Inv);
     108        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
     109        codeGen.Emit1(OpCodes.Exit);
     110        codeGen.GetCode(out code, out nParams);
     111        var h2 = ExprHash.GetHash(code, nParams);
     112
     113        Assert.AreEqual(h1, h2);
     114      }
     115      {
     116        // exp
     117        var codeGen = new CodeGenerator();
     118        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     119        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     120        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add);
     121        codeGen.Emit1(OpCodes.Exp);
     122        codeGen.Emit1(OpCodes.Exit);
     123        codeGen.GetCode(out code, out nParams);
     124        var h1 = ExprHash.GetHash(code, nParams);
     125
     126        codeGen = new CodeGenerator();
     127        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     128        codeGen.Emit1(OpCodes.Exp);
     129        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     130        codeGen.Emit1(OpCodes.Exp);
     131        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
     132        codeGen.GetCode(out code, out nParams);
     133        codeGen.Emit1(OpCodes.Exit);
     134        var h2 = ExprHash.GetHash(code, nParams);
     135
     136        Assert.AreEqual(h1, h2);
     137      }
     138      {
     139        // log
     140        var codeGen = new CodeGenerator();
     141        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     142        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     143        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
     144        codeGen.Emit1(OpCodes.Log);
     145        codeGen.Emit1(OpCodes.Exit);
     146        codeGen.GetCode(out code, out nParams);
     147        var h1 = ExprHash.GetHash(code, nParams);
     148
     149        codeGen = new CodeGenerator();
     150        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
     151        codeGen.Emit1(OpCodes.Log);
     152        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
     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    #endregion
     163
    15164    #region number of solutions
    16165    // the algorithm should visits each solution only once
Note: See TracChangeset for help on using the changeset viewer.