using System.Diagnostics; using HeuristicLab.Algorithms.Bandits; using HeuristicLab.Algorithms.GrammaticalOptimization; using HeuristicLab.Algorithms.MonteCarloTreeSearch; using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base; using HeuristicLab.Problems.GrammaticalOptimization; using System; using System.Collections.Generic; using System.ComponentModel; namespace Evaluation { public enum RunState { NotStarted, Running, Analyzing, Finished } public class Run : INotifyPropertyChanged { public Run() { } public Run(ISymbolicExpressionTreeProblem problem, IBanditPolicy banditPolicy, ISolver solver, int runNumber, int maxIterations, int maxLen) { Problem = problem; BanditPolicy = banditPolicy; Solver = solver; RunNumber = runNumber; MaxIterations = maxIterations; MaxLen = maxLen; selectionIndicators = new List(); Evaluations = 0; BestQuality = double.MinValue; RunState = RunState.NotStarted; FoundSolutions = new List(); CurrentSelectionIndicator = new SelectionIndicator(0, 0, 0); BestKnownQuality = problem.BestKnownQuality(maxLen); solver.SolutionEvaluated += (sentence, quality) => { Evaluations++; if (quality > BestQuality) { BestQuality = quality; BestSolution = sentence; BestSolutionFoundAt = evaluations; FoundSolution solution = new FoundSolution(DateTime.Now, Evaluations, quality, sentence); BestSolutionTime = solution.Time - StartTime; FoundSolutions.Add(solution); } }; if (solver is MonteCarloTreeSearch) { MonteCarloTreeSearch mcts = (MonteCarloTreeSearch)solver; mcts.IterationFinished += mcts_IterationFinished; } } private void mcts_IterationFinished(int goodSelections, int totalSelections) { //CurrentSelectionIndicator.GoodSelections = goodSelections; //CurrentSelectionIndicator.TotalSelections = totalSelections; //this.OnPropertyChanged("CurrentSelectionIndicator"); CurrentSelectionIndicator = new SelectionIndicator(goodSelections, totalSelections, selectionIndicators.Count); selectionIndicators.Add(CurrentSelectionIndicator); } private List selectionIndicators; public List SelectionIndicators { get { return selectionIndicators; } } private SelectionIndicator currentSelectionIndicator; public SelectionIndicator CurrentSelectionIndicator { get { return this.currentSelectionIndicator; } set { this.currentSelectionIndicator = value; this.OnPropertyChanged("CurrentSelectionIndicator"); } } private ISymbolicExpressionTreeProblem problem; public ISymbolicExpressionTreeProblem Problem { get { return this.problem; } set { this.problem = value; this.OnPropertyChanged("Problem"); } } private IBanditPolicy banditPolicy; public IBanditPolicy BanditPolicy { get { return this.banditPolicy; } set { this.banditPolicy = value; this.OnPropertyChanged("BanditPolicy"); } } private ISolver solver; public ISolver Solver { get { return this.solver; } set { this.solver = value; this.OnPropertyChanged("Solver"); } } private int runNumber; public int RunNumber { get { return this.runNumber; } set { this.runNumber = value; this.OnPropertyChanged("RunNumber"); } } private int maxIterations; public int MaxIterations { get { return this.maxIterations; } set { this.maxIterations = value; this.OnPropertyChanged("MaxIterations"); } } private int maxLen; public int MaxLen { get { return this.maxLen; } set { this.maxLen = value; this.OnPropertyChanged("MaxLen"); } } private int evaluations; public int Evaluations { get { return this.evaluations; } set { this.evaluations = value; this.OnPropertyChanged("Evaluations"); if (endTime > startTime) { // endTime is set... EvaluationsPerSecond = Math.Round(evaluations / TotalTime.TotalSeconds, 2); } else if (startTime > endTime) { // only startTime is set... TimeSpan currentTimeNeeded = DateTime.Now - startTime; EvaluationsPerSecond = Math.Round(evaluations / currentTimeNeeded.TotalSeconds, 2); } } } private double bestQuality; public double BestQuality { get { return this.bestQuality; } set { this.bestQuality = value; this.OnPropertyChanged("BestQuality"); } } private string bestSolution; public string BestSolution { get { return this.bestSolution; } set { this.bestSolution = value; this.OnPropertyChanged("BestSolution"); } } private double bestKnownQuality; public double BestKnownQuality { get { return this.bestKnownQuality; } set { this.bestKnownQuality = value; this.OnPropertyChanged("BestKnownQuality"); } } private int bestSolutionFoundAt; public int BestSolutionFoundAt { get { return this.bestSolutionFoundAt; } set { this.bestSolutionFoundAt = value; this.OnPropertyChanged("BestSolutionFoundAt"); } } private double evaluationsPerSecond; public double EvaluationsPerSecond { get { return this.evaluationsPerSecond; } set { this.evaluationsPerSecond = value; this.OnPropertyChanged("EvaluationsPerSecond"); } } private DateTime startTime; public DateTime StartTime { get { return this.startTime; } set { this.startTime = value; this.OnPropertyChanged("StartTime"); } } private DateTime endTime; public DateTime EndTime { get { return this.endTime; } set { this.endTime = value; this.OnPropertyChanged("EndTime"); TotalTime = EndTime - StartTime; } } private TimeSpan totalTime; public TimeSpan TotalTime { get { return this.totalTime; } set { this.totalTime = value; this.OnPropertyChanged("TotalTime"); } } private TimeSpan bestSolutionTime; public TimeSpan BestSolutionTime { get { return this.bestSolutionTime; } set { this.bestSolutionTime = value; this.OnPropertyChanged("BestSolutionTime"); } } public List FoundSolutions { get; set; } public string SvgFile { get; set; } private TreeInfos treeInfos; public TreeInfos TreeInfos { get { return this.treeInfos; } set { this.treeInfos = value; this.OnPropertyChanged("TreeInfos"); } } private RunState runState; public RunState RunState { get { return this.runState; } set { this.runState = value; this.OnPropertyChanged("RunState"); } } public override string ToString() { return string.Format("Run #{0}", this.RunNumber); } #region INotifyPropertyChanged members public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { try { if (PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } catch (Exception) { } } #endregion INotifyPropertyChanged members } }