1 | using System;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem {
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Linq;
|
---|
6 | using Common;
|
---|
7 | using Configuration;
|
---|
8 | using Core;
|
---|
9 | using Encodings.IntegerVectorEncoding;
|
---|
10 |
|
---|
11 | using HeuristicLab.BenchmarkSuite;
|
---|
12 | using HeuristicLab.BenchmarkSuite.Problems;
|
---|
13 | using HeuristicLab.Data;
|
---|
14 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
15 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
16 |
|
---|
17 | using Instances;
|
---|
18 | using Interpreter;
|
---|
19 | using Optimization;
|
---|
20 | using Parameters;
|
---|
21 | using Persistence.Default.CompositeSerializers.Storable;
|
---|
22 |
|
---|
23 | [StorableClass]
|
---|
24 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 180)]
|
---|
25 | [Item("Push Problem", "")]
|
---|
26 | public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer<Data> {
|
---|
27 | [Storable]
|
---|
28 | private readonly PushConfiguration config;
|
---|
29 | private PushInterpreterPool pool;
|
---|
30 | private IBenchmarkSuiteDataDescriptor dataDescriptor;
|
---|
31 |
|
---|
32 | public PushProblem() {
|
---|
33 | config = new PushConfiguration();
|
---|
34 | pool = new PushInterpreterPool(10000, 4096, 65536, config);
|
---|
35 |
|
---|
36 | InitEvents();
|
---|
37 | InitParameters();
|
---|
38 | Instructions = config;
|
---|
39 | }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | public PushProblem(bool deserializing)
|
---|
43 | : base(deserializing) {
|
---|
44 | }
|
---|
45 |
|
---|
46 | public PushProblem(PushProblem original, Cloner cloner)
|
---|
47 | : base(original, cloner) {
|
---|
48 | config = cloner.Clone(original.config);
|
---|
49 | pool = new PushInterpreterPool(65536, 1024, 65536, config);
|
---|
50 |
|
---|
51 | Instructions = config;
|
---|
52 |
|
---|
53 | this.InitEvents();
|
---|
54 | }
|
---|
55 |
|
---|
56 | [StorableHook(HookType.AfterDeserialization)]
|
---|
57 | // ReSharper disable once UnusedMember.Local
|
---|
58 | private void AfterDeserialization() {
|
---|
59 | pool = new PushInterpreterPool(config);
|
---|
60 | Instructions = config;
|
---|
61 |
|
---|
62 | InitEvents();
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void InitEvents() {
|
---|
66 | config.EnabledExpressionsChanged += EnabledExpressionsChanged;
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void EnabledExpressionsChanged(object sender, EnabledExpressionsChangedEventArgs e) {
|
---|
70 | this.Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;
|
---|
71 | this.Encoding.BoundsParameter.Value[0, 1] = config.EnabledExpressions.Count - 1;
|
---|
72 | }
|
---|
73 |
|
---|
74 | #region Parameters
|
---|
75 |
|
---|
76 | private const string EvalPushLimitParameterName = "EvalPushLimit";
|
---|
77 | private const string EvalPushLimitParameterDescription = "This is the maximum allowed number of \"executions\" in a single top-level call to the interpreter. The execution of a single Push instruction counts as one execution, as does the processing of a single literal, as does the descent into one layer of parentheses (that is, the processing of the \"(\" counts as one execution).";
|
---|
78 | private const string MaxProgramLengthParameterName = "MaxProgramLength";
|
---|
79 | private const string MaxProgramLengthParameterDescription = "This is the maximum size of an item on the CODE stack, expressed as a number of points. A point is an instruction, a literal, or a pair of parentheses.";
|
---|
80 | private const string TopLevelPushCodeParameterName = "TopLevelPushCode";
|
---|
81 | private const string TopLevelPushCodeParameterDescription = "When TRUE (which is the default), code passed to the top level of the interpreter will be pushed onto the CODE stack prior to execution.";
|
---|
82 | private const string TopLevelPopCodeParameterName = "TopLevelPopCode";
|
---|
83 | private const string TopLevelPopCodeParameterDescription = "When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE.";
|
---|
84 | private const string InstructionsParameterName = "Instructions";
|
---|
85 | private const string InstructionsParameterDescription = "Enables/Disables Instructions";
|
---|
86 | private const string DataParameterName = "Data";
|
---|
87 | private const string DataParameterDescription = "Program Synthesis";
|
---|
88 | private const string MinRandomIntegerParameterName = "MinRandomInteger";
|
---|
89 | private const string MinRandomIntegerParameterDescription = "The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.";
|
---|
90 | private const string MaxRandomIntegerParameterName = "MaxRandomInteger";
|
---|
91 | private const string MaxRandomIntegerParameterDescription = "The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.";
|
---|
92 | private const string MinRandomFloatParameterName = "MinRandomFloat";
|
---|
93 | private const string MinRandomFloatParameterDescription = "The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.";
|
---|
94 | private const string MaxRandomFloatParameterName = "MaxRandomFloat";
|
---|
95 | private const string MaxRandomFloatParameterDescription = "The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.";
|
---|
96 | private const string NewErcNameProbabilityParameterName = "NewErcNameProbability";
|
---|
97 | private const string NewErcNameProbabilityParameterDescription = "The probability that the selection of the ephemeral random NAME constant for inclusion in randomly generated code will produce a new name.";
|
---|
98 | private const string ErcProbabilityParameterName = "ErcProbability";
|
---|
99 | private const string ErcProbabilityParameterDescription = "The probability that the selection of a epheral random literal constant for inclusion in randomly generated code will produce a new literal.";
|
---|
100 | private const string MaxPointsInRandomInstructionParameterName = "MaxPointsInRandomInstruction";
|
---|
101 | private const string MaxPointsInRandomInstructionParameterDescription = "MaxPointsInRandomInstruction";
|
---|
102 | private const string DataBoundsParameterName = "DataBounds";
|
---|
103 | private const string MaxProgramDepthParameterName = "MaxProgramDepth";
|
---|
104 | private const string MaxProgramDepthParameterDescription = "";
|
---|
105 |
|
---|
106 | private void InitParameters() {
|
---|
107 |
|
---|
108 | Parameters.Add(new FixedValueParameter<DataBounds>(DataBoundsParameterName));
|
---|
109 |
|
---|
110 | Parameters.Add(new FixedValueParameter<IntValue>(
|
---|
111 | EvalPushLimitParameterName,
|
---|
112 | EvalPushLimitParameterDescription,
|
---|
113 | new IntValue(config.EvalPushLimit)));
|
---|
114 |
|
---|
115 | Parameters.Add(new FixedValueParameter<IntValue>(
|
---|
116 | MaxProgramLengthParameterName,
|
---|
117 | MaxProgramLengthParameterDescription,
|
---|
118 | new IntValue(config.MaxPointsInProgram)));
|
---|
119 |
|
---|
120 | Parameters.Add(new FixedValueParameter<BoolValue>(
|
---|
121 | TopLevelPushCodeParameterName,
|
---|
122 | TopLevelPushCodeParameterDescription,
|
---|
123 | new BoolValue(config.TopLevelPushCode)) { Hidden = true });
|
---|
124 |
|
---|
125 | Parameters.Add(new FixedValueParameter<BoolValue>(
|
---|
126 | TopLevelPopCodeParameterName,
|
---|
127 | TopLevelPopCodeParameterDescription,
|
---|
128 | new BoolValue(config.TopLevelPopCode)) { Hidden = true });
|
---|
129 |
|
---|
130 | Parameters.Add(new ValueParameter<IEnabledExpressionsConfiguration>(
|
---|
131 | InstructionsParameterName,
|
---|
132 | InstructionsParameterDescription));
|
---|
133 |
|
---|
134 | Parameters.Add(new FixedValueParameter<IntValue>(
|
---|
135 | MinRandomIntegerParameterName,
|
---|
136 | MinRandomIntegerParameterDescription,
|
---|
137 | new IntValue(config.MinRandomInteger)) { Hidden = true });
|
---|
138 |
|
---|
139 | Parameters.Add(new FixedValueParameter<IntValue>(
|
---|
140 | MaxRandomIntegerParameterName,
|
---|
141 | MaxRandomIntegerParameterDescription,
|
---|
142 | new IntValue(config.MaxRandomInteger)) { Hidden = true });
|
---|
143 |
|
---|
144 | Parameters.Add(new FixedValueParameter<DoubleValue>(
|
---|
145 | MinRandomFloatParameterName,
|
---|
146 | MinRandomFloatParameterDescription,
|
---|
147 | new DoubleValue(config.MinRandomFloat)) { Hidden = true });
|
---|
148 |
|
---|
149 | Parameters.Add(new FixedValueParameter<DoubleValue>(
|
---|
150 | MaxRandomFloatParameterName,
|
---|
151 | MaxRandomFloatParameterDescription,
|
---|
152 | new DoubleValue(config.MaxRandomFloat)) { Hidden = true });
|
---|
153 |
|
---|
154 | Parameters.Add(new FixedValueParameter<PercentValue>(
|
---|
155 | NewErcNameProbabilityParameterName,
|
---|
156 | NewErcNameProbabilityParameterDescription,
|
---|
157 | new PercentValue(config.NewErcNameProbability)) { Hidden = true });
|
---|
158 |
|
---|
159 | Parameters.Add(new FixedValueParameter<PercentValue>(
|
---|
160 | ErcProbabilityParameterName,
|
---|
161 | ErcProbabilityParameterDescription,
|
---|
162 | new PercentValue(config.ErcProbability)) { Hidden = true });
|
---|
163 |
|
---|
164 | Parameters.Add(new FixedValueParameter<IntValue>(
|
---|
165 | MaxPointsInRandomInstructionParameterName,
|
---|
166 | MaxPointsInRandomInstructionParameterDescription,
|
---|
167 | new IntValue(config.MaxPointsInRandomExpression)) { Hidden = true });
|
---|
168 |
|
---|
169 | Parameters.Add(
|
---|
170 | new FixedValueParameter<IntValue>(
|
---|
171 | MaxProgramDepthParameterName,
|
---|
172 | MaxProgramDepthParameterDescription,
|
---|
173 | new IntValue(config.MaxDepth)));
|
---|
174 |
|
---|
175 | Parameters.Add(new ValueParameter<Data>(
|
---|
176 | DataParameterName,
|
---|
177 | DataParameterDescription));
|
---|
178 |
|
---|
179 | Encoding.Bounds[0, 0] = 0;
|
---|
180 | Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;
|
---|
181 | Encoding.Length = config.MaxPointsInProgram;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public IValueParameter<DataBounds> DataBoundsParameter
|
---|
185 | {
|
---|
186 | get { return (IValueParameter<DataBounds>)Parameters[DataBoundsParameterName]; }
|
---|
187 | }
|
---|
188 |
|
---|
189 | public DataBounds DataBounds
|
---|
190 | {
|
---|
191 | get { return DataBoundsParameter.Value; }
|
---|
192 | set { DataBoundsParameter.Value = value; }
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | public IValueParameter<IEnabledExpressionsConfiguration> InstructionsParameter
|
---|
197 | {
|
---|
198 | get { return (IValueParameter<IEnabledExpressionsConfiguration>)Parameters[InstructionsParameterName]; }
|
---|
199 | }
|
---|
200 |
|
---|
201 | public IEnabledExpressionsConfiguration Instructions
|
---|
202 | {
|
---|
203 | get { return InstructionsParameter.Value; }
|
---|
204 | set { InstructionsParameter.Value = value; }
|
---|
205 | }
|
---|
206 |
|
---|
207 | public IValueParameter<Data> DataParameter
|
---|
208 | {
|
---|
209 | get { return (IValueParameter<Data>)Parameters[DataParameterName]; }
|
---|
210 | }
|
---|
211 |
|
---|
212 | public Data Data
|
---|
213 | {
|
---|
214 | get { return DataParameter.Value; }
|
---|
215 | set { DataParameter.Value = value; }
|
---|
216 | }
|
---|
217 |
|
---|
218 | public IValueParameter<IntValue> EvalPushLimitParameter
|
---|
219 | {
|
---|
220 | get { return (IValueParameter<IntValue>)Parameters[EvalPushLimitParameterName]; }
|
---|
221 | }
|
---|
222 |
|
---|
223 | public int EvalPushLimit
|
---|
224 | {
|
---|
225 | get { return config.EvalPushLimit; }
|
---|
226 | set
|
---|
227 | {
|
---|
228 | this.EvalPushLimitParameter.Value.Value = value;
|
---|
229 | config.EvalPushLimit = value;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | public IValueParameter<IntValue> MaxDepthParameter
|
---|
234 | {
|
---|
235 | get { return (IValueParameter<IntValue>)this.Parameters[MaxProgramDepthParameterName]; }
|
---|
236 | }
|
---|
237 |
|
---|
238 | public int MaxDepth
|
---|
239 | {
|
---|
240 | get { return config.MaxDepth; }
|
---|
241 | set
|
---|
242 | {
|
---|
243 | this.MaxDepthParameter.Value.Value = value;
|
---|
244 | config.MaxDepth = value;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | public IValueParameter<IntValue> MaxProgramLengthParameter
|
---|
249 | {
|
---|
250 | get { return (IValueParameter<IntValue>)this.Parameters[MaxProgramLengthParameterName]; }
|
---|
251 | }
|
---|
252 |
|
---|
253 | public int MaxProgramLength
|
---|
254 | {
|
---|
255 | get { return config.MaxPointsInProgram; }
|
---|
256 | set
|
---|
257 | {
|
---|
258 | this.MaxProgramLengthParameter.Value.Value = value;
|
---|
259 | this.Encoding.LengthParameter.Value.Value = value;
|
---|
260 | config.MaxPointsInProgram = value;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | public IValueParameter<BoolValue> TopLevelPushParameter
|
---|
265 | {
|
---|
266 | get { return (IValueParameter<BoolValue>)this.Parameters[TopLevelPushCodeParameterName]; }
|
---|
267 | }
|
---|
268 |
|
---|
269 | public bool TopLevelPushCode
|
---|
270 | {
|
---|
271 | get { return config.TopLevelPushCode; }
|
---|
272 | set
|
---|
273 | {
|
---|
274 | this.TopLevelPushParameter.Value.Value = value;
|
---|
275 | config.TopLevelPushCode = value;
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | public IValueParameter<BoolValue> TopLevelPopParameter
|
---|
280 | {
|
---|
281 | get { return (IValueParameter<BoolValue>)this.Parameters[TopLevelPopCodeParameterName]; }
|
---|
282 | }
|
---|
283 |
|
---|
284 | public bool TopLevelPopCode
|
---|
285 | {
|
---|
286 | get { return config.TopLevelPopCode; }
|
---|
287 | set
|
---|
288 | {
|
---|
289 | this.TopLevelPushParameter.Value.Value = value;
|
---|
290 | config.TopLevelPopCode = value;
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | public IValueParameter<IntValue> MinRandomIntegerParameter
|
---|
295 | {
|
---|
296 | get { return (IValueParameter<IntValue>)this.Parameters[MinRandomIntegerParameterName]; }
|
---|
297 | }
|
---|
298 |
|
---|
299 | public int MinRandomInteger
|
---|
300 | {
|
---|
301 | get { return config.MinRandomInteger; }
|
---|
302 | set
|
---|
303 | {
|
---|
304 | this.MinRandomIntegerParameter.Value.Value = value;
|
---|
305 | config.MinRandomInteger = value;
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | public IValueParameter<IntValue> MaxRandomIntegerParameter
|
---|
310 | {
|
---|
311 | get { return (IValueParameter<IntValue>)this.Parameters[MaxRandomIntegerParameterName]; }
|
---|
312 | }
|
---|
313 |
|
---|
314 | public int MaxRandomInteger
|
---|
315 | {
|
---|
316 | get { return config.MaxRandomInteger; }
|
---|
317 | set
|
---|
318 | {
|
---|
319 | this.MaxRandomIntegerParameter.Value.Value = value;
|
---|
320 | config.MaxRandomInteger = value;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | public IValueParameter<DoubleValue> MinRandomFloatParameter
|
---|
325 | {
|
---|
326 | get { return (IValueParameter<DoubleValue>)this.Parameters[MinRandomFloatParameterName]; }
|
---|
327 | }
|
---|
328 |
|
---|
329 | public double MinRandomFloat
|
---|
330 | {
|
---|
331 | get { return config.MinRandomFloat; }
|
---|
332 | set
|
---|
333 | {
|
---|
334 | this.MinRandomFloatParameter.Value.Value = value;
|
---|
335 | config.MinRandomFloat = value;
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | public IValueParameter<DoubleValue> MaxRandomFloatParameter
|
---|
340 | {
|
---|
341 | get { return (IValueParameter<DoubleValue>)this.Parameters[MaxRandomFloatParameterName]; }
|
---|
342 | }
|
---|
343 |
|
---|
344 | public double MaxRandomFloat
|
---|
345 | {
|
---|
346 | get { return config.MaxRandomFloat; }
|
---|
347 | set
|
---|
348 | {
|
---|
349 | this.MaxRandomFloatParameter.Value.Value = value;
|
---|
350 | config.MaxRandomFloat = value;
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | public IValueParameter<PercentValue> NewErcNameProbabilityParameter
|
---|
355 | {
|
---|
356 | get { return (IValueParameter<PercentValue>)this.Parameters[NewErcNameProbabilityParameterName]; }
|
---|
357 | }
|
---|
358 |
|
---|
359 | public double NewErcNameProbability
|
---|
360 | {
|
---|
361 | get { return config.NewErcNameProbability; }
|
---|
362 | set
|
---|
363 | {
|
---|
364 | this.NewErcNameProbabilityParameter.Value.Value = value;
|
---|
365 | config.NewErcNameProbability = value;
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | public IValueParameter<PercentValue> ErcProbabilityParameter
|
---|
370 | {
|
---|
371 | get { return (IValueParameter<PercentValue>)this.Parameters[ErcProbabilityParameterName]; }
|
---|
372 | }
|
---|
373 |
|
---|
374 | public double ErcProbability
|
---|
375 | {
|
---|
376 | get { return config.ErcProbability; }
|
---|
377 | set
|
---|
378 | {
|
---|
379 | this.ErcProbabilityParameter.Value.Value = value;
|
---|
380 | config.ErcProbability = value;
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | public IValueParameter<IntValue> MaxPointsInRandomInstructionParameter
|
---|
385 | {
|
---|
386 | get { return (IValueParameter<IntValue>)Parameters[MaxPointsInRandomInstructionParameterName]; }
|
---|
387 | }
|
---|
388 |
|
---|
389 | public int MaxPointsInRandomInstruction
|
---|
390 | {
|
---|
391 | get { return config.MaxPointsInRandomExpression; }
|
---|
392 | set
|
---|
393 | {
|
---|
394 | this.MaxPointsInRandomInstructionParameter.Value.Value = value;
|
---|
395 | config.MaxPointsInRandomExpression = value;
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | #endregion
|
---|
400 |
|
---|
401 | public override bool Maximization { get { return false; } }
|
---|
402 |
|
---|
403 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
404 | return new PushProblem(this, cloner);
|
---|
405 | }
|
---|
406 |
|
---|
407 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
408 | const string bestSolutionResultName = "Best Solution";
|
---|
409 | var bestQuality = Maximization ? qualities.Max() : qualities.Min();
|
---|
410 | var bestIdx = Array.IndexOf(qualities, bestQuality);
|
---|
411 | var bestIndividual = individuals[bestIdx];
|
---|
412 | var solution = new PushSolution(bestIndividual.IntegerVector(), bestQuality, Data, random, config, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End);
|
---|
413 |
|
---|
414 | if (!results.ContainsKey(bestSolutionResultName)) {
|
---|
415 | results.Add(new Result(bestSolutionResultName, solution));
|
---|
416 | } else if (((PushSolution)results[bestSolutionResultName].Value).Quality < qualities[bestIdx]) {
|
---|
417 | results[bestSolutionResultName].Value = solution;
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
422 | return PushEvaluator.Evaluate(individual, pool, random, Data, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End);
|
---|
423 | }
|
---|
424 |
|
---|
425 | public void Load(Data data) {
|
---|
426 | Data = data;
|
---|
427 | BestKnownQuality = data.BestResult;
|
---|
428 | MaxProgramLength = data.MaxSize;
|
---|
429 | EvalPushLimit = data.EvalLimit;
|
---|
430 |
|
---|
431 | config.EnabledExpressions = (IList<string>)ExpressionTable.GetEnabledExpressionsByStackTypes((StackTypes)data.EnabledDataTypes);
|
---|
432 |
|
---|
433 | // update enabled stack types
|
---|
434 | foreach (var stackType in ExpressionTable.StackTypeToNamesTable.Keys) {
|
---|
435 | var enabledStackExpressions = config.EnabledExpressions.Intersect(ExpressionTable.StackTypeToNamesTable[stackType]);
|
---|
436 | config.SetStack(stackType, enabledStackExpressions.Any());
|
---|
437 | }
|
---|
438 |
|
---|
439 | Encoding.Bounds[0, 0] = 0;
|
---|
440 | Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;
|
---|
441 | Encoding.Length = config.MaxPointsInProgram;
|
---|
442 |
|
---|
443 | // TODO
|
---|
444 | // data.MaxGenerations
|
---|
445 | // data.ProgEvalBudget
|
---|
446 |
|
---|
447 | DataBounds.TrainingRange.Start = 0;
|
---|
448 | DataBounds.TrainingRange.End = Data.OriginalTrainingCount - 1;
|
---|
449 | DataBounds.TestRange.Start = Data.OriginalTrainingCount;
|
---|
450 | DataBounds.TestRange.End = Data.OriginalTrainingCount + Data.OriginalTestCount;
|
---|
451 | }
|
---|
452 | }
|
---|
453 | } |
---|