Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problem.ProgramSynthesis.BenchmarkSuite/Problems/WordStats.cs @ 18057

Last change on this file since 18057 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 4.6 KB
Line 
1namespace HeuristicLab.BenchmarkSuite.Problems {
2  using System;
3  using System.Linq;
4
5  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Char;
6  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Integer;
7  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.IntegerVector;
8  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.String;
9
10  public class WordStats : BenchmarkSuiteDataDescriptor {
11    private const string name = "Word Stats - Hard";
12    private const string fileName = "WordStats.csv";
13    private const string description = "Given a string, print the number of words containing n characters for n from 1 to the length of the longest word. At the end of the output, print a line that gives the number of sentences and line that gives the average sentence length. A word is any string of consecutive non-whitespace characters(including sentence terminators). Every string will contain at least one sentence terminator(period, exclamation point, or question mark). The average sentence length is the number of words in the file divided by the number of sentence terminator characters.";
14
15    protected override string FileName { get { return fileName; } }
16    public override string Name { get { return name; } }
17    public override string Description { get { return description; } }
18    protected override int InputArgumentCount { get { return 1; } }
19    protected override int OutputArgumentCount { get { return 1; } }
20
21    public override ProblemData CreateProblemData() {
22      return new ProblemData(ProblemType.WordStats) {
23        Name = Name,
24        Description = Description,
25        ProgramExecutionBudget = 30000000,
26        Examples = CloneExamples(),
27        BestResult = 0,
28        WorstResult = 10000,
29        InputArgumentTypes = new[] { ExampleArgumentType.String },
30        OutputArgumentTypes = new[] { ExampleArgumentType.Print },
31        TrainingCount = 100,
32        TestCount = 1000,
33        EnabledDataTypes = DataTypes.Exec | DataTypes.Integer | DataTypes.Float | DataTypes.Boolean | DataTypes.Char | DataTypes.String | DataTypes.IntegerVector | DataTypes.FloatVector | DataTypes.StringVector | DataTypes.Print,
34        MaxSize = 1000,
35        EvalLimit = 6000,
36        FloatStringFormat = "0.0#########",
37        ErcOptions = {
38        ErcProbability = 0.05,
39          IntegerErcOptions = new IntegerErcOptions(
40            new IntegerRangeErc(-100, 100)),
41          IntegerVectorErcOptions = new IntegerVectorErcOptions(
42            new IntegerVectorConstantsErc(new int[0])),
43          CharErcOptions = new CharErcOptions(
44            new IntegerConstantErc('.', '?', '!', ' ', '\t', '\n', ':')),
45          StringErcOptions = new StringErcOptions(
46            new StringConstantErc("words of length ", ": ", "number of sentences: ", "average sentence length: "))
47        }
48      };
49    }
50
51    protected override Example ParseExample(string[] input, string[] output) {
52      long numberOfSentences;
53      if (!GetNumberOfSentences(output[0], out numberOfSentences))
54        throw new InvalidOperationException("Unable to find or parse line with number of sentences.");
55
56      double averageSentenceLength;
57      if (!GetAverageSentenceLength(output[0], out averageSentenceLength))
58        throw new InvalidOperationException("Unable to find line or parse with average sentence length.");
59
60      return new Example {
61        InputArgs = input,
62        OutputArgs = output,
63        InputString = input,
64        OutputPrint = output[0],
65
66        // help
67        OutputInteger = new[] { numberOfSentences },
68        OutputFloat = new[] { averageSentenceLength }
69      };
70    }
71
72    public static bool GetNumberOfSentences(string str, out long numberOfSentences) {
73      var lines = str.Split('\n');
74      var requiredLineIndex = lines.Length - 2;
75      numberOfSentences = 0;
76
77      if (requiredLineIndex < 0)
78        return false;
79
80      var line = lines[requiredLineIndex];
81      var numberOfSentencesStr = line.Split(' ').Last();
82
83
84      if (!long.TryParse(numberOfSentencesStr, out numberOfSentences))
85        return false;
86
87      return true;
88    }
89
90    public static bool GetAverageSentenceLength(string str, out double averageSentenceLength) {
91      var lines = str.Split('\n');
92      var requiredLineIndex = lines.Length - 1;
93      averageSentenceLength = 0;
94
95      if (requiredLineIndex < 0)
96        return false;
97
98      var line = lines[requiredLineIndex];
99      var averageSentenceLengthStr = line.Split(' ').Last();
100
101      if (!double.TryParse(averageSentenceLengthStr, out averageSentenceLength))
102        return false;
103
104      return true;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.