Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7160 was 6938, checked in by abeham, 13 years ago

#1663

  • Added an *Operators* parameter in Problem
  • Adapted KnapsackProblem, OnemaxProblem, SingleObjectiveTestFunctionProblem, TravelingSalesmanProblem, and VehicleRoutingProblem to derive from SingleObjectiveHeuristicOptimizationProblem<U, V>, renamed and marked old list as obsolete. Note that QuadraticAssignmentProblem, as well as the new DataAnalysisProblems already derived from that class. ArtificialAntProblem was not yet modified.
  • Added some test lists and adapted SamplesTest
  • Added plugin dependencies that the unit test was complaining about
File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  [Item("Problem", "Represents the base class for a problem.")]
33  [StorableClass]
34  public abstract class Problem : ParameterizedNamedItem, IProblem {
35    private static readonly string OperatorsParameterName = "Operators";
36
37    public IFixedValueParameter<OperatorCollection> OperatorsParameter {
38      get { return (IFixedValueParameter<OperatorCollection>)Parameters[OperatorsParameterName]; }
39    }
40
41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
43    }
44
45    [StorableConstructor]
46    protected Problem(bool deserializing)
47      : base(deserializing) {
48      operators = new OperatorCollection(); // operators must never be null
49    }
50    protected Problem(Problem original, Cloner cloner)
51      : base(original, cloner) {
52      operators = cloner.Clone(original.operators);
53      RegisterEventHandlers();
54    }
55
56    protected Problem()
57      : base() {
58      operators = new OperatorCollection();
59      Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", operators, false));
60      OperatorsParameter.Hidden = true;
61      RegisterEventHandlers();
62    }
63
64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() {
66      // BackwardsCompatibility3.3
67      #region Backwards compatible code, remove with 3.4
68      if (!Parameters.ContainsKey(OperatorsParameterName)) {
69        Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", operators, false));
70        OperatorsParameter.Hidden = true;
71      }
72      #endregion
73      RegisterEventHandlers();
74    }
75
76    private void RegisterEventHandlers() {
77      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
78      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
79      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
80    }
81
82    #region properties
83    private OperatorCollection operators;
84    [Storable(Name = "Operators")]
85    private IEnumerable<IOperator> StorableOperators {
86      get { return operators; }
87      set { operators = new OperatorCollection(value); }
88    }
89    protected OperatorCollection Operators {
90      get { return this.operators; }
91    }
92    IEnumerable<IOperator> IProblem.Operators { get { return operators; } }
93    #endregion
94
95    #region events
96    private void Operators_Changed(object sender, EventArgs e) {
97      OnOperatorsChanged();
98    }
99    public event EventHandler OperatorsChanged;
100    protected virtual void OnOperatorsChanged() {
101      EventHandler handler = OperatorsChanged;
102      if (handler != null)
103        handler(this, EventArgs.Empty);
104    }
105
106    public event EventHandler Reset;
107    protected virtual void OnReset() {
108      EventHandler handler = Reset;
109      if (handler != null)
110        handler(this, EventArgs.Empty);
111    }
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.