1 | using System;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem {
|
---|
4 | using System.Linq;
|
---|
5 |
|
---|
6 | using Common;
|
---|
7 | using Configuration;
|
---|
8 | using Core;
|
---|
9 | using Data.Pool;
|
---|
10 | using HeuristicLab.Data;
|
---|
11 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
12 | using HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer;
|
---|
13 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
14 | using HeuristicLab.Problems.ProgramSynthesis.Push.Individual;
|
---|
15 | using HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite;
|
---|
16 |
|
---|
17 | using Interpreter;
|
---|
18 | using Optimization;
|
---|
19 | using Parameters;
|
---|
20 | using Persistence.Default.CompositeSerializers.Storable;
|
---|
21 | using Random;
|
---|
22 |
|
---|
23 | [StorableClass]
|
---|
24 | public abstract class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {
|
---|
25 | [Storable]
|
---|
26 | protected readonly PushConfigurationParameterCollection config;
|
---|
27 | protected PushInterpreterPool pool;
|
---|
28 | protected readonly ObjectPool<IRandom> randomPool = new ObjectPool<IRandom>(() => new MersenneTwister(), Environment.ProcessorCount * 4);
|
---|
29 |
|
---|
30 | [Storable]
|
---|
31 | protected readonly IPushEvaluator PushEvaluator;
|
---|
32 |
|
---|
33 | private const string BestTrainingSolutionResultName = "Best Solution";
|
---|
34 | private const string TestQualityResultName = "Test Quality";
|
---|
35 |
|
---|
36 | protected PushProblem(PushBenchmarkSuiteEvaluator evaluator) {
|
---|
37 | config = new PushConfigurationParameterCollection();
|
---|
38 | PushEvaluator = evaluator;
|
---|
39 |
|
---|
40 | InitData();
|
---|
41 | InitEvents();
|
---|
42 | InitParameters();
|
---|
43 | InitEncoding();
|
---|
44 | InitOperators();
|
---|
45 | }
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | protected PushProblem(bool deserializing)
|
---|
49 | : base(deserializing) {
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected PushProblem(PushProblem original, Cloner cloner)
|
---|
53 | : base(original, cloner) {
|
---|
54 | config = cloner.Clone(original.config);
|
---|
55 | PushEvaluator = cloner.Clone(original.PushEvaluator);
|
---|
56 |
|
---|
57 | InitData();
|
---|
58 | InitEvents();
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableHook(HookType.AfterDeserialization)]
|
---|
62 | // ReSharper disable once UnusedMember.Local
|
---|
63 | private void AfterDeserialization() {
|
---|
64 | InitData();
|
---|
65 | InitEvents();
|
---|
66 | InitParameters();
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void InitData() {
|
---|
70 | pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, config);
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void InitEvents() {
|
---|
74 | config.EnabledExpressionsChanged += EnabledExpressionsChanged;
|
---|
75 | }
|
---|
76 |
|
---|
77 | private void EnabledExpressionsChanged(object sender, EnabledExpressionsChangedEventArgs e) {
|
---|
78 | Encoding.Bounds[0, 1] = config.EnabledExpressions.Count;
|
---|
79 | Encoding.BoundsParameter.Value[0, 1] = config.EnabledExpressions.Count;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public ILookupParameter<IntValue> SeedParamater
|
---|
83 | {
|
---|
84 | get { return (ILookupParameter<IntValue>)Parameters["Seed"]; }
|
---|
85 | }
|
---|
86 |
|
---|
87 | #region Parameters
|
---|
88 | private const string InitProgramLengthParameterName = "InitProgramLength";
|
---|
89 | private const string InitProgramLengthParameterDescription = "This is the initial size of a push program.";
|
---|
90 | private const string PushConfigurationParameterName = "PushConfiguration";
|
---|
91 |
|
---|
92 | public const string CasesScopeParameterName = "CaseQualities";
|
---|
93 | public const string CaseQualitiesScopeParameterName = "CaseQualities";
|
---|
94 |
|
---|
95 | private void InitParameters() {
|
---|
96 | foreach (var paramater in config.Parameters) {
|
---|
97 | if (!Parameters.ContainsKey(paramater.Name)) {
|
---|
98 | Parameters.Add(paramater);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (!Parameters.ContainsKey(PushConfigurationParameterName))
|
---|
103 | Parameters.Add(new ValueParameter<IReadOnlyPushConfiguration>(PushConfigurationParameterName, config) {
|
---|
104 | Hidden = true
|
---|
105 | });
|
---|
106 |
|
---|
107 | if (!Parameters.ContainsKey(InitProgramLengthParameterName)) {
|
---|
108 | Parameters.Add(new FixedValueParameter<IntValue>(
|
---|
109 | InitProgramLengthParameterName,
|
---|
110 | InitProgramLengthParameterDescription,
|
---|
111 | new IntValue(50)));
|
---|
112 | }
|
---|
113 |
|
---|
114 | Encoding.LengthParameter = InitProgramLengthParameter as IFixedValueParameter<IntValue>;
|
---|
115 |
|
---|
116 | if (!Parameters.ContainsKey(CasesScopeParameterName))
|
---|
117 | Parameters.Add(new LookupParameter<BoolArray>(CasesScopeParameterName, "The training cases that have been successfully executed."));
|
---|
118 |
|
---|
119 | if (!Parameters.ContainsKey(CaseQualitiesScopeParameterName))
|
---|
120 | Parameters.Add(new LookupParameter<DoubleArray>(CaseQualitiesScopeParameterName, "The quality of every single training case for each individual"));
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected override void OnReset() {
|
---|
124 | base.OnReset();
|
---|
125 |
|
---|
126 | // clear pools and free reserved memory
|
---|
127 | pool.Clear();
|
---|
128 | IndividualMapper.Clear();
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void InitEncoding() {
|
---|
132 | Encoding.Bounds[0, 0] = 0;
|
---|
133 | Encoding.Bounds[0, 1] = config.EnabledExpressions.Count;
|
---|
134 | Encoding.Length = config.MaxPointsInProgram;
|
---|
135 | }
|
---|
136 |
|
---|
137 | private void InitOperators() {
|
---|
138 | Operators.Add(new PushExpressionFrequencyAnalyzer());
|
---|
139 | }
|
---|
140 |
|
---|
141 | /// <summary>
|
---|
142 | /// This is the inital size of an push program generated by a solution creator
|
---|
143 | /// </summary>
|
---|
144 | public IValueParameter<IntValue> InitProgramLengthParameter
|
---|
145 | {
|
---|
146 | get { return (IValueParameter<IntValue>)Parameters[InitProgramLengthParameterName]; }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public int InitProgramLength
|
---|
150 | {
|
---|
151 | get { return InitProgramLengthParameter.Value.Value; }
|
---|
152 | set
|
---|
153 | {
|
---|
154 | InitProgramLengthParameter.Value.Value = value;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | #endregion
|
---|
158 |
|
---|
159 | public override bool Maximization { get { return false; } }
|
---|
160 |
|
---|
161 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
162 | IndividualMapper.Reset();
|
---|
163 |
|
---|
164 | var bestQuality = Maximization ? qualities.Max() : qualities.Min();
|
---|
165 | var bestIdx = Array.IndexOf(qualities, bestQuality);
|
---|
166 | var vector = individuals[bestIdx].IntegerVector();
|
---|
167 | var seed = vector.GetSeed();
|
---|
168 | var rand = randomPool.Allocate();
|
---|
169 | rand.Reset(seed);
|
---|
170 |
|
---|
171 | var program = (PushProgram)vector.ToPushProgram(config, rand).Clone();
|
---|
172 | var isIndividualBetter = AnalyzeBestTrainingSolution(program, bestQuality, results, rand);
|
---|
173 |
|
---|
174 | if (isIndividualBetter) {
|
---|
175 | rand.Reset(seed);
|
---|
176 | AnalyzeBestTestSolution(program, results, rand);
|
---|
177 | }
|
---|
178 |
|
---|
179 | randomPool.Free(rand);
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void AnalyzeBestTestSolution(PushProgram program, ResultCollection results, IRandom random) {
|
---|
183 | var testResult = PushEvaluator.EvaluateTraining(pool, program, random);
|
---|
184 |
|
---|
185 | if (!results.ContainsKey(TestQualityResultName)) {
|
---|
186 | results.Add(new Result(TestQualityResultName, new DoubleValue(testResult.AvgQuality)));
|
---|
187 | } else {
|
---|
188 | ((DoubleValue)results[TestQualityResultName].Value).Value = testResult.AvgQuality;
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | private bool AnalyzeBestTrainingSolution(PushProgram program, double bestQuality, ResultCollection results, IRandom random) {
|
---|
193 | if (!results.ContainsKey(BestTrainingSolutionResultName)) {
|
---|
194 | var solution = CreatePushSolution(
|
---|
195 | program,
|
---|
196 | bestQuality,
|
---|
197 | random,
|
---|
198 | config,
|
---|
199 | PushEvaluator);
|
---|
200 |
|
---|
201 | results.Add(new Result(BestTrainingSolutionResultName, solution));
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 |
|
---|
205 | var currentBestQuality = ((PushSolution)results[BestTrainingSolutionResultName].Value).Quality;
|
---|
206 |
|
---|
207 | if ((!Maximization && currentBestQuality > bestQuality) ||
|
---|
208 | (Maximization && currentBestQuality < bestQuality)) {
|
---|
209 | results[BestTrainingSolutionResultName].Value = CreatePushSolution(
|
---|
210 | program,
|
---|
211 | bestQuality,
|
---|
212 | random,
|
---|
213 | config,
|
---|
214 | PushEvaluator);
|
---|
215 | return true;
|
---|
216 | }
|
---|
217 |
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 |
|
---|
221 | private EvaluationResult EvaluateIntegerVector(IntegerVector vector) {
|
---|
222 | var rand = randomPool.Allocate();
|
---|
223 | var seed = vector.GetSeed();
|
---|
224 | rand.Reset(seed);
|
---|
225 |
|
---|
226 | var program = vector.ToPushProgram(config, rand);
|
---|
227 | var result = PushEvaluator.EvaluateTraining(pool, program, rand);
|
---|
228 | randomPool.Free(rand);
|
---|
229 |
|
---|
230 | return result;
|
---|
231 | }
|
---|
232 |
|
---|
233 | protected abstract PushSolution CreatePushSolution(
|
---|
234 | PushProgram program,
|
---|
235 | double bestQuality,
|
---|
236 | IRandom random,
|
---|
237 | IReadOnlyPushConfiguration config,
|
---|
238 | IPushEvaluator evaluator);
|
---|
239 |
|
---|
240 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
241 | var vector = individual.IntegerVector();
|
---|
242 | var result = EvaluateIntegerVector(vector);
|
---|
243 |
|
---|
244 | individual[CaseQualitiesScopeParameterName] = new DoubleArray(result.ExampleQualities);
|
---|
245 |
|
---|
246 | return result.AvgQuality;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | } |
---|