Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12833 was 12824, checked in by aballeit, 10 years ago

#2283 added copy to clipboard function (export for excel)

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