Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7887 was 7706, checked in by abeham, 13 years ago

#1695: fixed bug in parameter collection

File size: 4.9 KB
RevLine 
[4323]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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 {
[6938]36    private static readonly string OperatorsParameterName = "Operators";
37
38    public IFixedValueParameter<OperatorCollection> OperatorsParameter {
39      get { return (IFixedValueParameter<OperatorCollection>)Parameters[OperatorsParameterName]; }
40    }
41
[7201]42    public static new Image StaticItemImage {
[5287]43      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
[4323]44    }
45
46    [StorableConstructor]
[7431]47    protected Problem(bool deserializing) : base(deserializing) { }
[5809]48    protected Problem(Problem original, Cloner cloner)
[4722]49      : base(original, cloner) {
50      RegisterEventHandlers();
51    }
52
[4323]53    protected Problem()
54      : base() {
[7431]55      Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(), false));
[6938]56      OperatorsParameter.Hidden = true;
[4563]57      RegisterEventHandlers();
[4323]58    }
59
60    [StorableHook(HookType.AfterDeserialization)]
61    private void AfterDeserialization() {
[4563]62      RegisterEventHandlers();
[4323]63    }
64
[4563]65    private void RegisterEventHandlers() {
[4323]66      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
67      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
68      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
69    }
70
71    #region properties
[7431]72    // BackwardsCompatibility3.3
73    #region Backwards compatible code, remove with 3.4
[4563]74    [Storable(Name = "Operators")]
[4323]75    private IEnumerable<IOperator> StorableOperators {
[7431]76      get { return null; }
77      set {
78        if (!Parameters.ContainsKey(OperatorsParameterName)) {
79          Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(value), false));
80          OperatorsParameter.Hidden = true;
81        }
82      }
[4323]83    }
[7431]84    #endregion
[4564]85    protected OperatorCollection Operators {
[7431]86      get {
87        // BackwardsCompatibility3.3
88        #region Backwards compatible code, remove with 3.4
89        if (!Parameters.ContainsKey(OperatorsParameterName)) {
90          Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(), false));
91          OperatorsParameter.Hidden = true;
92        }
93        #endregion
94        return OperatorsParameter.Value;
95      }
[4323]96    }
[7431]97    IEnumerable<IOperator> IProblem.Operators { get { return Operators; } }
[4323]98    #endregion
99
[7706]100    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
101      var children = base.GetCollectedValues(param);
[7579]102      foreach (var child in children) {
103        if (child.Value is IOperator)
104          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
105        else yield return child;
106      }
107    }
108
[4323]109    #region events
110    private void Operators_Changed(object sender, EventArgs e) {
111      OnOperatorsChanged();
112    }
113    public event EventHandler OperatorsChanged;
114    protected virtual void OnOperatorsChanged() {
115      EventHandler handler = OperatorsChanged;
116      if (handler != null)
117        handler(this, EventArgs.Empty);
118    }
119
120    public event EventHandler Reset;
121    protected virtual void OnReset() {
122      EventHandler handler = Reset;
123      if (handler != null)
124        handler(this, EventArgs.Empty);
125    }
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.