source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Optimization/3.3/Problems/Problem.cs @ 16892

Last change on this file since 16892 was 16892, checked in by gkronber, 4 months ago

#2925 merged r16661:16890 from trunk to branch

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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    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(StorableConstructorFlag _) : base(_) { }
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>()) { GetsCollected = 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)) { GetsCollected = 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(OldName = "Operators")]
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>()) { GetsCollected = 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>()) { GetsCollected = false });
110          OperatorsParameter.Hidden = true;
111        }
112        #endregion
113        return OperatorsParameter.Value;
114      }
115    }
116    IEnumerable<IItem> IProblem.Operators { get { return GetOperators(); } }
117
118    protected virtual IEnumerable<IItem> GetOperators() {
119      return Operators;
120    }
121
122    public virtual IEnumerable<IParameterizedItem> ExecutionContextItems {
123      get { yield return this; }
124    }
125    #endregion
126
127    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
128      var children = base.GetCollectedValues(param);
129      foreach (var child in children) {
130        if (child.Value is IOperator)
131          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
132        else yield return child;
133      }
134    }
135
136    #region events
137    private void Operators_Changed(object sender, EventArgs e) {
138      OnOperatorsChanged();
139    }
140    public event EventHandler OperatorsChanged;
141    protected virtual void OnOperatorsChanged() {
142      EventHandler handler = OperatorsChanged;
143      if (handler != null)
144        handler(this, EventArgs.Empty);
145    }
146
147    public event EventHandler Reset;
148    protected virtual void OnReset() {
149      EventHandler handler = Reset;
150      if (handler != null)
151        handler(this, EventArgs.Empty);
152    }
153    #endregion
154  }
155}
Note: See TracBrowser for help on using the repository browser.