Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/24/14 19:44:48 (11 years ago)
Author:
gkronber
Message:

#2026 fixed a bug in the brute force solver

Location:
branches/HeuristicLab.Problems.GPDL/CodeGenerator
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/BruteForceCodeGen.cs

    r10392 r10393  
    3131        }
    3232      } else if(Grammar.subtreeCount[parentState] > 1) {
    33         var trees = new IEnumerable<Tree>[Grammar.subtreeCount[parentState]];
    34         for(int subtreeIdx = 0; subtreeIdx < Grammar.subtreeCount[parentState]; subtreeIdx++) {
    35           trees[subtreeIdx] = GenerateTrees(Grammar.transition[parentState][subtreeIdx], depth - 1);
    36         }
    37         foreach(var e in CartesianProduct(trees)) {
    38           yield return new Tree(-1, e.ToArray()); // altIdx is ignored
     33        var nums = Enumerable.Range(1, depth - 1);
     34        var depthCombinations = CartesianProduct(Enumerable.Repeat(nums, Grammar.subtreeCount[parentState]))
     35           .Where(comb => comb.Max() == depth - 1);
     36        foreach(var depthCombination in depthCombinations) {
     37          var trees = new IEnumerable<Tree>[Grammar.subtreeCount[parentState]];
     38          var depthCombinationEnumerator = depthCombination.GetEnumerator();
     39          depthCombinationEnumerator.MoveNext();
     40          for(int subtreeIdx = 0; subtreeIdx < Grammar.subtreeCount[parentState]; subtreeIdx++) {
     41            trees[subtreeIdx] = GenerateTrees(Grammar.transition[parentState][subtreeIdx], depthCombinationEnumerator.Current);
     42            depthCombinationEnumerator.MoveNext();
     43          }
     44          foreach(var e in CartesianProduct(trees)) {
     45            yield return new Tree(-1, e.ToArray()); // altIdx is ignored
     46          }
    3947        }
    4048      }
    4149    }
    4250
    43     public IEnumerable<IEnumerable<Tree>> CartesianProduct(IEnumerable<IEnumerable<Tree>> sets) {
     51    public IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sets) {
    4452      if(sets.Count() == 1) {
    4553        foreach(var e in sets.First()) {
    46           yield return new Tree[] {e};
     54          yield return new T[] {e};
    4755        }
    4856      }
     
    5159        foreach(var e in firstSet) {
    5260          foreach(var p in CartesianProduct(sets.Skip(1))) {
    53             yield return new Tree[] {e}.Concat(p);
     61            yield return new T[] {e}.Concat(p);
    5462          }
    5563        }
     
    9098        foreach(var t in GenerateTrees(0, depth)) {
    9199          var f = problem.Evaluate(t);
    92           //t.PrintTree(0); Console.WriteLine();
     100          // t.PrintTree(0); Console.WriteLine();
    93101          var size = t.GetSize();
    94102          sumSize += size;
     
    111119          }
    112120        }
     121        Console.WriteLine(""n={0}"",n);
    113122      }
    114123    }
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs

    r10392 r10393  
    149149    private void GenerateSolvers(GPDefNode ast, SourceBuilder solverSourceCode) {
    150150      var grammar = CreateGrammarFromAst(ast);
    151       // var randomSearchCodeGen = new RandomSearchCodeGen();
    152       // randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
     151      //var randomSearchCodeGen = new RandomSearchCodeGen();
     152      //randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
    153153      var bruteForceSearchCodeGen = new BruteForceCodeGen();
    154154      bruteForceSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
Note: See TracChangeset for help on using the changeset viewer.