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