Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Optimization/3.3/Problems/Problem.cs @ 15401

Last change on this file since 15401 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

File size: 6.2 KB
RevLine 
[4323]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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";
[13656]37    public IFixedValueParameter<ItemCollection<IItem>> OperatorsParameter
38    {
[7999]39      get { return (IFixedValueParameter<ItemCollection<IItem>>)Parameters[OperatorsParameterName]; }
[6938]40    }
41
[13656]42    public static new Image StaticItemImage
43    {
44      get { return new Bitmap(25, 25); }
[4323]45    }
46
47    [StorableConstructor]
[7431]48    protected Problem(bool deserializing) : base(deserializing) { }
[5809]49    protected Problem(Problem original, Cloner cloner)
[4722]50      : base(original, cloner) {
51      RegisterEventHandlers();
52    }
53
[4323]54    protected Problem()
55      : base() {
[7999]56      Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
[6938]57      OperatorsParameter.Hidden = true;
[4563]58      RegisterEventHandlers();
[4323]59    }
60
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
[7999]63      // BackwardsCompatibility3.3
64      #region Backwards compatible code, remove with 3.4
[8004]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), false));
71          OperatorsParameter.Hidden = true;
72        }
[7999]73      }
74      #endregion
75
[4563]76      RegisterEventHandlers();
[4323]77    }
78
[4563]79    private void RegisterEventHandlers() {
[7999]80      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
81      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
82      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
[4323]83    }
84
85    #region properties
[7431]86    // BackwardsCompatibility3.3
87    #region Backwards compatible code, remove with 3.4
[7999]88    [Storable(Name = "Operators", AllowOneWay = true)]
[13656]89    private IEnumerable<IOperator> StorableOperators
90    {
91      set
92      {
[8004]93        IParameter operatorsParam;
94        if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
95          var items = operatorsParam.ActualValue as ItemCollection<IItem>;
96          if (items == null) Parameters.Remove(operatorsParam);
97        }
98
99        //necessary to convert old experiments files where no parameter was used for saving the operators
[7431]100        if (!Parameters.ContainsKey(OperatorsParameterName)) {
[8004]101          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
[7431]102          OperatorsParameter.Hidden = true;
103        }
[8004]104        if (value != null) Operators.AddRange(value);
[7431]105      }
[4323]106    }
[7431]107    #endregion
[13656]108    protected ItemCollection<IItem> Operators
109    {
110      get
111      {
[7431]112        // BackwardsCompatibility3.3
113        #region Backwards compatible code, remove with 3.4
114        if (!Parameters.ContainsKey(OperatorsParameterName)) {
[7999]115          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
[7431]116          OperatorsParameter.Hidden = true;
117        }
118        #endregion
119        return OperatorsParameter.Value;
120      }
[4323]121    }
[11961]122    IEnumerable<IItem> IProblem.Operators { get { return GetOperators(); } }
123
124    protected virtual IEnumerable<IItem> GetOperators() {
125      return Operators;
126    }
127
[13656]128    public virtual IEnumerable<IParameterizedItem> ExecutionContextItems
129    {
[11961]130      get { yield return this; }
131    }
[4323]132    #endregion
133
[7706]134    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
135      var children = base.GetCollectedValues(param);
[7579]136      foreach (var child in children) {
137        if (child.Value is IOperator)
138          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
139        else yield return child;
140      }
141    }
142
[4323]143    #region events
144    private void Operators_Changed(object sender, EventArgs e) {
145      OnOperatorsChanged();
146    }
147    public event EventHandler OperatorsChanged;
148    protected virtual void OnOperatorsChanged() {
149      EventHandler handler = OperatorsChanged;
150      if (handler != null)
151        handler(this, EventArgs.Empty);
152    }
153
154    public event EventHandler Reset;
155    protected virtual void OnReset() {
156      EventHandler handler = Reset;
157      if (handler != null)
158        handler(this, EventArgs.Empty);
159    }
160    #endregion
161  }
162}
Note: See TracBrowser for help on using the repository browser.