Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16946 was 16946, checked in by mkommend, 5 years ago

#2521: Merged trunk changes up to r16945 into branch.

File size: 6.2 KB
RevLine 
[4323]1#region License Information
2/* HeuristicLab
[16723]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4323]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;
[16946]25using HEAL.Attic;
[4323]26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
[7579]29using HeuristicLab.Data;
[6938]30using HeuristicLab.Parameters;
[4323]31
[4564]32namespace HeuristicLab.Optimization {
[4596]33  [Item("Problem", "Represents the base class for a problem.")]
[16723]34  [StorableType("6DC97432-9BD1-4304-802A-1FC48A0E0468")]
[5809]35  public abstract class Problem : ParameterizedNamedItem, IProblem {
[8004]36    private const string OperatorsParameterName = "Operators";
[7999]37    public IFixedValueParameter<ItemCollection<IItem>> OperatorsParameter {
38      get { return (IFixedValueParameter<ItemCollection<IItem>>)Parameters[OperatorsParameterName]; }
[6938]39    }
40
[7201]41    public static new Image StaticItemImage {
[5287]42      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
[4323]43    }
44
45    [StorableConstructor]
[16723]46    protected Problem(StorableConstructorFlag _) : base(_) { }
[5809]47    protected Problem(Problem original, Cloner cloner)
[4722]48      : base(original, cloner) {
49      RegisterEventHandlers();
50    }
51
[4323]52    protected Problem()
53      : base() {
[16946]54      Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
[6938]55      OperatorsParameter.Hidden = true;
[4563]56      RegisterEventHandlers();
[4323]57    }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
[7999]61      // BackwardsCompatibility3.3
62      #region Backwards compatible code, remove with 3.4
[8004]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);
[16946]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 });
[8004]69          OperatorsParameter.Hidden = true;
70        }
[7999]71      }
72      #endregion
73
[4563]74      RegisterEventHandlers();
[4323]75    }
76
[4563]77    private void RegisterEventHandlers() {
[7999]78      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
79      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
80      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
[4323]81    }
82
83    #region properties
[7431]84    // BackwardsCompatibility3.3
85    #region Backwards compatible code, remove with 3.4
[16801]86    [Storable(OldName = "Operators")]
[4323]87    private IEnumerable<IOperator> StorableOperators {
[7431]88      set {
[8004]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
[7431]96        if (!Parameters.ContainsKey(OperatorsParameterName)) {
[16946]97          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
[7431]98          OperatorsParameter.Hidden = true;
99        }
[8004]100        if (value != null) Operators.AddRange(value);
[7431]101      }
[4323]102    }
[7431]103    #endregion
[7999]104    protected ItemCollection<IItem> Operators {
[7431]105      get {
106        // BackwardsCompatibility3.3
107        #region Backwards compatible code, remove with 3.4
108        if (!Parameters.ContainsKey(OperatorsParameterName)) {
[16946]109          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
[7431]110          OperatorsParameter.Hidden = true;
111        }
112        #endregion
113        return OperatorsParameter.Value;
114      }
[4323]115    }
[11961]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    }
[4323]125    #endregion
126
[7706]127    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
128      var children = base.GetCollectedValues(param);
[7579]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
[4323]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.