[12911] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Random;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.GeneticProgramming.LawnMower {
|
---|
| 34 | [StorableClass]
|
---|
| 35 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 160)]
|
---|
| 36 | [Item("Lawn Mower Problem", "The lawn mower demo problem for genetic programming.")]
|
---|
| 37 | public class Problem : SymbolicExpressionTreeProblem {
|
---|
| 38 | private const string LawnWidthParameterName = "LawnWidth";
|
---|
| 39 | private const string LawnLengthParameterName = "LawnLength";
|
---|
| 40 |
|
---|
| 41 | public IFixedValueParameter<IntValue> LawnWidthParameter {
|
---|
| 42 | get { return (IFixedValueParameter<IntValue>)Parameters[LawnWidthParameterName]; }
|
---|
| 43 | }
|
---|
| 44 | public IFixedValueParameter<IntValue> LawnLengthParameter {
|
---|
| 45 | get { return (IFixedValueParameter<IntValue>)Parameters[LawnLengthParameterName]; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public override bool Maximization {
|
---|
| 49 | get { return true; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[13280] | 52 | #region item cloning and persistence
|
---|
[12911] | 53 | [StorableConstructor]
|
---|
[13280] | 54 | protected Problem(bool deserializing) : base(deserializing) { }
|
---|
| 55 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 56 | private void AfterDeserialization() { }
|
---|
| 57 |
|
---|
| 58 | protected Problem(Problem original, Cloner cloner) : base(original, cloner) { }
|
---|
| 59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 60 | return new Problem(this, cloner);
|
---|
[12911] | 61 | }
|
---|
[13280] | 62 | #endregion
|
---|
| 63 |
|
---|
[12911] | 64 | public Problem()
|
---|
| 65 | : base() {
|
---|
| 66 | Parameters.Add(new FixedValueParameter<IntValue>(LawnWidthParameterName, "Width of the lawn.", new IntValue(8)));
|
---|
| 67 | Parameters.Add(new FixedValueParameter<IntValue>(LawnLengthParameterName, "Length of the lawn.", new IntValue(8)));
|
---|
| 68 |
|
---|
| 69 | var g = new SimpleSymbolicExpressionGrammar();
|
---|
| 70 | g.AddSymbols(new string[] { "Sum", "Prog" }, 2, 2);
|
---|
| 71 | g.AddSymbols(new string[] { "Frog" }, 1, 1);
|
---|
| 72 | g.AddTerminalSymbols(new string[] { "Left", "Forward" });
|
---|
| 73 | // initialize 20 ephemeral random constants in [0..32[
|
---|
| 74 | var fastRand = new FastRandom(314159);
|
---|
| 75 | for (int i = 0; i < 20; i++) {
|
---|
| 76 | g.AddTerminalSymbol(string.Format("{0},{1}", fastRand.Next(0, 32), fastRand.Next(0, 32)));
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | Encoding = new SymbolicExpressionTreeEncoding(g, 1000, 17);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 83 | const string bestSolutionResultName = "Best Solution";
|
---|
| 84 | var bestQuality = Maximization ? qualities.Max() : qualities.Min();
|
---|
| 85 | var bestIdx = Array.IndexOf(qualities, bestQuality);
|
---|
| 86 |
|
---|
| 87 | if (!results.ContainsKey(bestSolutionResultName)) {
|
---|
| 88 | results.Add(new Result(bestSolutionResultName, new Solution(trees[bestIdx], LawnLengthParameter.Value.Value, LawnWidthParameter.Value.Value, bestQuality)));
|
---|
| 89 | } else if (((Solution)(results[bestSolutionResultName].Value)).Quality < qualities[bestIdx]) {
|
---|
| 90 | results[bestSolutionResultName].Value = new Solution(trees[bestIdx], LawnLengthParameter.Value.Value, LawnWidthParameter.Value.Value, bestQuality);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
|
---|
| 95 | var length = LawnLengthParameter.Value.Value;
|
---|
| 96 | var width = LawnWidthParameter.Value.Value;
|
---|
| 97 |
|
---|
| 98 | var lawn = Interpreter.EvaluateLawnMowerProgram(length, width, tree);
|
---|
| 99 | // count number of squares that have been mowed
|
---|
| 100 | int numberOfMowedCells = 0;
|
---|
| 101 | for (int i = 0; i < length; i++)
|
---|
| 102 | for (int j = 0; j < width; j++)
|
---|
| 103 | if (lawn[i, j]) {
|
---|
| 104 | numberOfMowedCells++;
|
---|
| 105 | }
|
---|
| 106 | return numberOfMowedCells;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|