Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/25/15 10:38:20 (9 years ago)
Author:
gkronber
Message:

#2421 new version of artificial ant problem that uses SymbolicExpressionTreeEncoding

Location:
trunk/sources/HeuristicLab.Problems.ArtificialAnt
Files:
1 added
7 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.4/ArtificialAntProblem.cs

    r12504 r12895  
    3535namespace HeuristicLab.Problems.ArtificialAnt {
    3636  [Item("Artificial Ant Problem", "Represents the Artificial Ant problem.")]
    37   [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)]
    3837  [StorableClass]
    3938  public sealed class ArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<Evaluator, ISymbolicExpressionTreeCreator>, IStorableContent {
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/Analyzers/BestAntTrailAnalyzer.cs

    r12012 r12895  
    3636  [Item("BestAntTrailAnalyzer", "An operator for analyzing the best ant trail of an artificial ant problem.")]
    3737  [StorableClass]
    38   public sealed class BestAntTrailAnalyzer : SingleSuccessorOperator, IAntTrailAnalyzer, ISingleObjectiveOperator {
     38  public sealed class BestAntTrailAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
    3939    public bool EnabledByDefault {
    4040      get { return true; }
    4141    }
    4242
     43    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     44      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
     45    }
    4346    public ILookupParameter<BoolMatrix> WorldParameter {
    4447      get { return (ILookupParameter<BoolMatrix>)Parameters["World"]; }
    4548    }
    46     public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    47       get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
    48     }
    49     public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
    50       get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
     49    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
     50      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
    5151    }
    5252    public ILookupParameter<IntValue> MaxTimeStepsParameter {
     
    5656      get { return (ILookupParameter<AntTrail>)Parameters["BestSolution"]; }
    5757    }
    58     public ValueLookupParameter<ResultCollection> ResultsParameter {
    59       get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
     58    public ILookupParameter<ResultCollection> ResultsParameter {
     59      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
    6060    }
    6161
     
    6363      : base() {
    6464      Parameters.Add(new LookupParameter<BoolMatrix>("World", "The world with food items for the artificial ant."));
    65       Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solutions from which the best solution should be visualized."));
     65      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solutions from which the best solution should be visualized."));
    6666      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the artificial ant solutions which should be visualized."));
    6767      Parameters.Add(new LookupParameter<AntTrail>("BestSolution", "The visual representation of the best ant trail."));
    6868      Parameters.Add(new LookupParameter<IntValue>("MaxTimeSteps", "The maximal time steps that the artificial ant has available to collect all food items."));
    69       Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best artificial ant solution should be stored."));
     69      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection where the best artificial ant solution should be stored."));
    7070    }
    7171
     
    8080
    8181    public override IOperation Apply() {
    82       ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
     82      var expressions = SymbolicExpressionTreeParameter.ActualValue;
    8383      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
    8484      BoolMatrix world = WorldParameter.ActualValue;
    85       IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
     85      int maxTimeSteps = MaxTimeStepsParameter.ActualValue.Value;
    8686      ResultCollection results = ResultsParameter.ActualValue;
    8787
    88       int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => -x.Value).First().index;
     88      int bestIdx = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index; // find index of maximum element
    8989
    9090      AntTrail antTrail = BestSolutionParameter.ActualValue;
    9191      if (antTrail == null) {
    92         var bestAntTrail = new AntTrail(world, expressions[i], maxTimeSteps);
     92        var bestAntTrail = new AntTrail(world, expressions[bestIdx], maxTimeSteps, qualities[bestIdx].Value);
    9393        BestSolutionParameter.ActualValue = bestAntTrail;
    9494        results.Add(new Result("Best Artificial Ant Solution", bestAntTrail));
    95       } else {
    96         antTrail.World = world;
    97         antTrail.SymbolicExpressionTree = expressions[i];
    98         antTrail.MaxTimeSteps = maxTimeSteps;
    99         results["Best Artificial Ant Solution"].Value = antTrail;
     95      } else if (antTrail.Quality < qualities[bestIdx].Value) {
     96        results["Best Artificial Ant Solution"].Value = new AntTrail(world, expressions[bestIdx], maxTimeSteps, qualities[bestIdx].Value);
    10097      }
    10198      return base.Apply();
    10299    }
     100
    103101  }
    104102}
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/AntInterpreter.cs

    r12012 r12895  
    2525using HeuristicLab.Data;
    2626using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    27 using HeuristicLab.Problems.ArtificialAnt.Symbols;
    2827
    2928namespace HeuristicLab.Problems.ArtificialAnt {
    3029  public class AntInterpreter {
    3130
    32     public int MaxTimeSteps { get; set; }
    33     public int FoodEaten { get; set; }
    34     private BoolMatrix world;
    35     public BoolMatrix World {
    36       get { return world; }
    37       set {
    38         // create a clone of the world because the ant will remove the food items it can find.
    39         world = (BoolMatrix)value.Clone();
    40         CountFoodItems();
    41       }
    42     }
     31    public int MaxTimeSteps { get; private set; }
     32    public int FoodEaten { get; private set; }
     33    public BoolMatrix World { get; private set; }
    4334
    44     private SymbolicExpressionTree expression;
    45     public SymbolicExpressionTree Expression {
    46       get { return expression; }
    47       set {
    48         expression = value;
    49       }
    50     }
    51 
    52     private SymbolicExpressionTreeNode FindMatchingFunction(string name) {
    53       foreach (var defunBranch in expression.Root.Subtrees.OfType<DefunTreeNode>()) {
    54         if (defunBranch.FunctionName == name) return defunBranch;
    55       }
    56       throw new ArgumentException("Function definition for " + name + " not found.");
    57     }
    58 
     35    public ISymbolicExpressionTree Expression { get; private set; }
    5936
    6037
     
    6542    private int nFoodItems;
    6643    private Stack<ISymbolicExpressionTreeNode> nodeStack = new Stack<ISymbolicExpressionTreeNode>();
     44
     45    public AntInterpreter(ISymbolicExpressionTree tree, BoolMatrix world, int maxTimeSteps) {
     46      this.Expression = tree;
     47      this.MaxTimeSteps = maxTimeSteps;
     48      // create a clone of the world because the ant will remove the food items it can find.
     49      World = (BoolMatrix)world.Clone();
     50      CountFoodItems();
     51    }
    6752
    6853    private void CountFoodItems() {
     
    9782
    9883      var currentNode = nodeStack.Pop();
    99       if (currentNode.Symbol is Left) {
     84      if (currentNode.Symbol.Name == "Left") {
    10085        currentDirection = (currentDirection + 3) % 4;
    10186        ElapsedTime++;
    102       } else if (currentNode.Symbol is Right) {
     87      } else if (currentNode.Symbol.Name == "Right") {
    10388        currentDirection = (currentDirection + 1) % 4;
    10489        ElapsedTime++;
    105       } else if (currentNode.Symbol is Move) {
     90      } else if (currentNode.Symbol.Name == "Move") {
    10691        MoveAntForward();
    107         if (World[currentAntLocationRow, currentAntLocationColumn])
     92        if (World[currentAntLocationRow, currentAntLocationColumn]) {
    10893          FoodEaten++;
    109         World[currentAntLocationRow, currentAntLocationColumn] = false;
     94          World[currentAntLocationRow, currentAntLocationColumn] = false;
     95        }
    11096        ElapsedTime++;
    111       } else if (currentNode.Symbol is IfFoodAhead) {
     97      } else if (currentNode.Symbol.Name == "IfFoodAhead") {
    11298        int nextAntLocationRow;
    11399        int nextAntLocationColumn;
     
    118104          nodeStack.Push(currentNode.GetSubtree(1));
    119105        }
    120       } else if (currentNode.Symbol is Prog2) {
     106      } else if (currentNode.Symbol.Name == "Prog2") {
    121107        nodeStack.Push(currentNode.GetSubtree(1));
    122108        nodeStack.Push(currentNode.GetSubtree(0));
    123         return;
    124       } else if (currentNode.Symbol is Prog3) {
     109      } else if (currentNode.Symbol.Name == "Prog3") {
    125110        nodeStack.Push(currentNode.GetSubtree(2));
    126111        nodeStack.Push(currentNode.GetSubtree(1));
    127112        nodeStack.Push(currentNode.GetSubtree(0));
    128         return;
    129113      } else if (currentNode.Symbol is InvokeFunction) {
    130114        var invokeNode = currentNode as InvokeFunctionTreeNode;
     
    171155      }
    172156    }
     157
     158
     159    private SymbolicExpressionTreeNode FindMatchingFunction(string name) {
     160      foreach (var defunBranch in Expression.Root.Subtrees.OfType<DefunTreeNode>()) {
     161        if (defunBranch.FunctionName == name) return defunBranch;
     162      }
     163      throw new ArgumentException("Function definition for " + name + " not found.");
     164    }
    173165  }
    174166}
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/AntTrail.cs

    r12012 r12895  
    3939    }
    4040    [Storable]
    41     private SymbolicExpressionTree expression;
    42     public SymbolicExpressionTree SymbolicExpressionTree {
    43       get { return expression; }
    44       set {
    45         if (expression != value) {
    46           //if (expression != null) DeregisterSymbolicExpressionTreeEvents();
    47           expression = value;
    48           //if (expression != null) RegisterSymbolicExpressionTreeEvents();
    49           OnSymbolicExpressionTreeChanged();
    50         }
    51       }
    52     }
     41    public ISymbolicExpressionTree SymbolicExpressionTree { get; private set; }
     42    [Storable]
     43    public BoolMatrix World { get; private set; }
     44    [Storable]
     45    public int MaxTimeSteps { get; private set; }
     46    [Storable]
     47    public double Quality { get; private set; }
    5348
    54     [Storable]
    55     private BoolMatrix world;
    56     public BoolMatrix World {
    57       get { return world; }
    58       set {
    59         if (world != value) {
    60           if (world != null) DeregisterWorldEvents();
    61           world = value;
    62           if (world != null) RegisterWorldEvents();
    63           OnWorldChanged();
    64         }
    65       }
    66     }
    67     [Storable]
    68     private IntValue maxTimeSteps;
    69     public IntValue MaxTimeSteps {
    70       get { return maxTimeSteps; }
    71       set {
    72         if (maxTimeSteps != value) {
    73           if (maxTimeSteps != value) {
    74             if (maxTimeSteps != null) DeregisterMaxTimeStepsEvents();
    75             maxTimeSteps = value;
    76             if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
    77             OnWorldChanged();
    78           }
    79         }
    80       }
    81     }
    82 
    83     public AntTrail() : base() { }
    84     public AntTrail(BoolMatrix world, SymbolicExpressionTree expression, IntValue maxTimeSteps)
    85       : this() {
    86       this.world = world;
    87       this.expression = expression;
    88       this.maxTimeSteps = maxTimeSteps;
    89       Initialize();
     49    public AntTrail(BoolMatrix world, ISymbolicExpressionTree expression, int maxTimeSteps, double quality) {
     50      this.World = world;
     51      this.SymbolicExpressionTree = expression;
     52      this.MaxTimeSteps = maxTimeSteps;
     53      this.Quality = quality;
    9054    }
    9155
     
    9357    private AntTrail(bool deserializing) : base(deserializing) { }
    9458    [StorableHook(HookType.AfterDeserialization)]
    95     private void AfterDeserialization() {
    96       Initialize();
    97     }
     59    private void AfterDeserialization() { }
     60
    9861    private AntTrail(AntTrail original, Cloner cloner)
    9962      : base(original, cloner) {
    100       expression = cloner.Clone(original.expression);
    101       world = cloner.Clone(original.world);
    102       maxTimeSteps = cloner.Clone(original.maxTimeSteps);
    103       Initialize();
     63      SymbolicExpressionTree = cloner.Clone(original.SymbolicExpressionTree);
     64      World = cloner.Clone(original.World);
     65      MaxTimeSteps = original.MaxTimeSteps;
     66      Quality = original.Quality;
    10467    }
     68
    10569    public override IDeepCloneable Clone(Cloner cloner) {
    10670      return new AntTrail(this, cloner);
    10771    }
    108 
    109     private void Initialize() {
    110       //if (expression != null) RegisterSymbolicExpressionTreeEvents();
    111       if (world != null) RegisterWorldEvents();
    112       if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
    113     }
    114 
    115     #region Events
    116     public event EventHandler SymbolicExpressionTreeChanged;
    117     private void OnSymbolicExpressionTreeChanged() {
    118       var changed = SymbolicExpressionTreeChanged;
    119       if (changed != null) changed(this, EventArgs.Empty);
    120     }
    121     public event EventHandler WorldChanged;
    122     private void OnWorldChanged() {
    123       var changed = WorldChanged;
    124       if (changed != null) changed(this, EventArgs.Empty);
    125     }
    126     public event EventHandler MaxTimeStepsChanged;
    127     private void OnMaxTimeStepsChanged() {
    128       var changed = MaxTimeStepsChanged;
    129       if (changed != null) changed(this, EventArgs.Empty);
    130     }
    131 
    132     //private void RegisterSymbolicExpressionTreeEvents() {
    133     //  SymbolicExpressionTree.ItemChanged += new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
    134     //  SymbolicExpressionTree.Reset += new EventHandler(SymbolicExpressionTree_Reset);
    135     //}
    136     //private void DeregisterSymbolicExpressionTreeEvents() {
    137     //  SymbolicExpressionTree.ItemChanged -= new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
    138     //  SymbolicExpressionTree.Reset -= new EventHandler(SymbolicExpressionTree_Reset);
    139     //}
    140 
    141     private void RegisterWorldEvents() {
    142       World.ItemChanged += new EventHandler<EventArgs<int, int>>(World_ItemChanged);
    143       World.Reset += new EventHandler(World_Reset);
    144     }
    145     private void DeregisterWorldEvents() {
    146       World.ItemChanged -= new EventHandler<EventArgs<int, int>>(World_ItemChanged);
    147       World.Reset -= new EventHandler(World_Reset);
    148     }
    149     private void RegisterMaxTimeStepsEvents() {
    150       MaxTimeSteps.ValueChanged += new EventHandler(MaxTimeSteps_ValueChanged);
    151     }
    152     private void DeregisterMaxTimeStepsEvents() {
    153       MaxTimeSteps.ValueChanged -= new EventHandler(MaxTimeSteps_ValueChanged);
    154     }
    155 
    156     void MaxTimeSteps_ValueChanged(object sender, EventArgs e) {
    157       OnMaxTimeStepsChanged();
    158     }
    159     private void World_ItemChanged(object sender, EventArgs<int, int> e) {
    160       OnWorldChanged();
    161     }
    162     private void World_Reset(object sender, EventArgs e) {
    163       OnWorldChanged();
    164     }
    165     #endregion
    16672  }
    16773}
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/ArtificialAntProblem.cs

    r12504 r12895  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Diagnostics.Contracts;
    2425using System.Linq;
    2526using HeuristicLab.Common;
     
    3738  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)]
    3839  [StorableClass]
    39   public sealed class ArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<Evaluator, ISymbolicExpressionTreeCreator>, IStorableContent {
     40  public sealed class ArtificialAntProblem : SingleObjectiveBasicProblem<SymbolicExpressionTreeEncoding>, IStorableContent {
    4041    public string Filename { get; set; }
    4142
    4243    #region constant for default world (Santa Fe)
    43     private readonly bool[,] santaFeAntTrail = new bool[,] {
    44       {false, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    45       {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    46       {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, false, false, false},
    47       {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},
    48       {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},
    49       {false, false, false, true, true, true, true, false, true, true, true, true, true, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false},
    50       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
    51       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
    52       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
    53       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, true, false, false},
    54       {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
    55       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    56       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
    57       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
    58       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, true, true, true, false, false, false},
    59       {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
    60       {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    61       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    62       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},
    63       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false},
    64       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    65       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    66       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false},
    67       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
    68       {false, false, false, true, true, false, false, true, true, true, true, true, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    69       {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    70       {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    71       {false, true, false, false, false, false, false, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    72       {false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    73       {false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    74       {false, false, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false },
    75       {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false }
     44
     45    private static readonly char[][] santaFeAntTrail = new[] {
     46      " ###                            ".ToCharArray(),
     47      "   #                            ".ToCharArray(),
     48      "   #                    .###..  ".ToCharArray(),
     49      "   #                    #    #  ".ToCharArray(),
     50      "   #                    #    #  ".ToCharArray(),
     51      "   ####.#####       .##..    .  ".ToCharArray(),
     52      "            #       .        #  ".ToCharArray(),
     53      "            #       #        .  ".ToCharArray(),
     54      "            #       #        .  ".ToCharArray(),
     55      "            #       #        #  ".ToCharArray(),
     56      "            .       #        .  ".ToCharArray(),
     57      "            #       .        .  ".ToCharArray(),
     58      "            #       .        #  ".ToCharArray(),
     59      "            #       #        .  ".ToCharArray(),
     60      "            #       #  ...###.  ".ToCharArray(),
     61      "            .   .#...  #        ".ToCharArray(),
     62      "            .   .      .        ".ToCharArray(),
     63      "            #   .      .        ".ToCharArray(),
     64      "            #   #      .#...    ".ToCharArray(),
     65      "            #   #          #    ".ToCharArray(),
     66      "            #   #          .    ".ToCharArray(),
     67      "            #   #          .    ".ToCharArray(),
     68      "            #   .      ...#.    ".ToCharArray(),
     69      "            #   .      #        ".ToCharArray(),
     70      " ..##..#####.   #               ".ToCharArray(),
     71      " #              #               ".ToCharArray(),
     72      " #              #               ".ToCharArray(),
     73      " #     .#######..               ".ToCharArray(),
     74      " #     #                        ".ToCharArray(),
     75      " .     #                        ".ToCharArray(),
     76      " .####..                        ".ToCharArray(),
     77      "                                ".ToCharArray()
    7678    };
     79
     80
    7781    #endregion
    7882
    7983    #region Parameter Properties
    80     public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
    81       get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
    82     }
    83     public IValueParameter<IntValue> MaxExpressionLengthParameter {
    84       get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
    85     }
    86     public IValueParameter<IntValue> MaxExpressionDepthParameter {
    87       get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionDepth"]; }
    88     }
    89     public IValueParameter<IntValue> MaxFunctionDefinitionsParameter {
    90       get { return (IValueParameter<IntValue>)Parameters["MaximumFunctionDefinitions"]; }
    91     }
    92     public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
    93       get { return (ValueParameter<IntValue>)Parameters["MaximumFunctionArguments"]; }
    94     }
    9584    public IValueParameter<BoolMatrix> WorldParameter {
    9685      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
     
    11099      set { MaxTimeStepsParameter.Value = value; }
    111100    }
    112     public IntValue MaxExpressionLength {
    113       get { return MaxExpressionLengthParameter.Value; }
    114       set { MaxExpressionLengthParameter.Value = value; }
    115     }
    116     public IntValue MaxExpressionDepth {
    117       get { return MaxExpressionDepthParameter.Value; }
    118       set { MaxExpressionDepthParameter.Value = value; }
    119     }
    120     public IntValue MaxFunctionDefinitions {
    121       get { return MaxFunctionDefinitionsParameter.Value; }
    122       set { MaxFunctionDefinitionsParameter.Value = value; }
    123     }
    124     public IntValue MaxFunctionArguments {
    125       get { return MaxFunctionArgumentsParameter.Value; }
    126       set { MaxFunctionArgumentsParameter.Value = value; }
    127     }
    128     public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
    129       get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
    130     }
    131     public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
    132       get { return Operators.OfType<IAntTrailAnalyzer>(); }
    133     }
    134     #endregion
    135 
    136     // BackwardsCompatibility3.3
    137     #region Backwards compatible code, remove with 3.4
    138     [Obsolete]
    139     [Storable(Name = "operators")]
    140     private IEnumerable<IOperator> oldOperators {
    141       get { return null; }
    142       set {
    143         if (value != null && value.Any())
    144           Operators.AddRange(value);
    145       }
    146     }
    147     #endregion
    148 
     101    #endregion
     102
     103    public override bool Maximization {
     104      get { return true; }
     105    }
     106
     107    public ArtificialAntProblem()
     108      : base() {
     109      BoolMatrix world = new BoolMatrix(ToBoolMatrix(santaFeAntTrail));
     110      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
     111      Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
     112
     113      base.BestKnownQuality = 89;
     114      var g = new SimpleSymbolicExpressionGrammar();
     115      g.AddSymbols(new string[] { "IfFoodAhead", "Prog2" }, 2, 2);
     116      g.AddSymbols(new string[] { "Prog3" }, 3, 3);
     117      g.AddTerminalSymbols(new string[] { "Left", "Right", "Move" });
     118      base.Encoding = new SymbolicExpressionTreeEncoding(g, 20, 10);
     119
     120      InitializeOperators();
     121      RegisterEventHandlers();
     122    }
     123
     124
     125    public override double Evaluate(Individual individual, IRandom random) {
     126      var interpreter = new AntInterpreter(individual.SymbolicExpressionTree(), World, MaxTimeSteps.Value);
     127      interpreter.Run();
     128      return interpreter.FoodEaten;
     129    }
     130
     131    // persistence
    149132    [StorableConstructor]
    150133    private ArtificialAntProblem(bool deserializing) : base(deserializing) { }
     
    154137    }
    155138
     139    // cloning
    156140    private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner)
    157141      : base(original, cloner) {
     
    161145      return new ArtificialAntProblem(this, cloner);
    162146    }
    163     public ArtificialAntProblem()
    164       : base(new Evaluator(), new ProbabilisticTreeCreator()) {
    165       BoolMatrix world = new BoolMatrix(santaFeAntTrail);
    166       Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
    167       Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
    168       Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
    169       Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionArguments", "Maximal number of arguments of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
    170       Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
    171       Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
    172       Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
    173 
    174       Maximization.Value = true;
    175       MaximizationParameter.Hidden = true;
    176       BestKnownQuality = new DoubleValue(89);
    177       SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
    178       ((ProbabilisticTreeCreator)SolutionCreator).SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
    179       Evaluator.QualityParameter.ActualName = "FoodEaten";
    180       InitializeOperators();
    181       RegisterEventHandlers();
    182 
    183       ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
    184       ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
    185     }
    186147
    187148    #region Events
    188149    protected override void OnSolutionCreatorChanged() {
    189150      base.OnSolutionCreatorChanged();
    190       SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
    191       var grammarBased = SolutionCreator as ISymbolicExpressionTreeGrammarBasedOperator;
    192       if (grammarBased != null)
    193         grammarBased.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
    194 
    195       SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
    196151      ParameterizeAnalyzers();
    197152      ParameterizeOperators();
     
    199154    protected override void OnEvaluatorChanged() {
    200155      base.OnEvaluatorChanged();
    201       Evaluator.QualityParameter.ActualName = "FoodEaten";
    202       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    203156      ParameterizeAnalyzers();
    204157      ParameterizeOperators();
     
    216169    #region Helpers
    217170    private void RegisterEventHandlers() {
    218       SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
     171      Encoding.SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
    219172      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    220       MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
    221       MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
    222       MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
    223       MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
    224     }
    225 
    226     private void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
    227       ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
    228       ParameterizeOperators();
    229       ParameterizeAnalyzers();
    230     }
    231     private void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
    232       ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
    233       ParameterizeOperators();
    234       ParameterizeAnalyzers();
    235173    }
    236174
    237175    private void InitializeOperators() {
    238       Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
    239176      Operators.Add(new BestAntTrailAnalyzer());
    240       Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
    241       Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
    242177      ParameterizeAnalyzers();
    243178      ParameterizeOperators();
     
    245180
    246181    private void ParameterizeAnalyzers() {
    247       foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
     182      foreach (var analyzer in Operators.OfType<BestAntTrailAnalyzer>()) {
    248183        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    249         analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     184        analyzer.SymbolicExpressionTreeParameter.ActualName = Encoding.SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    250185        analyzer.WorldParameter.ActualName = WorldParameter.Name;
    251186        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
    252187      }
    253       foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
    254         analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     188    }
     189
     190    private void ParameterizeOperators() {
     191      // no problem-specific operators to parameterize
     192    }
     193
     194    private bool[,] ToBoolMatrix(char[][] ch) {
     195      var rows = ch.Length;
     196      var cols = ch[0].Length;
     197      var b = new bool[rows, cols];
     198      for (int r = 0; r < rows; r++) {
     199        Contract.Assert(ch[r].Length == cols); // all rows must have the same number of columns
     200        for (int c = 0; c < cols; c++) {
     201          b[r, c] = ch[r][c] == '#';
     202        }
    255203      }
    256     }
    257 
    258     private void ParameterizeOperators() {
    259       var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
    260 
    261       foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
    262         op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
    263       }
    264       foreach (ISymbolicExpressionTreeSizeConstraintOperator op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
    265         op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxExpressionDepthParameter.Name;
    266         op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxExpressionLengthParameter.Name;
    267 
    268       }
    269       foreach (Evaluator op in operators.OfType<Evaluator>()) {
    270         op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    271         op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
    272         op.WorldParameter.ActualName = WorldParameter.Name;
    273       }
    274       foreach (ISymbolicExpressionTreeCrossover op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
    275         op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    276         op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    277       }
    278       foreach (ISymbolicExpressionTreeManipulator op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
    279         op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    280       }
    281       foreach (ISymbolicExpressionTreeArchitectureAlteringOperator op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
    282         op.MaximumFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
    283         op.MaximumFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
    284       }
     204      return b;
    285205    }
    286206    #endregion
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/Plugin.cs.frame

    r12753 r12895  
    2626
    2727namespace HeuristicLab.Problems.ArtificialAnt {
    28   [Plugin("HeuristicLab.Problems.ArtificialAnt","Provides all necessary classes to solve the artificial ant problem.", "3.4.8.$WCREV$")]
    29   [PluginFile("HeuristicLab.Problems.ArtificialAnt-3.4.dll", PluginFileType.Assembly)]
     28  [Plugin("HeuristicLab.Problems.ArtificialAnt","Provides all necessary classes to solve the artificial ant problem.", "3.5.0.$WCREV$")]
     29  [PluginFile("HeuristicLab.Problems.ArtificialAnt-3.5.dll", PluginFileType.Assembly)]
    3030  [PluginDependency("HeuristicLab.Collections", "3.3")]
    3131  [PluginDependency("HeuristicLab.Common", "3.3")]
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/Properties/AssemblyInfo.cs.frame

    r12753 r12895  
    5252// You can specify all the values or you can default the Revision and Build Numbers
    5353// by using the '*' as shown below:
    54 [assembly: AssemblyVersion("3.4.0.0")]
    55 [assembly: AssemblyFileVersion("3.4.8.$WCREV$")]
     54[assembly: AssemblyVersion("3.5.0.0")]
     55[assembly: AssemblyFileVersion("3.5.0.$WCREV$")]
Note: See TracChangeset for help on using the changeset viewer.