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