Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/SentenceSetStatistics.cs @ 13599

Last change on this file since 13599 was 12893, checked in by gkronber, 9 years ago

#2283: experiments on grammatical optimization algorithms (maxreward instead of avg reward, ...)

File size: 2.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Common;
7
8namespace HeuristicLab.Problems.GrammaticalOptimization {
9  public class SentenceSetStatistics {
10    public int NumberOfSentences { get; private set; }
11    public string BestSentence { get; private set; }
12    public string FirstSentence { get; private set; }
13    public string LastSentence { get; private set; }
14    public double BestSentenceQuality { get; private set; }
15    public double BestSentenceIndex { get; private set; }
16    public double FirstSentenceQuality { get; private set; }
17    public double LastSentenceQuality { get; private set; }
18    public double AverageQuality { get { return sumQualities / NumberOfSentences; } }
19    // public double MedianQuality { get; private set; }
20    private double sumQualities;
21
22    private readonly double bestKnownQuality;
23    public SentenceSetStatistics(double bestKnownQuality = 1.0) {
24      this.bestKnownQuality = bestKnownQuality;
25      Clear();
26    }
27
28    public void Clear() {
29      BestSentenceQuality = double.NegativeInfinity;
30      BestSentence = string.Empty;
31      FirstSentence = string.Empty;
32      LastSentence = string.Empty;
33    }
34
35    public void AddSentence(string sentence, double quality) {
36      sumQualities += quality;
37      NumberOfSentences++;
38
39      if (NumberOfSentences == 1) {
40        FirstSentence = sentence;
41        FirstSentenceQuality = quality;
42      }
43
44      if (quality > BestSentenceQuality) {
45        BestSentence = sentence;
46        BestSentenceQuality = quality;
47        BestSentenceIndex = NumberOfSentences;
48      }
49
50      LastSentence = sentence;
51      LastSentenceQuality = quality;
52    }
53
54    public override string ToString() {
55      return
56        string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5,30} last {6,7:F5} {7,20}",
57      NumberOfSentences, AverageQuality,
58      BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, bestKnownQuality) ? 1.0 : 0.0,
59      BestSentenceIndex, TrimToSize(BestSentence, 30),
60      LastSentenceQuality, TrimToSize(LastSentence, 20)
61        //LastSentenceQuality, TrimToSize(LastSentence, 20)
62     );
63    }
64
65    private string TrimToSize(string s, int len) {
66      if (s.Length < len) return s;
67      else {
68        var sb = new StringBuilder(len);
69        sb.Append(s.Substring(0, len - 3));
70        sb.Append("...");
71        return sb.ToString();
72      }
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.