Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/31/10 14:26:45 (14 years ago)
Author:
gkronber
Message:

Extracted view for artificial ant problem into a separate plugin/project. #952 (Artificial Ant Problem for 3.3)

Location:
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs

    r3238 r3239  
    2222using System;
    2323using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     24using HeuristicLab.Data;
     25using System.Collections.Generic;
    2426
    2527namespace HeuristicLab.Problems.ArtificialAnt {
    26   internal class AntInterpreter {
    27     private const int N_FOOD_ITEMS = 89;
    28     private const int WORLD_WIDTH = 32;
    29     private const int WORLD_HEIGHT = 32;
    30     private const int FOOD = 1;
    31     private const int EMPTY = 0;
    32     private int[] SANTA_FE_TRAIL = new int[] {
    33       0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    34       0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    35       0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
    36       0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
    37       0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
    38       0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    39       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
    40       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    41       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    42       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
    43       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    44       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    45       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
    46       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    47       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
    48       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
    49       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    50       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    51       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
    52       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
    53       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    54       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    55       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
    56       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
    57       0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    58       0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    59       0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    60       0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    61       0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    62       0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    63       0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    64       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    65     };
    66 
    67     private int[] trail;
    68 
    69     public int ElapsedTime { get; set; }
     28  public class AntInterpreter {
    7029    public int MaxTimeSteps { get; set; }
    7130    public int FoodEaten { get; set; }
    72     private int currentDirection;
    73     private int currentLocation;
    74 
    75     public AntInterpreter() {
    76       currentLocation = 0;
    77       currentDirection = 0;
    78       FoodEaten = 0;
    79       trail = new int[WORLD_HEIGHT * WORLD_WIDTH];
    80       Array.Copy(SANTA_FE_TRAIL, trail, SANTA_FE_TRAIL.Length);
    81     }
    82 
    83     internal void Run(SymbolicExpressionTree tree) {
    84       while (FoodEaten < N_FOOD_ITEMS && ElapsedTime < MaxTimeSteps) {
    85         Step(tree.Root.SubTrees[0]);
     31    private BoolMatrix world;
     32    public BoolMatrix World {
     33      get { return world; }
     34      set {
     35        // create a clone of the world because the ant will remove the food items it can find.
     36        world = (BoolMatrix)value.Clone();
     37        CountFoodItems();
    8638      }
    8739    }
    8840
    89     internal void Step(SymbolicExpressionTreeNode currentNode) {
     41    private SymbolicExpressionTree expression;
     42    public SymbolicExpressionTree Expression {
     43      get { return expression; }
     44      set {
     45        expression = value;
     46      }
     47    }
     48    public int ElapsedTime { get; set; }
     49    private int currentDirection;
     50    private int currentAntLocationRow;
     51    private int currentAntLocationColumn;
     52    private int nFoodItems;
     53    private Stack<SymbolicExpressionTreeNode> nodeStack = new Stack<SymbolicExpressionTreeNode>();
     54
     55    private void CountFoodItems() {
     56      nFoodItems = 0;
     57      for (int i = 0; i < World.Rows; i++) {
     58        for (int j = 0; j < World.Columns; j++) {
     59          if (World[i, j]) nFoodItems++;
     60        }
     61      }
     62    }
     63
     64    public void AntLocation(out int row, out int column) {
     65      row = currentAntLocationRow;
     66      column = currentAntLocationColumn;
     67    }
     68
     69    public int AntDirection {
     70      get { return currentDirection; }
     71    }
     72
     73    public void Run() {
     74      while (ElapsedTime < MaxTimeSteps && FoodEaten < nFoodItems) {
     75        Step();
     76      }
     77    }
     78
     79    public void Step() {
     80      // expression evaluated completly => start at root again
     81      if (nodeStack.Count == 0)
     82        nodeStack.Push(Expression.Root.SubTrees[0]);
     83      var currentNode = nodeStack.Pop();
    9084      if (currentNode.Symbol is Left) {
    91         if (ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;
    9285        currentDirection = (currentDirection + 3) % 4;
    9386        ElapsedTime++;
    9487      } else if (currentNode.Symbol is Right) {
    95         if (ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;
    9688        currentDirection = (currentDirection + 1) % 4;
    9789        ElapsedTime++;
    9890      } else if (currentNode.Symbol is Move) {
    99         if (ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;
    100         currentLocation = NextField();
    101         FoodEaten += trail[currentLocation];
    102         trail[currentLocation] = EMPTY;
     91        MoveAntForward();
     92        if (World[currentAntLocationRow, currentAntLocationColumn])
     93          FoodEaten++;
     94        World[currentAntLocationRow, currentAntLocationColumn] = false;
    10395        ElapsedTime++;
    10496      } else if (currentNode.Symbol is IfFoodAhead) {
    105         if (trail[NextField()] == FOOD) {
    106           Step(currentNode.SubTrees[0]);
     97        int nextAntLocationRow;
     98        int nextAntLocationColumn;
     99        NextField(out nextAntLocationRow, out nextAntLocationColumn);
     100        if (World[nextAntLocationRow, nextAntLocationColumn]) {
     101          nodeStack.Push(currentNode.SubTrees[0]);
    107102        } else {
    108           Step(currentNode.SubTrees[1]);
     103          nodeStack.Push(currentNode.SubTrees[1]);
    109104        }
    110105      } else if (currentNode.Symbol is Prog2) {
    111         Step(currentNode.SubTrees[0]);
    112         Step(currentNode.SubTrees[1]);
     106        nodeStack.Push(currentNode.SubTrees[1]);
     107        nodeStack.Push(currentNode.SubTrees[0]);
    113108        return;
    114109      } else if (currentNode.Symbol is Prog3) {
    115         Step(currentNode.SubTrees[0]);
    116         Step(currentNode.SubTrees[1]);
    117         Step(currentNode.SubTrees[2]);
     110        nodeStack.Push(currentNode.SubTrees[2]);
     111        nodeStack.Push(currentNode.SubTrees[1]);
     112        nodeStack.Push(currentNode.SubTrees[0]);
    118113        return;
    119       } else  {
     114      } else {
    120115        throw new InvalidOperationException(currentNode.Symbol.ToString());
    121116      }
    122117    }
    123118
    124     private int NextField() {
    125       int currentLocationX = currentLocation % WORLD_WIDTH;
    126       int currentLocationY = currentLocation / WORLD_HEIGHT;
     119    private void MoveAntForward() {
     120      NextField(out currentAntLocationRow, out currentAntLocationColumn);
     121    }
    127122
     123    private void NextField(out int nextAntLocationRow, out int nextAntLocationColumn) {
    128124      switch (currentDirection) {
    129125        case 0:
    130           currentLocationX = (currentLocationX + 1) % WORLD_WIDTH; // EAST
     126          nextAntLocationColumn = (currentAntLocationColumn + 1) % World.Columns; // EAST
     127          nextAntLocationRow = currentAntLocationRow;
    131128          break;
    132129        case 1:
    133           currentLocationY = (currentLocationY + 1) % WORLD_HEIGHT; // SOUTH
     130          nextAntLocationRow = (currentAntLocationRow + 1) % World.Rows; // SOUTH
     131          nextAntLocationColumn = currentAntLocationColumn;
    134132          break;
    135133        case 2:
    136           currentLocationX = (currentLocationX + WORLD_WIDTH - 1) % WORLD_WIDTH; // WEST
     134          nextAntLocationColumn = (currentAntLocationColumn + World.Columns - 1) % World.Columns; // WEST
     135          nextAntLocationRow = currentAntLocationRow;
    137136          break;
    138137        case 3:
    139           currentLocationY = (currentLocationY + WORLD_HEIGHT - 1) % WORLD_HEIGHT; // NORTH
     138          nextAntLocationRow = (currentAntLocationRow + World.Rows - 1) % World.Rows; // NORTH
     139          nextAntLocationColumn = currentAntLocationColumn;
    140140          break;
    141141        default:
    142142          throw new InvalidOperationException();
    143143      }
    144       return currentLocationY * WORLD_WIDTH + currentLocationX;
    145144    }
    146145  }
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntTrail.cs

    r3238 r3239  
    4848    }
    4949
     50    private BoolMatrix world;
     51    [Storable]
     52    public BoolMatrix World {
     53      get { return world; }
     54      set {
     55        if (world != value) {
     56          if (world != null) DeregisterWorldEvents();
     57          world = value;
     58          if (world != null) RegisterWorldEvents();
     59          OnWorldChanged();
     60        }
     61      }
     62    }
     63    private IntValue maxTimeSteps;
     64    [Storable]
     65    public IntValue MaxTimeSteps {
     66      get { return maxTimeSteps; }
     67      set {
     68        if (maxTimeSteps != value) {
     69          if (maxTimeSteps != value) {
     70            if (maxTimeSteps != null) DeregisterMaxTimeStepsEvents();
     71            maxTimeSteps = value;
     72            if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
     73            OnWorldChanged();
     74          }
     75        }
     76      }
     77    }
     78
    5079    public AntTrail() : base() { }
    51     public AntTrail(SymbolicExpressionTree expression)
     80    public AntTrail(BoolMatrix world, SymbolicExpressionTree expression, IntValue maxTimeSteps)
    5281      : this() {
     82      World = world;
    5383      SymbolicExpressionTree = expression;
     84      MaxTimeSteps = maxTimeSteps;
    5485    }
    5586
     
    5889      cloner.RegisterClonedObject(this, clone);
    5990      clone.expression = (SymbolicExpressionTree)cloner.Clone(expression);
     91      clone.world = (BoolMatrix)cloner.Clone(world);
     92      clone.maxTimeSteps = (IntValue)cloner.Clone(maxTimeSteps);
    6093      return clone;
    6194    }
    6295
    63     //#region Events
     96    #region Events
    6497    public event EventHandler SymbolicExpressionTreeChanged;
    6598    private void OnSymbolicExpressionTreeChanged() {
    66       if (SymbolicExpressionTreeChanged != null)
    67         SymbolicExpressionTreeChanged(this, EventArgs.Empty);
     99      var changed = SymbolicExpressionTreeChanged;
     100      if (changed != null)
     101        changed(this, EventArgs.Empty);
     102    }
     103    public event EventHandler WorldChanged;
     104    private void OnWorldChanged() {
     105      var changed = WorldChanged;
     106      if (changed != null)
     107        changed(this, EventArgs.Empty);
     108    }
     109    public event EventHandler MaxTimeStepsChanged;
     110    private void OnMaxTimeStepsChanged() {
     111      var changed = MaxTimeStepsChanged;
     112      if (changed != null)
     113        changed(this, EventArgs.Empty);
    68114    }
    69115
     
    77123    //}
    78124
    79     //private void SymbolicExpressionTree_ItemChanged(object sender, EventArgs<int> e) {
    80     //  OnSymbolicExpressionTreeChanged();
    81     //}
    82     //private void SymbolicExpressionTree_Reset(object sender, EventArgs e) {
    83     //  OnSymbolicExpressionTreeChanged();
    84     //}
    85     //#endregion
     125    private void RegisterWorldEvents() {
     126      World.ItemChanged += new EventHandler<EventArgs<int, int>>(World_ItemChanged);
     127      World.Reset += new EventHandler(World_Reset);
     128    }
     129    private void DeregisterWorldEvents() {
     130      World.ItemChanged -= new EventHandler<EventArgs<int, int>>(World_ItemChanged);
     131      World.Reset -= new EventHandler(World_Reset);
     132    }
     133    private void RegisterMaxTimeStepsEvents() {
     134      MaxTimeSteps.ValueChanged += new EventHandler(MaxTimeSteps_ValueChanged);
     135    }
     136    private void DeregisterMaxTimeStepsEvents() {
     137      MaxTimeSteps.ValueChanged -= new EventHandler(MaxTimeSteps_ValueChanged);
     138    }
     139
     140    void MaxTimeSteps_ValueChanged(object sender, EventArgs e) {
     141      OnMaxTimeStepsChanged();
     142    }
     143    private void World_ItemChanged(object sender, EventArgs<int, int> e) {
     144      OnWorldChanged();
     145    }
     146    private void World_Reset(object sender, EventArgs e) {
     147      OnWorldChanged();
     148    }
     149    #endregion
    86150  }
    87151}
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs

    r3238 r3239  
    4242    }
    4343
     44    #region constant for default world (Santa Fe)
     45    private readonly bool[,] santaFeAntTrail = new bool[,] {
     46      {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},
     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, false, false, false, false, false, false, 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, false, true, true, true, false, false, false, false},
     49      {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},
     50      {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},
     51      {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},
     52      {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},
     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, false, false, false},
     54      {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},
     55      {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},
     56      {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},
     57      {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},
     58      {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},
     59      {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},
     60      {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},
     61      {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},
     62      {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},
     63      {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},
     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, true, 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, true, false, false, false, false},
     66      {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},
     67      {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},
     68      {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},
     69      {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},
     70      {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},
     71      {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},
     72      {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},
     73      {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},
     74      {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},
     75      {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},
     76      {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 },
     77      {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 }
     78    };
     79    #endregion
     80
    4481    #region Parameter Properties
    4582    public ValueParameter<BoolValue> MaximizationParameter {
     
    70107      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
    71108    }
     109    public ValueParameter<BoolMatrix> WorldParameter {
     110      get { return (ValueParameter<BoolMatrix>)Parameters["World"]; }
     111    }
     112    public ValueParameter<IntValue> MaxTimeStepsParameter {
     113      get { return (ValueParameter<IntValue>)Parameters["MaxTimeSteps"]; }
     114    }
    72115
    73116    public OptionalValueParameter<IAntTrailVisualizer> VisualizerParameter {
     
    86129
    87130    #region Properties
     131    public BoolMatrix World {
     132      get { return WorldParameter.Value; }
     133      set { WorldParameter.Value = value; }
     134    }
     135    public IntValue MaxTimeSteps {
     136      get { return MaxTimeStepsParameter.Value; }
     137      set { MaxTimeStepsParameter.Value = value; }
     138    }
     139    public IntValue MaxExpressionLength {
     140      get { return MaxExpressionLengthParameter.Value; }
     141      set { MaxExpressionLengthParameter.Value = value; }
     142    }
     143    public IntValue MaxExpressionDepth {
     144      get { return MaxExpressionDepthParameter.Value; }
     145      set { MaxExpressionDepthParameter.Value = value; }
     146    }
    88147    public SymbolicExpressionTreeCreator SolutionCreator {
    89148      get { return SolutionCreatorParameter.Value; }
     
    116175      get { return BestKnownQualityParameter.Value; }
    117176    }
    118     private List<IOperator> operators;
     177    private List<ISymbolicExpressionTreeOperator> operators;
    119178    public IEnumerable<IOperator> Operators {
    120       get { return operators; }
     179      get { return operators.Cast<IOperator>(); }
    121180    }
    122181    #endregion
     
    128187      ArtificialAntExpressionGrammar grammar = new ArtificialAntExpressionGrammar();
    129188      BestAntTrailVisualizer visualizer = new BestAntTrailVisualizer();
    130 
     189      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
    131190      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
    132191      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
     
    136195      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
    137196      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
     197      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
     198      Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
    138199      Parameters.Add(new ValueParameter<IAntTrailVisualizer>("Visualizer", "The operator which should be used to visualize artificial ant solutions.", visualizer));
    139200
     
    159220    public event EventHandler SolutionCreatorChanged;
    160221    private void OnSolutionCreatorChanged() {
    161       if (SolutionCreatorChanged != null)
    162         SolutionCreatorChanged(this, EventArgs.Empty);
     222      var changed = SolutionCreatorChanged;
     223      if (changed != null)
     224        changed(this, EventArgs.Empty);
    163225    }
    164226    public event EventHandler EvaluatorChanged;
    165227    private void OnEvaluatorChanged() {
    166       if (EvaluatorChanged != null)
    167         EvaluatorChanged(this, EventArgs.Empty);
     228      var changed = EvaluatorChanged;
     229      if (changed != null)
     230        changed(this, EventArgs.Empty);
    168231    }
    169232    public event EventHandler VisualizerChanged;
    170233    private void OnVisualizerChanged() {
    171       if (VisualizerChanged != null)
    172         VisualizerChanged(this, EventArgs.Empty);
     234      var changed = VisualizerChanged;
     235      if (changed != null)
     236        changed(this, EventArgs.Empty);
    173237    }
    174238
    175239    public event EventHandler OperatorsChanged;
    176240    private void OnOperatorsChanged() {
    177       if (OperatorsChanged != null)
    178         OperatorsChanged(this, EventArgs.Empty);
     241      var changed = OperatorsChanged;
     242      if (changed != null)
     243        changed(this, EventArgs.Empty);
    179244    }
    180245
     
    183248      ParameterizeSolutionCreator();
    184249      ParameterizeEvaluator();
     250      ParameterizeVisualizer();
    185251      ParameterizeOperators();
    186252      OnSolutionCreatorChanged();
     
    188254    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
    189255      ParameterizeEvaluator();
     256      ParameterizeVisualizer();
    190257      ParameterizeOperators();
    191258    }
    192259    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
     260      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    193261      ParameterizeEvaluator();
     262      ParameterizeVisualizer();
    194263      OnEvaluatorChanged();
    195264    }
     265
     266    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
     267      ParameterizeVisualizer();
     268      OnVisualizerChanged();
     269    }
     270
     271    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
     272      ParameterizeVisualizer();
     273    }
     274
    196275    #endregion
    197276
     
    203282      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
    204283      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
    205     }
     284      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     285      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
     286    }
     287
     288    private void InitializeOperators() {
     289      operators = new List<ISymbolicExpressionTreeOperator>();
     290      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
     291      ParameterizeOperators();
     292    }
     293
    206294    private void ParameterizeSolutionCreator() {
    207295      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
     296      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
     297      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
    208298    }
    209299    private void ParameterizeEvaluator() {
    210300      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    211     }
    212     private void InitializeOperators() {
    213       operators = new List<IOperator>();
    214       operators.Add(Evaluator);
    215       operators.Add(SolutionCreator);
    216       operators.Add(new SubtreeCrossover());
    217       ParameterizeOperators();
     301      Evaluator.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
     302      Evaluator.WorldParameter.ActualName = WorldParameter.Name;
     303    }
     304    private void ParameterizeVisualizer() {
     305      if (Visualizer != null) {
     306        Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
     307        var antTrailVisualizer = Visualizer as IAntTrailVisualizer;
     308        if (antTrailVisualizer != null) {
     309          antTrailVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     310          antTrailVisualizer.WorldParameter.ActualName = WorldParameter.Name;
     311          antTrailVisualizer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
     312        }
     313      }
    218314    }
    219315
    220316    private void ParameterizeOperators() {
    221       foreach (ProbabilisticTreeCreator op in Operators.OfType<ProbabilisticTreeCreator>()) {
     317      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
    222318        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
    223319        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
     
    226322      foreach (Evaluator op in Operators.OfType<Evaluator>()) {
    227323        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     324        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
     325        op.WorldParameter.ActualName = WorldParameter.Name;
    228326      }
    229327      foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
    230         op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
    231         op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
    232         op.SymbolicExpressionGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
    233328        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    234329        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    235330      }
    236331    }
    237     private void ParameterizeVisualizer() {
    238       if (Visualizer != null) {
    239         Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    240         if (Visualizer is IAntTrailVisualizer)
    241           ((IAntTrailVisualizer)Visualizer).SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    242       }
    243     }
     332
    244333
    245334    #endregion
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/BestAntTrailVisualizer.cs

    r3238 r3239  
    3636  [StorableClass]
    3737  public sealed class BestAntTrailVisualizer : SingleSuccessorOperator, IAntTrailVisualizer {
     38    public ILookupParameter<BoolMatrix> WorldParameter {
     39      get { return (ILookupParameter<BoolMatrix>)Parameters["World"]; }
     40    }
    3841    public ILookupParameter<ItemArray<SymbolicExpressionTree>> SymbolicExpressionTreeParameter {
    3942      get { return (ILookupParameter<ItemArray<SymbolicExpressionTree>>)Parameters["SymbolicExpressionTree"]; }
     
    4245      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Quality"]; }
    4346    }
     47    public ILookupParameter<IntValue> MaxTimeStepsParameter {
     48      get { return (ILookupParameter<IntValue>)Parameters["MaxTimeSteps"]; }
     49    }
     50
    4451    public ILookupParameter<AntTrail> AntTrailParameter {
    4552      get { return (ILookupParameter<AntTrail>)Parameters["AntTrail"]; }
     
    5158    public BestAntTrailVisualizer()
    5259      : base() {
    53       Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
     60      Parameters.Add(new LookupParameter<BoolMatrix>("World", "The world with food items for the artificial ant."));
    5461      Parameters.Add(new SubScopesLookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solutions from which the best solution should be visualized."));
    5562      Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the artificial ant solutions which should be visualized."));
    5663      Parameters.Add(new LookupParameter<AntTrail>("AntTrail", "The visual representation of the best ant trail."));
     64      Parameters.Add(new LookupParameter<IntValue>("MaxTimeSteps", "The maximal time steps that the artificial ant has available to collect all food items."));
    5765    }
    5866
     
    6068      ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
    6169      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
     70      BoolMatrix world = WorldParameter.ActualValue;
     71      IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
    6272
    6373      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => -x.Value).First().index;
    6474
    6575      AntTrail antTrail = AntTrailParameter.ActualValue;
    66       if (antTrail == null) AntTrailParameter.ActualValue = new AntTrail(expressions[i]);
     76      if (antTrail == null) AntTrailParameter.ActualValue = new AntTrail(world, expressions[i], maxTimeSteps);
    6777      else {
     78        antTrail.World = world;
    6879        antTrail.SymbolicExpressionTree = expressions[i];
     80        antTrail.MaxTimeSteps = maxTimeSteps;
    6981      }
    7082      return base.Apply();
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Evaluator.cs

    r3223 r3239  
    4040      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
    4141    }
     42    public ILookupParameter<BoolMatrix> WorldParameter {
     43      get { return (ILookupParameter<BoolMatrix>)Parameters["World"]; }
     44    }
     45    public ILookupParameter<IntValue> MaxTimeStepsParameter {
     46      get { return (ILookupParameter<IntValue>)Parameters["MaxTimeSteps"]; }
     47    }
     48
    4249    public Evaluator()
    4350      : base() {
    4451      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the evaluated artificial ant solution."));
    4552      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solution encoded as a symbolic expression tree that should be evaluated"));
     53      Parameters.Add(new LookupParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items."));
     54      Parameters.Add(new LookupParameter<IntValue>("MaxTimeSteps", "The maximal number of time steps that the artificial ant should be simulated."));
    4655    }
    4756
    4857    public sealed override IOperation Apply() {
     58      SymbolicExpressionTree expression = SymbolicExpressionTreeParameter.ActualValue;
     59      BoolMatrix world = WorldParameter.ActualValue;
     60      IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
    4961
    50       SymbolicExpressionTree solution = SymbolicExpressionTreeParameter.ActualValue;
    5162      AntInterpreter interpreter = new AntInterpreter();
    52       interpreter.MaxTimeSteps = 600;
    53       interpreter.Run(solution);
     63      interpreter.MaxTimeSteps = maxTimeSteps.Value;
     64      interpreter.World = world;
     65      interpreter.Expression = expression;
     66      interpreter.Run();
    5467
    5568      QualityParameter.ActualValue = new DoubleValue(interpreter.FoodEaten);
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/HeuristicLab.Problems.ArtificialAnt-3.3.csproj

    r3238 r3239  
    7171    </Reference>
    7272    <Reference Include="System.Drawing" />
    73     <Reference Include="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
    74       <SpecificVersion>False</SpecificVersion>
    75       <HintPath>..\..\..\..\..\..\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll</HintPath>
    76     </Reference>
    7773    <Reference Include="System.Xml.Linq">
    7874      <RequiredTargetFramework>3.5</RequiredTargetFramework>
     
    9288    <Compile Include="ArtificialAntExpressionGrammar.cs" />
    9389    <Compile Include="ArtificialAntProblem.cs" />
    94     <Compile Include="ArtificialAntProblemView.cs">
    95       <SubType>UserControl</SubType>
    96     </Compile>
    97     <Compile Include="ArtificialAntProblemView.Designer.cs">
    98       <DependentUpon>ArtificialAntProblemView.cs</DependentUpon>
    99     </Compile>
    10090    <Compile Include="AntTrail.cs" />
    101     <Compile Include="AntTrailView.cs">
    102       <SubType>UserControl</SubType>
    103     </Compile>
    104     <Compile Include="AntTrailView.Designer.cs">
    105       <DependentUpon>AntTrailView.cs</DependentUpon>
    106     </Compile>
    10791    <Compile Include="Properties\AssemblyInfo.cs" />
    10892    <Compile Include="Symbols\IfFoodAhead.cs" />
     
    130114      <Name>HeuristicLab.Common-3.2</Name>
    131115    </ProjectReference>
    132     <ProjectReference Include="..\..\HeuristicLab.Core.Views\3.3\HeuristicLab.Core.Views-3.3.csproj">
    133       <Project>{E226881D-315F-423D-B419-A766FE0D8685}</Project>
    134       <Name>HeuristicLab.Core.Views-3.3</Name>
    135     </ProjectReference>
    136116    <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
    137117      <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>
     
    146126      <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3</Name>
    147127    </ProjectReference>
    148     <ProjectReference Include="..\..\HeuristicLab.MainForm.WindowsForms\3.2\HeuristicLab.MainForm.WindowsForms-3.2.csproj">
    149       <Project>{AB687BBE-1BFE-476B-906D-44237135431D}</Project>
    150       <Name>HeuristicLab.MainForm.WindowsForms-3.2</Name>
    151     </ProjectReference>
    152     <ProjectReference Include="..\..\HeuristicLab.MainForm\3.2\HeuristicLab.MainForm-3.2.csproj">
    153       <Project>{3BD61258-31DA-4B09-89C0-4F71FEF5F05A}</Project>
    154       <Name>HeuristicLab.MainForm-3.2</Name>
    155     </ProjectReference>
    156128    <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
    157129      <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project>
    158130      <Name>HeuristicLab.Operators-3.3</Name>
    159     </ProjectReference>
    160     <ProjectReference Include="..\..\HeuristicLab.Optimization.Views\3.3\HeuristicLab.Optimization.Views-3.3.csproj">
    161       <Project>{662B4B15-8F4D-4AE5-B3EB-D91C215F5AF2}</Project>
    162       <Name>HeuristicLab.Optimization.Views-3.3</Name>
    163131    </ProjectReference>
    164132    <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/HeuristicLabProblemsArtificialAntPlugin.cs.frame

    r3223 r3239  
    3333  [PluginDependency("HeuristicLab.Data", "3.3.0.0")]
    3434  [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding","3.3.0.0")]
     35  [PluginDependency("HeuristicLab.Operators", "3.3.0.0")]
    3536  [PluginDependency("HeuristicLab.Optimization", "3.3.0.0")]
    36   [PluginDependency("HeuristicLab.Operators", "3.3.0.0")]
    3737  [PluginDependency("HeuristicLab.Parameters", "3.3.0.0")]
    3838  [PluginDependency("HeuristicLab.Persistence", "3.3.0.0")]
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/IAntTrailVisualizer.cs

    r3238 r3239  
    2424using HeuristicLab.Optimization;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627
    2728namespace HeuristicLab.Problems.ArtificialAnt {
    2829  public interface IAntTrailVisualizer : ISolutionsVisualizer, ISingleObjectiveSolutionsVisualizer {
    2930    ILookupParameter<ItemArray<SymbolicExpressionTree>> SymbolicExpressionTreeParameter { get; }
     31    ILookupParameter<BoolMatrix> WorldParameter { get; }
     32    ILookupParameter<IntValue> MaxTimeStepsParameter { get; }
    3033  }
    3134}
Note: See TracChangeset for help on using the changeset viewer.