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.Drawing;
|
---|
24 | using System.IO;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Clients.OKB {
|
---|
32 | [Item("OKB Experiment", "...")]
|
---|
33 | [Creatable("Optimization Knowledge Base (OKB)")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class OKBExperiment : NamedItem, IOptimizer, IStorableContent {
|
---|
36 | public string Filename { get; set; }
|
---|
37 |
|
---|
38 | public override Image ItemImage {
|
---|
39 | get {
|
---|
40 | if (Algorithm != null) return Algorithm.ItemImage;
|
---|
41 | else return HeuristicLab.Common.Resources.VSImageLibrary.Event;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private long algorithmId;
|
---|
46 | public long AlgorithmId {
|
---|
47 | get { return algorithmId; }
|
---|
48 | private set {
|
---|
49 | if (algorithmId != value) {
|
---|
50 | algorithmId = value;
|
---|
51 | OnAlgorithmIdChanged();
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 | private IAlgorithm algorithm;
|
---|
56 | private IAlgorithm Algorithm {
|
---|
57 | get { return algorithm; }
|
---|
58 | set {
|
---|
59 | DeregisterAlgorithmEvents();
|
---|
60 | algorithm = value;
|
---|
61 | RegisterAlgorithmEvents();
|
---|
62 | if ((algorithm != null) && (problem != null)) {
|
---|
63 | algorithm.Problem = problem;
|
---|
64 | algorithm.Prepare(true);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | private long problemId;
|
---|
69 | public long ProblemId {
|
---|
70 | get { return problemId; }
|
---|
71 | private set {
|
---|
72 | if (problemId != value) {
|
---|
73 | problemId = value;
|
---|
74 | OnProblemIdChanged();
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | private IProblem problem;
|
---|
79 | private IProblem Problem {
|
---|
80 | get { return problem; }
|
---|
81 | set {
|
---|
82 | problem = value;
|
---|
83 | if ((algorithm != null) && (problem != null)) {
|
---|
84 | algorithm.Problem = problem;
|
---|
85 | algorithm.Prepare(true);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public ExecutionState ExecutionState {
|
---|
91 | get {
|
---|
92 | if ((Algorithm != null) && (Problem != null)) return Algorithm.ExecutionState;
|
---|
93 | else return ExecutionState.Stopped;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | public TimeSpan ExecutionTime {
|
---|
97 | get {
|
---|
98 | if (Algorithm != null) return Algorithm.ExecutionTime;
|
---|
99 | else return TimeSpan.Zero;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | public IKeyedItemCollection<string, IParameter> AlgorithmParameters {
|
---|
103 | get {
|
---|
104 | if (Algorithm != null) return Algorithm.Parameters;
|
---|
105 | else return null;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | public IKeyedItemCollection<string, IParameter> ProblemParameters {
|
---|
109 | get {
|
---|
110 | if (Problem != null) return Problem.Parameters;
|
---|
111 | else return null;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | public ResultCollection Results {
|
---|
115 | get {
|
---|
116 | if (Algorithm != null) return Algorithm.Results;
|
---|
117 | else return null;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | private RunCollection runs;
|
---|
121 | public RunCollection Runs {
|
---|
122 | get { return runs; }
|
---|
123 | }
|
---|
124 |
|
---|
125 | #region Persistence Properties
|
---|
126 | [Storable(Name = "AlgorithmId")]
|
---|
127 | private long StorableAlgorithmId {
|
---|
128 | get { return algorithmId; }
|
---|
129 | set { algorithmId = value; }
|
---|
130 | }
|
---|
131 | [Storable(Name = "Algorithm")]
|
---|
132 | private IAlgorithm StorableAlgorithm {
|
---|
133 | get { return Algorithm; }
|
---|
134 | set { Algorithm = value; }
|
---|
135 | }
|
---|
136 | [Storable(Name = "ProblemId")]
|
---|
137 | private long StorableProblemId {
|
---|
138 | get { return problemId; }
|
---|
139 | set { problemId = value; }
|
---|
140 | }
|
---|
141 | [Storable(Name = "Problem")]
|
---|
142 | private IProblem StorableProblem {
|
---|
143 | get { return Problem; }
|
---|
144 | set { Problem = value; }
|
---|
145 | }
|
---|
146 | [Storable(Name = "Runs")]
|
---|
147 | private RunCollection StorableRuns {
|
---|
148 | get { return runs; }
|
---|
149 | set { runs = value; }
|
---|
150 | }
|
---|
151 | #endregion
|
---|
152 |
|
---|
153 | [StorableConstructor]
|
---|
154 | private OKBExperiment(bool deserializing) : base(deserializing) { }
|
---|
155 | private OKBExperiment(OKBExperiment original, Cloner cloner)
|
---|
156 | : base(original, cloner) {
|
---|
157 | algorithmId = original.algorithmId;
|
---|
158 | algorithm = cloner.Clone(original.algorithm);
|
---|
159 | problemId = original.problemId;
|
---|
160 | problem = cloner.Clone(original.problem);
|
---|
161 | runs = cloner.Clone(original.runs);
|
---|
162 | RegisterAlgorithmEvents();
|
---|
163 | }
|
---|
164 | public OKBExperiment()
|
---|
165 | : base() {
|
---|
166 | name = ItemName;
|
---|
167 | description = ItemDescription;
|
---|
168 | runs = new RunCollection();
|
---|
169 | }
|
---|
170 |
|
---|
171 | [StorableHook(HookType.AfterDeserialization)]
|
---|
172 | private void AfterDeserialization() {
|
---|
173 | RegisterAlgorithmEvents();
|
---|
174 | }
|
---|
175 |
|
---|
176 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
177 | return new OKBExperiment(this, cloner);
|
---|
178 | }
|
---|
179 |
|
---|
180 | public void Load(Algorithm algorithm) {
|
---|
181 | if (algorithm == null) {
|
---|
182 | Algorithm = null;
|
---|
183 | AlgorithmId = 0;
|
---|
184 | } else {
|
---|
185 | try {
|
---|
186 | if (AlgorithmId != algorithm.Id) {
|
---|
187 | AlgorithmData algorithmData = OKBClient.Instance.GetAlgorithmData(algorithm.Id);
|
---|
188 | using (MemoryStream stream = new MemoryStream(algorithmData.Data)) {
|
---|
189 | Algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
|
---|
190 | Algorithm.StoreAlgorithmInEachRun = true;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | AlgorithmId = algorithm.Id;
|
---|
194 | }
|
---|
195 | catch (Exception ex) {
|
---|
196 | Algorithm = null;
|
---|
197 | AlgorithmId = 0;
|
---|
198 | OnExceptionOccurred(ex);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 | public void Load(Problem problem) {
|
---|
203 | if (problem == null) {
|
---|
204 | Problem = null;
|
---|
205 | ProblemId = 0;
|
---|
206 | } else {
|
---|
207 | try {
|
---|
208 | if (ProblemId != problem.Id) {
|
---|
209 | ProblemData problemData = OKBClient.Instance.GetProblemData(problem.Id);
|
---|
210 | using (MemoryStream stream = new MemoryStream(problemData.Data)) {
|
---|
211 | Problem = XmlParser.Deserialize<IProblem>(stream);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | ProblemId = problem.Id;
|
---|
215 | }
|
---|
216 | catch (Exception ex) {
|
---|
217 | Problem = null;
|
---|
218 | ProblemId = 0;
|
---|
219 | OnExceptionOccurred(ex);
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | public void Prepare(bool clearRuns) {
|
---|
225 | if (Algorithm != null) Algorithm.Prepare(clearRuns);
|
---|
226 | }
|
---|
227 | public void Prepare() {
|
---|
228 | if (Algorithm != null) Algorithm.Prepare();
|
---|
229 | }
|
---|
230 | public void Start() {
|
---|
231 | if (Algorithm != null) Algorithm.Start();
|
---|
232 | }
|
---|
233 | public void Pause() {
|
---|
234 | if (Algorithm != null) Algorithm.Pause();
|
---|
235 | }
|
---|
236 | public void Stop() {
|
---|
237 | if (Algorithm != null) Algorithm.Stop();
|
---|
238 | }
|
---|
239 |
|
---|
240 | #region Events
|
---|
241 | public event EventHandler AlgorithmIdChanged;
|
---|
242 | private void OnAlgorithmIdChanged() {
|
---|
243 | EventHandler handler = AlgorithmIdChanged;
|
---|
244 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
245 | }
|
---|
246 | public event EventHandler ProblemIdChanged;
|
---|
247 | private void OnProblemIdChanged() {
|
---|
248 | EventHandler handler = ProblemIdChanged;
|
---|
249 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
250 | }
|
---|
251 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
252 | private void OnExceptionOccurred(Exception exception) {
|
---|
253 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
254 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
255 | }
|
---|
256 | public event EventHandler ExecutionStateChanged;
|
---|
257 | private void OnExecutionStateChanged() {
|
---|
258 | EventHandler handler = ExecutionStateChanged;
|
---|
259 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
260 | }
|
---|
261 | public event EventHandler ExecutionTimeChanged;
|
---|
262 | private void OnExecutionTimeChanged() {
|
---|
263 | EventHandler handler = ExecutionTimeChanged;
|
---|
264 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
265 | }
|
---|
266 | public event EventHandler Prepared;
|
---|
267 | private void OnPrepared() {
|
---|
268 | EventHandler handler = Prepared;
|
---|
269 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
270 | }
|
---|
271 | public event EventHandler Started;
|
---|
272 | private void OnStarted() {
|
---|
273 | EventHandler handler = Started;
|
---|
274 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
275 | }
|
---|
276 | public event EventHandler Paused;
|
---|
277 | private void OnPaused() {
|
---|
278 | EventHandler handler = Paused;
|
---|
279 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
280 | }
|
---|
281 | public event EventHandler Stopped;
|
---|
282 | private void OnStopped() {
|
---|
283 | EventHandler handler = Stopped;
|
---|
284 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
285 | }
|
---|
286 |
|
---|
287 | private void RegisterAlgorithmEvents() {
|
---|
288 | if (Algorithm != null) {
|
---|
289 | Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
|
---|
290 | Algorithm.ExecutionStateChanged += new EventHandler(algorithm_ExecutionStateChanged);
|
---|
291 | Algorithm.ExecutionTimeChanged += new EventHandler(algorithm_ExecutionTimeChanged);
|
---|
292 | Algorithm.ItemImageChanged += new EventHandler(algorithm_ItemImageChanged);
|
---|
293 | Algorithm.Paused += new EventHandler(algorithm_Paused);
|
---|
294 | Algorithm.Prepared += new EventHandler(algorithm_Prepared);
|
---|
295 | Algorithm.Started += new EventHandler(algorithm_Started);
|
---|
296 | Algorithm.Stopped += new EventHandler(algorithm_Stopped);
|
---|
297 | Algorithm.Runs.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<IRun>(algorithm_Runs_ItemsAdded);
|
---|
298 | Algorithm.Runs.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<IRun>(algorithm_Runs_ItemsRemoved);
|
---|
299 | Algorithm.Runs.CollectionReset += new Collections.CollectionItemsChangedEventHandler<IRun>(algorithm_Runs_CollectionReset);
|
---|
300 | }
|
---|
301 | }
|
---|
302 | private void DeregisterAlgorithmEvents() {
|
---|
303 | if (Algorithm != null) {
|
---|
304 | Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
|
---|
305 | Algorithm.ExecutionStateChanged -= new EventHandler(algorithm_ExecutionStateChanged);
|
---|
306 | Algorithm.ExecutionTimeChanged -= new EventHandler(algorithm_ExecutionTimeChanged);
|
---|
307 | Algorithm.ItemImageChanged -= new EventHandler(algorithm_ItemImageChanged);
|
---|
308 | Algorithm.Paused -= new EventHandler(algorithm_Paused);
|
---|
309 | Algorithm.Prepared -= new EventHandler(algorithm_Prepared);
|
---|
310 | Algorithm.Started -= new EventHandler(algorithm_Started);
|
---|
311 | Algorithm.Stopped -= new EventHandler(algorithm_Stopped);
|
---|
312 | Algorithm.Runs.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<IRun>(algorithm_Runs_ItemsAdded);
|
---|
313 | Algorithm.Runs.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<IRun>(algorithm_Runs_ItemsRemoved);
|
---|
314 | Algorithm.Runs.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<IRun>(algorithm_Runs_CollectionReset);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | private void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
318 | OnExceptionOccurred(e.Value);
|
---|
319 | }
|
---|
320 | private void algorithm_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
321 | OnExecutionStateChanged();
|
---|
322 | }
|
---|
323 | private void algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
324 | OnExecutionTimeChanged();
|
---|
325 | }
|
---|
326 | private void algorithm_ItemImageChanged(object sender, EventArgs e) {
|
---|
327 | OnItemImageChanged();
|
---|
328 | }
|
---|
329 | private void algorithm_Paused(object sender, EventArgs e) {
|
---|
330 | OnPaused();
|
---|
331 | }
|
---|
332 | private void algorithm_Prepared(object sender, EventArgs e) {
|
---|
333 | OnPrepared();
|
---|
334 | }
|
---|
335 | private void algorithm_Started(object sender, EventArgs e) {
|
---|
336 | OnStarted();
|
---|
337 | }
|
---|
338 | private void algorithm_Stopped(object sender, EventArgs e) {
|
---|
339 | try {
|
---|
340 | OKBClient.Instance.AddRun(AlgorithmId, ProblemId, algorithm);
|
---|
341 | }
|
---|
342 | catch (Exception ex) {
|
---|
343 | OnExceptionOccurred(ex);
|
---|
344 | }
|
---|
345 | OnStopped();
|
---|
346 | }
|
---|
347 | private void algorithm_Runs_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
348 | runs.AddRange(e.Items);
|
---|
349 | }
|
---|
350 | private void algorithm_Runs_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
351 | runs.RemoveRange(e.Items);
|
---|
352 | }
|
---|
353 | private void algorithm_Runs_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
354 | runs.Clear();
|
---|
355 | runs.AddRange(algorithm.Runs);
|
---|
356 | }
|
---|
357 | #endregion
|
---|
358 | }
|
---|
359 | }
|
---|