Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11902


Ignore:
Timestamp:
02/05/15 10:33:12 (10 years ago)
Author:
gkronber
Message:

#2283: preparations for transformation of expressions to canonical form

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/HeuristicLab.Common.csproj

    r11849 r11902  
    3636  <ItemGroup>
    3737    <Compile Include="ConsoleEx.cs" />
     38    <Compile Include="ExpressionExtender.cs" />
    3839    <Compile Include="Extensions.cs" />
    3940    <Compile Include="MostRecentlyUsedCache.cs" />
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.SymbReg/SymbolicRegressionProblem.cs

    r11895 r11902  
    1919  public class SymbolicRegressionProblem : ISymbolicExpressionTreeProblem {
    2020
     21    // C represents Koza-style ERC (the symbol is the index of the ERC), the values are initialized below
    2122    private const string grammarString = @"
    2223        G(E):
     
    2526        V -> <variables>
    2627        ";
    27     // C represents Koza-style ERC (the symbol is the index of the ERC), the values are initialized below
     28
     29    // when using constant opt optimal constants are generated in the evaluation step so we don't need them in the grammar
     30    private const string grammarConstOptString = @"
     31        G(E):
     32        E -> V | V+E | V*E | V%E | (E)
     33        V -> <variables>
     34        ";
    2835
    2936    // S .. Sum (+), N .. Neg. sum (-), P .. Product (*), D .. Division (%)
     
    3845        V -> <variables>
    3946        ";
     47    private const string treeGrammarConstOptString = @"
     48        G(E):
     49        E -> V | S | P | D
     50        S -> EE | EEE
     51        P -> EE | EEE
     52        D -> EE
     53        V -> <variables>
     54        ";
    4055
    4156    // when we use constants optimization we can completely ignore all constants by a simple strategy:
     
    5166    private readonly int d;
    5267    private readonly double[] erc;
    53 
    54 
    55     public SymbolicRegressionProblem(Random random, string partOfName) {
     68    private readonly bool useConstantOpt;
     69
     70
     71    public SymbolicRegressionProblem(Random random, string partOfName, bool useConstantOpt = true) {
    5672      var instanceProvider = new RegressionRealWorldInstanceProvider();
    5773      var dds = instanceProvider.GetDataDescriptors().OfType<RegressionDataDescriptor>();
     74      this.useConstantOpt = useConstantOpt;
    5875
    5976      var problemData = instanceProvider.LoadData(dds.Single(ds => ds.Name.Contains(partOfName)));
     
    7895      char firstVar = 'a';
    7996      char lastVar = Convert.ToChar(Convert.ToByte('a') + d - 1);
    80       this.grammar = new Grammar(grammarString.Replace("<variables>", firstVar + " .. " + lastVar));
    81       this.TreeBasedGPGrammar = new Grammar(treeGrammarString.Replace("<variables>", firstVar + " .. " + lastVar));
     97      if (!useConstantOpt) {
     98        this.grammar = new Grammar(grammarString.Replace("<variables>", firstVar + " .. " + lastVar));
     99        this.TreeBasedGPGrammar = new Grammar(treeGrammarString.Replace("<variables>", firstVar + " .. " + lastVar));
     100      } else {
     101        this.grammar = new Grammar(grammarConstOptString.Replace("<variables>", firstVar + " .. " + lastVar));
     102        this.TreeBasedGPGrammar = new Grammar(treeGrammarConstOptString.Replace("<variables>", firstVar + " .. " + lastVar));
     103      }
    82104    }
    83105
     
    93115
    94116    public double Evaluate(string sentence) {
    95       return OptimizeConstantsAndEvaluate(sentence);
     117      if (useConstantOpt)
     118        return OptimizeConstantsAndEvaluate(sentence);
     119      else {
     120        return SimpleEvaluate(sentence);
     121      }
    96122    }
    97123
     
    107133
    108134    public string CanonicalRepresentation(string phrase) {
    109       return phrase;
    110       //var terms = phrase.Split('+');
    111       //return string.Join("+", terms.Select(term => string.Join("", term.Replace("*", "").OrderBy(ch => ch)))
    112       //  .OrderBy(term => term));
     135      throw new NotImplementedException();
    113136    }
    114137
    115138    public IEnumerable<Feature> GetFeatures(string phrase) {
     139      // first generate canonical expression (which must not contain ())
     140      // recursively split into expressions
     141      // for each expression split into terms
     142      // for each term order factors to canonical // ../E = * 1/E
     143      var canonical = CanonicalRepresentation(phrase);
    116144      throw new NotImplementedException();
    117145    }
     
    212240        }
    213241        // nonterminal
    214         if (op == "+" || op == "-") sb.Append("(");
     242        if (treeNode.SubtreeCount > 1) sb.Append("(");
    215243        TreeToSentence(treeNode.Subtrees.First(), sb);
    216244        foreach (var subTree in treeNode.Subtrees.Skip(1)) {
     
    218246          TreeToSentence(subTree, sb);
    219247        }
    220         if (op == "+" || op == "-") sb.Append(")");
     248        if (treeNode.SubtreeCount > 1) sb.Append(")");
    221249      }
    222250    }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Test/Test.csproj

    r11851 r11902  
    6969  </Choose>
    7070  <ItemGroup>
     71    <Compile Include="TestCanonicalExpressions.cs" />
    7172    <Compile Include="TestSymbRegInstances.cs" />
    7273    <Compile Include="TestSequence.cs" />
     
    8889      <Project>{eea07488-1a51-412a-a52c-53b754a628b3}</Project>
    8990      <Name>HeuristicLab.Algorithms.GrammaticalOptimization</Name>
     91    </ProjectReference>
     92    <ProjectReference Include="..\HeuristicLab.Common\HeuristicLab.Common.csproj">
     93      <Project>{3A2FBBCB-F9DF-4970-87F3-F13337D941AD}</Project>
     94      <Name>HeuristicLab.Common</Name>
    9095    </ProjectReference>
    9196    <ProjectReference Include="..\HeuristicLab.Distributions\HeuristicLab.Distributions.csproj">
Note: See TracChangeset for help on using the changeset viewer.