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 BackgroundWorker worker = new BackgroundWorker();
|
---|
33 | private EvaluationViewModel vm;
|
---|
34 |
|
---|
35 | private DispatcherTimer updateCollectionTimer;
|
---|
36 |
|
---|
37 | private DrawingPage treeDrawingPage;
|
---|
38 |
|
---|
39 | public MainWindow()
|
---|
40 | {
|
---|
41 | InitializeComponent();
|
---|
42 | CenterWindowOnScreen();
|
---|
43 | this.DataContext = vm = new EvaluationViewModel();
|
---|
44 | vm.MaxLen = 50;
|
---|
45 | vm.MaxEvaluations = 250000;
|
---|
46 | vm.NrRuns = 1;
|
---|
47 | vm.CurrentRunString = "0/1";
|
---|
48 | vm.Threads = 6;
|
---|
49 | vm.VerticalAxisString = "SolutionQuality";
|
---|
50 | vm.HorizontalAxisString = "Iteration";
|
---|
51 | this.worker.WorkerSupportsCancellation = true;
|
---|
52 | this.worker.DoWork += worker_DoWork;
|
---|
53 | this.worker.ProgressChanged += worker_ProgressChanged;
|
---|
54 | this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;
|
---|
55 |
|
---|
56 | ////updateCollectionTimer = new DispatcherTimer();
|
---|
57 | ////updateCollectionTimer.Interval = TimeSpan.FromMilliseconds(100);
|
---|
58 | ////updateCollectionTimer.Tick += updateCollectionTimer_Tick;
|
---|
59 | ////updateCollectionTimer.Start();
|
---|
60 | }
|
---|
61 |
|
---|
62 | private void CenterWindowOnScreen()
|
---|
63 | {
|
---|
64 | double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
|
---|
65 | double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
|
---|
66 | double windowWidth = this.Width;
|
---|
67 | double windowHeight = this.Height;
|
---|
68 | this.Left = (screenWidth / 2) - (windowWidth / 2);
|
---|
69 | this.Top = (screenHeight / 2) - (windowHeight / 2);
|
---|
70 | }
|
---|
71 |
|
---|
72 | ////void updateCollectionTimer_Tick(object sender, EventArgs e)
|
---|
73 | ////{
|
---|
74 | //// while (newStats.Count > 0)
|
---|
75 | //// {
|
---|
76 | //// EvaluationStat stat = newStats.Pop();
|
---|
77 | //// stats.Add(stat);
|
---|
78 | //// }
|
---|
79 | ////}
|
---|
80 |
|
---|
81 | void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
---|
82 | {
|
---|
83 | ButtonRun.IsEnabled = true;
|
---|
84 | TextBoxMaxEvaluations.IsEnabled = true;
|
---|
85 | TextBoxMaxLen.IsEnabled = true;
|
---|
86 | TextBoxRuns.IsEnabled = true;
|
---|
87 | TextBoxThreads.IsEnabled = true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | private void DrawCharts(Run run)
|
---|
91 | {
|
---|
92 | ClearChart();
|
---|
93 |
|
---|
94 | var ds = new EnumerableDataSource<EvaluationStat>(run.EvaluationStats);
|
---|
95 | ds.SetXMapping(x => x.Iteration);
|
---|
96 | ds.SetYMapping(y => y.CurrentBestQuality);
|
---|
97 |
|
---|
98 | LineGraph graph = new LineGraph(ds);
|
---|
99 |
|
---|
100 | graph.StrokeThickness = 2;
|
---|
101 | graph.AddToPlotter(ChartPlotter);
|
---|
102 |
|
---|
103 | DrawTreeChart(run);
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void DrawTreeChart(Run run)
|
---|
107 | {
|
---|
108 | if (!string.IsNullOrEmpty(run.SvgFile))
|
---|
109 | {
|
---|
110 | treeDrawingPage.LoadDocument(run.SvgFile);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
---|
116 | {
|
---|
117 | Debug.WriteLine(e.UserState);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void worker_DoWork(object sender, DoWorkEventArgs e)
|
---|
121 | {
|
---|
122 |
|
---|
123 | Type algorithmType = vm.SelectedAlgorithm;
|
---|
124 |
|
---|
125 | ISymbolicExpressionTreeProblem problem = vm.SelectedProblem;
|
---|
126 |
|
---|
127 | Random random = new Random(DateTime.Now.Millisecond);
|
---|
128 |
|
---|
129 | Type policy = vm.SelectedPolicy;
|
---|
130 | IBanditPolicy policyInstance = null;
|
---|
131 |
|
---|
132 | if (policy == typeof(UCTPolicy))
|
---|
133 | {
|
---|
134 | policyInstance = new UCTPolicy();
|
---|
135 | }
|
---|
136 | else if (policy == typeof(ThresholdAscentPolicy))
|
---|
137 | {
|
---|
138 | policyInstance = new ThresholdAscentPolicy();
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | policyInstance = (IBanditPolicy)Activator.CreateInstance(policy);
|
---|
143 | }
|
---|
144 |
|
---|
145 | vm.BestKnownQuality = problem.BestKnownQuality(vm.MaxLen);
|
---|
146 |
|
---|
147 | for (int i = 0; i < vm.NrRuns; i++)
|
---|
148 | {
|
---|
149 | vm.CurrentRunString = string.Format("{0}/{1}", i + 1, vm.NrRuns);
|
---|
150 | vm.Evaluations = 0;
|
---|
151 | vm.CurrentBestQuality = 0;
|
---|
152 |
|
---|
153 | List<EvaluationStat> stats = new List<EvaluationStat>();
|
---|
154 |
|
---|
155 | ISolver solver = null;
|
---|
156 |
|
---|
157 | if (algorithmType == typeof(MonteCarloTreeSearch))
|
---|
158 | {
|
---|
159 | solver = new MonteCarloTreeSearch(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
|
---|
160 | }
|
---|
161 | else if (algorithmType == typeof(SequentialSearch))
|
---|
162 | {
|
---|
163 | solver = new SequentialSearch(problem, vm.MaxLen, random, 0,
|
---|
164 | new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, policyInstance));
|
---|
165 | }
|
---|
166 | else if (algorithmType == typeof(RandomSearch))
|
---|
167 | {
|
---|
168 | solver = new RandomSearch(problem, random, vm.MaxLen);
|
---|
169 | }
|
---|
170 | else if (algorithmType == typeof(StandardGP))
|
---|
171 | {
|
---|
172 | solver = new StandardGP(problem, random);
|
---|
173 | }
|
---|
174 | else if (algorithmType == typeof(OffspringSelectionGP))
|
---|
175 | {
|
---|
176 | solver = new OffspringSelectionGP(problem, random);
|
---|
177 | }
|
---|
178 |
|
---|
179 | solver.FoundNewBestSolution += (sentence, quality) =>
|
---|
180 | {
|
---|
181 | vm.BestSolutionFoundAt = vm.Evaluations;
|
---|
182 | vm.BestSolution = sentence;
|
---|
183 | };
|
---|
184 | solver.SolutionEvaluated += (sentence, quality) =>
|
---|
185 | {
|
---|
186 | vm.Evaluations++;
|
---|
187 | if (vm.CurrentBestQuality < quality)
|
---|
188 | {
|
---|
189 | vm.CurrentBestQuality = quality;
|
---|
190 | }
|
---|
191 | stats.Add(new EvaluationStat(DateTime.Now, vm.Evaluations, quality, vm.CurrentBestQuality));
|
---|
192 | };
|
---|
193 |
|
---|
194 | solver.Run(vm.MaxEvaluations);
|
---|
195 |
|
---|
196 |
|
---|
197 | if (stats.Count > 0)
|
---|
198 | {
|
---|
199 | TimeSpan totalTimeNeeded = stats[stats.Count - 1].Time - stats[0].Time;
|
---|
200 | vm.EvaluationsPerSec = Math.Round(vm.Evaluations / totalTimeNeeded.TotalSeconds, 2);
|
---|
201 |
|
---|
202 | TimeSpan timeForBestSolution = stats[vm.BestSolutionFoundAt - 1].Time - stats[0].Time;
|
---|
203 |
|
---|
204 | Run run = new Run(i, vm.Evaluations, vm.CurrentBestQuality, vm.BestKnownQuality,
|
---|
205 | vm.BestSolutionFoundAt, vm.BestSolution, vm.EvaluationsPerSec,
|
---|
206 | totalTimeNeeded, timeForBestSolution, stats);
|
---|
207 |
|
---|
208 |
|
---|
209 | MonteCarloTreeSearch mctsSolver = null;
|
---|
210 | if (algorithmType == typeof(MonteCarloTreeSearch))
|
---|
211 | {
|
---|
212 | mctsSolver = (MonteCarloTreeSearch)solver;
|
---|
213 |
|
---|
214 | run.TreeInfos = mctsSolver.GetTreeInfos();
|
---|
215 |
|
---|
216 | byte[] output = mctsSolver.GenerateSvg();
|
---|
217 | if (output.Length > 0)
|
---|
218 | {
|
---|
219 | run.SvgFile = string.Format("MCTS_SVG_#{0}_{1}.svg", i, DateTime.Now.Ticks);
|
---|
220 | File.WriteAllBytes(run.SvgFile, mctsSolver.GenerateSvg());
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | Dispatcher.BeginInvoke(new Action(() => vm.Runs.Add(run)));
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void ClearChart()
|
---|
230 | {
|
---|
231 | ChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
232 | }
|
---|
233 |
|
---|
234 | private void ButtonRun_OnClick(object sender, RoutedEventArgs e)
|
---|
235 | {
|
---|
236 | ClearChart();
|
---|
237 | vm.Runs.Clear();
|
---|
238 | vm.CurrentRun = null;
|
---|
239 | ButtonRun.IsEnabled = false;
|
---|
240 | TextBoxMaxEvaluations.IsEnabled = false;
|
---|
241 | TextBoxMaxLen.IsEnabled = false;
|
---|
242 | TextBoxRuns.IsEnabled = false;
|
---|
243 | TextBoxThreads.IsEnabled = false;
|
---|
244 | worker.RunWorkerAsync();
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void ButtonPause_OnClick(object sender, RoutedEventArgs e)
|
---|
248 | {
|
---|
249 | //if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch))
|
---|
250 | //{
|
---|
251 | // MonteCarloTreeSearch mcts = (MonteCarloTreeSearch)solver;
|
---|
252 | // mcts.PauseContinue();
|
---|
253 | // if (mcts.IsPaused)
|
---|
254 | // {
|
---|
255 | // ButtonPause.Content = "Continue";
|
---|
256 | // }
|
---|
257 | // else
|
---|
258 | // {
|
---|
259 | // ButtonPause.Content = "Pause";
|
---|
260 | // }
|
---|
261 | //}
|
---|
262 | }
|
---|
263 |
|
---|
264 | private void ButtonStop_OnClick(object sender, RoutedEventArgs e)
|
---|
265 | {
|
---|
266 | worker.CancelAsync();
|
---|
267 | }
|
---|
268 |
|
---|
269 | private void ComboBoxAlgorithms_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
---|
270 | {
|
---|
271 | if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch))
|
---|
272 | {
|
---|
273 | ComboBoxPolicies.IsEnabled = true;
|
---|
274 | TabItemTree.IsEnabled = true;
|
---|
275 | }
|
---|
276 | else if (vm.SelectedAlgorithm == typeof(SequentialSearch))
|
---|
277 | {
|
---|
278 | ComboBoxPolicies.IsEnabled = true;
|
---|
279 | TabItemTree.IsEnabled = false;
|
---|
280 | }
|
---|
281 | else
|
---|
282 | {
|
---|
283 | ComboBoxPolicies.IsEnabled = false;
|
---|
284 | TabItemTree.IsEnabled = false;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | private void Window_Loaded(object sender, RoutedEventArgs e)
|
---|
289 | {
|
---|
290 | treeDrawingPage = treeDrawing.Content as DrawingPage;
|
---|
291 | }
|
---|
292 |
|
---|
293 | private void ListBoxRuns_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
---|
294 | {
|
---|
295 | if (ListBoxRuns.SelectedItem != null)
|
---|
296 | {
|
---|
297 | vm.CurrentRun = (Run)ListBoxRuns.SelectedItem;
|
---|
298 | DrawCharts(vm.CurrentRun);
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | public void SaveToFile()
|
---|
303 | {
|
---|
304 | SaveFileDialog dlg = new SaveFileDialog();
|
---|
305 | dlg.FileName = "runs"; // Default file name
|
---|
306 | dlg.DefaultExt = ".xml"; // Default file extension
|
---|
307 |
|
---|
308 | // Show save file dialog box
|
---|
309 | Nullable<bool> result = dlg.ShowDialog();
|
---|
310 |
|
---|
311 | // Process save file dialog box results
|
---|
312 | if (result == true)
|
---|
313 | {
|
---|
314 | // Save document
|
---|
315 | string filename = dlg.FileName;
|
---|
316 |
|
---|
317 | XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Run>));
|
---|
318 | using (TextWriter writer = new StreamWriter(filename))
|
---|
319 | {
|
---|
320 | serializer.Serialize(writer, vm.Runs);
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | }
|
---|
325 |
|
---|
326 | public void LoadFromFile()
|
---|
327 | {
|
---|
328 | OpenFileDialog dlg = new OpenFileDialog();
|
---|
329 |
|
---|
330 | // Show save file dialog box
|
---|
331 | Nullable<bool> result = dlg.ShowDialog();
|
---|
332 |
|
---|
333 | // Process save file dialog box results
|
---|
334 | if (result == true)
|
---|
335 | {
|
---|
336 | // Load document
|
---|
337 | string filename = dlg.FileName;
|
---|
338 |
|
---|
339 | XmlSerializer deserializer = new XmlSerializer(typeof(ObservableCollection<Run>));
|
---|
340 | using (TextReader reader = new StreamReader(filename))
|
---|
341 | {
|
---|
342 | object obj = deserializer.Deserialize(reader);
|
---|
343 | vm.Runs = (ObservableCollection<Run>)obj;
|
---|
344 |
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | private void LoadButton_OnClick(object sender, RoutedEventArgs e)
|
---|
350 | {
|
---|
351 | LoadFromFile();
|
---|
352 | }
|
---|
353 |
|
---|
354 | private void SaveButton_OnClick(object sender, RoutedEventArgs e)
|
---|
355 | {
|
---|
356 | SaveToFile();
|
---|
357 | }
|
---|
358 |
|
---|
359 | private void TabControlRunComparison_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
---|
360 | {
|
---|
361 | ////if (TabControlRunComparison.SelectedItem == TabItemChartRunComparison)
|
---|
362 | ////{
|
---|
363 | //// ComparisonChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
364 |
|
---|
365 | //// var ds = new EnumerableDataSource<EvaluationStat>(run.EvaluationStats);
|
---|
366 | //// ds.SetXMapping(x => x.Iteration);
|
---|
367 | //// ds.SetYMapping(y => y.CurrentBestQuality);
|
---|
368 |
|
---|
369 | //// LineGraph graph = new LineGraph(ds);
|
---|
370 |
|
---|
371 | //// graph.StrokeThickness = 2;
|
---|
372 | //// graph.AddToPlotter(ChartPlotter);
|
---|
373 |
|
---|
374 | ////}
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|