[12762] | 1 | using System.Collections.ObjectModel;
|
---|
[12815] | 2 | using System.Security.AccessControl;
|
---|
[12824] | 3 | using System.Text;
|
---|
[12762] | 4 | using System.Threading;
|
---|
[12824] | 5 | using System.Windows.Documents;
|
---|
[12762] | 6 | using System.Xml.Serialization;
|
---|
[12503] | 7 | using Evaluation.ViewModel;
|
---|
| 8 | using HeuristicLab.Algorithms.Bandits;
|
---|
| 9 | using HeuristicLab.Algorithms.Bandits.BanditPolicies;
|
---|
| 10 | using HeuristicLab.Algorithms.GeneticProgramming;
|
---|
| 11 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
| 12 | using HeuristicLab.Algorithms.MonteCarloTreeSearch;
|
---|
| 13 | using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
|
---|
| 14 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 15 | using Microsoft.Research.DynamicDataDisplay;
|
---|
| 16 | using Microsoft.Research.DynamicDataDisplay.DataSources;
|
---|
[12762] | 17 | using System;
|
---|
| 18 | using System.Collections.Generic;
|
---|
| 19 | using System.ComponentModel;
|
---|
| 20 | using System.Diagnostics;
|
---|
| 21 | using System.IO;
|
---|
| 22 | using System.Windows;
|
---|
| 23 | using System.Windows.Controls;
|
---|
| 24 | using System.Windows.Threading;
|
---|
| 25 | using Microsoft.Win32;
|
---|
| 26 | using WpfTestSvgSample;
|
---|
[12503] | 27 |
|
---|
| 28 | namespace Evaluation
|
---|
| 29 | {
|
---|
| 30 | /// <summary>
|
---|
| 31 | /// Interaction logic for MainWindow.xaml
|
---|
| 32 | /// </summary>
|
---|
| 33 | public partial class MainWindow : Window
|
---|
| 34 | {
|
---|
| 35 | private EvaluationViewModel vm;
|
---|
| 36 |
|
---|
[12762] | 37 | private DrawingPage treeDrawingPage;
|
---|
| 38 |
|
---|
[12503] | 39 | public MainWindow()
|
---|
| 40 | {
|
---|
| 41 | InitializeComponent();
|
---|
[12762] | 42 | CenterWindowOnScreen();
|
---|
[12503] | 43 | this.DataContext = vm = new EvaluationViewModel();
|
---|
[12830] | 44 | vm.MaxLen = 23;
|
---|
[12829] | 45 | vm.MaxIterations = 500000;
|
---|
| 46 | vm.NrRuns = 20;
|
---|
[12762] | 47 | }
|
---|
[12503] | 48 |
|
---|
[12762] | 49 | private void CenterWindowOnScreen()
|
---|
| 50 | {
|
---|
| 51 | double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
|
---|
| 52 | double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
|
---|
| 53 | double windowWidth = this.Width;
|
---|
| 54 | double windowHeight = this.Height;
|
---|
| 55 | this.Left = (screenWidth / 2) - (windowWidth / 2);
|
---|
| 56 | this.Top = (screenHeight / 2) - (windowHeight / 2);
|
---|
[12503] | 57 | }
|
---|
| 58 |
|
---|
[12815] | 59 | private void DrawQualityChart(Run run)
|
---|
[12503] | 60 | {
|
---|
[12781] | 61 | List<FoundSolution> solutions = new List<FoundSolution>(run.FoundSolutions);
|
---|
[12762] | 62 |
|
---|
[12781] | 63 | if (run.BestSolutionFoundAt < run.Evaluations)
|
---|
| 64 | {
|
---|
| 65 | solutions.Add(new FoundSolution(run.EndTime, run.Evaluations, run.BestQuality, run.BestSolution));
|
---|
| 66 | }
|
---|
[12762] | 67 |
|
---|
[12781] | 68 | var ds = new EnumerableDataSource<FoundSolution>(solutions);
|
---|
| 69 |
|
---|
| 70 |
|
---|
[12503] | 71 | ds.SetXMapping(x => x.Iteration);
|
---|
[12781] | 72 | ds.SetYMapping(y => y.Quality);
|
---|
[12503] | 73 |
|
---|
| 74 | LineGraph graph = new LineGraph(ds);
|
---|
| 75 |
|
---|
| 76 | graph.StrokeThickness = 2;
|
---|
[12815] | 77 | graph.AddToPlotter(QualityChartPlotter);
|
---|
[12503] | 78 | }
|
---|
[12762] | 79 |
|
---|
[12815] | 80 | private void DrawSelectionChart(Run run)
|
---|
| 81 | {
|
---|
| 82 | List<SelectionIndicator> selectionIndicators = new List<SelectionIndicator>(run.SelectionIndicators);
|
---|
| 83 |
|
---|
| 84 | var ds = new EnumerableDataSource<SelectionIndicator>(selectionIndicators);
|
---|
| 85 |
|
---|
| 86 | ds.SetXMapping(x => x.TotalSelections);
|
---|
| 87 | ds.SetYMapping(y => y.Indicator);
|
---|
| 88 |
|
---|
| 89 | LineGraph graph = new LineGraph(ds);
|
---|
| 90 |
|
---|
| 91 | graph.StrokeThickness = 2;
|
---|
| 92 | graph.AddToPlotter(SelectionChartPlotter);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[12762] | 95 | private void DrawTreeChart(Run run)
|
---|
| 96 | {
|
---|
| 97 | if (!string.IsNullOrEmpty(run.SvgFile))
|
---|
| 98 | {
|
---|
| 99 | treeDrawingPage.LoadDocument(run.SvgFile);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[12781] | 103 | private void DoRun(Object threadContext)
|
---|
| 104 | {
|
---|
| 105 | Run run = (Run)threadContext;
|
---|
| 106 | run.RunState = RunState.Running;
|
---|
| 107 | run.StartTime = DateTime.Now;
|
---|
[12762] | 108 |
|
---|
[12815] | 109 | run.Solver.Run(run.MaxIterations);
|
---|
[12781] | 110 |
|
---|
| 111 | run.EndTime = DateTime.Now;
|
---|
| 112 | run.RunState = RunState.Analyzing;
|
---|
| 113 |
|
---|
| 114 | if (run.FoundSolutions.Count > 0)
|
---|
| 115 | {
|
---|
| 116 | if (run.Solver is MonteCarloTreeSearch)
|
---|
| 117 | {
|
---|
| 118 | MonteCarloTreeSearch mctsSolver = (MonteCarloTreeSearch)run.Solver;
|
---|
| 119 |
|
---|
| 120 | run.TreeInfos = mctsSolver.GetTreeInfos();
|
---|
| 121 |
|
---|
| 122 | //byte[] output = mctsSolver.GenerateSvg();
|
---|
| 123 | //if (output != null && output.Length > 0)
|
---|
| 124 | //{
|
---|
| 125 | // run.SvgFile = string.Format("MCTS_SVG_#{0}_{1}.svg", run.RunNumber, DateTime.Now.Ticks);
|
---|
| 126 | // File.WriteAllBytes(run.SvgFile, mctsSolver.GenerateSvg());
|
---|
| 127 | //}
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | run.RunState = RunState.Finished;
|
---|
| 131 | lock (vm.CompletedRuns)
|
---|
| 132 | {
|
---|
| 133 | int completed = 0;
|
---|
| 134 | foreach (Run r in vm.Runs)
|
---|
| 135 | {
|
---|
| 136 | if (r.RunState == RunState.Finished)
|
---|
| 137 | {
|
---|
| 138 | completed++;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | vm.CompletedRuns = string.Format("{0}/{1}", completed, vm.Runs.Count);
|
---|
| 142 | if (completed == vm.NrRuns)
|
---|
| 143 | {
|
---|
| 144 | Dispatcher.Invoke(AllRunsFinished);
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | private void AllRunsFinished()
|
---|
[12503] | 150 | {
|
---|
[12781] | 151 | ButtonRun.IsEnabled = true;
|
---|
[12815] | 152 | TextBoxMaxIterations.IsEnabled = true;
|
---|
[12781] | 153 | TextBoxMaxLen.IsEnabled = true;
|
---|
| 154 | TextBoxRuns.IsEnabled = true;
|
---|
[12503] | 155 | }
|
---|
| 156 |
|
---|
[12781] | 157 | private void StartRuns()
|
---|
[12503] | 158 | {
|
---|
[12781] | 159 | vm.CompletedRuns = string.Format("0/{0}", vm.NrRuns);
|
---|
[12762] | 160 |
|
---|
[12503] | 161 | Type algorithmType = vm.SelectedAlgorithm;
|
---|
| 162 |
|
---|
| 163 | ISymbolicExpressionTreeProblem problem = vm.SelectedProblem;
|
---|
| 164 |
|
---|
| 165 | Type policy = vm.SelectedPolicy;
|
---|
| 166 | IBanditPolicy policyInstance = null;
|
---|
| 167 |
|
---|
| 168 | if (policy == typeof(UCTPolicy))
|
---|
| 169 | {
|
---|
| 170 | policyInstance = new UCTPolicy();
|
---|
| 171 | }
|
---|
[12762] | 172 | else if (policy == typeof(ThresholdAscentPolicy))
|
---|
[12503] | 173 | {
|
---|
| 174 | policyInstance = new ThresholdAscentPolicy();
|
---|
| 175 | }
|
---|
[12815] | 176 | else if (policy == typeof(BoltzmannExplorationPolicy))
|
---|
| 177 | {
|
---|
| 178 | policyInstance = new BoltzmannExplorationPolicy(2);
|
---|
| 179 | }
|
---|
[12503] | 180 | else
|
---|
| 181 | {
|
---|
| 182 | policyInstance = (IBanditPolicy)Activator.CreateInstance(policy);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[12762] | 185 | for (int i = 0; i < vm.NrRuns; i++)
|
---|
| 186 | {
|
---|
| 187 | ISolver solver = null;
|
---|
[12830] | 188 |
|
---|
[12828] | 189 | Random random = new Random(Guid.NewGuid().GetHashCode());
|
---|
[12503] | 190 |
|
---|
[12781] | 191 | if (algorithmType == typeof(MonteCarloTreeSearch_PruneLeaves))
|
---|
[12762] | 192 | {
|
---|
[12781] | 193 | solver = new MonteCarloTreeSearch_PruneLeaves(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
|
---|
| 194 | }
|
---|
| 195 | else if (algorithmType == typeof(MonteCarloTreeSearch))
|
---|
| 196 | {
|
---|
[12762] | 197 | solver = new MonteCarloTreeSearch(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
|
---|
| 198 | }
|
---|
| 199 | else if (algorithmType == typeof(SequentialSearch))
|
---|
| 200 | {
|
---|
| 201 | solver = new SequentialSearch(problem, vm.MaxLen, random, 0,
|
---|
| 202 | new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, policyInstance));
|
---|
| 203 | }
|
---|
| 204 | else if (algorithmType == typeof(RandomSearch))
|
---|
| 205 | {
|
---|
| 206 | solver = new RandomSearch(problem, random, vm.MaxLen);
|
---|
| 207 | }
|
---|
| 208 | else if (algorithmType == typeof(StandardGP))
|
---|
| 209 | {
|
---|
| 210 | solver = new StandardGP(problem, random);
|
---|
| 211 | }
|
---|
| 212 | else if (algorithmType == typeof(OffspringSelectionGP))
|
---|
| 213 | {
|
---|
| 214 | solver = new OffspringSelectionGP(problem, random);
|
---|
| 215 | }
|
---|
[12503] | 216 |
|
---|
[12815] | 217 | Run run = new Run(problem, policyInstance, solver, i + 1, vm.MaxIterations, vm.MaxLen);
|
---|
[12762] | 218 |
|
---|
[12781] | 219 | vm.Runs.Add(run);
|
---|
[12762] | 220 |
|
---|
[12781] | 221 | ThreadPool.QueueUserWorkItem(DoRun, run);
|
---|
[12762] | 222 | }
|
---|
| 223 | }
|
---|
[12503] | 224 |
|
---|
[12815] | 225 | private void ClearQualityChart()
|
---|
[12762] | 226 | {
|
---|
[12815] | 227 | QualityChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
[12503] | 228 | }
|
---|
| 229 |
|
---|
[12781] | 230 | private void ClearComparisonChart()
|
---|
| 231 | {
|
---|
| 232 | ComparisonChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[12815] | 235 | private void ClearSelectionChart()
|
---|
| 236 | {
|
---|
| 237 | SelectionChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[12503] | 240 | private void ButtonRun_OnClick(object sender, RoutedEventArgs e)
|
---|
| 241 | {
|
---|
[12815] | 242 | ClearQualityChart();
|
---|
[12781] | 243 | ClearComparisonChart();
|
---|
[12815] | 244 | ClearSelectionChart();
|
---|
[12781] | 245 |
|
---|
[12762] | 246 | vm.Runs.Clear();
|
---|
[12781] | 247 | vm.SelectedRun = null;
|
---|
[12503] | 248 | ButtonRun.IsEnabled = false;
|
---|
[12815] | 249 | TextBoxMaxIterations.IsEnabled = false;
|
---|
[12762] | 250 | TextBoxMaxLen.IsEnabled = false;
|
---|
| 251 | TextBoxRuns.IsEnabled = false;
|
---|
[12781] | 252 | StartRuns();
|
---|
[12503] | 253 | }
|
---|
| 254 |
|
---|
| 255 | private void ButtonPause_OnClick(object sender, RoutedEventArgs e)
|
---|
| 256 | {
|
---|
[12762] | 257 | //if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch))
|
---|
| 258 | //{
|
---|
| 259 | // MonteCarloTreeSearch mcts = (MonteCarloTreeSearch)solver;
|
---|
| 260 | // mcts.PauseContinue();
|
---|
| 261 | // if (mcts.IsPaused)
|
---|
| 262 | // {
|
---|
| 263 | // ButtonPause.Content = "Continue";
|
---|
| 264 | // }
|
---|
| 265 | // else
|
---|
| 266 | // {
|
---|
| 267 | // ButtonPause.Content = "Pause";
|
---|
| 268 | // }
|
---|
| 269 | //}
|
---|
[12503] | 270 | }
|
---|
| 271 |
|
---|
| 272 | private void ButtonStop_OnClick(object sender, RoutedEventArgs e)
|
---|
| 273 | {
|
---|
[12781] | 274 | //worker.CancelAsync();
|
---|
[12503] | 275 | }
|
---|
| 276 |
|
---|
| 277 | private void ComboBoxAlgorithms_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
---|
| 278 | {
|
---|
[12781] | 279 | if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch) || vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch_PruneLeaves))
|
---|
[12503] | 280 | {
|
---|
| 281 | ComboBoxPolicies.IsEnabled = true;
|
---|
[12762] | 282 | TabItemTree.IsEnabled = true;
|
---|
[12503] | 283 | }
|
---|
[12762] | 284 | else if (vm.SelectedAlgorithm == typeof(SequentialSearch))
|
---|
| 285 | {
|
---|
| 286 | ComboBoxPolicies.IsEnabled = true;
|
---|
| 287 | TabItemTree.IsEnabled = false;
|
---|
| 288 | }
|
---|
[12503] | 289 | else
|
---|
| 290 | {
|
---|
| 291 | ComboBoxPolicies.IsEnabled = false;
|
---|
[12762] | 292 | TabItemTree.IsEnabled = false;
|
---|
[12503] | 293 | }
|
---|
| 294 | }
|
---|
[12762] | 295 |
|
---|
| 296 | private void Window_Loaded(object sender, RoutedEventArgs e)
|
---|
| 297 | {
|
---|
| 298 | treeDrawingPage = treeDrawing.Content as DrawingPage;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | private void ListBoxRuns_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
---|
| 302 | {
|
---|
[12781] | 303 | if (vm.SelectedRun != null)
|
---|
| 304 | {
|
---|
| 305 | vm.SelectedRun.Solver.FoundNewBestSolution -= SelectedRun_FoundNewBestSolution;
|
---|
| 306 | }
|
---|
[12762] | 307 | if (ListBoxRuns.SelectedItem != null)
|
---|
| 308 | {
|
---|
[12781] | 309 | vm.SelectedRun = (Run)ListBoxRuns.SelectedItem;
|
---|
| 310 |
|
---|
| 311 | vm.SelectedRun.Solver.FoundNewBestSolution += SelectedRun_FoundNewBestSolution;
|
---|
| 312 |
|
---|
[12815] | 313 | ClearQualityChart();
|
---|
| 314 | DrawQualityChart(vm.SelectedRun);
|
---|
| 315 |
|
---|
| 316 | if (vm.SelectedRun.RunState == RunState.Finished)
|
---|
| 317 | {
|
---|
| 318 | ClearSelectionChart();
|
---|
| 319 | DrawSelectionChart(vm.SelectedRun);
|
---|
| 320 | }
|
---|
[12781] | 321 | //DrawTreeChart(vm.SelectedRun);
|
---|
[12762] | 322 | }
|
---|
[12781] | 323 | else
|
---|
| 324 | {
|
---|
| 325 | vm.SelectedRun = null;
|
---|
| 326 | }
|
---|
[12762] | 327 | }
|
---|
| 328 |
|
---|
[12781] | 329 | void SelectedRun_FoundNewBestSolution(string arg1, double arg2)
|
---|
| 330 | {
|
---|
| 331 | Dispatcher.BeginInvoke(new Action(() =>
|
---|
| 332 | {
|
---|
[12815] | 333 | ClearQualityChart();
|
---|
| 334 | DrawQualityChart(vm.SelectedRun);
|
---|
[12781] | 335 | }));
|
---|
| 336 | }
|
---|
| 337 |
|
---|
[12762] | 338 | public void SaveToFile()
|
---|
| 339 | {
|
---|
| 340 | SaveFileDialog dlg = new SaveFileDialog();
|
---|
| 341 | dlg.FileName = "runs"; // Default file name
|
---|
| 342 | dlg.DefaultExt = ".xml"; // Default file extension
|
---|
| 343 |
|
---|
| 344 | // Show save file dialog box
|
---|
| 345 | Nullable<bool> result = dlg.ShowDialog();
|
---|
| 346 |
|
---|
| 347 | // Process save file dialog box results
|
---|
| 348 | if (result == true)
|
---|
| 349 | {
|
---|
| 350 | // Save document
|
---|
| 351 | string filename = dlg.FileName;
|
---|
| 352 |
|
---|
| 353 | XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Run>));
|
---|
| 354 | using (TextWriter writer = new StreamWriter(filename))
|
---|
| 355 | {
|
---|
| 356 | serializer.Serialize(writer, vm.Runs);
|
---|
| 357 | }
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | public void LoadFromFile()
|
---|
| 363 | {
|
---|
| 364 | OpenFileDialog dlg = new OpenFileDialog();
|
---|
| 365 |
|
---|
| 366 | // Show save file dialog box
|
---|
| 367 | Nullable<bool> result = dlg.ShowDialog();
|
---|
| 368 |
|
---|
| 369 | // Process save file dialog box results
|
---|
| 370 | if (result == true)
|
---|
| 371 | {
|
---|
| 372 | // Load document
|
---|
| 373 | string filename = dlg.FileName;
|
---|
| 374 |
|
---|
| 375 | XmlSerializer deserializer = new XmlSerializer(typeof(ObservableCollection<Run>));
|
---|
| 376 | using (TextReader reader = new StreamReader(filename))
|
---|
| 377 | {
|
---|
| 378 | object obj = deserializer.Deserialize(reader);
|
---|
| 379 | vm.Runs = (ObservableCollection<Run>)obj;
|
---|
| 380 |
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | private void LoadButton_OnClick(object sender, RoutedEventArgs e)
|
---|
| 386 | {
|
---|
[12781] | 387 | LoadFromFile();
|
---|
[12762] | 388 | }
|
---|
| 389 |
|
---|
| 390 | private void SaveButton_OnClick(object sender, RoutedEventArgs e)
|
---|
| 391 | {
|
---|
| 392 | SaveToFile();
|
---|
| 393 | }
|
---|
[12824] | 394 |
|
---|
| 395 | private void MenuItemCopyToClipboard_OnClick(object sender, RoutedEventArgs e)
|
---|
| 396 | {
|
---|
| 397 | StringBuilder tableExport = new StringBuilder();
|
---|
[12827] | 398 | tableExport.Append(
|
---|
[12824] | 399 | "Run\tMaxIterations\tEvaluations\tBestKnownQuality\tQuality\tQuality %\tFoundAt\tTotalTime\tSolutionTime\tEvaluationsPerSecond\tSolution");
|
---|
[12830] | 400 | if (ListViewRuns.Items.Count > 0 && ((Run)ListViewRuns.Items[0]).TreeInfos != null)
|
---|
[12827] | 401 | {
|
---|
| 402 | tableExport.Append("\tTotalNodes\tUnexpandedNodes\tExpandedNodes\tLeaveNodes\tDeepestLevel");
|
---|
| 403 | }
|
---|
| 404 | tableExport.AppendLine();
|
---|
[12824] | 405 | for (int i = 0; i < ListViewRuns.Items.Count; i++)
|
---|
| 406 | {
|
---|
| 407 | Run run = (Run)ListViewRuns.Items[i];
|
---|
[12827] | 408 | tableExport.Append(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}", run.RunNumber,
|
---|
[12824] | 409 | run.MaxIterations, run.Evaluations, run.BestKnownQuality, run.BestQuality,
|
---|
| 410 | run.BestQuality / run.BestKnownQuality, run.BestSolutionFoundAt, run.TotalTime, run.BestSolutionTime,
|
---|
| 411 | run.EvaluationsPerSecond, run.BestSolution));
|
---|
[12827] | 412 |
|
---|
| 413 | if (run.TreeInfos != null)
|
---|
| 414 | {
|
---|
| 415 | tableExport.Append(string.Format("\t{0}\t{1}\t{2}\t{3}\t{4}", run.TreeInfos.TotalNodes,
|
---|
| 416 | run.TreeInfos.UnexpandedNodes, run.TreeInfos.ExpandedNodes, run.TreeInfos.LeaveNodes,
|
---|
| 417 | run.TreeInfos.DeepestLevel));
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | tableExport.AppendLine();
|
---|
[12824] | 421 | }
|
---|
| 422 | Clipboard.SetData(DataFormats.Text, tableExport.ToString());
|
---|
| 423 | }
|
---|
[12503] | 424 | }
|
---|
| 425 | }
|
---|