Changeset 4139
- Timestamp:
- 08/03/10 15:23:57 (14 years ago)
- Location:
- branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/HeuristicLab.Hive.Experiment-3.3.csproj
r4133 r4139 96 96 </ItemGroup> 97 97 <ItemGroup> 98 <Compile Include="OptimizerList.cs" /> 98 99 <Compile Include="JobItemList.cs" /> 99 100 <Compile Include="JobItem.cs" /> -
branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/HiveExperiment.cs
r4135 r4139 51 51 public class HiveExperiment : NamedItem, IExecutable { 52 52 private const string itemName = "Hive Experiment"; 53 private const string itemDescription = "A n experiment which contains multiple batch runs of algorithms whichare executed in the Hive.";53 private const string itemDescription = "A runner for a single experiment, which's algorithms are executed in the Hive."; 54 54 private const int resultPollingIntervalMs = 15000; 55 55 private const int maxSnapshotRetries = 20; … … 76 76 77 77 private IDictionary<Guid, Thread> resultPollingThreads; 78 79 [Storable] 80 private IDictionary<Guid, IOptimizer> pendingOptimizers = new Dictionary<Guid, IOptimizer>(); 78 79 /// <summary> 80 /// Mapping from JobId to an optimizer. 81 /// Stores all pending optimizers. If an optimizer is finished it is removed from this collection 82 /// </summary> 83 [Storable] 84 private IDictionary<Guid, IOptimizer> pendingOptimizersByJobId = new Dictionary<Guid, IOptimizer>(); 85 86 /// <summary> 87 /// Stores a mapping from the child-optimizer to the parent optimizer. 88 /// Needed to replace a finished optimizer in the optimizer-tree. 89 /// Only pending optmizers are stored. 90 /// </summary> 91 [Storable] 92 private IDictionary<IOptimizer, IOptimizer> parentOptimizersByPendingOptimizer = new Dictionary<IOptimizer, IOptimizer>(); 81 93 82 94 [Storable] … … 156 168 clone.executionState = this.executionState; 157 169 clone.executionTime = this.executionTime; 158 clone.pendingOptimizers = new Dictionary<Guid, IOptimizer>(); 159 foreach (var pair in this.pendingOptimizers) 160 clone.pendingOptimizers[pair.Key] = (IOptimizer)cloner.Clone(pair.Value); 170 clone.pendingOptimizersByJobId = new Dictionary<Guid, IOptimizer>(); 171 foreach (var pair in this.pendingOptimizersByJobId) 172 clone.pendingOptimizersByJobId[pair.Key] = (IOptimizer)cloner.Clone(pair.Value); 173 foreach (var pair in this.parentOptimizersByPendingOptimizer) 174 clone.parentOptimizersByPendingOptimizer[pair.Key] = (IOptimizer)cloner.Clone(pair.Value); 161 175 clone.log = (ILog)cloner.Clone(log); 162 176 clone.stopPending = this.stopPending; … … 242 256 IExecutionEngineFacade executionEngineFacade = GetExecutionEngineFacade(); 243 257 244 pendingOptimizers = new Dictionary<Guid, IOptimizer>(); 258 pendingOptimizersByJobId = new Dictionary<Guid, IOptimizer>(); 259 parentOptimizersByPendingOptimizer = GetOptimizers(true); 245 260 IEnumerable<string> groups = ResourceGroups; 246 247 foreach (IOptimizer optimizer in GetOptimizers(false)) {261 262 foreach (IOptimizer optimizer in parentOptimizersByPendingOptimizer.Keys) { 248 263 SerializedJob serializedJob = CreateSerializedJob(optimizer); 249 264 ResponseObject<JobDto> response = executionEngineFacade.AddJobWithGroupStrings(serializedJob, groups); 250 pendingOptimizers .Add(response.Obj.Id, optimizer);265 pendingOptimizersByJobId.Add(response.Obj.Id, optimizer); 251 266 252 267 JobItem jobItem = new JobItem() { … … 263 278 LogMessage("Sent job to Hive (jobId: " + response.Obj.Id + ")"); 264 279 } 265 280 266 281 // start results polling after sending sending the jobs to the server (to avoid race conflicts at the optimizers-collection) 267 282 StartResultPolling(); … … 279 294 280 295 private void CreateResultPollingThreads() { 281 foreach (JobItem jobItem in JobItems) {296 foreach (JobItem jobItem in JobItems) { 282 297 if (!resultPollingThreads.ContainsKey(jobItem.JobDto.Id)) { 283 298 resultPollingThreads.Add(jobItem.JobDto.Id, CreateResultPollingThread(jobItem.JobDto)); … … 314 329 /// <param name="flatout">if false only top level optimizers are returned, if true the optimizer-tree is flatted</param> 315 330 /// <returns></returns> 316 private I Enumerable<IOptimizer> GetOptimizers(bool flatout) {331 private IDictionary<IOptimizer, IOptimizer> GetOptimizers(bool flatout) { 317 332 if (!flatout) { 318 return experiment.Optimizers; 333 var optimizers = new Dictionary<IOptimizer, IOptimizer>(); 334 foreach (IOptimizer opt in experiment.Optimizers) { 335 optimizers.Add(experiment, opt); 336 } 337 return optimizers; 319 338 } else { 320 throw new NotImplementedException(); 321 } 322 } 323 324 private void ReplaceOptimizer(IOptimizer originalOptimizer, IOptimizer newOptimizer) { 339 return FlatOptimizerTree(null, experiment); 340 } 341 } 342 343 /// <summary> 344 /// Recursively iterates all IOptimizers in the optimizer-tree and returns them. 345 /// 346 /// [chn] this could be implemented more cleanly if Experiment and BatchRun would implement an interface like: 347 /// interface IParallelizable { 348 /// IEnumerable<IOptimizer> GetOptimizers(); 349 /// } 350 /// </summary> 351 /// <returns>a dictionary mapping from the parent optimizer to the child optimizer</returns> 352 private IDictionary<IOptimizer, IOptimizer> FlatOptimizerTree(IOptimizer parent, IOptimizer optimizer) { 353 IDictionary<IOptimizer, IOptimizer> optimizers = new Dictionary<IOptimizer, IOptimizer>(); 354 if (optimizer is HeuristicLab.Optimization.Experiment) { 355 HeuristicLab.Optimization.Experiment experiment = optimizer as HeuristicLab.Optimization.Experiment; 356 foreach (IOptimizer opt in experiment.Optimizers) { 357 AddRange(optimizers, FlatOptimizerTree(experiment, opt)); 358 } 359 } else if (optimizer is BatchRun) { 360 BatchRun batchRun = optimizer as BatchRun; 361 for (int i = 0; i < batchRun.Repetitions; i++) { 362 IOptimizer opt = (IOptimizer)batchRun.Algorithm.Clone(); 363 AddRange(optimizers, FlatOptimizerTree(batchRun, opt)); 364 } 365 } else if (optimizer is EngineAlgorithm) { 366 optimizers.Add(optimizer, parent); 367 } else { 368 Logger.Warn("Optimizer of type " + optimizers.GetType().ToString() + " unknown"); 369 optimizers.Add(optimizer, parent); 370 } 371 return optimizers; 372 } 373 374 private void AddRange(IDictionary<IOptimizer, IOptimizer> optimizers, IDictionary<IOptimizer, IOptimizer> childs) { 375 foreach (KeyValuePair<IOptimizer, IOptimizer> kvp in childs) { 376 optimizers.Add(kvp); 377 } 378 } 379 380 private void ReplaceOptimizer(IOptimizer parentOptimizer, IOptimizer originalOptimizer, IOptimizer newOptimizer) { 325 381 lock (locker) { 326 int originalOptimizerIndex = experiment.Optimizers.IndexOf(originalOptimizer); 327 experiment.Optimizers[originalOptimizerIndex] = newOptimizer; 382 if (parentOptimizer is HeuristicLab.Optimization.Experiment) { 383 HeuristicLab.Optimization.Experiment exp = (HeuristicLab.Optimization.Experiment)parentOptimizer; 384 int originalOptimizerIndex = exp.Optimizers.IndexOf(originalOptimizer); 385 exp.Optimizers[originalOptimizerIndex] = newOptimizer; 386 } else if (parentOptimizer is BatchRun) { 387 BatchRun batchRun = (BatchRun)parentOptimizer; 388 if (newOptimizer is IAlgorithm) { 389 batchRun.Runs.Add(new Run((IAlgorithm)newOptimizer)); 390 } else { 391 throw new NotSupportedException("Only IAlgorithm types supported"); 392 } 393 } else { 394 throw new NotSupportedException("Invalid parentOptimizer"); 395 } 328 396 } 329 397 } … … 399 467 GetJobItemById(job.Id).LogMessage("Job successfully aborted"); 400 468 return; 401 } 469 } 402 470 403 471 // loop while … … 416 484 GetJobItemById(job.Id).LogMessage("Job finished"); 417 485 // job retrieved... replace the existing optimizers with the finished one 418 IOptimizer originalOptimizer = pendingOptimizers [job.Id];486 IOptimizer originalOptimizer = pendingOptimizersByJobId[job.Id]; 419 487 IOptimizer restoredOptimizer = ((OptimizerJob)restoredObject).Optimizer; 420 488 421 ReplaceOptimizer(originalOptimizer, restoredOptimizer); 422 pendingOptimizers.Remove(job.Id); 423 424 if (pendingOptimizers.Count == 0) { 489 ReplaceOptimizer(parentOptimizersByPendingOptimizer[originalOptimizer], originalOptimizer, restoredOptimizer); 490 pendingOptimizersByJobId.Remove(job.Id); 491 parentOptimizersByPendingOptimizer.Remove(originalOptimizer); 492 493 if (pendingOptimizersByJobId.Count == 0) { 425 494 // finished 426 495 this.ExecutionState = Core.ExecutionState.Stopped;
Note: See TracChangeset
for help on using the changeset viewer.