[12503] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.Diagnostics;
|
---|
| 5 | using System.Linq;
|
---|
| 6 | using System.Text;
|
---|
| 7 | using System.Threading.Tasks;
|
---|
| 8 | using System.Windows;
|
---|
| 9 | using System.Windows.Controls;
|
---|
| 10 | using System.Windows.Data;
|
---|
| 11 | using System.Windows.Documents;
|
---|
| 12 | using System.Windows.Input;
|
---|
| 13 | using System.Windows.Media;
|
---|
| 14 | using System.Windows.Media.Imaging;
|
---|
| 15 | using System.Windows.Navigation;
|
---|
| 16 | using System.Windows.Shapes;
|
---|
| 17 | using System.Windows.Threading;
|
---|
| 18 | using Evaluation.ViewModel;
|
---|
| 19 | using HeuristicLab.Algorithms.Bandits;
|
---|
| 20 | using HeuristicLab.Algorithms.Bandits.BanditPolicies;
|
---|
| 21 | using HeuristicLab.Algorithms.GeneticProgramming;
|
---|
| 22 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
| 23 | using HeuristicLab.Algorithms.MonteCarloTreeSearch;
|
---|
| 24 | using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
|
---|
| 25 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 26 | using Microsoft.Research.DynamicDataDisplay;
|
---|
| 27 | using Microsoft.Research.DynamicDataDisplay.DataSources;
|
---|
| 28 |
|
---|
| 29 | namespace Evaluation
|
---|
| 30 | {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Interaction logic for MainWindow.xaml
|
---|
| 33 | /// </summary>
|
---|
| 34 | public partial class MainWindow : Window
|
---|
| 35 | {
|
---|
| 36 | private BackgroundWorker worker = new BackgroundWorker();
|
---|
| 37 | private EvaluationViewModel vm;
|
---|
| 38 | private List<EvaluationStat> stats = new List<EvaluationStat>();
|
---|
| 39 |
|
---|
| 40 | private Stack<EvaluationStat> newStats = new Stack<EvaluationStat>();
|
---|
| 41 | private DispatcherTimer updateCollectionTimer;
|
---|
| 42 |
|
---|
| 43 | public MainWindow()
|
---|
| 44 | {
|
---|
| 45 | InitializeComponent();
|
---|
| 46 | this.DataContext = vm = new EvaluationViewModel();
|
---|
| 47 | this.worker.WorkerSupportsCancellation = true;
|
---|
| 48 | this.worker.DoWork += worker_DoWork;
|
---|
| 49 | this.worker.ProgressChanged += worker_ProgressChanged;
|
---|
| 50 | this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;
|
---|
| 51 |
|
---|
| 52 | ////updateCollectionTimer = new DispatcherTimer();
|
---|
| 53 | ////updateCollectionTimer.Interval = TimeSpan.FromMilliseconds(100);
|
---|
| 54 | ////updateCollectionTimer.Tick += updateCollectionTimer_Tick;
|
---|
| 55 | ////updateCollectionTimer.Start();
|
---|
| 56 |
|
---|
| 57 | vm.HorizontalAxisString = "Evaluations";
|
---|
| 58 | vm.VerticalAxisString = "BestKnownQuality";
|
---|
| 59 |
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | ////void updateCollectionTimer_Tick(object sender, EventArgs e)
|
---|
| 63 | ////{
|
---|
| 64 | //// while (newStats.Count > 0)
|
---|
| 65 | //// {
|
---|
| 66 | //// EvaluationStat stat = newStats.Pop();
|
---|
| 67 | //// stats.Add(stat);
|
---|
| 68 | //// }
|
---|
| 69 | ////}
|
---|
| 70 |
|
---|
| 71 | void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
---|
| 72 | {
|
---|
| 73 | if (stats.Count > 0)
|
---|
| 74 | {
|
---|
| 75 | TimeSpan timeNeeded = stats[stats.Count - 1].Time - stats[0].Time;
|
---|
| 76 | vm.EvaluationsPerSec = Math.Round(vm.Evaluations / timeNeeded.TotalSeconds, 2);
|
---|
| 77 | }
|
---|
| 78 | var ds = new EnumerableDataSource<EvaluationStat>(stats);
|
---|
| 79 | ds.SetXMapping(x => x.Iteration);
|
---|
| 80 | ds.SetYMapping(y => y.CurrentBestQuality);
|
---|
| 81 |
|
---|
| 82 | LineGraph graph = new LineGraph(ds);
|
---|
| 83 |
|
---|
| 84 | graph.StrokeThickness = 2;
|
---|
| 85 | graph.AddToPlotter(ChartPlotter);
|
---|
| 86 |
|
---|
| 87 | Debug.WriteLine("DONE");
|
---|
| 88 | ButtonRun.IsEnabled = true;
|
---|
| 89 | }
|
---|
| 90 | void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
---|
| 91 | {
|
---|
| 92 | Debug.WriteLine(e.UserState);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void worker_DoWork(object sender, DoWorkEventArgs e)
|
---|
| 96 | {
|
---|
| 97 | Type algorithmType = vm.SelectedAlgorithm;
|
---|
| 98 |
|
---|
| 99 | ISymbolicExpressionTreeProblem problem = vm.SelectedProblem;
|
---|
| 100 |
|
---|
| 101 | Random random = new Random(DateTime.Now.Millisecond);
|
---|
| 102 |
|
---|
| 103 | Type policy = vm.SelectedPolicy;
|
---|
| 104 | IBanditPolicy policyInstance = null;
|
---|
| 105 |
|
---|
| 106 | if (policy == typeof(UCTPolicy))
|
---|
| 107 | {
|
---|
| 108 | policyInstance = new UCTPolicy();
|
---|
| 109 | }
|
---|
| 110 | else if (policy == typeof (ThresholdAscentPolicy))
|
---|
| 111 | {
|
---|
| 112 | policyInstance = new ThresholdAscentPolicy();
|
---|
| 113 | }
|
---|
| 114 | else
|
---|
| 115 | {
|
---|
| 116 | policyInstance = (IBanditPolicy)Activator.CreateInstance(policy);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | vm.MaxLen = 1000;
|
---|
| 120 | vm.MaxEvaluations = 250000;
|
---|
| 121 |
|
---|
| 122 | vm.BestKnownQuality = problem.BestKnownQuality(vm.MaxLen);
|
---|
| 123 |
|
---|
| 124 | vm.Evaluations = 0;
|
---|
| 125 | vm.CurrentBestQuality = 0;
|
---|
| 126 |
|
---|
| 127 | stats.Clear();
|
---|
| 128 |
|
---|
| 129 | SolverBase solver = null;
|
---|
| 130 |
|
---|
| 131 | if (algorithmType == typeof(MonteCarloTreeSearch))
|
---|
| 132 | {
|
---|
| 133 | solver = new MonteCarloTreeSearch(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
|
---|
| 134 | }
|
---|
| 135 | else if (algorithmType == typeof(SequentialSearch))
|
---|
| 136 | {
|
---|
| 137 | solver = new SequentialSearch(problem, vm.MaxLen, random, 0,
|
---|
| 138 | new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, policyInstance));
|
---|
| 139 | }
|
---|
| 140 | else if (algorithmType == typeof(RandomSearch))
|
---|
| 141 | {
|
---|
| 142 | solver = new RandomSearch(problem, random, vm.MaxLen);
|
---|
| 143 | }
|
---|
| 144 | else if (algorithmType == typeof(StandardGP))
|
---|
| 145 | {
|
---|
| 146 | solver = new StandardGP(problem, random);
|
---|
| 147 | }
|
---|
| 148 | else if (algorithmType == typeof(OffspringSelectionGP))
|
---|
| 149 | {
|
---|
| 150 | solver = new OffspringSelectionGP(problem, random);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | solver.FoundNewBestSolution += (sentence, quality) => vm.BestSolutionFoundAt = vm.Evaluations;
|
---|
| 154 | solver.SolutionEvaluated += (sentence, quality) =>
|
---|
| 155 | {
|
---|
| 156 | vm.Evaluations++;
|
---|
| 157 | if (vm.CurrentBestQuality < quality)
|
---|
| 158 | {
|
---|
| 159 | vm.CurrentBestQuality = quality;
|
---|
| 160 | }
|
---|
| 161 | stats.Add(new EvaluationStat(DateTime.Now, vm.Evaluations, quality, vm.CurrentBestQuality));
|
---|
| 162 | };
|
---|
| 163 |
|
---|
| 164 | solver.Run(vm.MaxEvaluations);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | private void ButtonRun_OnClick(object sender, RoutedEventArgs e)
|
---|
| 168 | {
|
---|
| 169 | ChartPlotter.Children.RemoveAll<LineGraph>();
|
---|
| 170 | ButtonRun.IsEnabled = false;
|
---|
| 171 | worker.RunWorkerAsync();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | private void ButtonPause_OnClick(object sender, RoutedEventArgs e)
|
---|
| 175 | {
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | private void ButtonStop_OnClick(object sender, RoutedEventArgs e)
|
---|
| 179 | {
|
---|
| 180 | worker.CancelAsync();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | private void ComboBoxAlgorithms_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
---|
| 184 | {
|
---|
| 185 | if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch)
|
---|
| 186 | || vm.SelectedAlgorithm == typeof(SequentialSearch))
|
---|
| 187 | {
|
---|
| 188 | ComboBoxPolicies.IsEnabled = true;
|
---|
| 189 | }
|
---|
| 190 | else
|
---|
| 191 | {
|
---|
| 192 | ComboBoxPolicies.IsEnabled = false;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|