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