1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
6 | using HeuristicLab.Problems.DataAnalysis;
|
---|
7 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
8 | using HeuristicLab.Random;
|
---|
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Tests {
|
---|
12 | [TestClass]
|
---|
13 | public class IntervalCalculationComparison {
|
---|
14 | [TestMethod]
|
---|
15 | [TestCategory("Problems.DataAnalysis")]
|
---|
16 | [TestProperty("Time", "short")]
|
---|
17 | public void TestIntervalCalculationForRandomExpressions() {
|
---|
18 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
19 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
20 | // activate supported symbols
|
---|
21 | grammar.Symbols.First(s => s is Square).Enabled = true;
|
---|
22 | grammar.Symbols.First(s => s is SquareRoot).Enabled = true;
|
---|
23 | grammar.Symbols.First(s => s is Cube).Enabled = true;
|
---|
24 | grammar.Symbols.First(s => s is CubeRoot).Enabled = true;
|
---|
25 |
|
---|
26 | var varSy = (Variable)grammar.Symbols.First(s => s is Variable);
|
---|
27 | varSy.AllVariableNames = new string[] { "x", "y" };
|
---|
28 | varSy.VariableNames = varSy.AllVariableNames;
|
---|
29 | varSy.WeightMu = 1.0;
|
---|
30 | varSy.WeightSigma = 1.0;
|
---|
31 | var rand = new FastRandom(1234);
|
---|
32 | var eval1 = new IntervalEvaluator();
|
---|
33 | var eval2 = new IntervalInterpreter();
|
---|
34 |
|
---|
35 | IDictionary<string, Interval> posIntervals = new Dictionary<string, Interval>() {
|
---|
36 | { "x", new Interval(1, 2) },
|
---|
37 | { "y", new Interval(0, 1) }
|
---|
38 | };
|
---|
39 | IDictionary<string, Interval> negIntervals = new Dictionary<string, Interval>() {
|
---|
40 | { "x", new Interval(-2, -1) },
|
---|
41 | { "y", new Interval(-1, 0) }
|
---|
42 | };
|
---|
43 | IDictionary<string, Interval> fullIntervals = new Dictionary<string, Interval>() {
|
---|
44 | { "x", new Interval(-2, 2) },
|
---|
45 | { "y", new Interval(-1, 1) }
|
---|
46 | };
|
---|
47 | IDictionary<string, Interval> specialIntervals = new Dictionary<string, Interval>() {
|
---|
48 | { "x", new Interval(1, double.PositiveInfinity) },
|
---|
49 | { "y", new Interval(double.NegativeInfinity, double.PositiveInfinity) }
|
---|
50 | };
|
---|
51 |
|
---|
52 | var formatter = new InfixExpressionFormatter();
|
---|
53 | foreach (var interval in new[] { posIntervals, negIntervals, fullIntervals, specialIntervals }) {
|
---|
54 | int N = 10000;
|
---|
55 | var sb = new StringBuilder();
|
---|
56 | int i = 0;
|
---|
57 | while (i < N) {
|
---|
58 | var t = ProbabilisticTreeCreator.Create(rand, grammar, maxTreeLength: 5, maxTreeDepth: 5);
|
---|
59 | var r1 = eval1.Evaluate(t, interval);
|
---|
60 | var r2 = eval2.GetSymbolicExpressionTreeInterval(t, interval);
|
---|
61 | // Console.WriteLine(formatter.Format(t));
|
---|
62 |
|
---|
63 | if (double.IsNaN(r1.LowerBound) && double.IsNaN(r2.LowerBound) && double.IsNaN(r1.UpperBound) && double.IsNaN(r2.UpperBound)) continue;
|
---|
64 |
|
---|
65 | if (!double.IsNaN(r1.LowerBound) && !double.IsNaN(r2.LowerBound))
|
---|
66 | if ((double.IsPositiveInfinity(r1.LowerBound) && !double.IsPositiveInfinity(r2.LowerBound)) ||
|
---|
67 | (double.IsNegativeInfinity(r1.LowerBound) && !double.IsNegativeInfinity(r2.LowerBound)) ||
|
---|
68 | (Math.Abs(r1.LowerBound - r2.LowerBound) > Math.Abs(r1.LowerBound * 1e-4))
|
---|
69 | ) {
|
---|
70 | sb.AppendLine($"{r1} <> {r2} for {formatter.Format(t)} x={interval["x"]} y={interval["y"]}");
|
---|
71 | }
|
---|
72 | if (!double.IsNaN(r1.UpperBound) && !double.IsNaN(r2.UpperBound))
|
---|
73 | if ((double.IsPositiveInfinity(r1.UpperBound) && !double.IsPositiveInfinity(r2.UpperBound)) ||
|
---|
74 | (double.IsNegativeInfinity(r1.UpperBound) && !double.IsNegativeInfinity(r2.UpperBound)) ||
|
---|
75 | (Math.Abs(r1.UpperBound - r2.UpperBound) > Math.Abs(r1.UpperBound * 1e-4))
|
---|
76 | ) {
|
---|
77 | sb.AppendLine($"{r1} <> {r2} for {formatter.Format(t)} x={interval["x"]} y={interval["y"]}");
|
---|
78 | }
|
---|
79 | i++;
|
---|
80 | }
|
---|
81 | if (sb.Length > 0) {
|
---|
82 | Console.WriteLine(sb.ToString());
|
---|
83 | Assert.Fail("There were different interval calculation results");
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|