1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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 static new Image StaticItemImage {
|
---|
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 | }
|
---|