Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/BenchmarkSuite/Problems/WordStats.cs @ 18183

Last change on this file since 18183 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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