[10071] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10071] | 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/>.
|
---|
[10968] | 19 | *
|
---|
| 20 | * Author: Sabine Winkler
|
---|
[10071] | 21 | */
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
[16565] | 31 | using HEAL.Attic;
|
---|
[12915] | 32 | using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt;
|
---|
[10071] | 33 | using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
|
---|
[13233] | 34 | using HeuristicLab.Random;
|
---|
[10071] | 35 |
|
---|
| 36 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
[13238] | 37 | [Item("Grammatical Evolution Artificial Ant Problem (GE)", "Represents the Artificial Ant problem, implemented in Grammatical Evolution.")]
|
---|
[12504] | 38 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)]
|
---|
[16565] | 39 | [StorableType("B6F0EBC4-FA3B-42E6-958F-404FA89C81FA")]
|
---|
[12915] | 40 | public sealed class GEArtificialAntProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IStorableContent {
|
---|
[10071] | 41 |
|
---|
| 42 | #region Parameter Properties
|
---|
| 43 | public IValueParameter<BoolMatrix> WorldParameter {
|
---|
| 44 | get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
|
---|
| 45 | }
|
---|
[12915] | 46 | public IFixedValueParameter<IntValue> MaxTimeStepsParameter {
|
---|
| 47 | get { return (IFixedValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
|
---|
[10071] | 48 | }
|
---|
| 49 | public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
|
---|
| 50 | get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
|
---|
| 51 | }
|
---|
| 52 | #endregion
|
---|
| 53 |
|
---|
| 54 | #region Properties
|
---|
| 55 | public BoolMatrix World {
|
---|
| 56 | get { return WorldParameter.Value; }
|
---|
| 57 | set { WorldParameter.Value = value; }
|
---|
| 58 | }
|
---|
[12915] | 59 | public int MaxTimeSteps {
|
---|
| 60 | get { return MaxTimeStepsParameter.Value.Value; }
|
---|
| 61 | set { MaxTimeStepsParameter.Value.Value = value; }
|
---|
[10071] | 62 | }
|
---|
| 63 | #endregion
|
---|
| 64 |
|
---|
| 65 | [StorableConstructor]
|
---|
[16565] | 66 | private GEArtificialAntProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[10071] | 67 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[12915] | 68 | private void AfterDeserialization() { }
|
---|
| 69 |
|
---|
| 70 | public override bool Maximization {
|
---|
| 71 | get { return true; }
|
---|
[10071] | 72 | }
|
---|
| 73 |
|
---|
[12915] | 74 | [Storable]
|
---|
| 75 | // parameters of the wrapped problem cannot be changed therefore it is not strictly necessary to clone and store it
|
---|
| 76 | private readonly HeuristicLab.Problems.GeneticProgramming.ArtificialAnt.Problem wrappedAntProblem;
|
---|
| 77 |
|
---|
[10071] | 78 | private GEArtificialAntProblem(GEArtificialAntProblem original, Cloner cloner)
|
---|
| 79 | : base(original, cloner) {
|
---|
[12915] | 80 | this.wrappedAntProblem = cloner.Clone(original.wrappedAntProblem);
|
---|
[10071] | 81 | }
|
---|
| 82 |
|
---|
| 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new GEArtificialAntProblem(this, cloner);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public GEArtificialAntProblem()
|
---|
[12915] | 88 | : base() {
|
---|
| 89 | wrappedAntProblem = new HeuristicLab.Problems.GeneticProgramming.ArtificialAnt.Problem();
|
---|
| 90 | Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", wrappedAntProblem.World));
|
---|
| 91 | Parameters.Add(new FixedValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
|
---|
[10071] | 92 | Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper()));
|
---|
| 93 |
|
---|
[12915] | 94 | Encoding = new IntegerVectorEncoding(30) { Bounds = new IntMatrix(new int[,] { { 0, 100 } }) };
|
---|
[10071] | 95 |
|
---|
[12915] | 96 | BestKnownQuality = wrappedAntProblem.BestKnownQuality;
|
---|
[10071] | 97 | }
|
---|
| 98 |
|
---|
[13243] | 99 | private readonly object syncRoot = new object();
|
---|
[12915] | 100 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
| 101 | var vector = individual.IntegerVector();
|
---|
[10071] | 102 |
|
---|
[12915] | 103 | var bounds = Encoding.Bounds;
|
---|
| 104 | var len = Encoding.Length;
|
---|
| 105 | var grammar = wrappedAntProblem.Encoding.Grammar;
|
---|
| 106 | var mapper = GenotypeToPhenotypeMapperParameter.Value;
|
---|
[10071] | 107 |
|
---|
[13233] | 108 | // Evaluate might be called concurrently therefore access to random has to be synchronized.
|
---|
| 109 | // However, results depend on the order of execution. Therefore, results might be different for the same seed when using the parallel engine.
|
---|
| 110 | IRandom fastRand;
|
---|
[13243] | 111 | lock (syncRoot) {
|
---|
[13233] | 112 | fastRand = new FastRandom(random.Next());
|
---|
| 113 | }
|
---|
| 114 | var tree = mapper.Map(fastRand, bounds, len, grammar, vector);
|
---|
[10071] | 115 |
|
---|
[12915] | 116 | Interpreter interpreter = new Interpreter(tree, World, MaxTimeSteps);
|
---|
| 117 | interpreter.Run();
|
---|
[10071] | 118 |
|
---|
[12915] | 119 | return interpreter.FoodEaten;
|
---|
[10071] | 120 | }
|
---|
| 121 |
|
---|
[12915] | 122 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 123 | var bounds = Encoding.Bounds;
|
---|
| 124 | var len = Encoding.Length;
|
---|
| 125 | var grammar = wrappedAntProblem.Encoding.Grammar;
|
---|
| 126 | var mapper = GenotypeToPhenotypeMapperParameter.Value;
|
---|
[10071] | 127 |
|
---|
[12915] | 128 | var trees = individuals
|
---|
| 129 | .Select(ind => mapper.Map(random, bounds, len, grammar, ind.IntegerVector()))
|
---|
| 130 | .ToArray();
|
---|
[10071] | 131 |
|
---|
[12915] | 132 | wrappedAntProblem.Analyze(trees, qualities, results, random);
|
---|
[10071] | 133 | }
|
---|
| 134 | }
|
---|
| 135 | } |
---|