Changeset 13485 for branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/ExpertSystem.cs
- Timestamp:
- 12/18/15 12:42:13 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/ExpertSystem.cs
r13475 r13485 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.ComponentModel; 24 using System.Collections.Generic;25 25 using System.Drawing; 26 using System.IO; 26 27 using System.Linq; 28 using System.Threading.Tasks; 27 29 using HeuristicLab.Analysis; 30 using HeuristicLab.Common; 31 using HeuristicLab.Common.Resources; 28 32 using HeuristicLab.Core; 33 using HeuristicLab.Data; 34 using HeuristicLab.MainForm; 29 35 using HeuristicLab.Optimization; 30 36 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Common; 32 using HeuristicLab.Common.Resources; 33 using HeuristicLab.Data; 37 using HeuristicLab.Persistence.Default.Xml; 34 38 35 39 namespace HeuristicLab.OptimizationExpertSystem { … … 222 226 } 223 227 } finally { ProblemInstances.UpdateOfRunsInProgress = false; } 228 } 229 230 private static readonly HashSet<string> InterestingValueNames = new HashSet<string>() { 231 "QualityPerEvaluations", "Problem Name", "Problem Type", "Algorithm Name", "Algorithm Type", "Maximization" 232 }; 233 234 public async void UpdateKnowledgeBaseAsync(IProgress progress) { 235 progress.Start("Updating Knowledge Base from OKB"); 236 await Task.Factory.StartNew(() => { DoUpdateKnowledgeBase(progress); }, TaskCreationOptions.LongRunning); 237 } 238 239 public void UpdateKnowledgeBase() { 240 DoUpdateKnowledgeBase(new Progress(string.Empty, ProgressState.Started)); 241 } 242 243 private void DoUpdateKnowledgeBase(IProgress progress) { 244 var queryClient = Clients.OKB.Query.QueryClient.Instance; 245 var adminClient = Clients.OKB.Administration.AdministrationClient.Instance; 246 try { 247 progress.Status = "Refreshing query client..."; 248 queryClient.Refresh(); 249 var interestingValues = queryClient.ValueNames.Where(x => InterestingValueNames.Contains(x.Name)).ToList(); 250 251 var problemTypeFilter = (Clients.OKB.Query.StringComparisonAvailableValuesFilter)queryClient.Filters.Single(x => x.Label == "Problem Data Type Name"); 252 problemTypeFilter.Value = Problem.GetType().Name; 253 254 progress.Status = "Obtaining number of runs..."; 255 var count = queryClient.GetNumberOfRuns(problemTypeFilter); 256 if (count == 0) return; 257 258 var runIds = queryClient.GetRunIds(problemTypeFilter).ToList(); 259 adminClient.Refresh(); 260 var i = 0; 261 var conversions = new List<Task>(); 262 var runGroup = new Dictionary<Tuple<long, string>, List<IRun>>(); 263 while (i < count) { 264 var nextIds = runIds.Skip(i).Take(500).ToList(); 265 progress.Status = string.Format("Downloading runs {0} to {1} of {2}...", i, i + nextIds.Count, count); 266 var okbRuns = queryClient.GetRunsWithValues(nextIds, true, interestingValues); 267 conversions.Add(Task.Factory.StartNew(() => { 268 var hlRuns = okbRuns.AsParallel().Select(x => new { Id = x.Algorithm.Id, Name = x.Algorithm.Name, Run = queryClient.ConvertToOptimizationRun(x) }) 269 .GroupBy(x => Tuple.Create(x.Id, x.Name)).ToList(); 270 lock (runGroup) { 271 foreach (var hr in hlRuns) { 272 List<IRun> runList; 273 if (!runGroup.TryGetValue(hr.Key, out runList)) { 274 runList = new List<IRun>(); 275 runGroup[hr.Key] = runList; 276 } 277 runList.AddRange(hr.Select(x => x.Run)); 278 } 279 } 280 })); 281 i += nextIds.Count; 282 progress.ProgressValue = 0.8 * (i / (double)count); 283 } 284 Task.WaitAll(conversions.ToArray()); 285 var list = new ItemList<IAlgorithm>(); 286 i = 0; 287 foreach (var rGroup in runGroup) { 288 progress.Status = string.Format("Downloading algorithm {0}...", rGroup.Key.Item2); 289 var data = Clients.OKB.Administration.AdministrationClient.GetAlgorithmData(rGroup.Key.Item1); 290 if (data != null) { 291 using (var stream = new MemoryStream(data)) { 292 try { 293 var alg = (IAlgorithm)XmlParser.Deserialize<IContent>(stream); 294 alg.Runs.AddRange(rGroup.Value); 295 list.Add(alg); 296 } catch (Exception) { } 297 stream.Close(); 298 } 299 } 300 i++; 301 progress.ProgressValue = 0.8 + 0.2 * (i / (double)runGroup.Count); 302 } 303 AlgorithmInstances = list; 304 } finally { progress.Finish(); } 224 305 } 225 306
Note: See TracChangeset
for help on using the changeset viewer.