Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14759 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 6.2 KB
RevLine 
[4323]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
[7579]28using HeuristicLab.Data;
[6938]29using HeuristicLab.Parameters;
[4323]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[4564]32namespace HeuristicLab.Optimization {
[4596]33  [Item("Problem", "Represents the base class for a problem.")]
[4323]34  [StorableClass]
[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]
[7431]46    protected Problem(bool deserializing) : base(deserializing) { }
[5809]47    protected Problem(Problem original, Cloner cloner)
[4722]48      : base(original, cloner) {
49      RegisterEventHandlers();
50    }
51
[4323]52    protected Problem()
53      : base() {
[7999]54      Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), 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);
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        }
[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
[7999]86    [Storable(Name = "Operators", AllowOneWay = true)]
[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)) {
[8004]97          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), 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)) {
[7999]109          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), 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.