source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/SingleObjective/SingleObjectiveSolutionScope.cs @ 14429

Last change on this file since 14429 was 14429, checked in by abeham, 4 years ago

#2701, #2708: Made a new branch from ProblemRefactoring and removed ScopedBasicAlgorithm branch (which becomes MemPR branch)

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Drawing;
23using HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization.Algorithms.SingleObjective {
30  [Item("MemPRScope", "Scope for a MemPR individual.")]
31  [StorableClass]
32  public sealed class SingleObjectiveSolutionScope<T> : NamedItem, ISingleObjectiveSolutionScope<T> where T : class, IItem {
33    public new static Image StaticItemImage {
34      get { return HeuristicLab.Common.Resources.VSImageLibrary.OrgChart; }
35    }
36
37    [Storable]
38    private IScope parent;
39    public IScope Parent {
40      get { return parent; }
41      set {
42        if (parent != value) {
43          parent = value;
44        }
45      }
46    }
47
48    [Storable]
49    private VariableCollection variables;
50    public VariableCollection Variables {
51      get { return variables; }
52    }
53
54    [Storable]
55    private ScopeList subScopes;
56    public ScopeList SubScopes {
57      get { return subScopes; }
58    }
59
60    [Storable]
61    private Variable solution;
62    public T Solution {
63      get { return (T)solution.Value; }
64      set { solution.Value = value; }
65    }
66
67    [Storable]
68    private Variable fitness;
69    public double Fitness {
70      get { return ((DoubleValue)fitness.Value).Value; }
71      set { ((DoubleValue)fitness.Value).Value = value; }
72    }
73
74    [StorableConstructor]
75    private SingleObjectiveSolutionScope(bool deserializing) : base(deserializing) { }
76    private SingleObjectiveSolutionScope(SingleObjectiveSolutionScope<T> original, Cloner cloner)
77      : base(original, cloner) {
78      // the parent will not be deep-cloned
79      parent = original.parent;
80      variables = cloner.Clone(original.variables);
81      subScopes = cloner.Clone(original.subScopes);
82      foreach (var child in SubScopes)
83        child.Parent = this;
84      solution = cloner.Clone(original.solution);
85      fitness = cloner.Clone(original.fitness);
86
87      RegisterSubScopesEvents();
88    }
89    public SingleObjectiveSolutionScope(T code, string codeName, double fitness, string fitnessName) {
90      this.solution = new Variable(codeName, code);
91      this.fitness = new Variable(fitnessName, new DoubleValue(fitness));
92      variables = new VariableCollection(2) { this.solution, this.fitness };
93      subScopes = new ScopeList(2);
94
95      RegisterSubScopesEvents();
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new SingleObjectiveSolutionScope<T>(this, cloner);
100    }
101
102    [StorableHook(HookType.AfterDeserialization)]
103    private void AfterDeserialization() {
104      RegisterSubScopesEvents();
105    }
106
107    public void Clear() {
108      variables.Clear();
109      subScopes.Clear();
110    }
111
112    #region SubScopes Events
113    private void RegisterSubScopesEvents() {
114      if (subScopes != null) {
115        subScopes.ItemsAdded += SubScopes_ItemsAdded;
116        subScopes.ItemsRemoved += SubScopes_ItemsRemoved;
117        subScopes.ItemsReplaced += SubScopes_ItemsReplaced;
118        subScopes.CollectionReset += SubScopes_CollectionReset;
119      }
120    }
121    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
122      foreach (var item in e.Items) item.Value.Parent = this;
123    }
124    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
125      foreach (var item in e.Items) item.Value.Parent = null;
126    }
127    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
128      foreach (var oldItem in e.OldItems) oldItem.Value.Parent = null;
129      foreach (var item in e.Items) item.Value.Parent = this;
130    }
131    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
132      foreach (var oldItem in e.OldItems) oldItem.Value.Parent = null;
133      foreach (var item in e.Items) item.Value.Parent = this;
134    }
135    #endregion
136
137    public void Adopt(ISingleObjectiveSolutionScope<T> orphan) {
138      Solution = orphan.Solution;
139      Fitness = orphan.Fitness;
140    }
141
142    public void Adopt(ISolutionScope<T> orphan) {
143      Solution = orphan.Solution;
144      Fitness = double.NaN;
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.