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("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 | }
|
---|