Changeset 3274 for trunk/sources/HeuristicLab.Optimization/3.3
- Timestamp:
- 04/06/10 01:57:29 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Optimization/3.3
- Files:
-
- 2 added
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization/3.3/Algorithm.cs
r3262 r3274 124 124 } 125 125 126 public virtual void Prepare() { 126 public void Prepare() { 127 Prepare(true); 128 } 129 public virtual void Prepare(bool clearResults) { 127 130 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 128 131 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); -
trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs
r3265 r3274 33 33 [Creatable("Testing & Analysis")] 34 34 [StorableClass] 35 public sealed class BatchRun : NamedItem, I Executable{35 public sealed class BatchRun : NamedItem, IOptimizer { 36 36 public override Image ItemImage { 37 37 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; } … … 147 147 Prepare(true); 148 148 } 149 public void Prepare(bool clearR uns) {149 public void Prepare(bool clearResults) { 150 150 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 151 151 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 152 152 if (Algorithm != null) { 153 if (clearR uns) {153 if (clearResults) { 154 154 ExecutionTime = TimeSpan.Zero; 155 155 runs.Clear(); -
trunk/sources/HeuristicLab.Optimization/3.3/EngineAlgorithm.cs
r3262 r3274 22 22 using System; 23 23 using System.Linq; 24 using HeuristicLab.Collections;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 158 157 } 159 158 160 public override void Prepare() { 161 base.Prepare(); 159 public override void Prepare(bool clearResults) { 160 base.Prepare(clearResults); 161 162 ResultCollection results = Results; 162 163 globalScope.Clear(); 163 globalScope.Variables.Add(new Variable("Results", new ResultCollection())); 164 if (clearResults) 165 globalScope.Variables.Add(new Variable("Results", new ResultCollection())); 166 else 167 globalScope.Variables.Add(new Variable("Results", results)); 164 168 165 169 if (engine != null) { -
trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs
r3267 r3274 35 35 [Creatable("Testing & Analysis")] 36 36 [StorableClass] 37 public sealed class Experiment : NamedItem, I Executable{37 public sealed class Experiment : NamedItem, IOptimizer { 38 38 public override Image ItemImage { 39 39 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; } … … 62 62 } 63 63 64 private BatchRunList batchRuns;64 private OptimizerList optimizers; 65 65 [Storable] 66 public BatchRunList BatchRuns {67 get { return batchRuns; }66 public OptimizerList Optimizers { 67 get { return optimizers; } 68 68 private set { 69 if ( batchRuns != value) {70 if ( batchRuns != null) DeregisterBatchRunsEvents();71 batchRuns = value;72 if ( batchRuns != null) RegisterBatchRunsEvents();73 foreach ( BatchRun batchRun in batchRuns)74 Register BatchRunEvents(batchRun);69 if (optimizers != value) { 70 if (optimizers != null) DeregisterOptimizersEvents(); 71 optimizers = value; 72 if (optimizers != null) RegisterOptimizersEvents(); 73 foreach (IOptimizer optimizer in optimizers) 74 RegisterOptimizerEvents(optimizer); 75 75 } 76 76 } 77 }78 79 [Storable]80 private RunCollection runs;81 public RunCollection Runs {82 get { return runs; }83 77 } 84 78 … … 89 83 executionState = ExecutionState.Stopped; 90 84 executionTime = TimeSpan.Zero; 91 BatchRuns = new BatchRunList(); 92 runs = new RunCollection(); 85 Optimizers = new OptimizerList(); 93 86 stopPending = false; 94 87 } … … 96 89 executionState = ExecutionState.Stopped; 97 90 executionTime = TimeSpan.Zero; 98 BatchRuns = new BatchRunList(); 99 runs = new RunCollection(); 91 Optimizers = new OptimizerList(); 100 92 stopPending = false; 101 93 } … … 103 95 executionState = ExecutionState.Stopped; 104 96 executionTime = TimeSpan.Zero; 105 BatchRuns = new BatchRunList(); 106 runs = new RunCollection(); 97 Optimizers = new OptimizerList(); 107 98 stopPending = false; 108 99 } … … 112 103 clone.executionState = executionState; 113 104 clone.executionTime = executionTime; 114 clone.BatchRuns = (BatchRunList)cloner.Clone(batchRuns); 115 clone.runs = (RunCollection)cloner.Clone(runs); 105 clone.Optimizers = (OptimizerList)cloner.Clone(optimizers); 116 106 clone.stopPending = stopPending; 117 107 return clone; … … 121 111 Prepare(true); 122 112 } 123 public void Prepare(bool clearR uns) {113 public void Prepare(bool clearResults) { 124 114 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 125 115 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 126 if (BatchRuns.Count > 0) { 127 if (clearRuns) { 128 ExecutionTime = TimeSpan.Zero; 129 runs.Clear(); 130 } 131 foreach (BatchRun batchRun in BatchRuns.Where(x => x.ExecutionState != ExecutionState.Started)) 132 batchRun.Prepare(clearRuns); 116 if (Optimizers.Count > 0) { 117 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) 118 optimizer.Prepare(clearResults); 133 119 } 134 120 } … … 137 123 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 138 124 stopPending = false; 139 if ( BatchRuns.Count > 0) {140 BatchRun batchRun = BatchRuns.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));141 if ( batchRun != null) batchRun.Start();125 if (Optimizers.Count > 0) { 126 IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)); 127 if (optimizer != null) optimizer.Start(); 142 128 } 143 129 } … … 145 131 if (ExecutionState != ExecutionState.Started) 146 132 throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState)); 147 if ( BatchRuns.Count > 0) {148 foreach ( BatchRun batchRun in BatchRuns.Where(x => x.ExecutionState == ExecutionState.Started))149 batchRun.Pause();133 if (Optimizers.Count > 0) { 134 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) 135 optimizer.Pause(); 150 136 } 151 137 } … … 154 140 throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState)); 155 141 stopPending = true; 156 if ( BatchRuns.Count > 0) {157 foreach ( BatchRun batchRun in BatchRuns.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))158 batchRun.Stop();142 if (Optimizers.Count > 0) { 143 foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) 144 optimizer.Stop(); 159 145 } 160 146 } … … 201 187 } 202 188 203 private void RegisterBatchRunsEvents() { 204 BatchRuns.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<BatchRun>>(BatchRuns_CollectionReset); 205 BatchRuns.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<BatchRun>>(BatchRuns_ItemsAdded); 206 BatchRuns.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<BatchRun>>(BatchRuns_ItemsRemoved); 207 BatchRuns.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<BatchRun>>(BatchRuns_ItemsReplaced); 208 } 209 private void DeregisterBatchRunsEvents() { 210 BatchRuns.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<BatchRun>>(BatchRuns_CollectionReset); 211 BatchRuns.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<BatchRun>>(BatchRuns_ItemsAdded); 212 BatchRuns.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<BatchRun>>(BatchRuns_ItemsRemoved); 213 BatchRuns.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<BatchRun>>(BatchRuns_ItemsReplaced); 214 } 215 private void BatchRuns_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<BatchRun>> e) { 216 foreach (IndexedItem<BatchRun> item in e.OldItems) { 217 DeregisterBatchRunEvents(item.Value); 218 } 219 foreach (IndexedItem<BatchRun> item in e.Items) { 220 RegisterBatchRunEvents(item.Value); 221 } 222 } 223 private void BatchRuns_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<BatchRun>> e) { 224 foreach (IndexedItem<BatchRun> item in e.Items) { 225 RegisterBatchRunEvents(item.Value); 226 } 227 } 228 private void BatchRuns_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<BatchRun>> e) { 229 foreach (IndexedItem<BatchRun> item in e.Items) { 230 DeregisterBatchRunEvents(item.Value); 231 } 232 } 233 private void BatchRuns_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<BatchRun>> e) { 234 foreach (IndexedItem<BatchRun> item in e.OldItems) { 235 DeregisterBatchRunEvents(item.Value); 236 } 237 foreach (IndexedItem<BatchRun> item in e.Items) { 238 RegisterBatchRunEvents(item.Value); 239 } 240 } 241 242 private void RegisterBatchRunEvents(BatchRun batchRun) { 243 batchRun.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(batchRun_ExceptionOccurred); 244 batchRun.Paused += new EventHandler(batchRun_Paused); 245 batchRun.Prepared += new EventHandler(batchRun_Prepared); 246 batchRun.Started += new EventHandler(batchRun_Started); 247 batchRun.Stopped += new EventHandler(batchRun_Stopped); 248 } 249 private void DeregisterBatchRunEvents(BatchRun batchRun) { 250 batchRun.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(batchRun_ExceptionOccurred); 251 batchRun.Paused -= new EventHandler(batchRun_Paused); 252 batchRun.Prepared -= new EventHandler(batchRun_Prepared); 253 batchRun.Started -= new EventHandler(batchRun_Started); 254 batchRun.Stopped -= new EventHandler(batchRun_Stopped); 255 } 256 private void batchRun_ExceptionOccurred(object sender, EventArgs<Exception> e) { 189 private void RegisterOptimizersEvents() { 190 Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset); 191 Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded); 192 Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved); 193 Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced); 194 } 195 private void DeregisterOptimizersEvents() { 196 Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset); 197 Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded); 198 Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved); 199 Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced); 200 } 201 private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) { 202 foreach (IndexedItem<IOptimizer> item in e.OldItems) { 203 DeregisterOptimizerEvents(item.Value); 204 } 205 foreach (IndexedItem<IOptimizer> item in e.Items) { 206 RegisterOptimizerEvents(item.Value); 207 } 208 } 209 private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) { 210 foreach (IndexedItem<IOptimizer> item in e.Items) { 211 RegisterOptimizerEvents(item.Value); 212 } 213 } 214 private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) { 215 foreach (IndexedItem<IOptimizer> item in e.Items) { 216 DeregisterOptimizerEvents(item.Value); 217 } 218 } 219 private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) { 220 foreach (IndexedItem<IOptimizer> item in e.OldItems) { 221 DeregisterOptimizerEvents(item.Value); 222 } 223 foreach (IndexedItem<IOptimizer> item in e.Items) { 224 RegisterOptimizerEvents(item.Value); 225 } 226 } 227 228 private void RegisterOptimizerEvents(IOptimizer optimizer) { 229 optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred); 230 optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged); 231 optimizer.Paused += new EventHandler(optimizer_Paused); 232 optimizer.Prepared += new EventHandler(optimizer_Prepared); 233 optimizer.Started += new EventHandler(optimizer_Started); 234 optimizer.Stopped += new EventHandler(optimizer_Stopped); 235 } 236 237 private void DeregisterOptimizerEvents(IOptimizer optimizer) { 238 optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred); 239 optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged); 240 optimizer.Paused -= new EventHandler(optimizer_Paused); 241 optimizer.Prepared -= new EventHandler(optimizer_Prepared); 242 optimizer.Started -= new EventHandler(optimizer_Started); 243 optimizer.Stopped -= new EventHandler(optimizer_Stopped); 244 } 245 private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) { 257 246 OnExceptionOccurred(e.Value); 258 247 } 259 private void batchRun_Paused(object sender, EventArgs e) { 260 if (BatchRuns.All(x => x.ExecutionState != ExecutionState.Started)) 248 private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) { 249 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime); 250 } 251 private void optimizer_Paused(object sender, EventArgs e) { 252 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) 261 253 OnPaused(); 262 254 } 263 void batchRun_Prepared(object sender, EventArgs e) {255 private void optimizer_Prepared(object sender, EventArgs e) { 264 256 if (ExecutionState == ExecutionState.Stopped) 265 257 OnPrepared(); 266 258 } 267 private void batchRun_Started(object sender, EventArgs e) {259 private void optimizer_Started(object sender, EventArgs e) { 268 260 if (ExecutionState != ExecutionState.Started) 269 261 OnStarted(); 270 262 } 271 private void batchRun_Stopped(object sender, EventArgs e) {263 private void optimizer_Stopped(object sender, EventArgs e) { 272 264 bool stop = stopPending; 273 BatchRun batchRun = (BatchRun)sender; 274 ExecutionTime += batchRun.ExecutionTime; 275 runs.AddRange(batchRun.Runs); 276 277 if (BatchRuns.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) { 265 266 if (Optimizers.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) { 278 267 stopPending = false; 279 268 OnStopped(); … … 281 270 282 271 if (!stop) { 283 BatchRun next = BatchRuns.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));272 IOptimizer next = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)); 284 273 if (next != null) 285 274 next.Start(); -
trunk/sources/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj
r3267 r3274 87 87 <Compile Include="Algorithm.cs" /> 88 88 <Compile Include="BatchRun.cs" /> 89 <Compile Include=" BatchRunList.cs" />89 <Compile Include="OptimizerList.cs" /> 90 90 <Compile Include="Experiment.cs" /> 91 <Compile Include="Interfaces\IOptimizer.cs" /> 91 92 <Compile Include="RunCollection.cs" /> 92 93 <Compile Include="Run.cs" /> -
trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IAlgorithm.cs
r3262 r3274 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common;25 24 using HeuristicLab.Core; 26 25 … … 29 28 /// Interface to represent an algorithm. 30 29 /// </summary> 31 public interface IAlgorithm : IParameterizedNamedItem, I Executable{30 public interface IAlgorithm : IParameterizedNamedItem, IOptimizer { 32 31 Type ProblemType { get; } 33 32 IProblem Problem { get; set; } -
trunk/sources/HeuristicLab.Optimization/3.3/ResultCollection.cs
r3226 r3274 28 28 [StorableClass] 29 29 [Item("ResultCollection", "Represents a collection of results.")] 30 public sealedclass ResultCollection : NamedItemCollection<IResult> {30 public class ResultCollection : NamedItemCollection<IResult> { 31 31 public ResultCollection() : base() { } 32 32 public ResultCollection(int capacity) : base(capacity) { } -
trunk/sources/HeuristicLab.Optimization/3.3/RunCollection.cs
r3260 r3274 28 28 [StorableClass] 29 29 [Item("RunCollection", "Represents a collection of runs.")] 30 public sealedclass RunCollection : ItemCollection<Run> {30 public class RunCollection : ItemCollection<Run> { 31 31 public RunCollection() : base() { } 32 32 public RunCollection(int capacity) : base(capacity) { }
Note: See TracChangeset
for help on using the changeset viewer.