[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", "...")]
|
---|
[4548] | 33 | [Creatable("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;
|
---|
[4548] | 41 | else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
|
---|
| 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 | }
|
---|
[4549] | 120 | public RunCollection Runs {
|
---|
| 121 | get {
|
---|
| 122 | if (Algorithm != null) return Algorithm.Runs;
|
---|
| 123 | else return null;
|
---|
| 124 | }
|
---|
[4548] | 125 | }
|
---|
| 126 |
|
---|
[4558] | 127 | #region Persistence Properties
|
---|
| 128 | [Storable(Name = "AlgorithmId")]
|
---|
| 129 | private long AlgorithmIdPersistence {
|
---|
| 130 | get { return algorithmId; }
|
---|
| 131 | set { algorithmId = value; }
|
---|
| 132 | }
|
---|
| 133 | [Storable(Name = "Algorithm")]
|
---|
| 134 | private IAlgorithm AlgorithmPersistence {
|
---|
| 135 | get { return Algorithm; }
|
---|
| 136 | set { Algorithm = value; }
|
---|
| 137 | }
|
---|
| 138 | [Storable(Name = "ProblemId")]
|
---|
| 139 | private long ProblemIdPersistence {
|
---|
| 140 | get { return problemId; }
|
---|
| 141 | set { problemId = value; }
|
---|
| 142 | }
|
---|
| 143 | [Storable(Name = "Problem")]
|
---|
| 144 | private IProblem ProblemPersistence {
|
---|
| 145 | get { return Problem; }
|
---|
| 146 | set { Problem = value; }
|
---|
| 147 | }
|
---|
| 148 | #endregion
|
---|
| 149 |
|
---|
[4549] | 150 | public OKBExperiment()
|
---|
| 151 | : base() {
|
---|
| 152 | name = ItemName;
|
---|
| 153 | description = ItemDescription;
|
---|
[4548] | 154 | }
|
---|
[4549] | 155 | [StorableConstructor]
|
---|
[4558] | 156 | private OKBExperiment(bool deserializing) : base(deserializing) { }
|
---|
| 157 |
|
---|
| 158 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 159 | private void Initialize() {
|
---|
[4553] | 160 | RegisterAlgorithmEvents();
|
---|
| 161 | }
|
---|
[4548] | 162 |
|
---|
[4553] | 163 | public void Load(Algorithm algorithm) {
|
---|
| 164 | if (algorithm == null) {
|
---|
[4558] | 165 | Algorithm = null;
|
---|
[4553] | 166 | AlgorithmId = 0;
|
---|
| 167 | } else {
|
---|
[4558] | 168 | try {
|
---|
| 169 | if (AlgorithmId != algorithm.Id) {
|
---|
| 170 | AlgorithmData algorithmData = OKBClient.Instance.GetAlgorithmData(algorithm.Id);
|
---|
| 171 | using (MemoryStream stream = new MemoryStream(algorithmData.Data)) {
|
---|
| 172 | Algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
|
---|
[4566] | 173 | Algorithm.StoreAlgorithmInEachRun = true;
|
---|
[4558] | 174 | }
|
---|
| 175 | }
|
---|
| 176 | AlgorithmId = algorithm.Id;
|
---|
[4553] | 177 | }
|
---|
[4558] | 178 | catch (Exception ex) {
|
---|
| 179 | Algorithm = null;
|
---|
| 180 | AlgorithmId = 0;
|
---|
| 181 | OnExceptionOccurred(ex);
|
---|
| 182 | }
|
---|
[4549] | 183 | }
|
---|
[4548] | 184 | }
|
---|
[4553] | 185 | public void Load(Problem problem) {
|
---|
| 186 | if (problem == null) {
|
---|
[4558] | 187 | Problem = null;
|
---|
[4553] | 188 | ProblemId = 0;
|
---|
| 189 | } else {
|
---|
[4558] | 190 | try {
|
---|
| 191 | if (ProblemId != problem.Id) {
|
---|
| 192 | ProblemData problemData = OKBClient.Instance.GetProblemData(problem.Id);
|
---|
| 193 | using (MemoryStream stream = new MemoryStream(problemData.Data)) {
|
---|
| 194 | Problem = XmlParser.Deserialize<IProblem>(stream);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | ProblemId = problem.Id;
|
---|
[4553] | 198 | }
|
---|
[4558] | 199 | catch (Exception ex) {
|
---|
| 200 | Problem = null;
|
---|
| 201 | ProblemId = 0;
|
---|
| 202 | OnExceptionOccurred(ex);
|
---|
| 203 | }
|
---|
[4549] | 204 | }
|
---|
[4548] | 205 | }
|
---|
| 206 |
|
---|
[4549] | 207 | public void Prepare(bool clearRuns) {
|
---|
| 208 | if (Algorithm != null) Algorithm.Prepare(clearRuns);
|
---|
| 209 | }
|
---|
[4548] | 210 | public void Prepare() {
|
---|
[4553] | 211 | if (Algorithm != null) Algorithm.Prepare();
|
---|
[4548] | 212 | }
|
---|
| 213 | public void Start() {
|
---|
[4553] | 214 | if (Algorithm != null) Algorithm.Start();
|
---|
[4548] | 215 | }
|
---|
[4553] | 216 | public void Pause() {
|
---|
| 217 | if (Algorithm != null) Algorithm.Pause();
|
---|
| 218 | }
|
---|
[4548] | 219 | public void Stop() {
|
---|
[4553] | 220 | if (Algorithm != null) Algorithm.Stop();
|
---|
[4548] | 221 | }
|
---|
| 222 |
|
---|
[4549] | 223 | #region Events
|
---|
[4558] | 224 | public event EventHandler AlgorithmIdChanged;
|
---|
| 225 | private void OnAlgorithmIdChanged() {
|
---|
| 226 | EventHandler handler = AlgorithmIdChanged;
|
---|
[4549] | 227 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 228 | }
|
---|
[4558] | 229 | public event EventHandler ProblemIdChanged;
|
---|
| 230 | private void OnProblemIdChanged() {
|
---|
| 231 | EventHandler handler = ProblemIdChanged;
|
---|
| 232 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 233 | }
|
---|
[4549] | 234 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 235 | private void OnExceptionOccurred(Exception exception) {
|
---|
| 236 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
| 237 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
| 238 | }
|
---|
| 239 | public event EventHandler ExecutionStateChanged;
|
---|
| 240 | private void OnExecutionStateChanged() {
|
---|
| 241 | EventHandler handler = ExecutionStateChanged;
|
---|
| 242 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 243 | }
|
---|
| 244 | public event EventHandler ExecutionTimeChanged;
|
---|
| 245 | private void OnExecutionTimeChanged() {
|
---|
| 246 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 247 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 248 | }
|
---|
| 249 | public event EventHandler Prepared;
|
---|
| 250 | private void OnPrepared() {
|
---|
| 251 | EventHandler handler = Prepared;
|
---|
| 252 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 253 | }
|
---|
| 254 | public event EventHandler Started;
|
---|
| 255 | private void OnStarted() {
|
---|
| 256 | EventHandler handler = Started;
|
---|
| 257 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 258 | }
|
---|
| 259 | public event EventHandler Paused;
|
---|
| 260 | private void OnPaused() {
|
---|
| 261 | EventHandler handler = Paused;
|
---|
| 262 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 263 | }
|
---|
| 264 | public event EventHandler Stopped;
|
---|
| 265 | private void OnStopped() {
|
---|
| 266 | EventHandler handler = Stopped;
|
---|
| 267 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 268 | }
|
---|
[4548] | 269 |
|
---|
[4549] | 270 | private void RegisterAlgorithmEvents() {
|
---|
| 271 | if (Algorithm != null) {
|
---|
[4558] | 272 | Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
|
---|
| 273 | Algorithm.ExecutionStateChanged += new EventHandler(algorithm_ExecutionStateChanged);
|
---|
| 274 | Algorithm.ExecutionTimeChanged += new EventHandler(algorithm_ExecutionTimeChanged);
|
---|
| 275 | Algorithm.ItemImageChanged += new EventHandler(algorithm_ItemImageChanged);
|
---|
| 276 | Algorithm.Paused += new EventHandler(algorithm_Paused);
|
---|
| 277 | Algorithm.Prepared += new EventHandler(algorithm_Prepared);
|
---|
| 278 | Algorithm.Started += new EventHandler(algorithm_Started);
|
---|
| 279 | Algorithm.Stopped += new EventHandler(algorithm_Stopped);
|
---|
[4559] | 280 | Algorithm.Runs.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
|
---|
[4549] | 281 | }
|
---|
| 282 | }
|
---|
| 283 | private void DeregisterAlgorithmEvents() {
|
---|
| 284 | if (Algorithm != null) {
|
---|
[4558] | 285 | Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
|
---|
| 286 | Algorithm.ExecutionStateChanged -= new EventHandler(algorithm_ExecutionStateChanged);
|
---|
| 287 | Algorithm.ExecutionTimeChanged -= new EventHandler(algorithm_ExecutionTimeChanged);
|
---|
| 288 | Algorithm.ItemImageChanged -= new EventHandler(algorithm_ItemImageChanged);
|
---|
| 289 | Algorithm.Paused -= new EventHandler(algorithm_Paused);
|
---|
| 290 | Algorithm.Prepared -= new EventHandler(algorithm_Prepared);
|
---|
| 291 | Algorithm.Started -= new EventHandler(algorithm_Started);
|
---|
| 292 | Algorithm.Stopped -= new EventHandler(algorithm_Stopped);
|
---|
[4559] | 293 | Algorithm.Runs.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
|
---|
[4549] | 294 | }
|
---|
| 295 | }
|
---|
[4558] | 296 | private void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
[4549] | 297 | OnExceptionOccurred(e.Value);
|
---|
| 298 | }
|
---|
[4558] | 299 | private void algorithm_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
[4549] | 300 | OnExecutionStateChanged();
|
---|
| 301 | }
|
---|
[4558] | 302 | private void algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
[4549] | 303 | OnExecutionTimeChanged();
|
---|
| 304 | }
|
---|
[4558] | 305 | private void algorithm_ItemImageChanged(object sender, EventArgs e) {
|
---|
[4549] | 306 | OnItemImageChanged();
|
---|
| 307 | }
|
---|
[4558] | 308 | private void algorithm_Paused(object sender, EventArgs e) {
|
---|
[4549] | 309 | OnPaused();
|
---|
| 310 | }
|
---|
[4558] | 311 | private void algorithm_Prepared(object sender, EventArgs e) {
|
---|
[4549] | 312 | OnPrepared();
|
---|
| 313 | }
|
---|
[4558] | 314 | private void algorithm_Started(object sender, EventArgs e) {
|
---|
[4549] | 315 | OnStarted();
|
---|
| 316 | }
|
---|
[4558] | 317 | private void algorithm_Stopped(object sender, EventArgs e) {
|
---|
[4549] | 318 | OnStopped();
|
---|
| 319 | }
|
---|
[4559] | 320 | private void Runs_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 321 | try {
|
---|
[4591] | 322 | foreach (HeuristicLab.Optimization.Run run in e.Items)
|
---|
[4587] | 323 | OKBClient.Instance.AddRun(AlgorithmId, ProblemId, run);
|
---|
[4559] | 324 | }
|
---|
| 325 | catch (Exception ex) {
|
---|
| 326 | OnExceptionOccurred(ex);
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
[4548] | 329 | #endregion
|
---|
| 330 | }
|
---|
| 331 | }
|
---|