1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem {
|
---|
2 | using System.Linq;
|
---|
3 |
|
---|
4 | using Common;
|
---|
5 | using Configuration;
|
---|
6 | using Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
9 | using HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer;
|
---|
10 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Individual;
|
---|
12 | using HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator;
|
---|
13 | using Optimization;
|
---|
14 | using Persistence.Default.CompositeSerializers.Storable;
|
---|
15 |
|
---|
16 | [StorableClass]
|
---|
17 | public abstract class IntegerVectorPushProblem : PushProblemBase<IntegerVectorEncoding> {
|
---|
18 | protected IntegerVectorPushProblem(IPushEvaluator evaluator) : base(evaluator) {
|
---|
19 | InitEvents();
|
---|
20 | InitEncoding();
|
---|
21 | InitOperators();
|
---|
22 | }
|
---|
23 |
|
---|
24 | [StorableConstructor]
|
---|
25 | protected IntegerVectorPushProblem(bool deserializing)
|
---|
26 | : base(deserializing) {
|
---|
27 | }
|
---|
28 |
|
---|
29 | protected IntegerVectorPushProblem(IntegerVectorPushProblem original, Cloner cloner)
|
---|
30 | : base(original, cloner) {
|
---|
31 | InitEvents();
|
---|
32 | }
|
---|
33 |
|
---|
34 | [StorableHook(HookType.AfterDeserialization)]
|
---|
35 | // ReSharper disable once UnusedMember.Local
|
---|
36 | private void AfterDeserialization() {
|
---|
37 | InitEvents();
|
---|
38 | }
|
---|
39 |
|
---|
40 | private void InitEvents() {
|
---|
41 | Config.EnabledExpressionsChanged += EnabledExpressionsChanged;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private void EnabledExpressionsChanged(object sender, EnabledExpressionsChangedEventArgs e) {
|
---|
45 | Encoding.Bounds[0, 1] = Config.EnabledExpressions.Count;
|
---|
46 | Encoding.BoundsParameter.Value[0, 1] = Config.EnabledExpressions.Count;
|
---|
47 | }
|
---|
48 |
|
---|
49 | //protected override void OnReset() {
|
---|
50 | // base.OnReset();
|
---|
51 |
|
---|
52 | // // clear pools and free reserved memory
|
---|
53 | // Pool.Clear();
|
---|
54 | // IndividualMapper.Clear();
|
---|
55 | // RandomPool.Clear();
|
---|
56 | // Config.Seed = 0;
|
---|
57 | //}
|
---|
58 |
|
---|
59 | private void InitEncoding() {
|
---|
60 | Encoding.Bounds[0, 0] = 0;
|
---|
61 | Encoding.Bounds[0, 1] = Config.EnabledExpressions.Count;
|
---|
62 | Encoding.Length = Config.MaxPointsInProgram;
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void InitOperators() {
|
---|
66 |
|
---|
67 | var solutionCreator = Operators.OfType<PushSolutionCreator>().FirstOrDefault();
|
---|
68 |
|
---|
69 | if (solutionCreator == null) {
|
---|
70 | solutionCreator = new PushSolutionCreator();
|
---|
71 | Operators.Add(solutionCreator);
|
---|
72 | }
|
---|
73 |
|
---|
74 | solutionCreator.ErcOptions = Config.ErcOptions;
|
---|
75 |
|
---|
76 | if (!Operators.OfType<PushExpressionFrequencyAnalyzer>().Any()) {
|
---|
77 | Operators.Add(new PushExpressionFrequencyAnalyzer());
|
---|
78 | }
|
---|
79 |
|
---|
80 | SolutionCreator = solutionCreator;
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected override PushProgram MapIndividual(Individual individual, IRandom random) {
|
---|
84 | var program = individual.ToPushProgram(Config, random);
|
---|
85 |
|
---|
86 | return program;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
90 | // init seed of random pool
|
---|
91 | //Interlocked.CompareExchange(ref RandomPool.Seed, random.Next(), 0);
|
---|
92 | //Config.Seed = RandomPool.Seed;
|
---|
93 |
|
---|
94 | //var rand = RandomPool.ResetAndAllocate();
|
---|
95 | var program = MapIndividual(individual, random);
|
---|
96 | //RandomPool.Free(rand);
|
---|
97 |
|
---|
98 | //rand = RandomPool.ResetAndAllocate();
|
---|
99 | var result = PushEvaluator.EvaluateTraining(Pool, program, random);
|
---|
100 | //RandomPool.Free(rand);
|
---|
101 |
|
---|
102 | individual[CaseQualitiesScopeParameterName] = new DoubleArray(result.ExampleQualities);
|
---|
103 |
|
---|
104 | return result.AvgQuality;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | } |
---|