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 = 1000000;
|
---|
46 | vm.NrRuns = 10;
|
---|
47 | }
|
---|
48 |
|
---|
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);
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void DrawQualityChart(Run run)
|
---|
60 | {
|
---|
61 | List<FoundSolution> solutions = new List<FoundSolution>(run.FoundSolutions);
|
---|
62 |
|
---|
63 | if (run.BestSolutionFoundAt < run.Evaluations)
|
---|
64 | {
|
---|
65 | solutions.Add(new FoundSolution(run.EndTime, run.Evaluations, run.BestQuality, run.BestSolution));
|
---|
66 | }
|
---|
67 |
|
---|
68 | var ds = new EnumerableDataSource<FoundSolution>(solutions);
|
---|
69 |
|
---|
70 |
|
---|
71 | ds.SetXMapping(x => x.Iteration);
|
---|
72 | ds.SetYMapping(y => y.Quality);
|
---|
73 |
|
---|
74 | LineGraph graph = new LineGraph(ds);
|
---|
75 |
|
---|
76 | graph.StrokeThickness = 2;
|
---|
77 | graph.AddToPlotter(QualityChartPlotter);
|
---|
78 | }
|
---|
79 |
|
---|
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 |
|
---|
95 | private void DrawTreeChart(Run run)
|
---|
96 | {
|
---|
97 | if (!string.IsNullOrEmpty(run.SvgFile))
|
---|
98 | {
|
---|
99 | treeDrawingPage.LoadDocument(run.SvgFile);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void DoRun(Object threadContext)
|
---|
104 | {
|
---|
105 | Run run = (Run)threadContext;
|
---|
106 | run.RunState = RunState.Running;
|
---|
107 | run.StartTime = DateTime.Now;
|
---|
108 |
|
---|
109 | run.Solver.Run(run.MaxIterations);
|
---|
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()
|
---|
150 | {
|
---|
151 | ButtonRun.IsEnabled = true;
|
---|
152 | TextBoxMaxIterations.IsEnabled = true;
|
---|
153 | TextBoxMaxLen.IsEnabled = true;
|
---|
154 | TextBoxRuns.IsEnabled = true;
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void StartRuns()
|
---|
158 | {
|
---|
159 | vm.CompletedRuns = string.Format("0/{0}", vm.NrRuns);
|
---|
160 |
|
---|
161 | Type algorithmType = vm.SelectedAlgorithm;
|
---|
162 |
|
---|
163 | ISymbolicExpressionTreeProblem problem = vm.SelectedProblem;
|
---|
164 |
|
---|
165 | Random random = new Random(DateTime.Now.Millisecond);
|
---|
166 |
|
---|
167 | Type policy = vm.SelectedPolicy;
|
---|
168 | IBanditPolicy policyInstance = null;
|
---|
169 |
|
---|
170 | if (policy == typeof(UCTPolicy))
|
---|
171 | {
|
---|
172 | policyInstance = new UCTPolicy();
|
---|
173 | }
|
---|
174 | else if (policy == typeof(ThresholdAscentPolicy))
|
---|
175 | {
|
---|
176 | policyInstance = new ThresholdAscentPolicy();
|
---|
177 | }
|
---|
178 | else if (policy == typeof(BoltzmannExplorationPolicy))
|
---|
179 | {
|
---|
180 | policyInstance = new BoltzmannExplorationPolicy(2);
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | policyInstance = (IBanditPolicy)Activator.CreateInstance(policy);
|
---|
185 | }
|
---|
186 |
|
---|
187 | for (int i = 0; i < vm.NrRuns; i++)
|
---|
188 | {
|
---|
189 | ISolver solver = null;
|
---|
190 |
|
---|
191 | if (algorithmType == typeof(MonteCarloTreeSearch_PruneLeaves))
|
---|
192 | {
|
---|
193 | solver = new MonteCarloTreeSearch_PruneLeaves(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
|
---|
194 | }
|
---|
195 | else if (algorithmType == typeof(MonteCarloTreeSearch))
|
---|
196 | {
|
---|
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 | }
|
---|
216 |
|
---|
217 | Run run = new Run(problem, policyInstance, solver, i + 1, vm.MaxIterations, vm.MaxLen);
|
---|
218 |
|
---|
219 | vm.Runs.Add(run);
|
---|
220 |
|
---|
221 | ThreadPool.QueueUserWorkItem(DoRun, run);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | private void ClearQualityChart()
|
---|
226 | {
|
---|
227 | QualityChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
228 | }
|
---|
229 |
|
---|
230 | private void ClearComparisonChart()
|
---|
231 | {
|
---|
232 | ComparisonChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void ClearSelectionChart()
|
---|
236 | {
|
---|
237 | SelectionChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
238 | }
|
---|
239 |
|
---|
240 | private void ButtonRun_OnClick(object sender, RoutedEventArgs e)
|
---|
241 | {
|
---|
242 | ClearQualityChart();
|
---|
243 | ClearComparisonChart();
|
---|
244 | ClearSelectionChart();
|
---|
245 |
|
---|
246 | vm.Runs.Clear();
|
---|
247 | vm.SelectedRun = null;
|
---|
248 | ButtonRun.IsEnabled = false;
|
---|
249 | TextBoxMaxIterations.IsEnabled = false;
|
---|
250 | TextBoxMaxLen.IsEnabled = false;
|
---|
251 | TextBoxRuns.IsEnabled = false;
|
---|
252 | StartRuns();
|
---|
253 | }
|
---|
254 |
|
---|
255 | private void ButtonPause_OnClick(object sender, RoutedEventArgs e)
|
---|
256 | {
|
---|
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 | //}
|
---|
270 | }
|
---|
271 |
|
---|
272 | private void ButtonStop_OnClick(object sender, RoutedEventArgs e)
|
---|
273 | {
|
---|
274 | //worker.CancelAsync();
|
---|
275 | }
|
---|
276 |
|
---|
277 | private void ComboBoxAlgorithms_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
---|
278 | {
|
---|
279 | if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch) || vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch_PruneLeaves))
|
---|
280 | {
|
---|
281 | ComboBoxPolicies.IsEnabled = true;
|
---|
282 | TabItemTree.IsEnabled = true;
|
---|
283 | }
|
---|
284 | else if (vm.SelectedAlgorithm == typeof(SequentialSearch))
|
---|
285 | {
|
---|
286 | ComboBoxPolicies.IsEnabled = true;
|
---|
287 | TabItemTree.IsEnabled = false;
|
---|
288 | }
|
---|
289 | else
|
---|
290 | {
|
---|
291 | ComboBoxPolicies.IsEnabled = false;
|
---|
292 | TabItemTree.IsEnabled = false;
|
---|
293 | }
|
---|
294 | }
|
---|
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 | {
|
---|
303 | if (vm.SelectedRun != null)
|
---|
304 | {
|
---|
305 | vm.SelectedRun.Solver.FoundNewBestSolution -= SelectedRun_FoundNewBestSolution;
|
---|
306 | }
|
---|
307 | if (ListBoxRuns.SelectedItem != null)
|
---|
308 | {
|
---|
309 | vm.SelectedRun = (Run)ListBoxRuns.SelectedItem;
|
---|
310 |
|
---|
311 | vm.SelectedRun.Solver.FoundNewBestSolution += SelectedRun_FoundNewBestSolution;
|
---|
312 |
|
---|
313 | ClearQualityChart();
|
---|
314 | DrawQualityChart(vm.SelectedRun);
|
---|
315 |
|
---|
316 | if (vm.SelectedRun.RunState == RunState.Finished)
|
---|
317 | {
|
---|
318 | ClearSelectionChart();
|
---|
319 | DrawSelectionChart(vm.SelectedRun);
|
---|
320 | }
|
---|
321 | //DrawTreeChart(vm.SelectedRun);
|
---|
322 | }
|
---|
323 | else
|
---|
324 | {
|
---|
325 | vm.SelectedRun = null;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | void SelectedRun_FoundNewBestSolution(string arg1, double arg2)
|
---|
330 | {
|
---|
331 | Dispatcher.BeginInvoke(new Action(() =>
|
---|
332 | {
|
---|
333 | ClearQualityChart();
|
---|
334 | DrawQualityChart(vm.SelectedRun);
|
---|
335 | }));
|
---|
336 | }
|
---|
337 |
|
---|
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 | {
|
---|
387 | LoadFromFile();
|
---|
388 | }
|
---|
389 |
|
---|
390 | private void SaveButton_OnClick(object sender, RoutedEventArgs e)
|
---|
391 | {
|
---|
392 | SaveToFile();
|
---|
393 | }
|
---|
394 |
|
---|
395 | private void MenuItemCopyToClipboard_OnClick(object sender, RoutedEventArgs e)
|
---|
396 | {
|
---|
397 | StringBuilder tableExport = new StringBuilder();
|
---|
398 | tableExport.AppendLine(
|
---|
399 | "Run\tMaxIterations\tEvaluations\tBestKnownQuality\tQuality\tQuality %\tFoundAt\tTotalTime\tSolutionTime\tEvaluationsPerSecond\tSolution");
|
---|
400 | for (int i = 0; i < ListViewRuns.Items.Count; i++)
|
---|
401 | {
|
---|
402 | Run run = (Run)ListViewRuns.Items[i];
|
---|
403 | tableExport.AppendLine(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,
|
---|
404 | run.MaxIterations, run.Evaluations, run.BestKnownQuality, run.BestQuality,
|
---|
405 | run.BestQuality / run.BestKnownQuality, run.BestSolutionFoundAt, run.TotalTime, run.BestSolutionTime,
|
---|
406 | run.EvaluationsPerSecond, run.BestSolution));
|
---|
407 | }
|
---|
408 | Clipboard.SetData(DataFormats.Text, tableExport.ToString());
|
---|
409 | }
|
---|
410 | }
|
---|
411 | }
|
---|