Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/IndividualMapper.cs @ 14875

Last change on this file since 14875 was 14875, checked in by pkimmesw, 8 years ago

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File size: 2.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem {
2
3  using HeuristicLab.Core;
4  using HeuristicLab.Encodings.IntegerVectorEncoding;
5  using HeuristicLab.Optimization;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
9  using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
10  using HeuristicLab.Random;
11
12  public static class IndividualMapper {
13    public static PushProgram ToPushProgram(this Individual individual, IReadOnlyPushConfiguration config, ObjectPool<IRandom> randomPool) {
14      return individual.IntegerVector().ToPushProgram(config, randomPool);
15    }
16
17    public static PushProgram ToPushProgram(this Individual individual, IReadOnlyPushConfiguration config) {
18      return individual.IntegerVector().ToPushProgram(config);
19    }
20
21    public static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config, ObjectPool<IRandom> randomPool) {
22      var random = randomPool.Allocate();
23      var seed = vector.GetSeed();
24
25      random.Reset(seed);
26      var program = vector.ToPushProgram(config, random);
27      randomPool.Free(random);
28
29      return program;
30    }
31
32    public static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config) {
33      var seed = (uint)vector.GetSeed();
34      return vector.ToPushProgram(config, new MersenneTwister(seed));
35    }
36
37    private static int GetSeed(this IntegerVector vector) {
38      var seed = 0;
39      for (var i = 0; i < vector.Length; i++)
40        seed += (i + 1) * vector[i];
41      return seed;
42    }
43
44    private static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config, IRandom random) {
45      var expressions = new Expression[vector.Length];
46      var enabledExpressions = config.EnabledExpressions;
47      var ercOptions = config.ErcOptions;
48
49      for (var i = 0; i < vector.Length; i++)
50        expressions[i] = CodeGeneratorUtils.CreateExpressionOrErc(vector[i], random, enabledExpressions, ercOptions);
51
52      return new PushProgram(expressions);
53    }
54  }
55}
Note: See TracBrowser for help on using the repository browser.