1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 System.IO;
|
---|
26 | using HeuristicLab.Collections;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Persistence.Default.Xml;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Clients.OKB.RunCreation {
|
---|
34 | [Item("OKB Algorithm", "An algorithm which is stored in the OKB.")]
|
---|
35 | [Creatable("Optimization Knowledge Base (OKB)")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class OKBAlgorithm : Item, IAlgorithm, IStorableContent {
|
---|
38 | public string Filename { get; set; }
|
---|
39 |
|
---|
40 | private long algorithmId;
|
---|
41 | public long AlgorithmId {
|
---|
42 | get { return algorithmId; }
|
---|
43 | }
|
---|
44 | private IAlgorithm algorithm;
|
---|
45 | public IAlgorithm Algorithm {
|
---|
46 | get { return algorithm; }
|
---|
47 | private set {
|
---|
48 | if (value == null) throw new ArgumentNullException("Algorithm", "Algorithm cannot be null.");
|
---|
49 | if (value != algorithm) {
|
---|
50 | CancelEventArgs<string> e = new CancelEventArgs<string>(value.Name);
|
---|
51 | OnNameChanging(e);
|
---|
52 | if (!e.Cancel) {
|
---|
53 | IProblem problem = algorithm.Problem;
|
---|
54 | DeregisterAlgorithmEvents();
|
---|
55 | algorithm = value;
|
---|
56 | RegisterAlgorithmEvents();
|
---|
57 |
|
---|
58 | OnToStringChanged();
|
---|
59 | OnItemImageChanged();
|
---|
60 | OnNameChanged();
|
---|
61 | OnDescriptionChanged();
|
---|
62 | OnAlgorithmChanged();
|
---|
63 | OnStoreAlgorithmInEachRunChanged();
|
---|
64 |
|
---|
65 | try {
|
---|
66 | algorithm.Problem = problem;
|
---|
67 | }
|
---|
68 | catch (ArgumentException) {
|
---|
69 | algorithm.Problem = null;
|
---|
70 | }
|
---|
71 | algorithm.Prepare(true);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public IEnumerable<IOptimizer> NestedOptimizers {
|
---|
78 | get {
|
---|
79 | if (Algorithm == null) yield break;
|
---|
80 | yield return Algorithm;
|
---|
81 | foreach (IOptimizer optimizer in Algorithm.NestedOptimizers)
|
---|
82 | yield return optimizer;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override Image ItemImage {
|
---|
87 | get { return Algorithm.ItemImage; }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public string Name {
|
---|
91 | get { return Algorithm.Name; }
|
---|
92 | set { throw new NotSupportedException("Name cannot be changed."); }
|
---|
93 | }
|
---|
94 | public string Description {
|
---|
95 | get { return Algorithm.Description; }
|
---|
96 | set { throw new NotSupportedException("Description cannot be changed."); }
|
---|
97 | }
|
---|
98 | public bool CanChangeName {
|
---|
99 | get { return false; }
|
---|
100 | }
|
---|
101 | public bool CanChangeDescription {
|
---|
102 | get { return false; }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public IKeyedItemCollection<string, IParameter> Parameters {
|
---|
106 | get { return Algorithm.Parameters; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public ExecutionState ExecutionState {
|
---|
110 | get { return Algorithm.ExecutionState; }
|
---|
111 | }
|
---|
112 | public TimeSpan ExecutionTime {
|
---|
113 | get { return Algorithm.ExecutionTime; }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public Type ProblemType {
|
---|
117 | get { return Algorithm.ProblemType; }
|
---|
118 | }
|
---|
119 | public IProblem Problem {
|
---|
120 | get { return Algorithm.Problem; }
|
---|
121 | set { Algorithm.Problem = value; }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public ResultCollection Results {
|
---|
125 | get { return Algorithm.Results; }
|
---|
126 | }
|
---|
127 |
|
---|
128 | private int runsCounter;
|
---|
129 | private RunCollection runs;
|
---|
130 | public RunCollection Runs {
|
---|
131 | get { return runs; }
|
---|
132 | }
|
---|
133 | public bool StoreAlgorithmInEachRun {
|
---|
134 | get { return Algorithm.StoreAlgorithmInEachRun; }
|
---|
135 | set { Algorithm.StoreAlgorithmInEachRun = value; }
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 | #region Persistence Properties
|
---|
141 | [Storable(Name = "AlgorithmId")]
|
---|
142 | private long StorableAlgorithmId {
|
---|
143 | get { return algorithmId; }
|
---|
144 | set { algorithmId = value; }
|
---|
145 | }
|
---|
146 | [Storable(Name = "Algorithm")]
|
---|
147 | private IAlgorithm StorableAlgorithm {
|
---|
148 | get { return algorithm; }
|
---|
149 | set {
|
---|
150 | algorithm = value;
|
---|
151 | RegisterAlgorithmEvents();
|
---|
152 | }
|
---|
153 | }
|
---|
154 | [Storable(Name = "RunsCounter")]
|
---|
155 | private int StorableRunsCounter {
|
---|
156 | get { return runsCounter; }
|
---|
157 | set { runsCounter = value; }
|
---|
158 | }
|
---|
159 | [Storable(Name = "Runs")]
|
---|
160 | private RunCollection StorableRuns {
|
---|
161 | get { return runs; }
|
---|
162 | set {
|
---|
163 | runs = value;
|
---|
164 | RegisterRunsEvents();
|
---|
165 | }
|
---|
166 | }
|
---|
167 | #endregion
|
---|
168 |
|
---|
169 | [StorableConstructor]
|
---|
170 | private OKBAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
171 | private OKBAlgorithm(OKBAlgorithm original, Cloner cloner)
|
---|
172 | : base(original, cloner) {
|
---|
173 | algorithmId = original.algorithmId;
|
---|
174 | algorithm = cloner.Clone(original.algorithm);
|
---|
175 | RegisterAlgorithmEvents();
|
---|
176 | runsCounter = original.runsCounter;
|
---|
177 | runs = cloner.Clone(original.runs);
|
---|
178 | RegisterRunsEvents();
|
---|
179 | }
|
---|
180 | public OKBAlgorithm()
|
---|
181 | : base() {
|
---|
182 | algorithmId = -1;
|
---|
183 | algorithm = new EmptyAlgorithm("No algorithm selected. Please choose an algorithm from the OKB.");
|
---|
184 | RegisterAlgorithmEvents();
|
---|
185 | runsCounter = 0;
|
---|
186 | runs = new RunCollection();
|
---|
187 | RegisterRunsEvents();
|
---|
188 | }
|
---|
189 |
|
---|
190 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
191 | return new OKBAlgorithm(this, cloner);
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void Load(long algorithmId) {
|
---|
195 | if (this.algorithmId != algorithmId) {
|
---|
196 | IAlgorithm algorithm;
|
---|
197 | byte[] algorithmData = RunCreationClient.GetAlgorithmData(algorithmId);
|
---|
198 | using (MemoryStream stream = new MemoryStream(algorithmData)) {
|
---|
199 | algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
|
---|
200 | }
|
---|
201 | this.algorithmId = algorithmId;
|
---|
202 | Algorithm = algorithm;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | public void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
207 | Algorithm.CollectParameterValues(values);
|
---|
208 | }
|
---|
209 | public void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
210 | Algorithm.CollectResultValues(values);
|
---|
211 | }
|
---|
212 |
|
---|
213 | public void Prepare() {
|
---|
214 | Algorithm.Prepare(true);
|
---|
215 | }
|
---|
216 | public void Prepare(bool clearRuns) {
|
---|
217 | if (clearRuns) runs.Clear();
|
---|
218 | Algorithm.Prepare(true);
|
---|
219 | }
|
---|
220 | public void Start() {
|
---|
221 | Algorithm.Start();
|
---|
222 | }
|
---|
223 | public void Pause() {
|
---|
224 | Algorithm.Pause();
|
---|
225 | }
|
---|
226 | public void Stop() {
|
---|
227 | Algorithm.Stop();
|
---|
228 | }
|
---|
229 |
|
---|
230 | #region Events
|
---|
231 | public event EventHandler AlgorithmChanged;
|
---|
232 | private void OnAlgorithmChanged() {
|
---|
233 | EventHandler handler = AlgorithmChanged;
|
---|
234 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
235 | }
|
---|
236 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
237 | private void OnNameChanging(CancelEventArgs<string> e) {
|
---|
238 | var handler = NameChanging;
|
---|
239 | if (handler != null) handler(this, e);
|
---|
240 | }
|
---|
241 | public event EventHandler NameChanged;
|
---|
242 | private void OnNameChanged() {
|
---|
243 | var handler = NameChanged;
|
---|
244 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
245 | }
|
---|
246 | public event EventHandler DescriptionChanged;
|
---|
247 | private void OnDescriptionChanged() {
|
---|
248 | var handler = DescriptionChanged;
|
---|
249 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
250 | }
|
---|
251 | public event EventHandler ExecutionStateChanged;
|
---|
252 | private void OnExecutionStateChanged() {
|
---|
253 | var handler = ExecutionStateChanged;
|
---|
254 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
255 | }
|
---|
256 | public event EventHandler ExecutionTimeChanged;
|
---|
257 | private void OnExecutionTimeChanged() {
|
---|
258 | var handler = ExecutionTimeChanged;
|
---|
259 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
260 | }
|
---|
261 | public event EventHandler ProblemChanged;
|
---|
262 | private void OnProblemChanged() {
|
---|
263 | var handler = ProblemChanged;
|
---|
264 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
265 | }
|
---|
266 | public event EventHandler StoreAlgorithmInEachRunChanged;
|
---|
267 | private void OnStoreAlgorithmInEachRunChanged() {
|
---|
268 | var handler = StoreAlgorithmInEachRunChanged;
|
---|
269 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
270 | }
|
---|
271 | public event EventHandler Prepared;
|
---|
272 | private void OnPrepared() {
|
---|
273 | var handler = Prepared;
|
---|
274 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
275 | }
|
---|
276 | public event EventHandler Started;
|
---|
277 | private void OnStarted() {
|
---|
278 | var handler = Started;
|
---|
279 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
280 | }
|
---|
281 | public event EventHandler Paused;
|
---|
282 | private void OnPaused() {
|
---|
283 | var handler = Paused;
|
---|
284 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
285 | }
|
---|
286 | public event EventHandler Stopped;
|
---|
287 | private void OnStopped() {
|
---|
288 | runsCounter++;
|
---|
289 | runs.Add(new HeuristicLab.Optimization.Run(string.Format("{0} Run {1}", Name, runsCounter), this));
|
---|
290 | var handler = Stopped;
|
---|
291 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
292 | }
|
---|
293 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
294 | private void OnExceptionOccurred(Exception exception) {
|
---|
295 | var handler = ExceptionOccurred;
|
---|
296 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
297 | }
|
---|
298 |
|
---|
299 | private void RegisterAlgorithmEvents() {
|
---|
300 | if (Algorithm != null) {
|
---|
301 | Algorithm.ToStringChanged += new EventHandler(Algorithm_ToStringChanged);
|
---|
302 | Algorithm.ItemImageChanged += new EventHandler(Algorithm_ItemImageChanged);
|
---|
303 | Algorithm.NameChanging += new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
|
---|
304 | Algorithm.NameChanged += new EventHandler(Algorithm_NameChanged);
|
---|
305 | Algorithm.DescriptionChanged += new EventHandler(Algorithm_DescriptionChanged);
|
---|
306 | Algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
|
---|
307 | Algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
|
---|
308 | Algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
|
---|
309 | Algorithm.StoreAlgorithmInEachRunChanged += new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
|
---|
310 | Algorithm.Prepared += new EventHandler(Algorithm_Prepared);
|
---|
311 | Algorithm.Started += new EventHandler(Algorithm_Started);
|
---|
312 | Algorithm.Paused += new EventHandler(Algorithm_Paused);
|
---|
313 | Algorithm.Stopped += new EventHandler(Algorithm_Stopped);
|
---|
314 | Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | private void DeregisterAlgorithmEvents() {
|
---|
318 | if (Algorithm != null) {
|
---|
319 | Algorithm.ToStringChanged -= new EventHandler(Algorithm_ToStringChanged);
|
---|
320 | Algorithm.ItemImageChanged -= new EventHandler(Algorithm_ItemImageChanged);
|
---|
321 | Algorithm.NameChanging -= new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
|
---|
322 | Algorithm.NameChanged -= new EventHandler(Algorithm_NameChanged);
|
---|
323 | Algorithm.DescriptionChanged -= new EventHandler(Algorithm_DescriptionChanged);
|
---|
324 | Algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
|
---|
325 | Algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
|
---|
326 | Algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
|
---|
327 | Algorithm.StoreAlgorithmInEachRunChanged -= new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
|
---|
328 | Algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
|
---|
329 | Algorithm.Started -= new EventHandler(Algorithm_Started);
|
---|
330 | Algorithm.Paused -= new EventHandler(Algorithm_Paused);
|
---|
331 | Algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
|
---|
332 | Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | private void Algorithm_ToStringChanged(object sender, EventArgs e) {
|
---|
337 | OnToStringChanged();
|
---|
338 | }
|
---|
339 | private void Algorithm_ItemImageChanged(object sender, EventArgs e) {
|
---|
340 | OnItemImageChanged();
|
---|
341 | }
|
---|
342 | private void Algorithm_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
343 | OnNameChanging(e);
|
---|
344 | }
|
---|
345 | private void Algorithm_NameChanged(object sender, EventArgs e) {
|
---|
346 | OnNameChanged();
|
---|
347 | }
|
---|
348 | private void Algorithm_DescriptionChanged(object sender, EventArgs e) {
|
---|
349 | OnDescriptionChanged();
|
---|
350 | }
|
---|
351 | private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
352 | OnExecutionStateChanged();
|
---|
353 | }
|
---|
354 | private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
355 | OnExecutionTimeChanged();
|
---|
356 | }
|
---|
357 | private void Algorithm_ProblemChanged(object sender, EventArgs e) {
|
---|
358 | OnProblemChanged();
|
---|
359 | }
|
---|
360 | private void Algorithm_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
|
---|
361 | OnStoreAlgorithmInEachRunChanged();
|
---|
362 | }
|
---|
363 | private void Algorithm_Prepared(object sender, EventArgs e) {
|
---|
364 | OnPrepared();
|
---|
365 | }
|
---|
366 | private void Algorithm_Started(object sender, EventArgs e) {
|
---|
367 | OnStarted();
|
---|
368 | }
|
---|
369 | private void Algorithm_Paused(object sender, EventArgs e) {
|
---|
370 | OnPaused();
|
---|
371 | }
|
---|
372 | private void Algorithm_Stopped(object sender, EventArgs e) {
|
---|
373 | OnStopped();
|
---|
374 | }
|
---|
375 | private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
376 | OnExceptionOccurred(e.Value);
|
---|
377 | }
|
---|
378 |
|
---|
379 | private void RegisterRunsEvents() {
|
---|
380 | runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
381 | }
|
---|
382 | private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
383 | runsCounter = runs.Count;
|
---|
384 | }
|
---|
385 | #endregion
|
---|
386 | }
|
---|
387 | }
|
---|