Changeset 12893 for branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs
- Timestamp:
- 08/24/15 13:56:27 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.