1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Solution {
|
---|
2 | using Common;
|
---|
3 | using Configuration;
|
---|
4 | using Core;
|
---|
5 | using Evaluator;
|
---|
6 | using Expressions;
|
---|
7 | using Persistence.Default.CompositeSerializers.Storable;
|
---|
8 |
|
---|
9 | [StorableClass]
|
---|
10 | public class PushSolution : NamedItem, IPushSolution {
|
---|
11 | [Storable]
|
---|
12 | public readonly double Quality;
|
---|
13 | [Storable]
|
---|
14 | protected readonly IRandom Random;
|
---|
15 | [Storable]
|
---|
16 | public readonly IReadOnlyPushConfiguration Config;
|
---|
17 | [Storable]
|
---|
18 | public readonly IPushEvaluator Evaluator;
|
---|
19 | [Storable]
|
---|
20 | protected readonly bool simplify;
|
---|
21 | [Storable]
|
---|
22 | public PushProgram Program;
|
---|
23 |
|
---|
24 |
|
---|
25 | public PushSolution(PushProgram program, double quality, IRandom random, IReadOnlyPushConfiguration config, IPushEvaluator evaluator, bool simplify = false)
|
---|
26 | : base("Solution", "A push solution.") {
|
---|
27 | Quality = quality;
|
---|
28 | Random = random;
|
---|
29 | Config = config;
|
---|
30 | Evaluator = evaluator;
|
---|
31 | this.simplify = simplify;
|
---|
32 |
|
---|
33 | Program = simplify
|
---|
34 | ? Simplifier.Simplifier.Simplify(program, Config, p => Evaluator.EvaluateTraining(Config, p, Random).AvgQuality)
|
---|
35 | : program;
|
---|
36 |
|
---|
37 | }
|
---|
38 |
|
---|
39 | public PushSolution(PushSolution origin, Cloner cloner) : base(origin, cloner) {
|
---|
40 | Program = cloner.Clone(origin.Program);
|
---|
41 | Quality = origin.Quality;
|
---|
42 | Random = cloner.Clone(origin.Random);
|
---|
43 | Config = cloner.Clone(origin.Config);
|
---|
44 | Evaluator = cloner.Clone(origin.Evaluator);
|
---|
45 | Program = origin.Program; // push programs are immutable like all expressions
|
---|
46 | simplify = origin.simplify;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public IRandom GetRandom() {
|
---|
50 | return (IRandom)Random.Clone();
|
---|
51 | }
|
---|
52 |
|
---|
53 | public virtual PushSolution Simplify() {
|
---|
54 | return new PushSolution(Program, Quality, Random, Config, Evaluator, true);
|
---|
55 | }
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | protected PushSolution(bool deserializing) : base(deserializing) { }
|
---|
59 |
|
---|
60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
61 | return new PushSolution(this, cloner);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|