Changeset 3262 for trunk/sources/HeuristicLab.Optimization/3.3
- Timestamp:
- 04/04/10 05:22:47 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Optimization/3.3
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization/3.3/Algorithm.cs
r3261 r3262 36 36 public override Image ItemImage { 37 37 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; } 38 } 39 40 [Storable] 41 private ExecutionState executionState; 42 public ExecutionState ExecutionState { 43 get { return executionState; } 44 private set { 45 if (executionState != value) { 46 executionState = value; 47 OnExecutionStateChanged(); 48 } 49 } 50 } 51 52 [Storable] 53 private TimeSpan executionTime; 54 public TimeSpan ExecutionTime { 55 get { return executionTime; } 56 protected set { 57 executionTime = value; 58 OnExecutionTimeChanged(); 59 } 38 60 } 39 61 … … 68 90 public abstract ResultCollection Results { get; } 69 91 70 public abstract TimeSpan ExecutionTime { get; } 71 72 private bool running; 73 public bool Running { 74 get { return running; } 75 protected set { 76 if (running != value) { 77 running = value; 78 OnRunningChanged(); 79 } 80 } 81 } 82 83 public abstract bool Finished { get; } 84 85 private bool canceled; 86 protected bool Canceled { 87 get { return canceled; } 88 private set { 89 if (canceled != value) { 90 canceled = value; 91 OnCanceledChanged(); 92 } 93 } 94 } 95 96 protected Algorithm() : base() { } 97 protected Algorithm(string name) : base(name) { } 98 protected Algorithm(string name, ParameterCollection parameters) : base(name, parameters) { } 99 protected Algorithm(string name, string description) : base(name, description) { } 100 protected Algorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { } 92 protected Algorithm() 93 : base() { 94 executionState = ExecutionState.Stopped; 95 executionTime = TimeSpan.Zero; 96 } 97 protected Algorithm(string name) 98 : base(name) { 99 executionState = ExecutionState.Stopped; 100 executionTime = TimeSpan.Zero; 101 } 102 protected Algorithm(string name, ParameterCollection parameters) 103 : base(name, parameters) { 104 executionState = ExecutionState.Stopped; 105 executionTime = TimeSpan.Zero; 106 } 107 protected Algorithm(string name, string description) 108 : base(name, description) { 109 executionState = ExecutionState.Stopped; 110 executionTime = TimeSpan.Zero; 111 } 112 protected Algorithm(string name, string description, ParameterCollection parameters) 113 : base(name, description, parameters) { 114 executionState = ExecutionState.Stopped; 115 executionTime = TimeSpan.Zero; 116 } 101 117 102 118 public override IDeepCloneable Clone(Cloner cloner) { 103 119 Algorithm clone = (Algorithm)base.Clone(cloner); 120 clone.executionState = executionState; 121 clone.executionTime = executionTime; 104 122 clone.Problem = (IProblem)cloner.Clone(problem); 105 clone.running = running;106 clone.canceled = canceled;107 123 return clone; 108 124 } 109 125 110 public void Prepare() { 111 OnPrepared(); 112 } 113 public void Start() { 114 OnStarted(); 115 Running = true; 116 Canceled = false; 117 } 118 public void Stop() { 119 Canceled = true; 126 public virtual void Prepare() { 127 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 128 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 129 ExecutionTime = TimeSpan.Zero; 130 } 131 public virtual void Start() { 132 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 133 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 134 } 135 public virtual void Pause() { 136 if (ExecutionState != ExecutionState.Started) 137 throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState)); 138 } 139 public virtual void Stop() { 140 if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused)) 141 throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState)); 120 142 } 121 143 … … 130 152 131 153 #region Events 154 public event EventHandler ExecutionStateChanged; 155 protected virtual void OnExecutionStateChanged() { 156 EventHandler handler = ExecutionStateChanged; 157 if (handler != null) handler(this, EventArgs.Empty); 158 } 159 public event EventHandler ExecutionTimeChanged; 160 protected virtual void OnExecutionTimeChanged() { 161 EventHandler handler = ExecutionTimeChanged; 162 if (handler != null) handler(this, EventArgs.Empty); 163 } 132 164 public event EventHandler ProblemChanged; 133 165 protected virtual void OnProblemChanged() { 134 if (ProblemChanged != null) 135 ProblemChanged(this, EventArgs.Empty); 136 } 137 public event EventHandler ExecutionTimeChanged; 138 protected virtual void OnExecutionTimeChanged() { 139 if (ExecutionTimeChanged != null) 140 ExecutionTimeChanged(this, EventArgs.Empty); 141 } 142 public event EventHandler RunningChanged; 143 protected virtual void OnRunningChanged() { 144 if (RunningChanged != null) 145 RunningChanged(this, EventArgs.Empty); 166 EventHandler handler = ProblemChanged; 167 if (handler != null) handler(this, EventArgs.Empty); 146 168 } 147 169 public event EventHandler Prepared; 148 170 protected virtual void OnPrepared() { 149 if (Prepared != null) 150 Prepared(this, EventArgs.Empty); 171 ExecutionState = ExecutionState.Prepared; 172 EventHandler handler = Prepared; 173 if (handler != null) handler(this, EventArgs.Empty); 151 174 } 152 175 public event EventHandler Started; 153 176 protected virtual void OnStarted() { 154 if (Started != null) 155 Started(this, EventArgs.Empty); 177 ExecutionState = ExecutionState.Started; 178 EventHandler handler = Started; 179 if (handler != null) handler(this, EventArgs.Empty); 180 } 181 public event EventHandler Paused; 182 protected virtual void OnPaused() { 183 ExecutionState = ExecutionState.Paused; 184 EventHandler handler = Paused; 185 if (handler != null) handler(this, EventArgs.Empty); 156 186 } 157 187 public event EventHandler Stopped; 158 188 protected virtual void OnStopped() { 159 Canceled = false; 160 Running = false; 161 if (Stopped != null) 162 Stopped(this, EventArgs.Empty); 163 } 164 protected virtual void OnCanceledChanged() { } 189 ExecutionState = ExecutionState.Stopped; 190 EventHandler handler = Stopped; 191 if (handler != null) handler(this, EventArgs.Empty); 192 } 165 193 public event EventHandler<EventArgs<Exception>> ExceptionOccurred; 166 194 protected virtual void OnExceptionOccurred(Exception exception) { 167 if (ExceptionOccurred != null)168 ExceptionOccurred(this, new EventArgs<Exception>(exception));195 EventHandler<EventArgs<Exception>> handler = ExceptionOccurred; 196 if (handler != null) handler(this, new EventArgs<Exception>(exception)); 169 197 } 170 198 -
trunk/sources/HeuristicLab.Optimization/3.3/EngineAlgorithm.cs
r3261 r3262 94 94 } 95 95 96 public override TimeSpan ExecutionTime {97 get {98 if (engine == null) return TimeSpan.Zero;99 else return engine.ExecutionTime;100 }101 }102 103 public override bool Finished {104 get {105 if (engine == null) return true;106 else return engine.Finished;107 }108 }109 110 96 protected EngineAlgorithm() 111 97 : base() { … … 172 158 } 173 159 174 protected override void OnCanceledChanged() { 175 if (Canceled && (engine != null)) 176 engine.Stop(); 177 } 178 protected override void OnPrepared() { 160 public override void Prepare() { 161 base.Prepare(); 179 162 globalScope.Clear(); 180 163 globalScope.Variables.Add(new Variable("Results", new ResultCollection())); … … 195 178 engine.Prepare(context); 196 179 } 197 base.OnPrepared();198 }199 protected override void OnStarted() {180 } 181 public override void Start() { 182 base.Start(); 200 183 if (engine != null) engine.Start(); 201 base.OnStarted(); 202 } 203 204 protected virtual void OnOperatorGraphChanged() { } 205 184 } 185 public override void Pause() { 186 base.Pause(); 187 if (engine != null) engine.Pause(); 188 } 189 public override void Stop() { 190 base.Stop(); 191 if (engine != null) engine.Stop(); 192 } 193 194 #region Events 206 195 public event EventHandler EngineChanged; 207 196 protected virtual void OnEngineChanged() { … … 209 198 EngineChanged(this, EventArgs.Empty); 210 199 } 211 212 private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) { 213 Prepare(); 214 } 200 protected virtual void OnOperatorGraphChanged() { } 201 215 202 private void RegisterEngineEvents() { 216 203 Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred); 217 204 Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged); 205 Engine.Paused += new EventHandler(Engine_Paused); 206 Engine.Prepared += new EventHandler(Engine_Prepared); 207 Engine.Started += new EventHandler(Engine_Started); 218 208 Engine.Stopped += new EventHandler(Engine_Stopped); 219 209 } 220 221 210 private void DeregisterEngineEvents() { 222 211 Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred); 223 212 Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged); 213 Engine.Paused -= new EventHandler(Engine_Paused); 214 Engine.Prepared -= new EventHandler(Engine_Prepared); 215 Engine.Started -= new EventHandler(Engine_Started); 224 216 Engine.Stopped -= new EventHandler(Engine_Stopped); 225 217 } 226 227 218 private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) { 228 219 OnExceptionOccurred(e.Value); 229 220 } 230 221 private void Engine_ExecutionTimeChanged(object sender, EventArgs e) { 231 OnExecutionTimeChanged(); 222 ExecutionTime = Engine.ExecutionTime; 223 } 224 private void Engine_Paused(object sender, EventArgs e) { 225 OnPaused(); 226 } 227 private void Engine_Prepared(object sender, EventArgs e) { 228 OnPrepared(); 229 } 230 private void Engine_Started(object sender, EventArgs e) { 231 OnStarted(); 232 232 } 233 233 private void Engine_Stopped(object sender, EventArgs e) { 234 234 OnStopped(); 235 235 } 236 237 private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) { 238 Prepare(); 239 } 240 #endregion 236 241 } 237 242 } -
trunk/sources/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj
r3260 r3262 86 86 <None Include="HeuristicLabOptimizationPlugin.cs.frame" /> 87 87 <Compile Include="Algorithm.cs" /> 88 <Compile Include="BatchRun.cs" />89 88 <Compile Include="RunCollection.cs" /> 90 89 <Compile Include="Run.cs" /> -
trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IAlgorithm.cs
r3260 r3262 29 29 /// Interface to represent an algorithm. 30 30 /// </summary> 31 public interface IAlgorithm : IParameterizedNamedItem {31 public interface IAlgorithm : IParameterizedNamedItem, IExecutable { 32 32 Type ProblemType { get; } 33 33 IProblem Problem { get; set; } 34 34 ResultCollection Results { get; } 35 TimeSpan ExecutionTime { get; }36 bool Running { get; }37 bool Finished { get; }38 39 void Prepare();40 void Start();41 void Stop();42 35 43 36 void CollectResultValues(IDictionary<string, IItem> values); 44 37 45 38 event EventHandler ProblemChanged; 46 event EventHandler ExecutionTimeChanged;47 event EventHandler RunningChanged;48 event EventHandler Prepared;49 event EventHandler Started;50 event EventHandler Stopped;51 event EventHandler<EventArgs<Exception>> ExceptionOccurred;52 39 } 53 40 } -
trunk/sources/HeuristicLab.Optimization/3.3/UserDefinedAlgorithm.cs
r3017 r3262 55 55 public event EventHandler OperatorGraphChanged; 56 56 protected override void OnOperatorGraphChanged() { 57 if (OperatorGraphChanged != null)58 OperatorGraphChanged(this, EventArgs.Empty);57 EventHandler handler = OperatorGraphChanged; 58 if (handler != null) handler(this, EventArgs.Empty); 59 59 } 60 60 }
Note: See TracChangeset
for help on using the changeset viewer.