Changeset 4423 for branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/Jobs/OptimizerJob.cs
- Timestamp:
- 09/17/10 10:26:55 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/Jobs/OptimizerJob.cs
r4368 r4423 1 1 using System; 2 using System.Linq; 2 3 using System.Collections.Generic; 3 4 using HeuristicLab.Core; … … 5 6 using HeuristicLab.Optimization; 6 7 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 8 using HeuristicLab.Common; 9 using HeuristicLab.Hive.Contracts.BusinessObjects; 7 10 8 11 namespace HeuristicLab.Hive.Experiment.Jobs { 9 12 [StorableClass] 10 13 public class OptimizerJob : IJob { 11 [Storable]12 protected Guid internalId;13 public Guid InternalId {14 get { return internalId; }15 }16 17 14 [Storable] 18 15 protected IOptimizer optimizer; … … 20 17 get { return optimizer; } 21 18 set { 22 if (optimizer != null && value != optimizer) { 23 DeregisterEvents(); 24 } 25 optimizer = value; 26 if (optimizer != null) { 27 RegisterEvents(); 19 if (value != optimizer) { 20 if (optimizer != null) { 21 DeregisterEvents(); 22 } 23 optimizer = value; 24 if (optimizer != null) { 25 RegisterEvents(); 26 } 27 OnOptimizerChanged(); 28 28 } 29 29 } 30 30 } 31 32 [Storable]33 protected double progress = 0.0;34 31 35 32 [Storable] … … 40 37 41 38 [Storable] 42 protected ICollection<IJob> childJobs;43 public IEnumerable<IJob> ChildJobs {44 get { return childJobs; }45 }46 47 [Storable]48 39 protected bool computeInParallel; 49 40 public bool ComputeInParallel { 50 41 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; } 42 set { 43 if (computeInParallel != value) { 44 computeInParallel = value; 45 OnComputeInParallelChanged(); 46 } 47 } 48 } 49 50 [Storable] 51 private int indexInParentOptimizerList = -1; 52 public int IndexInParentOptimizerList { 53 get { return indexInParentOptimizerList; } 54 set { this.indexInParentOptimizerList = value; } 55 } 56 57 58 [Storable] 59 private bool collectChildJobs; 60 public bool CollectChildJobs { 61 get { return collectChildJobs; } 62 set { collectChildJobs = value; } 66 63 } 67 64 68 65 public OptimizerJob() { 69 this.internalId = Guid.NewGuid();70 66 this.log = new Log(); 71 this.childJobs = new List<IJob>();72 this.ComputeInParallel = false;73 this.CoresNeeded = 1;74 this.MemoryNeeded = 0;75 67 } 76 68 … … 78 70 : this() { 79 71 this.optimizer = optimizer; 72 73 if (optimizer is Optimization.Experiment) { 74 this.ComputeInParallel = true; 75 } else if (optimizer is Optimization.BatchRun) { 76 this.ComputeInParallel = false; 77 } else { 78 this.ComputeInParallel = false; 79 } 80 80 } 81 81 … … 85 85 } 86 86 87 /// <summary> 88 /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull. 89 /// </summary> 90 public Optimization.Experiment OptimizerAsExperiment { 91 get { return Optimizer as Optimization.Experiment; } 92 } 93 94 /// <summary> 95 /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull. 96 /// </summary> 97 public Optimization.BatchRun OptimizerAsBatchRun { 98 get { return Optimizer as Optimization.BatchRun; } 99 } 100 87 101 #region IJob Members 88 89 public double Progress {90 get { return optimizer.ExecutionState == ExecutionState.Stopped ? 1.0 : this.progress; }91 }92 102 93 103 public virtual ExecutionState ExecutionState { … … 95 105 } 96 106 107 public TimeSpan ExecutionTime { 108 get { return optimizer.ExecutionTime; } 109 } 110 97 111 public virtual void Run() { 98 112 throw new NotSupportedException(); … … 104 118 105 119 public virtual void Start() { 106 optimizer.Start(); 120 if ((optimizer is Optimization.Experiment && OptimizerAsExperiment.Optimizers.Count == 0) || // experiment would not fire OnStopped if it has 0 optimizers 121 (optimizer is Optimization.BatchRun && OptimizerAsBatchRun.Algorithm == null)) { // batchrun would not fire OnStopped if algorithm == null 122 OnJobStopped(); 123 } else { 124 optimizer.Start(); 125 } 107 126 } 108 127 … … 112 131 113 132 public virtual void Resume(IEnumerable<IJob> childJobs) { 114 throw new NotSupportedException();133 OnJobStopped(); 115 134 } 116 135 … … 122 141 123 142 public event EventHandler JobFailed; 124 protected virtual void OnJobFailed( Common.EventArgs<Exception> e) {143 protected virtual void OnJobFailed(EventArgs<Exception> e) { 125 144 EventHandler handler = JobFailed; 126 145 if (handler != null) handler(this, e); 127 146 } 128 147 129 public event EventHandler< Common.EventArgs<IJob>> NewChildJob;148 public event EventHandler<EventArgs<IJob>> NewChildJob; 130 149 protected virtual void OnNewChildJob(IJob job) { 131 150 EventHandler<Common.EventArgs<IJob>> handler = NewChildJob; 132 if (handler != null) handler(this, new Common.EventArgs<IJob>(job));151 if (handler != null) handler(this, new EventArgs<IJob>(job)); 133 152 } 134 153 … … 139 158 } 140 159 160 public event EventHandler DeleteChildJobs; 161 protected virtual void OnDeleteChildJobs() { 162 EventHandler handler = DeleteChildJobs; 163 if (handler != null) handler(this, EventArgs.Empty); 164 } 165 166 public event EventHandler ComputeInParallelChanged; 167 protected virtual void OnComputeInParallelChanged() { 168 EventHandler handler = ComputeInParallelChanged; 169 if (handler != null) handler(this, EventArgs.Empty); 170 } 171 172 public event EventHandler OptimizerChanged; 173 protected virtual void OnOptimizerChanged() { 174 EventHandler handler = OptimizerChanged; 175 if (handler != null) handler(this, EventArgs.Empty); 176 } 141 177 #endregion 142 178 … … 165 201 166 202 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 203 OnJobFailed(e); 170 204 } 171 205 172 206 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 207 OnJobStopped(); 176 208 } … … 237 269 #region IDeepCloneable Members 238 270 239 public Common.IDeepCloneable Clone(Common.Cloner cloner) { 240 throw new NotImplementedException(); 271 public Common.IDeepCloneable Clone(Cloner cloner) { 272 OptimizerJob clone = (OptimizerJob)Activator.CreateInstance(this.GetType()); 273 cloner.RegisterClonedObject(this, clone); 274 clone.Optimizer = (IOptimizer)cloner.Clone(this.Optimizer); 275 clone.log = (ILog)cloner.Clone(this.Log); 276 clone.ComputeInParallel = this.ComputeInParallel; 277 clone.IndexInParentOptimizerList = this.IndexInParentOptimizerList; 278 clone.CollectChildJobs = this.CollectChildJobs; 279 clone.RegisterEvents(); 280 return clone; 241 281 } 242 282 … … 246 286 247 287 public object Clone() { 248 throw new NotImplementedException();288 return Clone(new Cloner()); 249 289 } 250 290 … … 258 298 return Name; 259 299 } 260 300 261 301 public virtual bool IsParallelizable { 262 get { return false; } 263 } 302 get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; } 303 } 304 264 305 } 265 306 }
Note: See TracChangeset
for help on using the changeset viewer.