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