Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CellularGeneticAlgorithm/HeuristicLab.Algorithms.CellularGeneticAlgorithm/3.4/NeighborhoodSelector.cs @ 13282

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

Added first working version of cellular GA (#1781)

File size: 2.3 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("NeighborhoodSelector", "Selects a neighborhood for the cellular GA.")]
13  [StorableClass]
14  public abstract class NeighborhoodSelector: SingleSuccessorOperator {
15    protected ScopeParameter CurrentScopeParameter {
16      get { return (ScopeParameter)Parameters["CurrentScope"]; }
17    }
18
19    public IScope CurrentScope {
20      get { return CurrentScopeParameter.ActualValue; }
21    }
22
23    [StorableConstructor]
24    protected NeighborhoodSelector(bool deserializing) : base(deserializing) { }
25
26    public NeighborhoodSelector()
27      : base() {
28        Parameters.Add(new ScopeParameter("CurrentScope", "The current scope for which sub-scopes should be selected."));
29    }
30
31    protected NeighborhoodSelector(NeighborhoodSelector original, Cloner cloner)
32      : base(original, cloner) {
33    }
34
35    protected IScope ShallowCopy(IScope scope) {
36      IScope copy = new Scope(scope.Name, scope.Description);
37      foreach (IVariable variable in scope.Variables) {
38        Variable copyVar = new Variable();
39        copyVar.Name = variable.Name;
40        copyVar.Value = variable.Value;
41        copy.Variables.Add(copyVar);
42      }
43
44      return copy;
45    }
46
47    protected abstract List<IScope> Select(IScope individual, List<IScope> population);
48
49    public override IOperation Apply() {
50      IScope individual = CurrentScope;
51      List<IScope> population = new List<IScope>(CurrentScope.Parent.SubScopes);
52
53      IScope root = new Scope();
54      CurrentScope.SubScopes.Add(root);
55
56      IScope root2 = ShallowCopy(individual);
57      root.SubScopes.Add(ShallowCopy(individual));
58      root.SubScopes.Add(root2);
59
60      IScope root3 = new Scope();
61      IScope root4 = new Scope();
62      root2.SubScopes.Add(root3);
63      root2.SubScopes.Add(root4);
64
65      root3.SubScopes.Add(ShallowCopy(individual));
66      foreach (IScope neighbor in Select(individual, population)) {
67        root4.SubScopes.Add(ShallowCopy(neighbor));
68      }
69     
70      return base.Apply();
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.