Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/15 09:14:38 (10 years ago)
Author:
gkronber
Message:

#2283: worked on transformation of arithmetic expressions to canonical form

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization/Test
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Test/RunDemo.cs

    r11981 r12014  
    77using HeuristicLab.Algorithms.Bandits.BanditPolicies;
    88using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
     9using HeuristicLab.Algorithms.Bandits.Models;
    910using HeuristicLab.Algorithms.GrammaticalOptimization;
     11using Microsoft.VisualStudio.TestTools.UnitTesting;
     12using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy;
    1013
    1114namespace 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
    1520      //var globalRandom = new Random(31415);
    1621      var localRandSeed = new Random().Next();
     
    1924      var policyFactories = new Func<IBanditPolicy>[]
    2025        {
    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),
    8085         () => 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),
    8994        };
    9095
     
    96101        //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:200, correctReward:1, decoyReward:0.5, phrasesAsSets:false), 15),
    97102        //(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)
    99105      };
    100106
    101107      foreach (var instanceFactory in instanceFactories) {
    102108        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 */}) {
    104110            foreach (var policyFactory in policyFactories) {
    105111              var myRandomTries = randomTries;
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Test/Test.csproj

    r11981 r12014  
    88    <AppDesignerFolder>Properties</AppDesignerFolder>
    99    <RootNamespace>HeuristicLab.Problems.GrammaticalOptimization.Test</RootNamespace>
    10     <AssemblyName>HeuristicLab.Problems.GrammaticalOptimization.Test</AssemblyName>
     10    <AssemblyName>Test</AssemblyName>
    1111    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    1212    <FileAlignment>512</FileAlignment>
     
    6868    <Compile Include="Properties\AssemblyInfo.cs" />
    6969    <Compile Include="TestSolvers.cs" />
     70    <Compile Include="TestTunedSettings.cs" />
    7071  </ItemGroup>
    7172  <ItemGroup>
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Test/TestCanonicalExpressions.cs

    r11972 r12014  
    55using System.Threading.Tasks;
    66using HeuristicLab.Algorithms.Bandits;
     7using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
     8using HeuristicLab.Algorithms.GrammaticalOptimization;
     9using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
    710using Microsoft.VisualStudio.TestTools.UnitTesting;
    811
     
    6467      Assert.AreEqual("a*b+b*c", extender.CanonicalRepresentation("b*(c-a)"));
    6568      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)"));
    6670    }
    6771    [TestMethod]
    6872    public void TestDivisionExpansion() {
    6973      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)"));
    7478
    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)"));
    7781      // a*b*e%(c+d)%(a+b)
    7882    }
     
    8084    public void TestDivisionCancellation() {
    8185      var extender = new ExpressionExtender();
    82       Assert.AreEqual("1", extender.CanonicalRepresentation("a%a"));
     86      Assert.AreEqual("|", extender.CanonicalRepresentation("a%a"));
    8387      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"));
    8690      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);
    90117    }
    91118  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Test/TestSymbRegInstances.cs

    r11895 r12014  
    11using System;
    22using System.Linq;
     3using HeuristicLab.Algorithms.Bandits.BanditPolicies;
     4using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
    35using HeuristicLab.Algorithms.GeneticProgramming;
    46using HeuristicLab.Algorithms.GrammaticalOptimization;
     
    1214    [TestMethod]
    1315    public void TestGetDataDescriptors() {
     16      var problem = new SymbolicRegressionProblem(new Random(), "Tower");
     17      Assert.IsNotNull(problem);
     18    }
    1419
     20    [TestMethod]
     21    public void TestConstantOptimization() {
     22      double r2;
    1523      var problem = new SymbolicRegressionProblem(new Random(), "Tower");
    16       double r2;
    1724      Assert.AreEqual(problem.Evaluate("a*b"), problem.OptimizeConstantsAndEvaluate("a*b"));
    1825      Assert.AreEqual(problem.OptimizeConstantsAndEvaluate("a*b"), problem.Evaluate("a*b"));
     
    2330      Assert.IsTrue(problem.OptimizeConstantsAndEvaluate("0*a+1*b") >= problem.Evaluate("a+b"));
    2431    }
     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
    2544  }
    2645}
Note: See TracChangeset for help on using the changeset viewer.