[4548] | 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 |
|
---|
[4549] | 22 | using System;
|
---|
| 23 | using System.Drawing;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[4548] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
[4549] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[4548] | 30 |
|
---|
| 31 | namespace HeuristicLab.Clients.OKB {
|
---|
[4549] | 32 | [Item("OKB Experiment", "...")]
|
---|
[5338] | 33 | [Creatable("Optimization Knowledge Base (OKB)")]
|
---|
[4549] | 34 | [StorableClass]
|
---|
[4548] | 35 | public sealed class OKBExperiment : NamedItem, IOptimizer, IStorableContent {
|
---|
| 36 | public string Filename { get; set; }
|
---|
| 37 |
|
---|
| 38 | public override Image ItemImage {
|
---|
| 39 | get {
|
---|
[4549] | 40 | if (Algorithm != null) return Algorithm.ItemImage;
|
---|
[5289] | 41 | else return HeuristicLab.Common.Resources.VSImageLibrary.Event;
|
---|
[4548] | 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[4558] | 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 | }
|
---|
[4548] | 55 | private IAlgorithm algorithm;
|
---|
| 56 | private IAlgorithm Algorithm {
|
---|
| 57 | get { return algorithm; }
|
---|
[4549] | 58 | set {
|
---|
| 59 | DeregisterAlgorithmEvents();
|
---|
| 60 | algorithm = value;
|
---|
| 61 | RegisterAlgorithmEvents();
|
---|
[4558] | 62 | if ((algorithm != null) && (problem != null)) {
|
---|
| 63 | algorithm.Problem = problem;
|
---|
| 64 | algorithm.Prepare(true);
|
---|
| 65 | }
|
---|
[4549] | 66 | }
|
---|
[4548] | 67 | }
|
---|
[4558] | 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 | }
|
---|
[4548] | 78 | private IProblem problem;
|
---|
[4558] | 79 | private IProblem Problem {
|
---|
[4548] | 80 | get { return problem; }
|
---|
[4558] | 81 | set {
|
---|
[4549] | 82 | problem = value;
|
---|
[4558] | 83 | if ((algorithm != null) && (problem != null)) {
|
---|
| 84 | algorithm.Problem = problem;
|
---|
| 85 | algorithm.Prepare(true);
|
---|
| 86 | }
|
---|
[4549] | 87 | }
|
---|
[4548] | 88 | }
|
---|
| 89 |
|
---|
[4553] | 90 | public ExecutionState ExecutionState {
|
---|
| 91 | get {
|
---|
[4558] | 92 | if ((Algorithm != null) && (Problem != null)) return Algorithm.ExecutionState;
|
---|
[4553] | 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 | }
|
---|
[4558] | 102 | public IKeyedItemCollection<string, IParameter> AlgorithmParameters {
|
---|
[4549] | 103 | get {
|
---|
| 104 | if (Algorithm != null) return Algorithm.Parameters;
|
---|
| 105 | else return null;
|
---|
| 106 | }
|
---|
[4548] | 107 | }
|
---|
[4558] | 108 | public IKeyedItemCollection<string, IParameter> ProblemParameters {
|
---|
| 109 | get {
|
---|
| 110 | if (Problem != null) return Problem.Parameters;
|
---|
| 111 | else return null;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[4549] | 114 | public ResultCollection Results {
|
---|
| 115 | get {
|
---|
| 116 | if (Algorithm != null) return Algorithm.Results;
|
---|
| 117 | else return null;
|
---|
| 118 | }
|
---|
[4548] | 119 | }
|
---|
[5304] | 120 | private RunCollection runs;
|
---|
[4549] | 121 | public RunCollection Runs {
|
---|
[5304] | 122 | get { return runs; }
|
---|
[4548] | 123 | }
|
---|
| 124 |
|
---|
[4558] | 125 | #region Persistence Properties
|
---|
| 126 | [Storable(Name = "AlgorithmId")]
|
---|
[5304] | 127 | private long StorableAlgorithmId {
|
---|
[4558] | 128 | get { return algorithmId; }
|
---|
| 129 | set { algorithmId = value; }
|
---|
| 130 | }
|
---|
| 131 | [Storable(Name = "Algorithm")]
|
---|
[5304] | 132 | private IAlgorithm StorableAlgorithm {
|
---|
[4558] | 133 | get { return Algorithm; }
|
---|
| 134 | set { Algorithm = value; }
|
---|
| 135 | }
|
---|
| 136 | [Storable(Name = "ProblemId")]
|
---|
[5304] | 137 | private long StorableProblemId {
|
---|
[4558] | 138 | get { return problemId; }
|
---|
| 139 | set { problemId = value; }
|
---|
| 140 | }
|
---|
| 141 | [Storable(Name = "Problem")]
|
---|
[5304] | 142 | private IProblem StorableProblem {
|
---|
[4558] | 143 | get { return Problem; }
|
---|
| 144 | set { Problem = value; }
|
---|
| 145 | }
|
---|
[5304] | 146 | [Storable(Name = "Runs")]
|
---|
| 147 | private RunCollection StorableRuns {
|
---|
| 148 | get { return runs; }
|
---|
| 149 | set { runs = value; }
|
---|
| 150 | }
|
---|
[4558] | 151 | #endregion
|
---|
| 152 |
|
---|
[4918] | 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);
|
---|
[5304] | 161 | runs = cloner.Clone(original.runs);
|
---|
[4918] | 162 | RegisterAlgorithmEvents();
|
---|
| 163 | }
|
---|
[4549] | 164 | public OKBExperiment()
|
---|
| 165 | : base() {
|
---|
| 166 | name = ItemName;
|
---|
| 167 | description = ItemDescription;
|
---|
[5304] | 168 | runs = new RunCollection();
|
---|
[4548] | 169 | }
|
---|
[4558] | 170 |
|
---|
| 171 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4918] | 172 | private void AfterDeserialization() {
|
---|
[4553] | 173 | RegisterAlgorithmEvents();
|
---|
| 174 | }
|
---|
[4548] | 175 |
|
---|
[4918] | 176 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 177 | return new OKBExperiment(this, cloner);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[4553] | 180 | public void Load(Algorithm algorithm) {
|
---|
| 181 | if (algorithm == null) {
|
---|
[4558] | 182 | Algorithm = null;
|
---|
[4553] | 183 | AlgorithmId = 0;
|
---|
| 184 | } else {
|
---|
[4558] | 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);
|
---|
[4566] | 190 | Algorithm.StoreAlgorithmInEachRun = true;
|
---|
[4558] | 191 | }
|
---|
| 192 | }
|
---|
| 193 | AlgorithmId = algorithm.Id;
|
---|
[4553] | 194 | }
|
---|
[4558] | 195 | catch (Exception ex) {
|
---|
| 196 | Algorithm = null;
|
---|
| 197 | AlgorithmId = 0;
|
---|
| 198 | OnExceptionOccurred(ex);
|
---|
| 199 | }
|
---|
[4549] | 200 | }
|
---|
[4548] | 201 | }
|
---|
[4553] | 202 | public void Load(Problem problem) {
|
---|
| 203 | if (problem == null) {
|
---|
[4558] | 204 | Problem = null;
|
---|
[4553] | 205 | ProblemId = 0;
|
---|
| 206 | } else {
|
---|
[4558] | 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;
|
---|
[4553] | 215 | }
|
---|
[4558] | 216 | catch (Exception ex) {
|
---|
| 217 | Problem = null;
|
---|
| 218 | ProblemId = 0;
|
---|
| 219 | OnExceptionOccurred(ex);
|
---|
| 220 | }
|
---|
[4549] | 221 | }
|
---|
[4548] | 222 | }
|
---|
| 223 |
|
---|
[4549] | 224 | public void Prepare(bool clearRuns) {
|
---|
| 225 | if (Algorithm != null) Algorithm.Prepare(clearRuns);
|
---|
| 226 | }
|
---|
[4548] | 227 | public void Prepare() {
|
---|
[4553] | 228 | if (Algorithm != null) Algorithm.Prepare();
|
---|
[4548] | 229 | }
|
---|
| 230 | public void Start() {
|
---|
[4553] | 231 | if (Algorithm != null) Algorithm.Start();
|
---|
[4548] | 232 | }
|
---|
[4553] | 233 | public void Pause() {
|
---|
| 234 | if (Algorithm != null) Algorithm.Pause();
|
---|
| 235 | }
|
---|
[4548] | 236 | public void Stop() {
|
---|
[4553] | 237 | if (Algorithm != null) Algorithm.Stop();
|
---|
[4548] | 238 | }
|
---|
| 239 |
|
---|
[4549] | 240 | #region Events
|
---|
[4558] | 241 | public event EventHandler AlgorithmIdChanged;
|
---|
| 242 | private void OnAlgorithmIdChanged() {
|
---|
| 243 | EventHandler handler = AlgorithmIdChanged;
|
---|
[4549] | 244 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 245 | }
|
---|
[4558] | 246 | public event EventHandler ProblemIdChanged;
|
---|
| 247 | private void OnProblemIdChanged() {
|
---|
| 248 | EventHandler handler = ProblemIdChanged;
|
---|
| 249 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 250 | }
|
---|
[4549] | 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 | }
|
---|
[4548] | 286 |
|
---|
[4549] | 287 | private void RegisterAlgorithmEvents() {
|
---|
| 288 | if (Algorithm != null) {
|
---|
[4558] | 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);
|
---|
[5304] | 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);
|
---|
[4549] | 300 | }
|
---|
| 301 | }
|
---|
| 302 | private void DeregisterAlgorithmEvents() {
|
---|
| 303 | if (Algorithm != null) {
|
---|
[4558] | 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);
|
---|
[5304] | 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);
|
---|
[4549] | 315 | }
|
---|
| 316 | }
|
---|
[4558] | 317 | private void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
[4549] | 318 | OnExceptionOccurred(e.Value);
|
---|
| 319 | }
|
---|
[4558] | 320 | private void algorithm_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
[4549] | 321 | OnExecutionStateChanged();
|
---|
| 322 | }
|
---|
[4558] | 323 | private void algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
[4549] | 324 | OnExecutionTimeChanged();
|
---|
| 325 | }
|
---|
[4558] | 326 | private void algorithm_ItemImageChanged(object sender, EventArgs e) {
|
---|
[4549] | 327 | OnItemImageChanged();
|
---|
| 328 | }
|
---|
[4558] | 329 | private void algorithm_Paused(object sender, EventArgs e) {
|
---|
[4549] | 330 | OnPaused();
|
---|
| 331 | }
|
---|
[4558] | 332 | private void algorithm_Prepared(object sender, EventArgs e) {
|
---|
[4549] | 333 | OnPrepared();
|
---|
| 334 | }
|
---|
[4558] | 335 | private void algorithm_Started(object sender, EventArgs e) {
|
---|
[4549] | 336 | OnStarted();
|
---|
| 337 | }
|
---|
[4558] | 338 | private void algorithm_Stopped(object sender, EventArgs e) {
|
---|
[4559] | 339 | try {
|
---|
[4929] | 340 | OKBClient.Instance.AddRun(AlgorithmId, ProblemId, algorithm);
|
---|
[4559] | 341 | }
|
---|
| 342 | catch (Exception ex) {
|
---|
| 343 | OnExceptionOccurred(ex);
|
---|
| 344 | }
|
---|
[5317] | 345 | OnStopped();
|
---|
[4559] | 346 | }
|
---|
[5304] | 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 | }
|
---|
[4548] | 357 | #endregion
|
---|
| 358 | }
|
---|
| 359 | }
|
---|