1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 |
|
---|
11 | namespace 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 | }
|
---|