[12840] | 1 | using System.Diagnostics;
|
---|
| 2 | using HeuristicLab.Algorithms.Bandits;
|
---|
[12781] | 3 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
[12815] | 4 | using HeuristicLab.Algorithms.MonteCarloTreeSearch;
|
---|
[12781] | 5 | using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base;
|
---|
| 6 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 7 | using System;
|
---|
[12762] | 8 | using System.Collections.Generic;
|
---|
[12781] | 9 | using System.ComponentModel;
|
---|
[12762] | 10 |
|
---|
| 11 | namespace Evaluation
|
---|
| 12 | {
|
---|
[12781] | 13 |
|
---|
| 14 | public enum RunState
|
---|
[12762] | 15 | {
|
---|
[12781] | 16 | NotStarted,
|
---|
| 17 | Running,
|
---|
| 18 | Analyzing,
|
---|
| 19 | Finished
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public class Run : INotifyPropertyChanged
|
---|
| 23 | {
|
---|
[12762] | 24 | public Run()
|
---|
| 25 | {
|
---|
| 26 |
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[12781] | 29 | public Run(ISymbolicExpressionTreeProblem problem, IBanditPolicy banditPolicy, ISolver solver, int runNumber,
|
---|
[12815] | 30 | int maxIterations, int maxLen)
|
---|
[12762] | 31 | {
|
---|
[12781] | 32 | Problem = problem;
|
---|
| 33 | BanditPolicy = banditPolicy;
|
---|
| 34 | Solver = solver;
|
---|
[12762] | 35 | RunNumber = runNumber;
|
---|
[12815] | 36 | MaxIterations = maxIterations;
|
---|
[12781] | 37 | MaxLen = maxLen;
|
---|
| 38 |
|
---|
[12815] | 39 | selectionIndicators = new List<SelectionIndicator>();
|
---|
[12781] | 40 | Evaluations = 0;
|
---|
| 41 | BestQuality = double.MinValue;
|
---|
| 42 | RunState = RunState.NotStarted;
|
---|
| 43 | FoundSolutions = new List<FoundSolution>();
|
---|
[12840] | 44 | CurrentSelectionIndicator = new SelectionIndicator(0, 0, 0);
|
---|
[12781] | 45 |
|
---|
| 46 | BestKnownQuality = problem.BestKnownQuality(maxLen);
|
---|
| 47 | solver.SolutionEvaluated += (sentence, quality) =>
|
---|
| 48 | {
|
---|
| 49 | Evaluations++;
|
---|
| 50 | if (quality > BestQuality)
|
---|
| 51 | {
|
---|
| 52 | BestQuality = quality;
|
---|
| 53 | BestSolution = sentence;
|
---|
| 54 | BestSolutionFoundAt = evaluations;
|
---|
[12824] | 55 | FoundSolution solution = new FoundSolution(DateTime.Now, Evaluations, quality, sentence);
|
---|
| 56 | BestSolutionTime = solution.Time - StartTime;
|
---|
| 57 | FoundSolutions.Add(solution);
|
---|
[12781] | 58 | }
|
---|
| 59 | };
|
---|
[12815] | 60 | if (solver is MonteCarloTreeSearch)
|
---|
| 61 | {
|
---|
| 62 | MonteCarloTreeSearch mcts = (MonteCarloTreeSearch)solver;
|
---|
[12840] | 63 | mcts.IterationFinished += mcts_IterationFinished;
|
---|
[12815] | 64 | }
|
---|
[12762] | 65 | }
|
---|
| 66 |
|
---|
[12840] | 67 | private void mcts_IterationFinished(int goodSelections, int totalSelections)
|
---|
[12815] | 68 | {
|
---|
[12840] | 69 | //CurrentSelectionIndicator.GoodSelections = goodSelections;
|
---|
| 70 | //CurrentSelectionIndicator.TotalSelections = totalSelections;
|
---|
| 71 | //this.OnPropertyChanged("CurrentSelectionIndicator");
|
---|
| 72 | CurrentSelectionIndicator = new SelectionIndicator(goodSelections, totalSelections, selectionIndicators.Count);
|
---|
| 73 | selectionIndicators.Add(CurrentSelectionIndicator);
|
---|
[12815] | 74 | }
|
---|
| 75 |
|
---|
| 76 | private List<SelectionIndicator> selectionIndicators;
|
---|
| 77 |
|
---|
| 78 | public List<SelectionIndicator> SelectionIndicators { get { return selectionIndicators; } }
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | private SelectionIndicator currentSelectionIndicator;
|
---|
| 82 |
|
---|
| 83 | public SelectionIndicator CurrentSelectionIndicator
|
---|
| 84 | {
|
---|
| 85 | get { return this.currentSelectionIndicator; }
|
---|
| 86 | set { this.currentSelectionIndicator = value; this.OnPropertyChanged("CurrentSelectionIndicator"); }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[12781] | 89 | private ISymbolicExpressionTreeProblem problem;
|
---|
[12762] | 90 |
|
---|
[12781] | 91 | public ISymbolicExpressionTreeProblem Problem
|
---|
| 92 | {
|
---|
| 93 | get { return this.problem; }
|
---|
| 94 | set
|
---|
| 95 | {
|
---|
| 96 | this.problem = value;
|
---|
| 97 | this.OnPropertyChanged("Problem");
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[12762] | 100 |
|
---|
[12781] | 101 | private IBanditPolicy banditPolicy;
|
---|
[12762] | 102 |
|
---|
[12781] | 103 | public IBanditPolicy BanditPolicy
|
---|
| 104 | {
|
---|
| 105 | get { return this.banditPolicy; }
|
---|
| 106 | set
|
---|
| 107 | {
|
---|
| 108 | this.banditPolicy = value;
|
---|
| 109 | this.OnPropertyChanged("BanditPolicy");
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[12762] | 112 |
|
---|
[12781] | 113 | private ISolver solver;
|
---|
| 114 |
|
---|
| 115 | public ISolver Solver
|
---|
| 116 | {
|
---|
| 117 | get { return this.solver; }
|
---|
| 118 | set
|
---|
| 119 | {
|
---|
| 120 | this.solver = value;
|
---|
| 121 | this.OnPropertyChanged("Solver");
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private int runNumber;
|
---|
| 126 |
|
---|
| 127 | public int RunNumber
|
---|
| 128 | {
|
---|
| 129 | get { return this.runNumber; }
|
---|
| 130 | set
|
---|
| 131 | {
|
---|
| 132 | this.runNumber = value;
|
---|
| 133 | this.OnPropertyChanged("RunNumber");
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[12815] | 137 | private int maxIterations;
|
---|
[12781] | 138 |
|
---|
[12815] | 139 | public int MaxIterations
|
---|
[12781] | 140 | {
|
---|
[12815] | 141 | get { return this.maxIterations; }
|
---|
[12781] | 142 | set
|
---|
| 143 | {
|
---|
[12815] | 144 | this.maxIterations = value;
|
---|
| 145 | this.OnPropertyChanged("MaxIterations");
|
---|
[12781] | 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | private int maxLen;
|
---|
| 150 |
|
---|
| 151 | public int MaxLen
|
---|
| 152 | {
|
---|
| 153 | get { return this.maxLen; }
|
---|
| 154 | set
|
---|
| 155 | {
|
---|
| 156 | this.maxLen = value;
|
---|
| 157 | this.OnPropertyChanged("MaxLen");
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | private int evaluations;
|
---|
| 162 |
|
---|
| 163 | public int Evaluations
|
---|
| 164 | {
|
---|
| 165 | get { return this.evaluations; }
|
---|
| 166 | set
|
---|
| 167 | {
|
---|
| 168 | this.evaluations = value;
|
---|
| 169 | this.OnPropertyChanged("Evaluations");
|
---|
| 170 | if (endTime > startTime)
|
---|
| 171 | {
|
---|
| 172 | // endTime is set...
|
---|
| 173 | EvaluationsPerSecond = Math.Round(evaluations / TotalTime.TotalSeconds, 2);
|
---|
| 174 | }
|
---|
| 175 | else if (startTime > endTime)
|
---|
| 176 | {
|
---|
| 177 | // only startTime is set...
|
---|
| 178 | TimeSpan currentTimeNeeded = DateTime.Now - startTime;
|
---|
| 179 | EvaluationsPerSecond = Math.Round(evaluations / currentTimeNeeded.TotalSeconds, 2);
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | private double bestQuality;
|
---|
| 185 |
|
---|
| 186 | public double BestQuality
|
---|
| 187 | {
|
---|
| 188 | get { return this.bestQuality; }
|
---|
| 189 | set
|
---|
| 190 | {
|
---|
| 191 | this.bestQuality = value;
|
---|
| 192 | this.OnPropertyChanged("BestQuality");
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | private string bestSolution;
|
---|
| 197 |
|
---|
| 198 | public string BestSolution
|
---|
| 199 | {
|
---|
| 200 | get { return this.bestSolution; }
|
---|
| 201 | set
|
---|
| 202 | {
|
---|
| 203 | this.bestSolution = value;
|
---|
| 204 | this.OnPropertyChanged("BestSolution");
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | private double bestKnownQuality;
|
---|
| 209 |
|
---|
| 210 | public double BestKnownQuality
|
---|
| 211 | {
|
---|
| 212 | get { return this.bestKnownQuality; }
|
---|
| 213 | set
|
---|
| 214 | {
|
---|
| 215 | this.bestKnownQuality = value;
|
---|
| 216 | this.OnPropertyChanged("BestKnownQuality");
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | private int bestSolutionFoundAt;
|
---|
| 221 |
|
---|
| 222 | public int BestSolutionFoundAt
|
---|
| 223 | {
|
---|
| 224 | get { return this.bestSolutionFoundAt; }
|
---|
| 225 | set
|
---|
| 226 | {
|
---|
| 227 | this.bestSolutionFoundAt = value;
|
---|
| 228 | this.OnPropertyChanged("BestSolutionFoundAt");
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | private double evaluationsPerSecond;
|
---|
| 233 |
|
---|
| 234 | public double EvaluationsPerSecond
|
---|
| 235 | {
|
---|
| 236 | get { return this.evaluationsPerSecond; }
|
---|
| 237 | set
|
---|
| 238 | {
|
---|
| 239 | this.evaluationsPerSecond = value;
|
---|
| 240 | this.OnPropertyChanged("EvaluationsPerSecond");
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | private DateTime startTime;
|
---|
| 245 |
|
---|
| 246 | public DateTime StartTime
|
---|
| 247 | {
|
---|
| 248 | get { return this.startTime; }
|
---|
| 249 | set
|
---|
| 250 | {
|
---|
| 251 | this.startTime = value;
|
---|
| 252 | this.OnPropertyChanged("StartTime");
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | private DateTime endTime;
|
---|
| 257 |
|
---|
| 258 | public DateTime EndTime
|
---|
| 259 | {
|
---|
| 260 | get { return this.endTime; }
|
---|
| 261 | set
|
---|
| 262 | {
|
---|
| 263 | this.endTime = value;
|
---|
| 264 | this.OnPropertyChanged("EndTime");
|
---|
| 265 | TotalTime = EndTime - StartTime;
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | private TimeSpan totalTime;
|
---|
| 270 |
|
---|
| 271 | public TimeSpan TotalTime
|
---|
| 272 | {
|
---|
| 273 | get { return this.totalTime; }
|
---|
| 274 | set
|
---|
| 275 | {
|
---|
| 276 | this.totalTime = value;
|
---|
| 277 | this.OnPropertyChanged("TotalTime");
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | private TimeSpan bestSolutionTime;
|
---|
| 282 |
|
---|
| 283 | public TimeSpan BestSolutionTime
|
---|
| 284 | {
|
---|
| 285 | get { return this.bestSolutionTime; }
|
---|
| 286 | set
|
---|
| 287 | {
|
---|
| 288 | this.bestSolutionTime = value;
|
---|
| 289 | this.OnPropertyChanged("BestSolutionTime");
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | public List<FoundSolution> FoundSolutions { get; set; }
|
---|
| 294 |
|
---|
[12762] | 295 | public string SvgFile { get; set; }
|
---|
| 296 |
|
---|
[12781] | 297 | private TreeInfos treeInfos;
|
---|
[12762] | 298 |
|
---|
[12781] | 299 | public TreeInfos TreeInfos
|
---|
| 300 | {
|
---|
| 301 | get { return this.treeInfos; }
|
---|
| 302 | set
|
---|
| 303 | {
|
---|
| 304 | this.treeInfos = value;
|
---|
| 305 | this.OnPropertyChanged("TreeInfos");
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | private RunState runState;
|
---|
| 310 |
|
---|
| 311 | public RunState RunState
|
---|
| 312 | {
|
---|
| 313 | get { return this.runState; }
|
---|
| 314 | set
|
---|
| 315 | {
|
---|
| 316 | this.runState = value;
|
---|
| 317 | this.OnPropertyChanged("RunState");
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[12762] | 321 | public override string ToString()
|
---|
| 322 | {
|
---|
| 323 | return string.Format("Run #{0}", this.RunNumber);
|
---|
| 324 | }
|
---|
[12781] | 325 |
|
---|
| 326 | #region INotifyPropertyChanged members
|
---|
| 327 |
|
---|
| 328 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 329 |
|
---|
| 330 | protected void OnPropertyChanged(string propertyName)
|
---|
| 331 | {
|
---|
[12815] | 332 | try
|
---|
| 333 | {
|
---|
| 334 | if (PropertyChanged != null)
|
---|
| 335 | this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
---|
| 336 | }
|
---|
| 337 | catch (Exception) { }
|
---|
[12781] | 338 | }
|
---|
| 339 |
|
---|
| 340 | #endregion INotifyPropertyChanged members
|
---|
[12762] | 341 | }
|
---|
| 342 | }
|
---|