Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/Problem.cs @ 17334

Last change on this file since 17334 was 17334, checked in by mkommend, 4 years ago

#2521: Moved IStorableContent from generic problem implementation to IProblem.

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Optimization {
33  [Item("Problem", "Represents the base class for a problem.")]
34  [StorableType("6DC97432-9BD1-4304-802A-1FC48A0E0468")]
35  public abstract class Problem : ParameterizedNamedItem, IProblem {
36    public string Filename { get; set; }
37
38    private const string OperatorsParameterName = "Operators";
39    public IFixedValueParameter<ItemCollection<IItem>> OperatorsParameter {
40      get { return (IFixedValueParameter<ItemCollection<IItem>>)Parameters[OperatorsParameterName]; }
41    }
42
43    public static new Image StaticItemImage {
44      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
45    }
46
47    [StorableConstructor]
48    protected Problem(StorableConstructorFlag _) : base(_) { }
49    protected Problem(Problem original, Cloner cloner)
50      : base(original, cloner) {
51      RegisterEventHandlers();
52    }
53
54    protected Problem()
55      : base() {
56      Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
57      OperatorsParameter.Hidden = true;
58      RegisterEventHandlers();
59    }
60
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      // BackwardsCompatibility3.3
64      #region Backwards compatible code, remove with 3.4
65      IParameter operatorsParam;
66      if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
67        var operators = operatorsParam.ActualValue as OperatorCollection;
68        if (operators != null) {
69          Parameters.Remove(OperatorsParameterName);
70          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(operators)) { GetsCollected = false });
71          OperatorsParameter.Hidden = true;
72        }
73      }
74      #endregion
75
76      RegisterEventHandlers();
77    }
78
79    private void RegisterEventHandlers() {
80      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
81      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
82      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
83    }
84
85    #region properties
86    // BackwardsCompatibility3.3
87    #region Backwards compatible code, remove with 3.4
88    [Storable(OldName = "Operators")]
89    private IEnumerable<IOperator> StorableOperators {
90      set {
91        IParameter operatorsParam;
92        if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
93          var items = operatorsParam.ActualValue as ItemCollection<IItem>;
94          if (items == null) Parameters.Remove(operatorsParam);
95        }
96
97        //necessary to convert old experiments files where no parameter was used for saving the operators
98        if (!Parameters.ContainsKey(OperatorsParameterName)) {
99          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
100          OperatorsParameter.Hidden = true;
101        }
102        if (value != null) Operators.AddRange(value);
103      }
104    }
105    #endregion
106    protected ItemCollection<IItem> Operators {
107      get {
108        // BackwardsCompatibility3.3
109        #region Backwards compatible code, remove with 3.4
110        if (!Parameters.ContainsKey(OperatorsParameterName)) {
111          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
112          OperatorsParameter.Hidden = true;
113        }
114        #endregion
115        return OperatorsParameter.Value;
116      }
117    }
118    IEnumerable<IItem> IProblem.Operators { get { return GetOperators(); } }
119
120    protected virtual IEnumerable<IItem> GetOperators() {
121      return Operators;
122    }
123
124    public virtual IEnumerable<IParameterizedItem> ExecutionContextItems {
125      get { yield return this; }
126    }
127    #endregion
128
129    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
130      var children = base.GetCollectedValues(param);
131      foreach (var child in children) {
132        if (child.Value is IOperator)
133          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
134        else yield return child;
135      }
136    }
137
138    #region events
139    private void Operators_Changed(object sender, EventArgs e) {
140      OnOperatorsChanged();
141    }
142    public event EventHandler OperatorsChanged;
143    protected virtual void OnOperatorsChanged() {
144      EventHandler handler = OperatorsChanged;
145      if (handler != null)
146        handler(this, EventArgs.Empty);
147    }
148
149    public event EventHandler Reset;
150    protected virtual void OnReset() {
151      EventHandler handler = Reset;
152      if (handler != null)
153        handler(this, EventArgs.Empty);
154    }
155    #endregion
156  }
157}
Note: See TracBrowser for help on using the repository browser.