[4368] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using HeuristicLab.Core;
|
---|
| 4 | using HeuristicLab.Hive.JobBase;
|
---|
| 5 | using HeuristicLab.Optimization;
|
---|
| 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Hive.Experiment.Jobs {
|
---|
| 9 | [StorableClass]
|
---|
| 10 | public class OptimizerJob : IJob {
|
---|
| 11 | [Storable]
|
---|
| 12 | protected Guid internalId;
|
---|
| 13 | public Guid InternalId {
|
---|
| 14 | get { return internalId; }
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | [Storable]
|
---|
| 18 | protected IOptimizer optimizer;
|
---|
| 19 | public IOptimizer Optimizer {
|
---|
| 20 | get { return optimizer; }
|
---|
| 21 | set {
|
---|
| 22 | if (optimizer != null && value != optimizer) {
|
---|
| 23 | DeregisterEvents();
|
---|
| 24 | }
|
---|
| 25 | optimizer = value;
|
---|
| 26 | if (optimizer != null) {
|
---|
| 27 | RegisterEvents();
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | [Storable]
|
---|
| 33 | protected double progress = 0.0;
|
---|
| 34 |
|
---|
| 35 | [Storable]
|
---|
| 36 | protected ILog log;
|
---|
| 37 | public ILog Log {
|
---|
| 38 | get { return log; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | [Storable]
|
---|
| 42 | protected ICollection<IJob> childJobs;
|
---|
| 43 | public IEnumerable<IJob> ChildJobs {
|
---|
| 44 | get { return childJobs; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | [Storable]
|
---|
| 48 | protected bool computeInParallel;
|
---|
| 49 | public bool ComputeInParallel {
|
---|
| 50 | get { return computeInParallel; }
|
---|
| 51 | set { computeInParallel = value; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | [Storable]
|
---|
| 55 | protected int coresNeeded;
|
---|
| 56 | public int CoresNeeded {
|
---|
| 57 | get { return coresNeeded; }
|
---|
| 58 | set { coresNeeded = value; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | [Storable]
|
---|
| 62 | protected int memoryNeeded;
|
---|
| 63 | public int MemoryNeeded {
|
---|
| 64 | get { return memoryNeeded; }
|
---|
| 65 | set { memoryNeeded = value; }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public OptimizerJob() {
|
---|
| 69 | this.internalId = Guid.NewGuid();
|
---|
| 70 | this.log = new Log();
|
---|
| 71 | this.childJobs = new List<IJob>();
|
---|
| 72 | this.ComputeInParallel = false;
|
---|
| 73 | this.CoresNeeded = 1;
|
---|
| 74 | this.MemoryNeeded = 0;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public OptimizerJob(IOptimizer optimizer)
|
---|
| 78 | : this() {
|
---|
| 79 | this.optimizer = optimizer;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 83 | protected virtual void AfterDeserialization() {
|
---|
| 84 | RegisterEvents();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | #region IJob Members
|
---|
| 88 |
|
---|
| 89 | public double Progress {
|
---|
| 90 | get { return optimizer.ExecutionState == ExecutionState.Stopped ? 1.0 : this.progress; }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public virtual ExecutionState ExecutionState {
|
---|
| 94 | get { return optimizer.ExecutionState; }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public virtual void Run() {
|
---|
| 98 | throw new NotSupportedException();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | public virtual void Prepare() {
|
---|
| 102 | optimizer.Prepare();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | public virtual void Start() {
|
---|
| 106 | optimizer.Start();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public virtual void Stop() {
|
---|
| 110 | optimizer.Stop();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | public virtual void Resume(IEnumerable<IJob> childJobs) {
|
---|
| 114 | throw new NotSupportedException();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public event EventHandler JobStopped;
|
---|
| 118 | protected virtual void OnJobStopped() {
|
---|
| 119 | EventHandler handler = JobStopped;
|
---|
| 120 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | public event EventHandler JobFailed;
|
---|
| 124 | protected virtual void OnJobFailed(Common.EventArgs<Exception> e) {
|
---|
| 125 | EventHandler handler = JobFailed;
|
---|
| 126 | if (handler != null) handler(this, e);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public event EventHandler<Common.EventArgs<IJob>> NewChildJob;
|
---|
| 130 | protected virtual void OnNewChildJob(IJob job) {
|
---|
| 131 | EventHandler<Common.EventArgs<IJob>> handler = NewChildJob;
|
---|
| 132 | if (handler != null) handler(this, new Common.EventArgs<IJob>(job));
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | public event EventHandler WaitForChildJobs;
|
---|
| 136 | protected virtual void OnWaitForChildJobs() {
|
---|
| 137 | EventHandler handler = WaitForChildJobs;
|
---|
| 138 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | #endregion
|
---|
| 142 |
|
---|
| 143 | #region Events
|
---|
| 144 | protected virtual void RegisterEvents() {
|
---|
| 145 | optimizer.Stopped += new EventHandler(optimizer_Stopped);
|
---|
| 146 | optimizer.ExceptionOccurred += new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
| 147 | optimizer.DescriptionChanged += this.DescriptionChanged;
|
---|
| 148 | optimizer.FilenameChanged += this.FilenameChanged;
|
---|
| 149 | optimizer.ItemImageChanged += this.ItemImageChanged;
|
---|
| 150 | optimizer.NameChanged += this.NameChanged;
|
---|
| 151 | optimizer.NameChanging += this.NameChanging;
|
---|
| 152 | optimizer.ToStringChanged += this.ToStringChanged;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | protected virtual void DeregisterEvents() {
|
---|
| 156 | optimizer.Stopped -= new EventHandler(optimizer_Stopped);
|
---|
| 157 | optimizer.ExceptionOccurred -= new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
| 158 | optimizer.DescriptionChanged -= this.DescriptionChanged;
|
---|
| 159 | optimizer.FilenameChanged -= this.FilenameChanged;
|
---|
| 160 | optimizer.ItemImageChanged -= this.ItemImageChanged;
|
---|
| 161 | optimizer.NameChanged -= this.NameChanged;
|
---|
| 162 | optimizer.NameChanging -= this.NameChanging;
|
---|
| 163 | optimizer.ToStringChanged -= this.ToStringChanged;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | protected virtual void optimizer_ExceptionOccurred(object sender, Common.EventArgs<Exception> e) {
|
---|
| 167 | if (optimizer.ExecutionState != ExecutionState.Stopped) this.progress = 0.0;
|
---|
| 168 | else this.progress = 1.0;
|
---|
| 169 | OnJobFailed(e);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | protected virtual void optimizer_Stopped(object sender, EventArgs e) {
|
---|
| 173 | if (optimizer.ExecutionState != ExecutionState.Stopped) this.progress = 0.0;
|
---|
| 174 | else this.progress = 1.0;
|
---|
| 175 | OnJobStopped();
|
---|
| 176 | }
|
---|
| 177 | #endregion
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | #region INamedItem Members
|
---|
| 181 |
|
---|
| 182 | public bool CanChangeDescription {
|
---|
| 183 | get { return optimizer.CanChangeDescription; }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | public bool CanChangeName {
|
---|
| 187 | get { return optimizer.CanChangeName; }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | public string Description {
|
---|
| 191 | get { return optimizer.Description; }
|
---|
| 192 | set { optimizer.Description = value; }
|
---|
| 193 | }
|
---|
| 194 | public event EventHandler DescriptionChanged;
|
---|
| 195 |
|
---|
| 196 | public string Name {
|
---|
| 197 | get { return optimizer.Name; }
|
---|
| 198 | set { optimizer.Name = value; }
|
---|
| 199 | }
|
---|
| 200 | public event EventHandler NameChanged;
|
---|
| 201 | public event EventHandler<Common.CancelEventArgs<string>> NameChanging;
|
---|
| 202 |
|
---|
| 203 | #endregion
|
---|
| 204 |
|
---|
| 205 | #region IItem Members
|
---|
| 206 |
|
---|
| 207 | public string ItemDescription {
|
---|
| 208 | get { return optimizer.ItemDescription; }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | public System.Drawing.Image ItemImage {
|
---|
| 212 | get { return optimizer.ItemImage; }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | public event EventHandler ItemImageChanged;
|
---|
| 216 | public string ItemName {
|
---|
| 217 | get { return optimizer.ItemName; }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | public Version ItemVersion {
|
---|
| 221 | get { return optimizer.ItemVersion; }
|
---|
| 222 | }
|
---|
| 223 | public event EventHandler ToStringChanged;
|
---|
| 224 |
|
---|
| 225 | #endregion
|
---|
| 226 |
|
---|
| 227 | #region IStorableContent Members
|
---|
| 228 |
|
---|
| 229 | public string Filename {
|
---|
| 230 | get;
|
---|
| 231 | set;
|
---|
| 232 | }
|
---|
| 233 | public event EventHandler FilenameChanged;
|
---|
| 234 |
|
---|
| 235 | #endregion
|
---|
| 236 |
|
---|
| 237 | #region IDeepCloneable Members
|
---|
| 238 |
|
---|
| 239 | public Common.IDeepCloneable Clone(Common.Cloner cloner) {
|
---|
| 240 | throw new NotImplementedException();
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | #endregion
|
---|
| 244 |
|
---|
| 245 | #region ICloneable Members
|
---|
| 246 |
|
---|
| 247 | public object Clone() {
|
---|
| 248 | throw new NotImplementedException();
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | #endregion
|
---|
| 252 |
|
---|
| 253 | /// <summary>
|
---|
| 254 | /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
|
---|
| 255 | /// </summary>
|
---|
| 256 | /// <returns>The current instance as a string.</returns>
|
---|
| 257 | public override string ToString() {
|
---|
| 258 | return Name;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | public virtual bool IsParallelizable {
|
---|
| 262 | get { return false; }
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | }
|
---|