Changeset 12893 for branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems
- Timestamp:
- 08/24/15 13:56:27 (9 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs
r12290 r12893 63 63 } 64 64 65 public bool IsOptimalPhrase(string phrase) { 66 throw new NotImplementedException(); 67 } 68 65 69 public string CanonicalRepresentation(string phrase) { 66 70 throw new NotImplementedException(); … … 68 72 } 69 73 70 public IEnumerable<Feature> GetFeatures(string phrase) 71 { 72 return new[] {new Feature(phrase, 1.0)}; 74 public IEnumerable<Feature> GetFeatures(string phrase) { 75 return new[] { new Feature(phrase, 1.0) }; 73 76 } 74 77 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/FindPhrasesProblem.cs
r12391 r12893 169 169 return new Feature[] {new Feature(phrase, 1.0),}; 170 170 } 171 public bool IsOptimalPhrase(string phrase) { 172 throw new NotImplementedException(); 173 } 171 174 172 175 public override string ToString() { -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/HardPalindromeProblem.cs
r12099 r12893 55 55 throw new NotImplementedException(); 56 56 } 57 public bool IsOptimalPhrase(string phrase) { 58 throw new NotImplementedException(); 59 } 57 60 58 61 public IGrammar TreeBasedGPGrammar { get; private set; } -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/PalindromeProblem.cs
r12099 r12893 96 96 throw new NotImplementedException(); 97 97 } 98 public bool IsOptimalPhrase(string phrase) { 99 throw new NotImplementedException(); 100 } 98 101 99 102 public IGrammar TreeBasedGPGrammar { get; private set; } -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/PermutationProblem.cs
r12099 r12893 47 47 throw new NotImplementedException(); 48 48 } 49 public bool IsOptimalPhrase(string phrase) { 50 throw new NotImplementedException(); 51 } 52 49 53 public IGrammar TreeBasedGPGrammar { get; private set; } 50 54 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/PrimePolynomialProblem.cs
r12291 r12893 215 215 return sb.ToString(); 216 216 } 217 public bool IsOptimalPhrase(string phrase) { 218 // throw new NotImplementedException(); 219 return false; // TODO 220 } 217 221 218 222 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPairProblem.cs
r12391 r12893 76 76 77 77 } 78 public bool IsOptimalPhrase(string phrase) { 79 throw new NotImplementedException(); 80 } 78 81 79 82 public IGrammar TreeBasedGPGrammar { get; private set; } -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPhraseSequenceProblem.cs
r12391 r12893 153 153 return new Feature[] {new Feature(phrase, 1.0)}; 154 154 } 155 public bool IsOptimalPhrase(string phrase) { 156 throw new NotImplementedException(); 157 } 155 158 156 159 public IGrammar TreeBasedGPGrammar { get; private set; } -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalRoadProblem.cs
r12099 r12893 33 33 return phrase; 34 34 } 35 public bool IsOptimalPhrase(string phrase) { 36 throw new NotImplementedException(); 37 } 35 38 36 39 public IEnumerable<Feature> GetFeatures(string phrase) -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSequenceProblem.cs
r12391 r12893 104 104 throw new NotImplementedException(); 105 105 } 106 public bool IsOptimalPhrase(string phrase) { 107 throw new NotImplementedException(); 108 } 106 109 107 110 public IGrammar TreeBasedGPGrammar { get; private set; } -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSymbolProblem.cs
r12099 r12893 51 51 throw new NotImplementedException(); 52 52 } 53 public bool IsOptimalPhrase(string phrase) { 54 throw new NotImplementedException(); 55 } 53 56 54 57 public IGrammar TreeBasedGPGrammar { get; private set; } -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalTreeProblem.cs
r12099 r12893 263 263 throw new NotImplementedException(); 264 264 } 265 public bool IsOptimalPhrase(string phrase) { 266 throw new NotImplementedException(); 267 } 265 268 266 269 public IGrammar TreeBasedGPGrammar { get; private set; } -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs
r12876 r12893 30 30 // here we use a modified grammar which has only one syntax tree for each sentence 31 31 32 public IEnumerable<string> OptimalSentences { 33 get { 34 return new string[] 35 { 36 "?(m)(ll?(m)(r))mr", 37 "?(m)(rr?(m)(l))ml", 38 "r?(m)(ll?(m)(r))m", 39 "l?(m)(rr?(m)(l))m", 40 "mr?(m)(ll?(m)(r))", 41 "ml?(m)(rr?(m)(l))", 42 "?(m)(ll?(m)(l))ml", 43 "?(m)(rr?(m)(r))mr", 44 "l?(m)(ll?(m)(l))m", 45 "r?(m)(rr?(m)(r))m", 46 "ml?(m)(ll?(m)(l))", 47 "mr?(m)(rr?(m)(r))", 48 }; 49 } 50 } 32 51 33 52 private readonly IGrammar grammar; … … 138 157 139 158 public IEnumerable<Feature> GetFeatures(string phrase) { 140 yield return new Feature(phrase, 1.0); 159 for (int idx = 0; idx < 15; idx++) { 160 foreach (var t in Grammar.TerminalSymbols) 161 yield return new Feature(t.ToString(), phrase.Length > idx ? phrase[idx] == t ? 1 : 0 : 0); 162 } 141 163 //var ant = new Ant(false); 142 164 //int p = 0; … … 166 188 } 167 189 } 190 public bool IsOptimalPhrase(string phrase) { 191 var firstNonTerminalIdx = new Sequence(phrase).FirstNonTerminalIndex; 192 if (firstNonTerminalIdx == -1) firstNonTerminalIdx = phrase.Length; 193 return OptimalSentences.Any(s => s.Substring(0, firstNonTerminalIdx) == phrase.Substring(0, firstNonTerminalIdx)); 194 } 195 168 196 } 169 197 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs
r12391 r12893 6 6 using System.Linq; 7 7 using System.Net; 8 using System.Runtime.InteropServices; 8 9 using System.Security; 9 10 using System.Security.AccessControl; … … 60 61 public string Name { get { return "SymbolicRegressionPoly10"; } } 61 62 62 public SymbolicRegressionPoly10Problem( ) {63 public SymbolicRegressionPoly10Problem(int n = 500) { 63 64 this.grammar = new Grammar(grammarString); 64 65 this.TreeBasedGPGrammar = new Grammar(hlGrammarString); 65 66 66 this.N = 500;67 this.N = n; 67 68 this.x = new double[N][]; 68 69 this.y = new double[N]; … … 99 100 } 100 101 102 private static double[] erc = new double[] { 0.0, 1.0 }; 101 103 public double Evaluate(string sentence) { 102 104 var interpreter = new ExpressionInterpreter(); 103 return HeuristicLab.Common.Extensions.RSq(y, Enumerable.Range(0, N).Select(i => interpreter.Interpret(sentence, x[i])).ToArray());105 return Math.Round(HeuristicLab.Common.Extensions.RSq(y, Enumerable.Range(0, N).Select(i => interpreter.Interpret(sentence, x[i], erc)).ToArray()), 3); 104 106 } 105 107 … … 166 168 167 169 168 if (phrase.EndsWith("E")) phrase = phrase.TrimEnd('*', '+', 'E');170 //if (phrase.EndsWith("E")) phrase = phrase.TrimEnd('*', '+', 'E'); 169 171 //var len = 5; 170 172 //var start = Math.Max(0, phrase.Length - len); … … 174 176 // 175 177 176 var terms = phrase.Split('+'); 177 foreach (var t in terms.Distinct()) yield return new Feature(t, 1.0); 178 179 for (int i = 0; i < terms.Length; i++) { 180 for (int j = i + 1; j < terms.Length; j++) { 181 yield return new Feature(terms[i] + " " + terms[j], 1.0); 178 //var terms = phrase.Split('+'); 179 //foreach (var t in terms.Distinct()) yield return new Feature(t, 1.0); 180 // 181 //for (int i = 0; i < terms.Length; i++) { 182 // for (int j = i + 1; j < terms.Length; j++) { 183 // yield return new Feature(terms[i] + " " + terms[j], 1.0); 184 // } 185 //} 186 var t = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" }; 187 for (int t0Idx = 0; t0Idx < t.Length - 1; t0Idx++) { 188 for (int t1Idx = t0Idx; t1Idx < t.Length; t1Idx++) { 189 var a = t[t0Idx] + "*" + t[t1Idx]; 190 var b = t[t1Idx] + "*" + t[t0Idx]; 191 yield return new Feature(a, phrase.Contains(a) || phrase.Contains(b) ? 1 : 0); 182 192 } 183 193 } … … 225 235 } 226 236 237 private static string[][] optimalTerms = new string[][] 238 { 239 new string[] { "a*b","b*a",}, 240 new string[] { "c*d","d*c",}, 241 new string[] { "e*f","f*e",}, 242 new string[] { "a*g*i","a*i*g","g*a*i","g*i*a","i*a*g","i*g*a"}, 243 new string[] { "c*j*f","c*f*j","j*c*f","j*f*c","f*c*j","f*j*c"}, 244 }; 245 246 private static int[][] permute5 = new int[][] 247 { 248 new int[] { 4, 3, 2, 0, 1 }, 249 new int[] { 4, 3, 2, 1, 0 }, 250 new int[] { 4, 3, 0, 2, 1 }, 251 new int[] { 4, 3, 1, 2, 0 }, 252 new int[] { 4, 3, 0, 1, 2 }, 253 new int[] { 4, 3, 1, 0, 2 }, 254 new int[] { 4, 2, 3, 0, 1 }, 255 new int[] { 4, 2, 3, 1, 0 }, 256 new int[] { 4, 0, 3, 2, 1 }, 257 new int[] { 4, 1, 3, 2, 0 }, 258 new int[] { 4, 0, 3, 1, 2 }, 259 new int[] { 4, 1, 3, 0, 2 }, 260 new int[] { 4, 2, 0, 3, 1 }, 261 new int[] { 4, 2, 1, 3, 0 }, 262 new int[] { 4, 0, 2, 3, 1 }, 263 new int[] { 4, 1, 2, 3, 0 }, 264 new int[] { 4, 0, 1, 3, 2 }, 265 new int[] { 4, 1, 0, 3, 2 }, 266 new int[] { 4, 2, 0, 1, 3 }, 267 new int[] { 4, 2, 1, 0, 3 }, 268 new int[] { 4, 0, 2, 1, 3 }, 269 new int[] { 4, 1, 2, 0, 3 }, 270 new int[] { 4, 0, 1, 2, 3 }, 271 new int[] { 4, 1, 0, 2, 3 }, 272 new int[] { 3, 4, 2, 0, 1 }, 273 new int[] { 3, 4, 2, 1, 0 }, 274 new int[] { 3, 4, 0, 2, 1 }, 275 new int[] { 3, 4, 1, 2, 0 }, 276 new int[] { 3, 4, 0, 1, 2 }, 277 new int[] { 3, 4, 1, 0, 2 }, 278 new int[] { 2, 4, 3, 0, 1 }, 279 new int[] { 2, 4, 3, 1, 0 }, 280 new int[] { 0, 4, 3, 2, 1 }, 281 new int[] { 1, 4, 3, 2, 0 }, 282 new int[] { 0, 4, 3, 1, 2 }, 283 new int[] { 1, 4, 3, 0, 2 }, 284 new int[] { 2, 4, 0, 3, 1 }, 285 new int[] { 2, 4, 1, 3, 0 }, 286 new int[] { 0, 4, 2, 3, 1 }, 287 new int[] { 1, 4, 2, 3, 0 }, 288 new int[] { 0, 4, 1, 3, 2 }, 289 new int[] { 1, 4, 0, 3, 2 }, 290 new int[] { 2, 4, 0, 1, 3 }, 291 new int[] { 2, 4, 1, 0, 3 }, 292 new int[] { 0, 4, 2, 1, 3 }, 293 new int[] { 1, 4, 2, 0, 3 }, 294 new int[] { 0, 4, 1, 2, 3 }, 295 new int[] { 1, 4, 0, 2, 3 }, 296 new int[] { 3, 2, 4, 0, 1 }, 297 new int[] { 3, 2, 4, 1, 0 }, 298 new int[] { 3, 0, 4, 2, 1 }, 299 new int[] { 3, 1, 4, 2, 0 }, 300 new int[] { 3, 0, 4, 1, 2 }, 301 new int[] { 3, 1, 4, 0, 2 }, 302 new int[] { 2, 3, 4, 0, 1 }, 303 new int[] { 2, 3, 4, 1, 0 }, 304 new int[] { 0, 3, 4, 2, 1 }, 305 new int[] { 1, 3, 4, 2, 0 }, 306 new int[] { 0, 3, 4, 1, 2 }, 307 new int[] { 1, 3, 4, 0, 2 }, 308 new int[] { 2, 0, 4, 3, 1 }, 309 new int[] { 2, 1, 4, 3, 0 }, 310 new int[] { 0, 2, 4, 3, 1 }, 311 new int[] { 1, 2, 4, 3, 0 }, 312 new int[] { 0, 1, 4, 3, 2 }, 313 new int[] { 1, 0, 4, 3, 2 }, 314 new int[] { 2, 0, 4, 1, 3 }, 315 new int[] { 2, 1, 4, 0, 3 }, 316 new int[] { 0, 2, 4, 1, 3 }, 317 new int[] { 1, 2, 4, 0, 3 }, 318 new int[] { 0, 1, 4, 2, 3 }, 319 new int[] { 1, 0, 4, 2, 3 }, 320 new int[] { 3, 2, 0, 4, 1 }, 321 new int[] { 3, 2, 1, 4, 0 }, 322 new int[] { 3, 0, 2, 4, 1 }, 323 new int[] { 3, 1, 2, 4, 0 }, 324 new int[] { 3, 0, 1, 4, 2 }, 325 new int[] { 3, 1, 0, 4, 2 }, 326 new int[] { 2, 3, 0, 4, 1 }, 327 new int[] { 2, 3, 1, 4, 0 }, 328 new int[] { 0, 3, 2, 4, 1 }, 329 new int[] { 1, 3, 2, 4, 0 }, 330 new int[] { 0, 3, 1, 4, 2 }, 331 new int[] { 1, 3, 0, 4, 2 }, 332 new int[] { 2, 0, 3, 4, 1 }, 333 new int[] { 2, 1, 3, 4, 0 }, 334 new int[] { 0, 2, 3, 4, 1 }, 335 new int[] { 1, 2, 3, 4, 0 }, 336 new int[] { 0, 1, 3, 4, 2 }, 337 new int[] { 1, 0, 3, 4, 2 }, 338 new int[] { 2, 0, 1, 4, 3 }, 339 new int[] { 2, 1, 0, 4, 3 }, 340 new int[] { 0, 2, 1, 4, 3 }, 341 new int[] { 1, 2, 0, 4, 3 }, 342 new int[] { 0, 1, 2, 4, 3 }, 343 new int[] { 1, 0, 2, 4, 3 }, 344 new int[] { 3, 2, 0, 1, 4 }, 345 new int[] { 3, 2, 1, 0, 4 }, 346 new int[] { 3, 0, 2, 1, 4 }, 347 new int[] { 3, 1, 2, 0, 4 }, 348 new int[] { 3, 0, 1, 2, 4 }, 349 new int[] { 3, 1, 0, 2, 4 }, 350 new int[] { 2, 3, 0, 1, 4 }, 351 new int[] { 2, 3, 1, 0, 4 }, 352 new int[] { 0, 3, 2, 1, 4 }, 353 new int[] { 1, 3, 2, 0, 4 }, 354 new int[] { 0, 3, 1, 2, 4 }, 355 new int[] { 1, 3, 0, 2, 4 }, 356 new int[] { 2, 0, 3, 1, 4 }, 357 new int[] { 2, 1, 3, 0, 4 }, 358 new int[] { 0, 2, 3, 1, 4 }, 359 new int[] { 1, 2, 3, 0, 4 }, 360 new int[] { 0, 1, 3, 2, 4 }, 361 new int[] { 1, 0, 3, 2, 4 }, 362 new int[] { 2, 0, 1, 3, 4 }, 363 new int[] { 2, 1, 0, 3, 4 }, 364 new int[] { 0, 2, 1, 3, 4 }, 365 new int[] { 1, 2, 0, 3, 4 }, 366 new int[] { 0, 1, 2, 3, 4 }, 367 new int[] { 1, 0, 2, 3, 4 }, 368 }; 369 370 371 private static HashSet<string>[] optimalSentences; 372 static SymbolicRegressionPoly10Problem() { 373 optimalSentences = Enumerable.Range(0, 24).Select(i => new HashSet<string>()).ToArray(); 374 375 foreach (var t0Idx in new[] { 0, 1 }) 376 foreach (var t1Idx in new[] { 0, 1 }) 377 foreach (var t2Idx in new[] { 0, 1 }) 378 foreach (var t3Idx in new[] { 0, 1, 2, 3, 4, 5 }) 379 foreach (var t4Idx in new[] { 0, 1, 2, 3, 4, 5 }) { 380 foreach (var p in permute5) { 381 int[] idx = new int[] { t0Idx, t1Idx, t2Idx, t3Idx, t4Idx }; 382 optimalSentences[23].Add(string.Join("+", p.Select(pi => optimalTerms[pi][idx[pi]]))); 383 } 384 } 385 for (int i = 0; i < 23; i += 2) { 386 var newElements = new HashSet<string>(); 387 foreach (var sentence in optimalSentences[23]) { 388 newElements.Add(sentence.Substring(0, i) + "E"); 389 } 390 foreach (var e in newElements) optimalSentences[i + 1].Add(e); 391 } 392 } 393 394 public bool IsOptimalPhrase(string phrase) { 395 return optimalSentences[phrase.Length].Contains(phrase); 396 } 397 227 398 private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) { 228 399 if (treeNode.SubtreeCount == 0) {
Note: See TracChangeset
for help on using the changeset viewer.