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