- Timestamp:
- 01/12/18 13:33:23 (7 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs
r15287 r15603 64 64 } 65 65 } 66 67 [Storable] 68 private TimeSpan executionTime; 69 public TimeSpan ExecutionTime { 70 get { return executionTime; } 71 protected set { 72 executionTime = value; 73 OnExecutionTimeChanged(); 74 } 75 } 66 67 public abstract TimeSpan ExecutionTime { get; } 76 68 77 69 public virtual Type ProblemType { … … 133 125 : base() { 134 126 executionState = ExecutionState.Stopped; 135 executionTime = TimeSpan.Zero;136 127 storeAlgorithmInEachRun = false; 137 128 runsCounter = 0; … … 141 132 : base(name) { 142 133 executionState = ExecutionState.Stopped; 143 executionTime = TimeSpan.Zero;144 134 storeAlgorithmInEachRun = false; 145 135 runsCounter = 0; … … 149 139 : base(name, parameters) { 150 140 executionState = ExecutionState.Stopped; 151 executionTime = TimeSpan.Zero;152 141 storeAlgorithmInEachRun = false; 153 142 runsCounter = 0; … … 157 146 : base(name, description) { 158 147 executionState = ExecutionState.Stopped; 159 executionTime = TimeSpan.Zero;160 148 storeAlgorithmInEachRun = false; 161 149 runsCounter = 0; … … 165 153 : base(name, description, parameters) { 166 154 executionState = ExecutionState.Stopped; 167 executionTime = TimeSpan.Zero;168 155 storeAlgorithmInEachRun = false; 169 156 runsCounter = 0; … … 181 168 if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState)); 182 169 executionState = original.executionState; 183 executionTime = original.executionTime;184 170 problem = cloner.Clone(original.problem); 185 171 storeAlgorithmInEachRun = original.storeAlgorithmInEachRun; … … 259 245 if (handler != null) handler(this, EventArgs.Empty); 260 246 } 261 public event EventHandler ExecutionTimeChanged;262 protected virtual void OnExecutionTimeChanged() {263 EventHandler handler = ExecutionTimeChanged;264 if (handler != null) handler(this, EventArgs.Empty);265 }266 247 public event EventHandler ProblemChanged; 267 248 protected virtual void OnProblemChanged() { … … 276 257 public event EventHandler Prepared; 277 258 protected virtual void OnPrepared() { 278 ExecutionTime = TimeSpan.Zero;279 259 foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<object>() { Runs }).OfType<IStatefulItem>()) { 280 260 statefulObject.InitializeState(); … … 347 327 } 348 328 #endregion 329 330 [Obsolete("Deprecate, does nothing, needs to be removed from IExecutable")] 331 public event EventHandler ExecutionTimeChanged; 349 332 } 350 333 } -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r15367 r15603 21 21 22 22 using System; 23 using System.Linq;24 23 using System.Threading; 25 24 using HeuristicLab.Common; … … 32 31 33 32 private bool pausePending; 34 private DateTime lastUpdateTime;33 private bool executionTimeActive; 35 34 36 35 public string Filename { get; set; } … … 52 51 } 53 52 53 [Storable] 54 private TimeSpan executionTime; 55 private DateTime startDate; 56 public override TimeSpan ExecutionTime { 57 get { return executionTime + (executionTimeActive ? DateTime.UtcNow - startDate : TimeSpan.Zero); } 58 } 59 54 60 [StorableConstructor] 55 61 protected BasicAlgorithm(bool deserializing) : base(deserializing) { } … … 58 64 results = cloner.Clone(original.Results); 59 65 initialized = original.initialized; 66 executionTime = original.executionTime; 60 67 } 61 68 protected BasicAlgorithm() 62 69 : base() { 63 70 results = new ResultCollection(); 71 executionTime = TimeSpan.Zero; 64 72 } 65 73 66 74 public override void Prepare() { 75 executionTime = TimeSpan.Zero; 67 76 if (Problem == null) return; 68 77 base.Prepare(); … … 73 82 74 83 public override void Start(CancellationToken cancellationToken) { 84 startDate = DateTime.UtcNow; 85 executionTimeActive = true; 75 86 base.Start(cancellationToken); 76 87 CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); … … 117 128 private void Run(object state) { 118 129 CancellationToken cancellationToken = (CancellationToken)state; 119 lastUpdateTime = DateTime.UtcNow; 120 System.Timers.Timer timer = new System.Timers.Timer(250); 121 timer.AutoReset = true; 122 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 123 timer.Start(); 124 try { 125 if (!initialized) 126 Initialize(cancellationToken); 127 initialized = true; 128 Run(cancellationToken); 129 } 130 finally { 131 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 132 timer.Stop(); 133 ExecutionTime += DateTime.UtcNow - lastUpdateTime; 134 } 130 if (!initialized) 131 Initialize(cancellationToken); 132 initialized = true; 133 Run(cancellationToken); 135 134 } 136 135 … … 138 137 protected abstract void Run(CancellationToken cancellationToken); 139 138 140 #region Events 141 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { 142 System.Timers.Timer timer = (System.Timers.Timer)sender; 143 timer.Enabled = false; 144 DateTime now = DateTime.UtcNow; 145 ExecutionTime += now - lastUpdateTime; 146 lastUpdateTime = now; 147 timer.Enabled = true; 139 protected override void OnPaused() { 140 executionTime += DateTime.UtcNow - startDate; 141 executionTimeActive = false; 142 base.OnPaused(); 148 143 } 149 #endregion150 144 145 protected override void OnStopped() { 146 if (executionTimeActive) { // don't do if going from pause to stop 147 executionTime += DateTime.UtcNow - startDate; 148 executionTimeActive = false; 149 } 150 base.OnStopped(); 151 } 151 152 } 152 153 } -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs
r15287 r15603 71 71 } 72 72 73 public override TimeSpan ExecutionTime { 74 get { return engine.ExecutionTime; } 75 } 76 73 77 public override ResultCollection Results { 74 78 get { … … 195 199 private void RegisterEngineEvents() { 196 200 Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred); 197 Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);198 201 Engine.Paused += new EventHandler(Engine_Paused); 199 202 Engine.Prepared += new EventHandler(Engine_Prepared); … … 203 206 private void DeregisterEngineEvents() { 204 207 Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred); 205 Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);206 208 Engine.Paused -= new EventHandler(Engine_Paused); 207 209 Engine.Prepared -= new EventHandler(Engine_Prepared); … … 211 213 private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) { 212 214 OnExceptionOccurred(e.Value); 213 }214 private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {215 ExecutionTime = Engine.ExecutionTime;216 215 } 217 216 private void Engine_Paused(object sender, EventArgs e) { -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationAlgorithm.cs
r14185 r15603 21 21 22 22 using System; 23 using System.Threading; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 37 38 } 38 39 39 protected HeuristicOptimizationAlgorithm() : base() { } 40 protected HeuristicOptimizationAlgorithm(string name) : base(name) { } 41 protected HeuristicOptimizationAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { } 42 protected HeuristicOptimizationAlgorithm(string name, string description) : base(name, description) { } 43 protected HeuristicOptimizationAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { } 40 [Storable] 41 private TimeSpan executionTime; 42 private DateTime startDate; 43 public override TimeSpan ExecutionTime { 44 get { return executionTime + (ExecutionState == ExecutionState.Started ? DateTime.UtcNow - startDate : TimeSpan.Zero); } 45 } 46 47 protected HeuristicOptimizationAlgorithm() : base() { 48 executionTime = TimeSpan.Zero; 49 } 50 protected HeuristicOptimizationAlgorithm(string name) : base(name) { 51 executionTime = TimeSpan.Zero; 52 } 53 protected HeuristicOptimizationAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { 54 executionTime = TimeSpan.Zero; 55 } 56 protected HeuristicOptimizationAlgorithm(string name, string description) : base(name, description) { 57 executionTime = TimeSpan.Zero; 58 } 59 protected HeuristicOptimizationAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { 60 executionTime = TimeSpan.Zero; 61 } 44 62 45 63 [StorableConstructor] 46 64 protected HeuristicOptimizationAlgorithm(bool deserializing) : base(deserializing) { } 47 protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner) : base(original, cloner) { } 65 protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner) 66 : base(original, cloner) { 67 executionTime = original.executionTime; 68 } 69 70 public override void Prepare() { 71 executionTime = TimeSpan.Zero; 72 base.Prepare(); 73 } 74 75 public override void Start(CancellationToken cancellationToken) { 76 startDate = DateTime.UtcNow; 77 base.Start(cancellationToken); 78 } 48 79 49 80 #region Events … … 62 93 protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { } 63 94 #endregion 95 96 protected override void OnPaused() { 97 var elapsed = DateTime.UtcNow - startDate; 98 executionTime += elapsed; 99 base.OnPaused(); 100 } 101 102 protected override void OnStopped() { 103 if (ExecutionState == ExecutionState.Started) { 104 var elapsed = DateTime.UtcNow - startDate; 105 executionTime += elapsed; 106 } 107 base.OnStopped(); 108 } 64 109 } 65 110 }
Note: See TracChangeset
for help on using the changeset viewer.