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