Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Optimization/3.3/Problems/Problem.cs @ 7842

Last change on this file since 7842 was 7842, checked in by gkronber, 12 years ago

merged r7609:7840 from trunk into time series branch

File size: 4.9 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
38    public IFixedValueParameter<OperatorCollection> OperatorsParameter {
39      get { return (IFixedValueParameter<OperatorCollection>)Parameters[OperatorsParameterName]; }
40    }
41
42    public static new Image StaticItemImage {
43      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
44    }
45
46    [StorableConstructor]
47    protected Problem(bool deserializing) : base(deserializing) { }
48    protected Problem(Problem original, Cloner cloner)
49      : base(original, cloner) {
50      RegisterEventHandlers();
51    }
52
53    protected Problem()
54      : base() {
55      Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(), false));
56      OperatorsParameter.Hidden = true;
57      RegisterEventHandlers();
58    }
59
60    [StorableHook(HookType.AfterDeserialization)]
61    private void AfterDeserialization() {
62      RegisterEventHandlers();
63    }
64
65    private void RegisterEventHandlers() {
66      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
67      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
68      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
69    }
70
71    #region properties
72    // BackwardsCompatibility3.3
73    #region Backwards compatible code, remove with 3.4
74    [Storable(Name = "Operators")]
75    private IEnumerable<IOperator> StorableOperators {
76      get { return null; }
77      set {
78        if (!Parameters.ContainsKey(OperatorsParameterName)) {
79          Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(value), false));
80          OperatorsParameter.Hidden = true;
81        }
82      }
83    }
84    #endregion
85    protected OperatorCollection Operators {
86      get {
87        // BackwardsCompatibility3.3
88        #region Backwards compatible code, remove with 3.4
89        if (!Parameters.ContainsKey(OperatorsParameterName)) {
90          Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(), false));
91          OperatorsParameter.Hidden = true;
92        }
93        #endregion
94        return OperatorsParameter.Value;
95      }
96    }
97    IEnumerable<IOperator> IProblem.Operators { get { return Operators; } }
98    #endregion
99
100    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
101      var children = base.GetCollectedValues(param);
102      foreach (var child in children) {
103        if (child.Value is IOperator)
104          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
105        else yield return child;
106      }
107    }
108
109    #region events
110    private void Operators_Changed(object sender, EventArgs e) {
111      OnOperatorsChanged();
112    }
113    public event EventHandler OperatorsChanged;
114    protected virtual void OnOperatorsChanged() {
115      EventHandler handler = OperatorsChanged;
116      if (handler != null)
117        handler(this, EventArgs.Empty);
118    }
119
120    public event EventHandler Reset;
121    protected virtual void OnReset() {
122      EventHandler handler = Reset;
123      if (handler != null)
124        handler(this, EventArgs.Empty);
125    }
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.