Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/01/15 19:49:25 (10 years ago)
Author:
gkronber
Message:

#2283 various fixes for tree-based GP

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

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/HeuristicLab.Algorithms.Bandits.csproj

    r11842 r11847  
    3434      <HintPath>..\..\..\trunk\sources\bin\ALGLIB-3.7.0.dll</HintPath>
    3535    </Reference>
     36    <Reference Include="HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3">
     37      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3.dll</HintPath>
     38    </Reference>
    3639    <Reference Include="System" />
    3740    <Reference Include="System.Core" />
    38     <Reference Include="System.Xml.Linq" />
    39     <Reference Include="System.Data.DataSetExtensions" />
    40     <Reference Include="Microsoft.CSharp" />
    41     <Reference Include="System.Data" />
    42     <Reference Include="System.Xml" />
    4341  </ItemGroup>
    4442  <ItemGroup>
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming

    • Property svn:ignore set to
      bin
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/HeuristicLab.Algorithms.GeneticProgramming.csproj

    r11846 r11847  
    3434      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.GeneticAlgorithm-3.3.dll</HintPath>
    3535    </Reference>
     36    <Reference Include="HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3">
     37      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3.dll</HintPath>
     38    </Reference>
    3639    <Reference Include="HeuristicLab.Collections-3.3">
    3740      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath>
     
    5457    <Reference Include="HeuristicLab.Optimization-3.3">
    5558      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
     59    </Reference>
     60    <Reference Include="HeuristicLab.ParallelEngine-3.3">
     61      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.ParallelEngine-3.3.dll</HintPath>
    5662    </Reference>
    5763    <Reference Include="HeuristicLab.Parameters-3.3">
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/OffspringSelectionGP.cs

    r11846 r11847  
    2525      this.random = random;
    2626      // default parameter values
    27       PopulationSize = 300;
     27      PopulationSize = 100;
    2828      MutationRate = 0.15;
    2929      MaxSolutionSize = 100;
     
    3333    public override void Run(int maxEvaluations) {
    3434      var hlProblem = new GenericSymbExprProblem(problem);
    35       hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution
     35      hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution, don't scale quality to 0..1
     36      hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
     37      hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
    3638
    3739      using (var wh = new AutoResetEvent(false)) {
    38         var ga = new GeneticAlgorithm.GeneticAlgorithm();
    39         ga.Engine = new SequentialEngine.SequentialEngine();
    40         ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
    41         ga.Stopped += (sender, args) => { wh.Set(); };
     40        var osga = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm();
     41        osga.Engine = new ParallelEngine.ParallelEngine();
     42        osga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
     43        osga.Stopped += (sender, args) => { wh.Set(); };
    4244
    43         ga.Problem = hlProblem;
    44         var mutator = (MultiSymbolicExpressionTreeManipulator)ga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
     45        osga.Problem = hlProblem;
     46        var mutator = (MultiSymbolicExpressionTreeManipulator)osga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
    4547        foreach (var op in mutator.Operators) {
    4648          if (op.Name == "ChangeNodeTypeManipulation"
     
    4850          else mutator.Operators.SetItemCheckedState(op, false);
    4951        }
    50         ga.Mutator = mutator;
    51         ga.Crossover = ga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
    52         ga.Selector = ga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelector");
     52        osga.Mutator = mutator;
     53        osga.Crossover = osga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
     54        osga.Selector = osga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelection");
    5355
    54         ga.PopulationSize.Value = PopulationSize;
    55         ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize
    56         ga.MutationProbability.Value = MutationRate;
     56        osga.PopulationSize.Value = PopulationSize;
     57        osga.MaximumGenerations.Value = 100000; // some very large value (we stop based on evaluations)
     58        osga.MaximumSelectionPressure.Value = 1000;
     59        osga.MaximumEvaluatedSolutions.Value = maxEvaluations;
     60        osga.MutationProbability.Value = MutationRate;
    5761
    58         ga.SetSeedRandomly = new BoolValue(false);
    59         ga.Seed = new IntValue(random.Next());
     62        osga.SetSeedRandomly = new BoolValue(false);
     63        osga.Seed = new IntValue(random.Next());
    6064
    61         ga.Prepare();
    62         ga.Start();
     65        osga.Prepare();
     66        osga.Start();
    6367
    6468        wh.WaitOne();
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/StandardGP.cs

    r11846 r11847  
    3535    public override void Run(int maxEvaluations) {
    3636      var hlProblem = new GenericSymbExprProblem(problem);
    37       hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution
     37      hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution, don't scale quality to 0..1
     38      hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
     39      hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
     40
    3841
    3942      using (var wh = new AutoResetEvent(false)) {
    4043        var ga = new GeneticAlgorithm.GeneticAlgorithm();
    41         ga.Engine = new SequentialEngine.SequentialEngine();
     44        ga.Engine = new ParallelEngine.ParallelEngine();
    4245        ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
    4346        ga.Stopped += (sender, args) => { wh.Set(); };
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization.csproj

    r11846 r11847  
    3636    <Reference Include="System" />
    3737    <Reference Include="System.Core" />
    38     <Reference Include="System.Xml.Linq" />
    39     <Reference Include="System.Data.DataSetExtensions" />
    40     <Reference Include="Microsoft.CSharp" />
    41     <Reference Include="System.Data" />
    42     <Reference Include="System.Xml" />
    4338  </ItemGroup>
    4439  <ItemGroup>
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/HeuristicLab.Common.csproj

    r11799 r11847  
    3333    <Reference Include="System" />
    3434    <Reference Include="System.Core" />
    35     <Reference Include="System.Drawing" />
    36     <Reference Include="System.Xml.Linq" />
    37     <Reference Include="System.Data.DataSetExtensions" />
    38     <Reference Include="Microsoft.CSharp" />
    39     <Reference Include="System.Data" />
    40     <Reference Include="System.Xml" />
    4135  </ItemGroup>
    4236  <ItemGroup>
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.csproj

    r11846 r11847  
    6969      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
    7070    </Reference>
    71     <Reference Include="HeuristicLab.SequentialEngine-3.3">
    72       <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.SequentialEngine-3.3.dll</HintPath>
    73     </Reference>
    7471    <Reference Include="System" />
    7572    <Reference Include="System.Core" />
     
    7774    <Reference Include="System.Xml.Linq" />
    7875    <Reference Include="System.Data.DataSetExtensions" />
    79     <Reference Include="Microsoft.CSharp" />
    8076    <Reference Include="System.Data" />
    8177    <Reference Include="System.Xml" />
     
    115111    </ProjectReference>
    116112  </ItemGroup>
     113  <ItemGroup>
     114    <Folder Include="TreeRepresentation\" />
     115  </ItemGroup>
    117116  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    118117  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs

    r11832 r11847  
    55using System.Text;
    66using System.Text.RegularExpressions;
     7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    78
    89namespace HeuristicLab.Problems.GrammaticalOptimization {
    910  // 4-bit even parity
    10   public class EvenParityProblem : IProblem {
     11  public class EvenParityProblem : ISymbolicExpressionTreeProblem {
    1112    // + == OR
    1213    // * == AND
     
    1516S -> a | b | c | d | a*S | b*S | c*S | d*S | a+S | b+S | c+S | d+S | !S | (S)
    1617";
     18    // A = AND, O = OR, N = NOT, C = Clause
     19    private const string hlGrammarString = @"
     20G(E):
     21E -> A | O | N | C | a | b | c | d
     22A -> EE | EEE
     23O -> EE | EEE
     24N -> E
     25C -> E
     26    ";
    1727
    1828    private readonly IGrammar grammar;
    19     private readonly ExpressionInterpreter interpreter = new ExpressionInterpreter();
    2029    public EvenParityProblem() {
    2130      this.grammar = new Grammar(grammarString);
     31      this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString));
    2232    }
    2333
    2434    public double BestKnownQuality(int maxLen) {
    2535      // for now only an upper bound is returned, ideally all fitness cases are predicted correctly
    26       return Math.Pow(2, 4);
     36      return 16;
    2737    }
    2838
     
    3242
    3343    public double Evaluate(string sentence) {
     44      var interpreter = new ExpressionInterpreter(); // for concurrent evaluation
    3445      var vars = new bool[4];
    3546      var nCorrect = 0;
     
    5566    }
    5667
    57     public IEnumerable<Feature> GetFeatures(string phrase)
    58     {
     68    public IEnumerable<Feature> GetFeatures(string phrase) {
    5969      throw new NotImplementedException();
     70    }
     71
     72    public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; }
     73    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
     74      var sb = new StringBuilder();
     75
     76      TreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb);
     77
     78      return sb.ToString();
     79    }
     80
     81    private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) {
     82      if (treeNode.SubtreeCount == 0) {
     83        // terminal
     84        sb.Append(treeNode.Symbol.Name);
     85      } else {
     86        switch (treeNode.Symbol.Name) {
     87          case "O": {
     88              sb.Append("(");
     89              TreeToSentence(treeNode.Subtrees.First(), sb);
     90              foreach (var subTree in treeNode.Subtrees.Skip(1)) {
     91                sb.Append("+");
     92                TreeToSentence(subTree, sb);
     93              }
     94              sb.Append(")");
     95              break;
     96            }
     97          case "A": {
     98              TreeToSentence(treeNode.Subtrees.First(), sb);
     99              foreach (var subTree in treeNode.Subtrees.Skip(1)) {
     100                sb.Append("*");
     101                TreeToSentence(subTree, sb);
     102              }
     103              break;
     104            }
     105          case "N": {
     106              Debug.Assert(treeNode.SubtreeCount == 1);
     107              sb.Append("!(");
     108              TreeToSentence(treeNode.Subtrees.Single(), sb);
     109              sb.Append(")");
     110              break;
     111            }
     112          case "C": {
     113              Debug.Assert(treeNode.SubtreeCount == 1);
     114              sb.Append("(");
     115              TreeToSentence(treeNode.Subtrees.Single(), sb);
     116              sb.Append(")");
     117              break;
     118            }
     119          default: {
     120              Debug.Assert(treeNode.SubtreeCount == 1);
     121              TreeToSentence(treeNode.Subtrees.Single(), sb);
     122              break;
     123            }
     124        }
     125      }
    60126    }
    61127  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/GrammaticalOptimizationEvaluator.cs

    r11846 r11847  
    11using System;
    2 using System.Collections.Generic;
    3 using System.Diagnostics;
    4 using System.Linq;
    5 using System.Text;
    6 using System.Text.RegularExpressions;
    72using HeuristicLab.Common;
    83using HeuristicLab.Core;
     
    105using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    116using HeuristicLab.Operators;
    12 using HeuristicLab.Optimization;
    137using HeuristicLab.Parameters;
    14 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    158using HeuristicLab.PluginInfrastructure;
    169
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/HardPalindromeProblem.cs

    r11832 r11847  
    33using System.Linq;
    44using System.Text;
     5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    56
    67namespace HeuristicLab.Problems.GrammaticalOptimization {
    7   public class HardPalindromeProblem : IProblem {
     8  public class HardPalindromeProblem : ISymbolicExpressionTreeProblem {
    89    // length of the longest palindrome in the sentence + number of different symbols occurring in the palindrome
    910    private const string grammarString = @"
     
    1213T -> a .. z
    1314";
     15    private const string hlGrammarString = @"
     16G(S):
     17S -> a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | SS
     18";
    1419
    1520    private readonly IGrammar grammar;
    1621    public HardPalindromeProblem() {
    1722      this.grammar = new Grammar(grammarString);
     23      this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString));
    1824    }
    1925
     
    4753      throw new NotImplementedException();
    4854    }
     55
     56    public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; }
     57    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
     58      var sb = new StringBuilder();
     59      foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
     60        if (s.Symbol.Name == "S") continue;
     61        sb.Append(s.Symbol.Name);
     62      }
     63      return sb.ToString();
     64    }
    4965  }
    5066}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/PalindromeProblem.cs

    r11832 r11847  
    33using System.Linq;
    44using System.Text;
     5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    56
    67namespace HeuristicLab.Problems.GrammaticalOptimization {
    7   public class PalindromeProblem : IProblem {
     8  public class PalindromeProblem : ISymbolicExpressionTreeProblem {
    89    // length of the longest palindrome in the sentence
    910    private const string grammarString = @"
     
    1314";
    1415
     16
     17    private const string hlGrammarString = @"
     18G(S):
     19S -> a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | SS
     20";
     21
    1522    private readonly IGrammar grammar;
    1623    public PalindromeProblem() {
    1724      this.grammar = new Grammar(grammarString);
     25      this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString));
    1826    }
    1927
     
    8492    }
    8593
    86     public IEnumerable<Feature> GetFeatures(string phrase)
    87     {
     94    public IEnumerable<Feature> GetFeatures(string phrase) {
    8895      throw new NotImplementedException();
     96    }
     97
     98    public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; }
     99    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
     100      var sb = new StringBuilder();
     101      foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
     102        if (s.Symbol.Name == "S") continue;
     103        sb.Append(s.Symbol.Name);
     104      }
     105      return sb.ToString();
    89106    }
    90107  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPairProblem.cs

    r11832 r11847  
    55using System.Text;
    66using System.Text.RegularExpressions;
     7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    78
    89namespace HeuristicLab.Problems.GrammaticalOptimization {
    910  // counts the number of times a pair of symbols occurs in a sentence
    10   public class RoyalPairProblem : IProblem {
     11  public class RoyalPairProblem : ISymbolicExpressionTreeProblem {
    1112    private const string grammarString = @"
    1213G(S):
     
    1415";
    1516
     17    private const string hlGrammarString = @"
     18G(S):
     19S -> a | b | SS
     20";
     21
    1622    private readonly IGrammar grammar;
    1723    public RoyalPairProblem() {
    1824      this.grammar = new Grammar(grammarString);
     25      this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString));
    1926      // TODO: allow configuration of the number of symbols
    2027    }
     
    3643
    3744    public string CanonicalRepresentation(string phrase) {
    38       //throw new NotImplementedException();
    3945      return phrase;
    4046    }
     
    4450      throw new NotImplementedException();
    4551    }
     52    public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; }
     53    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
     54      var sb = new StringBuilder();
     55      foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
     56        if (s.Symbol.Name == "S") continue;
     57        sb.Append(s.Symbol.Name);
     58      }
     59      return sb.ToString();
     60    }
    4661  }
    4762}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSymbolProblem.cs

    r11832 r11847  
    33using System.Diagnostics;
    44using System.Linq;
     5using System.Text;
    56using System.Text.RegularExpressions;
     7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    68
    79namespace HeuristicLab.Problems.GrammaticalOptimization {
    810  // counts the number of times a symbol occurs in a sentence
    9   public class RoyalSymbolProblem : IProblem {
     11  public class RoyalSymbolProblem : ISymbolicExpressionTreeProblem {
    1012    private const string grammarString = @"
    1113G(S):
    1214S -> a | aS | b | bS
    1315";
     16
     17    private const string hlGrammarString = @"
     18G(S):
     19S -> a | b | SS
     20";
     21
    1422    // Obj(s \in L(G(S))) = Anzahl "a"-Symbole in s
    1523
     
    1725    public RoyalSymbolProblem() {
    1826      this.grammar = new Grammar(grammarString);
     27      this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString));
    1928      //TODO: allow configuration of the number of symbols
    2029    }
     
    3544    }
    3645    public string CanonicalRepresentation(string phrase) {
    37       throw new NotImplementedException();
    3846      return phrase;
    3947    }
    4048
    41     public IEnumerable<Feature> GetFeatures(string phrase)
    42     {
     49    public IEnumerable<Feature> GetFeatures(string phrase) {
    4350      throw new NotImplementedException();
     51    }
     52
     53    public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; }
     54    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
     55      var sb = new StringBuilder();
     56      foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
     57        if (s.Symbol.Name == "S") continue;
     58        sb.Append(s.Symbol.Name);
     59      }
     60      return sb.ToString();
    4461    }
    4562  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs

    r11832 r11847  
    11using System;
    22using System.Collections.Generic;
     3using System.Diagnostics;
    34using System.Linq;
    45using System.Runtime.InteropServices;
     
    67using System.Text;
    78using System.Text.RegularExpressions;
     9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    810
    911namespace HeuristicLab.Problems.GrammaticalOptimization {
    10   public class SantaFeAntProblem : IProblem {
     12  public class SantaFeAntProblem : ISymbolicExpressionTreeProblem {
    1113    private const string grammarString = @"
    1214G(A):
    1315A -> l | r | m | ?(A)(A) | lA | rA | mA
    1416";
     17    // for tree-based GP in HL we need a different grammar for the same language
     18    // A = Ant, P = Prog2 and Prog3, I = IfFoodAhead
     19    private const string hlGrammarString = @"
     20    G(A):
     21    A -> l | r | m | P | I
     22    P -> AA | AAA
     23    I -> AA
     24    ";
    1525
    1626
     
    2535    public SantaFeAntProblem() {
    2636      this.grammar = new Grammar(grammarString);
     37      this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString));
    2738    }
    2839
     
    116127    public IEnumerable<Feature> GetFeatures(string phrase) {
    117128      return Enumerable.Repeat(new Feature(CanonicalRepresentation(phrase), 1.0), 1);
     129    }
     130
     131    public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; }
     132    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
     133      var sb = new StringBuilder();
     134      ConvertTreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb);
     135      return sb.ToString();
     136    }
     137    private void ConvertTreeToSentence(ISymbolicExpressionTreeNode node, StringBuilder sb) {
     138      if (node.SubtreeCount == 0) {
     139        // terminal
     140        sb.Append(node.Symbol.Name);
     141      } else if (node.Symbol.Name == "I") {
     142        Debug.Assert(node.SubtreeCount == 2);
     143        sb.Append("?(");
     144        ConvertTreeToSentence(node.GetSubtree(0), sb);
     145        sb.Append(")(");
     146        ConvertTreeToSentence(node.GetSubtree(1), sb);
     147        sb.Append(")");
     148      } else {
     149        foreach (var subTree in node.Subtrees) { ConvertTreeToSentence(subTree, sb); }
     150      }
    118151    }
    119152  }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/SentenceSetStatistics.cs

    r11846 r11847  
    1919    // public double MedianQuality { get; private set; }
    2020    private double sumQualities;
     21
     22    private readonly double bestKnownQuality;
     23    public SentenceSetStatistics(double bestKnownQuality = 1.0) {
     24      this.bestKnownQuality = bestKnownQuality;
     25    }
    2126
    2227    public void AddSentence(string sentence, double quality) {
     
    4348        string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5,30} first {6,7:F5} {7,20} last {8,7:F5} {9,20}",
    4449      NumberOfSentences, AverageQuality,
    45       BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, 1.0) ? 1.0 : 0.0,
     50      BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, bestKnownQuality) ? 1.0 : 0.0,
    4651      BestSentenceIndex, TrimToSize(BestSentence, 30),
    4752      FirstSentenceQuality, TrimToSize(FirstSentence, 20),
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Main.csproj

    r11846 r11847  
    3636  </PropertyGroup>
    3737  <ItemGroup>
    38     <Reference Include="HeuristicLab.Algorithms.GeneticAlgorithm-3.3">
    39       <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.GeneticAlgorithm-3.3.dll</HintPath>
    40     </Reference>
    4138    <Reference Include="System" />
    4239    <Reference Include="System.Core" />
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs

    r11846 r11847  
    303303      const int maxIterations = 100000;
    304304      const int seed = 31415;
    305       var globalStatistics = new SentenceSetStatistics();
    306 
    307       var gp = new StandardGP(new SymbolicRegressionPoly10Problem(), new Random(seed));
     305      var globalStatistics = new SentenceSetStatistics(512);
     306
     307      var gp = new StandardGP(new HardPalindromeProblem(), new Random(seed));
    308308      gp.FoundNewBestSolution += (sentence, quality) => {
    309309        Console.WriteLine("{0}", globalStatistics);
     
    317317        }
    318318      };
     319
     320      gp.PopulationSize = 1000;
     321      gp.MaxSolutionSize = 31 + 2;
     322      gp.MaxSolutionDepth = 20;
    319323
    320324      var sw = new Stopwatch();
Note: See TracChangeset for help on using the changeset viewer.