Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2283 added SelectionIndicator for MCTS and problems SantaFeAnt, SymbolicRegression10, RoyalSymbol; updated SantaFeAnt grammar

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