using System.Collections.ObjectModel;
using System.Threading;
using System.Xml.Serialization;
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;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Microsoft.Win32;
using WpfTestSvgSample;
namespace Evaluation
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private EvaluationViewModel vm;
private DispatcherTimer updateCollectionTimer;
private DrawingPage treeDrawingPage;
public MainWindow()
{
InitializeComponent();
CenterWindowOnScreen();
this.DataContext = vm = new EvaluationViewModel();
vm.MaxLen = 100;
vm.MaxEvaluations = 1000000;
vm.NrRuns = 10;
vm.VerticalAxisString = "SolutionQuality";
vm.HorizontalAxisString = "Iteration";
}
private void CenterWindowOnScreen()
{
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = this.Width;
double windowHeight = this.Height;
this.Left = (screenWidth / 2) - (windowWidth / 2);
this.Top = (screenHeight / 2) - (windowHeight / 2);
}
private void DrawLineChart(Run run)
{
List solutions = new List(run.FoundSolutions);
if (run.BestSolutionFoundAt < run.Evaluations)
{
solutions.Add(new FoundSolution(run.EndTime, run.Evaluations, run.BestQuality, run.BestSolution));
}
var ds = new EnumerableDataSource(solutions);
ds.SetXMapping(x => x.Iteration);
ds.SetYMapping(y => y.Quality);
LineGraph graph = new LineGraph(ds);
graph.StrokeThickness = 2;
graph.AddToPlotter(ChartPlotter);
}
private void DrawTreeChart(Run run)
{
if (!string.IsNullOrEmpty(run.SvgFile))
{
treeDrawingPage.LoadDocument(run.SvgFile);
}
}
private void DoRun(Object threadContext)
{
Run run = (Run)threadContext;
run.RunState = RunState.Running;
run.StartTime = DateTime.Now;
run.Solver.Run(run.MaxEvaluations);
run.EndTime = DateTime.Now;
run.RunState = RunState.Analyzing;
if (run.FoundSolutions.Count > 0)
{
if (run.Solver is MonteCarloTreeSearch)
{
MonteCarloTreeSearch mctsSolver = (MonteCarloTreeSearch)run.Solver;
run.TreeInfos = mctsSolver.GetTreeInfos();
//byte[] output = mctsSolver.GenerateSvg();
//if (output != null && output.Length > 0)
//{
// run.SvgFile = string.Format("MCTS_SVG_#{0}_{1}.svg", run.RunNumber, DateTime.Now.Ticks);
// File.WriteAllBytes(run.SvgFile, mctsSolver.GenerateSvg());
//}
}
}
run.RunState = RunState.Finished;
lock (vm.CompletedRuns)
{
int completed = 0;
foreach (Run r in vm.Runs)
{
if (r.RunState == RunState.Finished)
{
completed++;
}
}
vm.CompletedRuns = string.Format("{0}/{1}", completed, vm.Runs.Count);
if (completed == vm.NrRuns)
{
Dispatcher.Invoke(AllRunsFinished);
}
}
}
private void AllRunsFinished()
{
ButtonRun.IsEnabled = true;
TextBoxMaxEvaluations.IsEnabled = true;
TextBoxMaxLen.IsEnabled = true;
TextBoxRuns.IsEnabled = true;
}
private void StartRuns()
{
vm.CompletedRuns = string.Format("0/{0}", vm.NrRuns);
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);
}
for (int i = 0; i < vm.NrRuns; i++)
{
ISolver solver = null;
if (algorithmType == typeof(MonteCarloTreeSearch_PruneLeaves))
{
solver = new MonteCarloTreeSearch_PruneLeaves(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
}
else 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);
}
Run run = new Run(problem, policyInstance, solver, i + 1, vm.MaxEvaluations, vm.MaxLen);
vm.Runs.Add(run);
ThreadPool.QueueUserWorkItem(DoRun, run);
}
}
private void ClearLineChart()
{
ChartPlotter.Children.RemoveAll();
}
private void ClearComparisonChart()
{
ComparisonChartPlotter.Children.RemoveAll();
}
private void ButtonRun_OnClick(object sender, RoutedEventArgs e)
{
ClearLineChart();
ClearComparisonChart();
vm.Runs.Clear();
vm.SelectedRun = null;
ButtonRun.IsEnabled = false;
TextBoxMaxEvaluations.IsEnabled = false;
TextBoxMaxLen.IsEnabled = false;
TextBoxRuns.IsEnabled = false;
StartRuns();
}
private void ButtonPause_OnClick(object sender, RoutedEventArgs e)
{
//if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch))
//{
// MonteCarloTreeSearch mcts = (MonteCarloTreeSearch)solver;
// mcts.PauseContinue();
// if (mcts.IsPaused)
// {
// ButtonPause.Content = "Continue";
// }
// else
// {
// ButtonPause.Content = "Pause";
// }
//}
}
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(MonteCarloTreeSearch_PruneLeaves))
{
ComboBoxPolicies.IsEnabled = true;
TabItemTree.IsEnabled = true;
}
else if (vm.SelectedAlgorithm == typeof(SequentialSearch))
{
ComboBoxPolicies.IsEnabled = true;
TabItemTree.IsEnabled = false;
}
else
{
ComboBoxPolicies.IsEnabled = false;
TabItemTree.IsEnabled = false;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
treeDrawingPage = treeDrawing.Content as DrawingPage;
}
private void ListBoxRuns_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (vm.SelectedRun != null)
{
vm.SelectedRun.Solver.FoundNewBestSolution -= SelectedRun_FoundNewBestSolution;
}
if (ListBoxRuns.SelectedItem != null)
{
vm.SelectedRun = (Run)ListBoxRuns.SelectedItem;
vm.SelectedRun.Solver.FoundNewBestSolution += SelectedRun_FoundNewBestSolution;
ClearLineChart();
DrawLineChart(vm.SelectedRun);
//DrawTreeChart(vm.SelectedRun);
}
else
{
vm.SelectedRun = null;
}
}
void SelectedRun_FoundNewBestSolution(string arg1, double arg2)
{
Dispatcher.BeginInvoke(new Action(() =>
{
ClearLineChart();
DrawLineChart(vm.SelectedRun);
}));
}
public void SaveToFile()
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "runs"; // Default file name
dlg.DefaultExt = ".xml"; // Default file extension
// Show save file dialog box
Nullable result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection));
using (TextWriter writer = new StreamWriter(filename))
{
serializer.Serialize(writer, vm.Runs);
}
}
}
public void LoadFromFile()
{
OpenFileDialog dlg = new OpenFileDialog();
// Show save file dialog box
Nullable result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Load document
string filename = dlg.FileName;
XmlSerializer deserializer = new XmlSerializer(typeof(ObservableCollection));
using (TextReader reader = new StreamReader(filename))
{
object obj = deserializer.Deserialize(reader);
vm.Runs = (ObservableCollection)obj;
}
}
}
private void LoadButton_OnClick(object sender, RoutedEventArgs e)
{
LoadFromFile();
}
private void SaveButton_OnClick(object sender, RoutedEventArgs e)
{
SaveToFile();
}
}
}