Changeset 3275 for trunk/sources/HeuristicLab.Optimization
- Timestamp:
- 04/06/10 03:37:29 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Optimization/3.3
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/Algorithm.cs ¶
r3274 r3275 90 90 public abstract ResultCollection Results { get; } 91 91 92 [Storable] 93 private RunCollection runs; 94 public RunCollection Runs { 95 get { return runs; } 96 } 97 92 98 protected Algorithm() 93 99 : base() { 94 100 executionState = ExecutionState.Stopped; 95 101 executionTime = TimeSpan.Zero; 102 runs = new RunCollection(); 96 103 } 97 104 protected Algorithm(string name) … … 99 106 executionState = ExecutionState.Stopped; 100 107 executionTime = TimeSpan.Zero; 108 runs = new RunCollection(); 101 109 } 102 110 protected Algorithm(string name, ParameterCollection parameters) … … 104 112 executionState = ExecutionState.Stopped; 105 113 executionTime = TimeSpan.Zero; 114 runs = new RunCollection(); 106 115 } 107 116 protected Algorithm(string name, string description) … … 109 118 executionState = ExecutionState.Stopped; 110 119 executionTime = TimeSpan.Zero; 120 runs = new RunCollection(); 111 121 } 112 122 protected Algorithm(string name, string description, ParameterCollection parameters) … … 114 124 executionState = ExecutionState.Stopped; 115 125 executionTime = TimeSpan.Zero; 126 runs = new RunCollection(); 116 127 } 117 128 … … 121 132 clone.executionTime = executionTime; 122 133 clone.Problem = (IProblem)cloner.Clone(problem); 134 clone.runs = (RunCollection)cloner.Clone(runs); 123 135 return clone; 124 136 } 125 137 126 public void Prepare() { 127 Prepare(true); 128 } 129 public virtual void Prepare(bool clearResults) { 138 public virtual void Prepare() { 130 139 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 131 140 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 132 ExecutionTime = TimeSpan.Zero; 141 } 142 public void Prepare(bool clearRuns) { 143 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 144 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 145 if (clearRuns) runs.Clear(); 146 Prepare(); 133 147 } 134 148 public virtual void Start() { … … 172 186 public event EventHandler Prepared; 173 187 protected virtual void OnPrepared() { 188 ExecutionTime = TimeSpan.Zero; 174 189 ExecutionState = ExecutionState.Prepared; 175 190 EventHandler handler = Prepared; … … 190 205 public event EventHandler Stopped; 191 206 protected virtual void OnStopped() { 207 Run run = new Run("Run (" + ExecutionTime.ToString() + ")"); 208 CollectParameterValues(run.Parameters); 209 CollectResultValues(run.Results); 210 runs.Add(run); 192 211 ExecutionState = ExecutionState.Stopped; 193 212 EventHandler handler = Stopped; -
TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs ¶
r3274 r3275 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Collections; 27 28 28 29 namespace HeuristicLab.Optimization { … … 83 84 if (algorithm != null) RegisterAlgorithmEvents(); 84 85 OnAlgorithmChanged(); 85 Prepare( );86 Prepare(true); 86 87 } 87 88 } … … 97 98 OnRepetitionsChanged(); 98 99 if ((runs.Count < repetitions) && (Algorithm != null) && (Algorithm.ExecutionState == ExecutionState.Stopped)) 99 Prepare( false);100 Prepare(); 100 101 } 101 102 } … … 145 146 146 147 public void Prepare() { 147 Prepare( true);148 } 149 public void Prepare(bool clearR esults) {148 Prepare(false); 149 } 150 public void Prepare(bool clearRuns) { 150 151 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 151 152 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 152 153 if (Algorithm != null) { 153 if (clearR esults) {154 if (clearRuns) { 154 155 ExecutionTime = TimeSpan.Zero; 155 156 runs.Clear(); 156 157 } 157 Algorithm.Prepare( );158 Algorithm.Prepare(clearRuns); 158 159 } 159 160 } … … 233 234 algorithm.Started += new EventHandler(Algorithm_Started); 234 235 algorithm.Stopped += new EventHandler(Algorithm_Stopped); 236 algorithm.Runs.CollectionReset += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_CollectionReset); 237 algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsAdded); 238 algorithm.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsRemoved); 235 239 } 236 240 private void DeregisterAlgorithmEvents() { … … 241 245 algorithm.Started -= new EventHandler(Algorithm_Started); 242 246 algorithm.Stopped -= new EventHandler(Algorithm_Stopped); 243 } 244 247 algorithm.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_CollectionReset); 248 algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsAdded); 249 algorithm.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsRemoved); 250 } 245 251 private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) { 246 252 OnExceptionOccurred(e.Value); … … 261 267 private void Algorithm_Stopped(object sender, EventArgs e) { 262 268 ExecutionTime += Algorithm.ExecutionTime; 263 runs.Add(new Run("Run " + Algorithm.ExecutionTime.ToString(), Algorithm));264 269 OnStopped(); 265 270 … … 269 274 } 270 275 } 276 private void Algorithm_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<Run> e) { 277 Runs.RemoveRange(e.OldItems); 278 Runs.AddRange(e.Items); 279 } 280 private void Algorithm_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<Run> e) { 281 Runs.AddRange(e.Items); 282 } 283 private void Algorithm_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Run> e) { 284 Runs.RemoveRange(e.Items); 285 } 271 286 #endregion 272 287 } -
TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/EngineAlgorithm.cs ¶
r3274 r3275 157 157 } 158 158 159 public override void Prepare(bool clearResults) { 160 base.Prepare(clearResults); 161 162 ResultCollection results = Results; 159 public override void Prepare() { 160 base.Prepare(); 163 161 globalScope.Clear(); 164 if (clearResults) 165 globalScope.Variables.Add(new Variable("Results", new ResultCollection())); 166 else 167 globalScope.Variables.Add(new Variable("Results", results)); 162 globalScope.Variables.Add(new Variable("Results", new ResultCollection())); 168 163 169 164 if (engine != null) { -
TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs ¶
r3274 r3275 77 77 } 78 78 79 [Storable] 80 private RunCollection runs; 81 public RunCollection Runs { 82 get { return runs; } 83 } 84 79 85 private bool stopPending; 80 86 … … 84 90 executionTime = TimeSpan.Zero; 85 91 Optimizers = new OptimizerList(); 92 runs = new RunCollection(); 86 93 stopPending = false; 87 94 } … … 90 97 executionTime = TimeSpan.Zero; 91 98 Optimizers = new OptimizerList(); 99 runs = new RunCollection(); 92 100 stopPending = false; 93 101 } … … 96 104 executionTime = TimeSpan.Zero; 97 105 Optimizers = new OptimizerList(); 106 runs = new RunCollection(); 98 107 stopPending = false; 99 108 } … … 104 113 clone.executionTime = executionTime; 105 114 clone.Optimizers = (OptimizerList)cloner.Clone(optimizers); 115 clone.runs = (RunCollection)cloner.Clone(runs); 106 116 clone.stopPending = stopPending; 107 117 return clone; … … 109 119 110 120 public void Prepare() { 111 Prepare( true);112 } 113 public void Prepare(bool clearR esults) {121 Prepare(false); 122 } 123 public void Prepare(bool clearRuns) { 114 124 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 115 125 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 116 126 if (Optimizers.Count > 0) { 127 if (clearRuns) runs.Clear(); 117 128 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) 118 optimizer.Prepare(clearR esults);129 optimizer.Prepare(clearRuns); 119 130 } 120 131 } … … 233 244 optimizer.Started += new EventHandler(optimizer_Started); 234 245 optimizer.Stopped += new EventHandler(optimizer_Stopped); 235 } 236 246 optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_CollectionReset); 247 optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_ItemsAdded); 248 optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_ItemsRemoved); 249 } 237 250 private void DeregisterOptimizerEvents(IOptimizer optimizer) { 238 251 optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred); … … 242 255 optimizer.Started -= new EventHandler(optimizer_Started); 243 256 optimizer.Stopped -= new EventHandler(optimizer_Stopped); 257 optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_CollectionReset); 258 optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_ItemsAdded); 259 optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_ItemsRemoved); 244 260 } 245 261 private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) { … … 275 291 } 276 292 } 293 private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<Run> e) { 294 Runs.RemoveRange(e.OldItems); 295 Runs.AddRange(e.Items); 296 } 297 private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<Run> e) { 298 Runs.AddRange(e.Items); 299 } 300 private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Run> e) { 301 Runs.RemoveRange(e.Items); 302 } 277 303 #endregion 278 304 } -
TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IOptimizer.cs ¶
r3274 r3275 26 26 /// Interface to represent optimizers such as algorithms, batch runs, or experiments. 27 27 /// </summary> 28 public interface IOptimizer : IExecutable { 29 void Prepare(bool clearResults); 28 public interface IOptimizer : INamedItem, IExecutable { 29 RunCollection Runs { get; } 30 31 void Prepare(bool clearRuns); 30 32 } 31 33 } -
TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/Run.cs ¶
r3260 r3275 59 59 results = new Dictionary<string, IItem>(); 60 60 } 61 public Run(string name, IAlgorithm algorithm)62 : base(name) {63 parameters = new Dictionary<string, IItem>();64 results = new Dictionary<string, IItem>();65 algorithm.CollectParameterValues(parameters);66 algorithm.CollectResultValues(results);67 }68 61 public Run(string name, string description) 69 62 : base(name, description) { 70 63 parameters = new Dictionary<string, IItem>(); 71 64 results = new Dictionary<string, IItem>(); 72 }73 public Run(string name, string description, IAlgorithm algorithm)74 : base(name, description) {75 parameters = new Dictionary<string, IItem>();76 results = new Dictionary<string, IItem>();77 algorithm.CollectParameterValues(parameters);78 algorithm.CollectResultValues(results);79 65 } 80 66
Note: See TracChangeset
for help on using the changeset viewer.