Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11171 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 const 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      IParameter operatorsParam;
64      if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
65        var operators = operatorsParam.ActualValue as OperatorCollection;
66        if (operators != null) {
67          Parameters.Remove(OperatorsParameterName);
68          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(operators), false));
69          OperatorsParameter.Hidden = true;
70        }
71      }
72      #endregion
73
74      RegisterEventHandlers();
75    }
76
77    private void RegisterEventHandlers() {
78      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
79      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
80      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
81    }
82
83    #region properties
84    // BackwardsCompatibility3.3
85    #region Backwards compatible code, remove with 3.4
86    [Storable(Name = "Operators", AllowOneWay = true)]
87    private IEnumerable<IOperator> StorableOperators {
88      set {
89        IParameter operatorsParam;
90        if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
91          var items = operatorsParam.ActualValue as ItemCollection<IItem>;
92          if (items == null) Parameters.Remove(operatorsParam);
93        }
94
95        //necessary to convert old experiments files where no parameter was used for saving the operators
96        if (!Parameters.ContainsKey(OperatorsParameterName)) {
97          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
98          OperatorsParameter.Hidden = true;
99        }
100        if (value != null) Operators.AddRange(value);
101      }
102    }
103    #endregion
104    protected ItemCollection<IItem> Operators {
105      get {
106        // BackwardsCompatibility3.3
107        #region Backwards compatible code, remove with 3.4
108        if (!Parameters.ContainsKey(OperatorsParameterName)) {
109          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
110          OperatorsParameter.Hidden = true;
111        }
112        #endregion
113        return OperatorsParameter.Value;
114      }
115    }
116    IEnumerable<IItem> IProblem.Operators { get { return Operators; } }
117    #endregion
118
119    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
120      var children = base.GetCollectedValues(param);
121      foreach (var child in children) {
122        if (child.Value is IOperator)
123          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
124        else yield return child;
125      }
126    }
127
128    #region events
129    private void Operators_Changed(object sender, EventArgs e) {
130      OnOperatorsChanged();
131    }
132    public event EventHandler OperatorsChanged;
133    protected virtual void OnOperatorsChanged() {
134      EventHandler handler = OperatorsChanged;
135      if (handler != null)
136        handler(this, EventArgs.Empty);
137    }
138
139    public event EventHandler Reset;
140    protected virtual void OnReset() {
141      EventHandler handler = Reset;
142      if (handler != null)
143        handler(this, EventArgs.Empty);
144    }
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.