1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization {
|
---|
33 | [Item("Problem", "Represents the base class for a problem.")]
|
---|
34 | [StorableClass]
|
---|
35 | public abstract class Problem : ParameterizedNamedItem, IProblem {
|
---|
36 | private const string OperatorsParameterName = "Operators";
|
---|
37 | public IFixedValueParameter<ItemCollection<IItem>> OperatorsParameter
|
---|
38 | {
|
---|
39 | get { return (IFixedValueParameter<ItemCollection<IItem>>)Parameters[OperatorsParameterName]; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static new Image StaticItemImage
|
---|
43 | {
|
---|
44 | get { return new Bitmap(25, 25); }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | protected Problem(bool deserializing) : base(deserializing) { }
|
---|
49 | protected Problem(Problem original, Cloner cloner)
|
---|
50 | : base(original, cloner) {
|
---|
51 | RegisterEventHandlers();
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected Problem()
|
---|
55 | : base() {
|
---|
56 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
|
---|
57 | OperatorsParameter.Hidden = true;
|
---|
58 | RegisterEventHandlers();
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableHook(HookType.AfterDeserialization)]
|
---|
62 | private void AfterDeserialization() {
|
---|
63 | // BackwardsCompatibility3.3
|
---|
64 | #region Backwards compatible code, remove with 3.4
|
---|
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 | }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | RegisterEventHandlers();
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void RegisterEventHandlers() {
|
---|
80 | Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
81 | Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
82 | Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
83 | }
|
---|
84 |
|
---|
85 | #region properties
|
---|
86 | // BackwardsCompatibility3.3
|
---|
87 | #region Backwards compatible code, remove with 3.4
|
---|
88 | [Storable(Name = "Operators", AllowOneWay = true)]
|
---|
89 | private IEnumerable<IOperator> StorableOperators
|
---|
90 | {
|
---|
91 | set
|
---|
92 | {
|
---|
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
|
---|
100 | if (!Parameters.ContainsKey(OperatorsParameterName)) {
|
---|
101 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
|
---|
102 | OperatorsParameter.Hidden = true;
|
---|
103 | }
|
---|
104 | if (value != null) Operators.AddRange(value);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | #endregion
|
---|
108 | protected ItemCollection<IItem> Operators
|
---|
109 | {
|
---|
110 | get
|
---|
111 | {
|
---|
112 | // BackwardsCompatibility3.3
|
---|
113 | #region Backwards compatible code, remove with 3.4
|
---|
114 | if (!Parameters.ContainsKey(OperatorsParameterName)) {
|
---|
115 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
|
---|
116 | OperatorsParameter.Hidden = true;
|
---|
117 | }
|
---|
118 | #endregion
|
---|
119 | return OperatorsParameter.Value;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | IEnumerable<IItem> IProblem.Operators { get { return GetOperators(); } }
|
---|
123 |
|
---|
124 | protected virtual IEnumerable<IItem> GetOperators() {
|
---|
125 | return Operators;
|
---|
126 | }
|
---|
127 |
|
---|
128 | public virtual IEnumerable<IParameterizedItem> ExecutionContextItems
|
---|
129 | {
|
---|
130 | get { yield return this; }
|
---|
131 | }
|
---|
132 | #endregion
|
---|
133 |
|
---|
134 | protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
|
---|
135 | var children = base.GetCollectedValues(param);
|
---|
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 |
|
---|
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 | }
|
---|