Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/Evaluation/MainWindow.xaml.cs @ 12832

Last change on this file since 12832 was 12832, checked in by aballeit, 9 years ago

#2283 limit parallelism

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