Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CellularGeneticAlgorithm/HeuristicLab.Algorithms.CellularGeneticAlgorithm/3.4/VonNeumannNeighborhoodSelector.cs @ 10394

Last change on this file since 10394 was 7540, checked in by svonolfe, 13 years ago

Added first working version of cellular GA (#1781)

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Operators;
8using HeuristicLab.Common;
9using HeuristicLab.Parameters;
10
11namespace HeuristicLab.Algorithms.CellularGeneticAlgorithm {
12  [Item("VonNeumannNeighborhoodSelector", "Selects a neighborhood for the cellular GA.")]
13  [StorableClass]
14  public class VonNeumannNeighborhoodSelector: NeighborhoodSelector {
15    [StorableConstructor]
16    private VonNeumannNeighborhoodSelector(bool deserializing) : base(deserializing) { }
17
18    public VonNeumannNeighborhoodSelector()
19      : base() {
20    }
21
22    private VonNeumannNeighborhoodSelector(VonNeumannNeighborhoodSelector original, Cloner cloner)
23      : base(original, cloner) {
24    }
25
26    public override IDeepCloneable Clone(Cloner cloner) {
27      return new VonNeumannNeighborhoodSelector(this, cloner);
28    }
29
30    private IScope Get(List<IScope> population, int x, int y) {
31      int rows = (int)Math.Ceiling(Math.Sqrt(population.Count));
32      int index = (y * rows + x) % population.Count;
33      return population[index];
34    }
35
36    protected override List<IScope> Select(IScope individual, List<IScope> population) {
37      List<IScope> result = new List<IScope>();
38
39      int rows = (int)Math.Ceiling(Math.Sqrt(population.Count));
40      int index = population.IndexOf(individual);
41      int x = index % rows;
42      int y = index / rows;
43
44      result.Add(Get(population, x, y - 1 < 0 ? rows - 1 : y - 1));
45      result.Add(Get(population, x, (y + 1) % rows));
46      result.Add(Get(population, (x + 1) % rows, y));
47      result.Add(Get(population, x - 1 < 0 ? rows - 1 : x - 1, y));
48     
49      return result;
50    }
51  }
52}
Note: See TracBrowser for help on using the repository browser.