1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
26 | using HeuristicLab.Collections;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization {
|
---|
33 | [StorableType("C213CE21-A970-4886-BC4C-9790B9897738")]
|
---|
34 | public abstract class Problem : ParameterizedNamedItem, IProblem {
|
---|
35 | public string Filename { get; set; }
|
---|
36 |
|
---|
37 | public static new Image StaticItemImage {
|
---|
38 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | public ResultCollection Results { get; private set; } = new ResultCollection();
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | protected Problem(StorableConstructorFlag _) : base(_) { }
|
---|
46 | protected Problem(Problem original, Cloner cloner) : base(original, cloner) {
|
---|
47 | Results = cloner.Clone(original.Results);
|
---|
48 | }
|
---|
49 | public Problem() : base() { }
|
---|
50 |
|
---|
51 | protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
|
---|
52 | var children = base.GetCollectedValues(param);
|
---|
53 | foreach (var child in children) {
|
---|
54 | if (child.Value is IOperator)
|
---|
55 | yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
|
---|
56 | else yield return child;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public event EventHandler Reset;
|
---|
61 | protected virtual void OnReset() {
|
---|
62 | EventHandler handler = Reset;
|
---|
63 | if (handler != null)
|
---|
64 | handler(this, EventArgs.Empty);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | [Item("Problem", "Represents the base class for a problem.")]
|
---|
71 | [StorableType("6DC97432-9BD1-4304-802A-1FC48A0E0468")]
|
---|
72 | public abstract class EncodedProblem : Problem, IEncodedProblem {
|
---|
73 |
|
---|
74 | private const string OperatorsParameterName = "Operators";
|
---|
75 | public IFixedValueParameter<ItemCollection<IItem>> OperatorsParameter {
|
---|
76 | get { return (IFixedValueParameter<ItemCollection<IItem>>)Parameters[OperatorsParameterName]; }
|
---|
77 | }
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | protected EncodedProblem(StorableConstructorFlag _) : base(_) { }
|
---|
81 | protected EncodedProblem(EncodedProblem original, Cloner cloner)
|
---|
82 | : base(original, cloner) {
|
---|
83 | RegisterEventHandlers();
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected EncodedProblem()
|
---|
87 | : base() {
|
---|
88 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
|
---|
89 | OperatorsParameter.Hidden = true;
|
---|
90 | RegisterEventHandlers();
|
---|
91 | }
|
---|
92 |
|
---|
93 | [StorableHook(HookType.AfterDeserialization)]
|
---|
94 | private void AfterDeserialization() {
|
---|
95 | // BackwardsCompatibility3.3
|
---|
96 | #region Backwards compatible code, remove with 3.4
|
---|
97 | IParameter operatorsParam;
|
---|
98 | if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
|
---|
99 | var operators = operatorsParam.ActualValue as OperatorCollection;
|
---|
100 | if (operators != null) {
|
---|
101 | Parameters.Remove(OperatorsParameterName);
|
---|
102 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(operators)) { GetsCollected = false });
|
---|
103 | OperatorsParameter.Hidden = true;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | #endregion
|
---|
107 |
|
---|
108 | RegisterEventHandlers();
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void RegisterEventHandlers() {
|
---|
112 | Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
113 | Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
114 | Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
115 | }
|
---|
116 |
|
---|
117 | #region properties
|
---|
118 | // BackwardsCompatibility3.3
|
---|
119 | #region Backwards compatible code, remove with 3.4
|
---|
120 | [Storable(OldName = "Operators")]
|
---|
121 | private IEnumerable<IOperator> StorableOperators {
|
---|
122 | set {
|
---|
123 | IParameter operatorsParam;
|
---|
124 | if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
|
---|
125 | var items = operatorsParam.ActualValue as ItemCollection<IItem>;
|
---|
126 | if (items == null) Parameters.Remove(operatorsParam);
|
---|
127 | }
|
---|
128 |
|
---|
129 | //necessary to convert old experiments files where no parameter was used for saving the operators
|
---|
130 | if (!Parameters.ContainsKey(OperatorsParameterName)) {
|
---|
131 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
|
---|
132 | OperatorsParameter.Hidden = true;
|
---|
133 | }
|
---|
134 | if (value != null) Operators.AddRange(value);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | #endregion
|
---|
138 | protected ItemCollection<IItem> Operators {
|
---|
139 | get {
|
---|
140 | // BackwardsCompatibility3.3
|
---|
141 | #region Backwards compatible code, remove with 3.4
|
---|
142 | if (!Parameters.ContainsKey(OperatorsParameterName)) {
|
---|
143 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
|
---|
144 | OperatorsParameter.Hidden = true;
|
---|
145 | }
|
---|
146 | #endregion
|
---|
147 | return OperatorsParameter.Value;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | IEnumerable<IItem> IEncodedProblem.Operators { get { return GetOperators(); } }
|
---|
151 |
|
---|
152 | protected virtual IEnumerable<IItem> GetOperators() {
|
---|
153 | return Operators;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public virtual IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
157 | get { yield return this; }
|
---|
158 | }
|
---|
159 | #endregion
|
---|
160 |
|
---|
161 |
|
---|
162 | protected virtual void ParameterizeOperators() { }
|
---|
163 |
|
---|
164 |
|
---|
165 | #region events
|
---|
166 | private void Operators_Changed(object sender, EventArgs e) {
|
---|
167 | OnOperatorsChanged();
|
---|
168 | ParameterizeOperators();
|
---|
169 | }
|
---|
170 | public event EventHandler OperatorsChanged;
|
---|
171 | protected virtual void OnOperatorsChanged() {
|
---|
172 | EventHandler handler = OperatorsChanged;
|
---|
173 | if (handler != null)
|
---|
174 | handler(this, EventArgs.Empty);
|
---|
175 | }
|
---|
176 | #endregion
|
---|
177 | }
|
---|
178 | }
|
---|