Changeset 11902
- Timestamp:
- 02/05/15 10:33:12 (10 years ago)
- 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 36 36 <ItemGroup> 37 37 <Compile Include="ConsoleEx.cs" /> 38 <Compile Include="ExpressionExtender.cs" /> 38 39 <Compile Include="Extensions.cs" /> 39 40 <Compile Include="MostRecentlyUsedCache.cs" /> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.SymbReg/SymbolicRegressionProblem.cs
r11895 r11902 19 19 public class SymbolicRegressionProblem : ISymbolicExpressionTreeProblem { 20 20 21 // C represents Koza-style ERC (the symbol is the index of the ERC), the values are initialized below 21 22 private const string grammarString = @" 22 23 G(E): … … 25 26 V -> <variables> 26 27 "; 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 "; 28 35 29 36 // S .. Sum (+), N .. Neg. sum (-), P .. Product (*), D .. Division (%) … … 38 45 V -> <variables> 39 46 "; 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 "; 40 55 41 56 // when we use constants optimization we can completely ignore all constants by a simple strategy: … … 51 66 private readonly int d; 52 67 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) { 56 72 var instanceProvider = new RegressionRealWorldInstanceProvider(); 57 73 var dds = instanceProvider.GetDataDescriptors().OfType<RegressionDataDescriptor>(); 74 this.useConstantOpt = useConstantOpt; 58 75 59 76 var problemData = instanceProvider.LoadData(dds.Single(ds => ds.Name.Contains(partOfName))); … … 78 95 char firstVar = 'a'; 79 96 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 } 82 104 } 83 105 … … 93 115 94 116 public double Evaluate(string sentence) { 95 return OptimizeConstantsAndEvaluate(sentence); 117 if (useConstantOpt) 118 return OptimizeConstantsAndEvaluate(sentence); 119 else { 120 return SimpleEvaluate(sentence); 121 } 96 122 } 97 123 … … 107 133 108 134 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(); 113 136 } 114 137 115 138 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); 116 144 throw new NotImplementedException(); 117 145 } … … 212 240 } 213 241 // nonterminal 214 if ( op == "+" || op == "-") sb.Append("(");242 if (treeNode.SubtreeCount > 1) sb.Append("("); 215 243 TreeToSentence(treeNode.Subtrees.First(), sb); 216 244 foreach (var subTree in treeNode.Subtrees.Skip(1)) { … … 218 246 TreeToSentence(subTree, sb); 219 247 } 220 if ( op == "+" || op == "-") sb.Append(")");248 if (treeNode.SubtreeCount > 1) sb.Append(")"); 221 249 } 222 250 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/Test/Test.csproj
r11851 r11902 69 69 </Choose> 70 70 <ItemGroup> 71 <Compile Include="TestCanonicalExpressions.cs" /> 71 72 <Compile Include="TestSymbRegInstances.cs" /> 72 73 <Compile Include="TestSequence.cs" /> … … 88 89 <Project>{eea07488-1a51-412a-a52c-53b754a628b3}</Project> 89 90 <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> 90 95 </ProjectReference> 91 96 <ProjectReference Include="..\HeuristicLab.Distributions\HeuristicLab.Distributions.csproj">
Note: See TracChangeset
for help on using the changeset viewer.