Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/Evaluation/Run.cs @ 12840

Last change on this file since 12840 was 12840, checked in by aballeit, 9 years ago

#2283 Final

File size: 9.6 KB
Line 
1using System.Diagnostics;
2using HeuristicLab.Algorithms.Bandits;
3using HeuristicLab.Algorithms.GrammaticalOptimization;
4using HeuristicLab.Algorithms.MonteCarloTreeSearch;
5using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base;
6using HeuristicLab.Problems.GrammaticalOptimization;
7using System;
8using System.Collections.Generic;
9using System.ComponentModel;
10
11namespace Evaluation
12{
13
14    public enum RunState
15    {
16        NotStarted,
17        Running,
18        Analyzing,
19        Finished
20    }
21
22    public class Run : INotifyPropertyChanged
23    {
24        public Run()
25        {
26
27        }
28
29        public Run(ISymbolicExpressionTreeProblem problem, IBanditPolicy banditPolicy, ISolver solver, int runNumber,
30            int maxIterations, int maxLen)
31        {
32            Problem = problem;
33            BanditPolicy = banditPolicy;
34            Solver = solver;
35            RunNumber = runNumber;
36            MaxIterations = maxIterations;
37            MaxLen = maxLen;
38
39            selectionIndicators = new List<SelectionIndicator>();
40            Evaluations = 0;
41            BestQuality = double.MinValue;
42            RunState = RunState.NotStarted;
43            FoundSolutions = new List<FoundSolution>();
44            CurrentSelectionIndicator = new SelectionIndicator(0, 0, 0);
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;
55                    FoundSolution solution = new FoundSolution(DateTime.Now, Evaluations, quality, sentence);
56                    BestSolutionTime = solution.Time - StartTime;
57                    FoundSolutions.Add(solution);
58                }
59            };
60            if (solver is MonteCarloTreeSearch)
61            {
62                MonteCarloTreeSearch mcts = (MonteCarloTreeSearch)solver;
63                mcts.IterationFinished += mcts_IterationFinished;
64            }
65        }
66
67        private void mcts_IterationFinished(int goodSelections, int totalSelections)
68        {
69            //CurrentSelectionIndicator.GoodSelections = goodSelections;
70            //CurrentSelectionIndicator.TotalSelections = totalSelections;
71            //this.OnPropertyChanged("CurrentSelectionIndicator");
72            CurrentSelectionIndicator = new SelectionIndicator(goodSelections, totalSelections, selectionIndicators.Count);
73            selectionIndicators.Add(CurrentSelectionIndicator);
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
89        private ISymbolicExpressionTreeProblem problem;
90
91        public ISymbolicExpressionTreeProblem Problem
92        {
93            get { return this.problem; }
94            set
95            {
96                this.problem = value;
97                this.OnPropertyChanged("Problem");
98            }
99        }
100
101        private IBanditPolicy banditPolicy;
102
103        public IBanditPolicy BanditPolicy
104        {
105            get { return this.banditPolicy; }
106            set
107            {
108                this.banditPolicy = value;
109                this.OnPropertyChanged("BanditPolicy");
110            }
111        }
112
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
137        private int maxIterations;
138
139        public int MaxIterations
140        {
141            get { return this.maxIterations; }
142            set
143            {
144                this.maxIterations = value;
145                this.OnPropertyChanged("MaxIterations");
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
295        public string SvgFile { get; set; }
296
297        private TreeInfos treeInfos;
298
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
321        public override string ToString()
322        {
323            return string.Format("Run #{0}", this.RunNumber);
324        }
325
326        #region INotifyPropertyChanged members
327
328        public event PropertyChangedEventHandler PropertyChanged;
329
330        protected void OnPropertyChanged(string propertyName)
331        {
332            try
333            {
334                if (PropertyChanged != null)
335                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
336            }
337            catch (Exception) { }
338        }
339
340        #endregion INotifyPropertyChanged members
341    }
342}
Note: See TracBrowser for help on using the repository browser.