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