Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Algorithms.MemPR/3.3/SingleObjectiveSolutionScope.cs @ 16124

Last change on this file since 16124 was 14694, checked in by abeham, 8 years ago

#2457: fixed memory leak in MemPR

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.Algorithms.MemPR.Interfaces;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.MemPR {
31  [Item("Solution Scope", "Scope for an individual/solution of a single-objective single-encoded problem.")]
32  [StorableClass]
33  public sealed class SingleObjectiveSolutionScope<T> : NamedItem, ISingleObjectiveSolutionScope<T> where T : class, IItem {
34    public new static Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.OrgChart; }
36    }
37
38    [Storable]
39    private IScope parent;
40    public IScope Parent {
41      get { return parent; }
42      set {
43        if (parent != value) {
44          parent = value;
45        }
46      }
47    }
48
49    [Storable]
50    private VariableCollection variables;
51    public VariableCollection Variables {
52      get { return variables; }
53    }
54
55    [Storable]
56    private ScopeList subScopes;
57    public ScopeList SubScopes {
58      get { return subScopes; }
59    }
60
61    [Storable]
62    private Variable solution;
63    public T Solution {
64      get { return (T)solution.Value; }
65      set { solution.Value = value; }
66    }
67
68    [Storable]
69    private Variable fitness;
70    public double Fitness {
71      get { return ((DoubleValue)fitness.Value).Value; }
72      set { ((DoubleValue)fitness.Value).Value = value; }
73    }
74
75    [StorableConstructor]
76    private SingleObjectiveSolutionScope(bool deserializing) : base(deserializing) { }
77    private SingleObjectiveSolutionScope(SingleObjectiveSolutionScope<T> original, Cloner cloner)
78      : base(original, cloner) {
79      // the parent will not be deep-cloned
80      parent = original.parent;
81      variables = cloner.Clone(original.variables);
82      subScopes = cloner.Clone(original.subScopes);
83      foreach (var child in SubScopes)
84        child.Parent = this;
85      solution = cloner.Clone(original.solution);
86      fitness = cloner.Clone(original.fitness);
87
88      RegisterSubScopesEvents();
89    }
90    public SingleObjectiveSolutionScope(T code, string codeName, double fitness, string fitnessName) {
91      this.solution = new Variable(codeName, code);
92      this.fitness = new Variable(fitnessName, new DoubleValue(fitness));
93      variables = new VariableCollection(new [] { this.solution, this.fitness });
94      subScopes = new ScopeList(2);
95
96      RegisterSubScopesEvents();
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new SingleObjectiveSolutionScope<T>(this, cloner);
101    }
102
103    [StorableHook(HookType.AfterDeserialization)]
104    private void AfterDeserialization() {
105      RegisterSubScopesEvents();
106    }
107
108    public void Clear() {
109      variables.Clear();
110      subScopes.Clear();
111    }
112
113    #region SubScopes Events
114    private void RegisterSubScopesEvents() {
115      if (subScopes != null) {
116        subScopes.ItemsAdded += SubScopes_ItemsAdded;
117        subScopes.ItemsRemoved += SubScopes_ItemsRemoved;
118        subScopes.ItemsReplaced += SubScopes_ItemsReplaced;
119        subScopes.CollectionReset += SubScopes_CollectionReset;
120      }
121    }
122    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
123      foreach (var item in e.Items) item.Value.Parent = this;
124    }
125    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
126      foreach (var item in e.Items) item.Value.Parent = null;
127    }
128    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
129      foreach (var oldItem in e.OldItems) oldItem.Value.Parent = null;
130      foreach (var item in e.Items) item.Value.Parent = this;
131    }
132    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
133      foreach (var oldItem in e.OldItems) oldItem.Value.Parent = null;
134      foreach (var item in e.Items) item.Value.Parent = this;
135    }
136    #endregion
137
138    public void Adopt(ISingleObjectiveSolutionScope<T> orphan) {
139      Solution = (T)orphan.Solution.Clone();
140      Fitness = orphan.Fitness;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.