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