source: trunk/sources/HeuristicLab.Optimization/3.3/Problems/Problem.cs @ 7999

Last change on this file since 7999 was 7999, checked in by ascheibe, 8 years ago

#1864

  • switched IOperators collection to IItems
  • adapted algorithms and problems
  • added AfterDeserialization hooks for problem, external evaluation problem and user-defined problem
File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
23using System.Collections.Generic;
24using System.Drawing;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Optimization {
33  [Item("Problem", "Represents the base class for a problem.")]
34  [StorableClass]
35  public abstract class Problem : ParameterizedNamedItem, IProblem {
36    private static readonly string OperatorsParameterName = "Operators";
37    public IFixedValueParameter<ItemCollection<IItem>> OperatorsParameter {
38      get { return (IFixedValueParameter<ItemCollection<IItem>>)Parameters[OperatorsParameterName]; }
39    }
40
41    public static new Image StaticItemImage {
42      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
43    }
44
45    [StorableConstructor]
46    protected Problem(bool deserializing) : base(deserializing) { }
47    protected Problem(Problem original, Cloner cloner)
48      : base(original, cloner) {
49      RegisterEventHandlers();
50    }
51
52    protected Problem()
53      : base() {
54      Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
55      OperatorsParameter.Hidden = true;
56      RegisterEventHandlers();
57    }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
61      // BackwardsCompatibility3.3
62      #region Backwards compatible code, remove with 3.4
63      if (Parameters.ContainsKey(OperatorsParameterName) && Parameters[OperatorsParameterName] is FixedValueParameter<OperatorCollection>) {
64        OperatorCollection tmp = ((IFixedValueParameter<OperatorCollection>)Parameters[OperatorsParameterName]).Value;
65        Parameters.Remove(OperatorsParameterName);
66        Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(tmp), false));       
67        OperatorsParameter.Hidden = true;
68      }
69      #endregion
70
71      RegisterEventHandlers();
72    }
73
74    private void RegisterEventHandlers() {
75      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
76      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
77      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
78    }
79
80    #region properties
81    // BackwardsCompatibility3.3
82    #region Backwards compatible code, remove with 3.4
83    [Storable(Name = "Operators", AllowOneWay = true)]
84    private IEnumerable<IOperator> StorableOperators {
85      set {
86        if (!Parameters.ContainsKey(OperatorsParameterName)) {
87          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(value), false));
88          OperatorsParameter.Hidden = true;
89        }
90      }
91    }
92    #endregion
93    protected ItemCollection<IItem> Operators {
94      get {
95        // BackwardsCompatibility3.3
96        #region Backwards compatible code, remove with 3.4
97        if (!Parameters.ContainsKey(OperatorsParameterName)) {
98          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
99          OperatorsParameter.Hidden = true;
100        }
101        #endregion
102        return OperatorsParameter.Value;
103      }
104    }
105    IEnumerable<IItem> IProblem.Operators { get { return Operators; } }
106    #endregion
107
108    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
109      var children = base.GetCollectedValues(param);
110      foreach (var child in children) {
111        if (child.Value is IOperator)
112          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
113        else yield return child;
114      }
115    }
116
117    #region events
118    private void Operators_Changed(object sender, EventArgs e) {
119      OnOperatorsChanged();
120    }
121    public event EventHandler OperatorsChanged;
122    protected virtual void OnOperatorsChanged() {
123      EventHandler handler = OperatorsChanged;
124      if (handler != null)
125        handler(this, EventArgs.Empty);
126    }
127
128    public event EventHandler Reset;
129    protected virtual void OnReset() {
130      EventHandler handler = Reset;
131      if (handler != null)
132        handler(this, EventArgs.Empty);
133    }
134    #endregion
135  }
136}
Note: See TracBrowser for help on using the repository browser.