using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Evaluation.ViewModel;
using HeuristicLab.Algorithms.Bandits;
using HeuristicLab.Algorithms.Bandits.BanditPolicies;
using HeuristicLab.Algorithms.GeneticProgramming;
using HeuristicLab.Algorithms.GrammaticalOptimization;
using HeuristicLab.Algorithms.MonteCarloTreeSearch;
using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
using HeuristicLab.Problems.GrammaticalOptimization;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
namespace Evaluation
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private BackgroundWorker worker = new BackgroundWorker();
private EvaluationViewModel vm;
private List stats = new List();
private Stack newStats = new Stack();
private DispatcherTimer updateCollectionTimer;
public MainWindow()
{
InitializeComponent();
this.DataContext = vm = new EvaluationViewModel();
this.worker.WorkerSupportsCancellation = true;
this.worker.DoWork += worker_DoWork;
this.worker.ProgressChanged += worker_ProgressChanged;
this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;
////updateCollectionTimer = new DispatcherTimer();
////updateCollectionTimer.Interval = TimeSpan.FromMilliseconds(100);
////updateCollectionTimer.Tick += updateCollectionTimer_Tick;
////updateCollectionTimer.Start();
vm.HorizontalAxisString = "Evaluations";
vm.VerticalAxisString = "BestKnownQuality";
}
////void updateCollectionTimer_Tick(object sender, EventArgs e)
////{
//// while (newStats.Count > 0)
//// {
//// EvaluationStat stat = newStats.Pop();
//// stats.Add(stat);
//// }
////}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (stats.Count > 0)
{
TimeSpan timeNeeded = stats[stats.Count - 1].Time - stats[0].Time;
vm.EvaluationsPerSec = Math.Round(vm.Evaluations / timeNeeded.TotalSeconds, 2);
}
var ds = new EnumerableDataSource(stats);
ds.SetXMapping(x => x.Iteration);
ds.SetYMapping(y => y.CurrentBestQuality);
LineGraph graph = new LineGraph(ds);
graph.StrokeThickness = 2;
graph.AddToPlotter(ChartPlotter);
Debug.WriteLine("DONE");
ButtonRun.IsEnabled = true;
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Debug.WriteLine(e.UserState);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
Type algorithmType = vm.SelectedAlgorithm;
ISymbolicExpressionTreeProblem problem = vm.SelectedProblem;
Random random = new Random(DateTime.Now.Millisecond);
Type policy = vm.SelectedPolicy;
IBanditPolicy policyInstance = null;
if (policy == typeof(UCTPolicy))
{
policyInstance = new UCTPolicy();
}
else if (policy == typeof (ThresholdAscentPolicy))
{
policyInstance = new ThresholdAscentPolicy();
}
else
{
policyInstance = (IBanditPolicy)Activator.CreateInstance(policy);
}
vm.MaxLen = 1000;
vm.MaxEvaluations = 250000;
vm.BestKnownQuality = problem.BestKnownQuality(vm.MaxLen);
vm.Evaluations = 0;
vm.CurrentBestQuality = 0;
stats.Clear();
SolverBase solver = null;
if (algorithmType == typeof(MonteCarloTreeSearch))
{
solver = new MonteCarloTreeSearch(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
}
else if (algorithmType == typeof(SequentialSearch))
{
solver = new SequentialSearch(problem, vm.MaxLen, random, 0,
new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericGrammarPolicy(problem, policyInstance));
}
else if (algorithmType == typeof(RandomSearch))
{
solver = new RandomSearch(problem, random, vm.MaxLen);
}
else if (algorithmType == typeof(StandardGP))
{
solver = new StandardGP(problem, random);
}
else if (algorithmType == typeof(OffspringSelectionGP))
{
solver = new OffspringSelectionGP(problem, random);
}
solver.FoundNewBestSolution += (sentence, quality) => vm.BestSolutionFoundAt = vm.Evaluations;
solver.SolutionEvaluated += (sentence, quality) =>
{
vm.Evaluations++;
if (vm.CurrentBestQuality < quality)
{
vm.CurrentBestQuality = quality;
}
stats.Add(new EvaluationStat(DateTime.Now, vm.Evaluations, quality, vm.CurrentBestQuality));
};
solver.Run(vm.MaxEvaluations);
}
private void ButtonRun_OnClick(object sender, RoutedEventArgs e)
{
ChartPlotter.Children.RemoveAll();
ButtonRun.IsEnabled = false;
worker.RunWorkerAsync();
}
private void ButtonPause_OnClick(object sender, RoutedEventArgs e)
{
}
private void ButtonStop_OnClick(object sender, RoutedEventArgs e)
{
worker.CancelAsync();
}
private void ComboBoxAlgorithms_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch)
|| vm.SelectedAlgorithm == typeof(SequentialSearch))
{
ComboBoxPolicies.IsEnabled = true;
}
else
{
ComboBoxPolicies.IsEnabled = false;
}
}
}
}