1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HEAL.Attic;
|
---|
5 | using HeuristicLab.Analysis;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 | using HeuristicLab.Parameters;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
13 | [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
|
---|
14 | public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
|
---|
15 | private const string ConstraintViolationsResultName = "Constraint Violations";
|
---|
16 | private const string ProblemDataParameterName = "ProblemData";
|
---|
17 | private const string ConstraintViolationParameterName = "ConstraintViolations";
|
---|
18 |
|
---|
19 | #region parameter properties
|
---|
20 |
|
---|
21 | public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter =>
|
---|
22 | (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName];
|
---|
23 |
|
---|
24 | public IResultParameter<DataTable> ConstraintViolationParameter =>
|
---|
25 | (IResultParameter<DataTable>) Parameters[ConstraintViolationParameterName];
|
---|
26 |
|
---|
27 | #endregion
|
---|
28 |
|
---|
29 | public override bool EnabledByDefault => false;
|
---|
30 |
|
---|
31 | [StorableConstructor]
|
---|
32 | protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
33 |
|
---|
34 | protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) :
|
---|
35 | base(original, cloner) { }
|
---|
36 |
|
---|
37 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
38 | return new SymbolicRegressionConstraintAnalyzer(this, cloner);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public SymbolicRegressionConstraintAnalyzer() {
|
---|
42 | Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName,
|
---|
43 | "The problem data of the symbolic data analysis problem."));
|
---|
44 | Parameters.Add(new ResultParameter<DataTable>(ConstraintViolationParameterName,
|
---|
45 | "Shows the number of constraint violations!"));
|
---|
46 | ConstraintViolationParameter.DefaultValue = new DataTable(ConstraintViolationParameterName) {
|
---|
47 | VisualProperties = {
|
---|
48 | XAxisTitle
|
---|
49 | = "Generations",
|
---|
50 | YAxisTitle
|
---|
51 | = "Constraint Violations"
|
---|
52 | }
|
---|
53 | };
|
---|
54 | }
|
---|
55 |
|
---|
56 | [StorableHook(HookType.AfterDeserialization)]
|
---|
57 | private void AfterDeserialization() { }
|
---|
58 |
|
---|
59 | public override IOperation Apply() {
|
---|
60 | var problemData = RegressionProblemDataParameter.ActualValue;
|
---|
61 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
62 |
|
---|
63 | var results = ResultCollectionParameter.ActualValue;
|
---|
64 | var constraints = problemData.IntervalConstraints.EnabledConstraints;
|
---|
65 | var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
|
---|
66 | var newDataTable = ConstraintViolationParameter.ActualValue;
|
---|
67 |
|
---|
68 | if (newDataTable.Rows.Count == 0)
|
---|
69 | foreach (var constraint in constraints)
|
---|
70 | newDataTable.Rows.Add(new DataRow(constraint.Expression));
|
---|
71 |
|
---|
72 | var interpreter = new IntervalInterpreter();
|
---|
73 | foreach (var constraint in constraints) {
|
---|
74 | var violations = trees.Select(tree => ConstraintSatisfied(constraint, interpreter, variableRanges, tree))
|
---|
75 | .Count(satisfied => !satisfied);
|
---|
76 | newDataTable.Rows[constraint.Expression].Values.Add(violations);
|
---|
77 | }
|
---|
78 |
|
---|
79 | return base.Apply();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static bool ConstraintSatisfied(IntervalConstraint constraint,
|
---|
83 | IntervalInterpreter intervalInterpreter,
|
---|
84 | IReadOnlyDictionary<string, Interval> variableRanges,
|
---|
85 | ISymbolicExpressionTree solution) {
|
---|
86 | if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
|
---|
87 | throw new
|
---|
88 | ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.",
|
---|
89 | nameof(IntervalConstraintsParser));
|
---|
90 |
|
---|
91 | Interval resultInterval;
|
---|
92 |
|
---|
93 | if (!constraint.IsDerivative) {
|
---|
94 | resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | var tree = solution;
|
---|
98 | for (var i = 0; i < constraint.NumberOfDerivations; ++i) {
|
---|
99 | if (!IntervalInterpreter.IsCompatible(tree) || !DerivativeCalculator.IsCompatible(tree))
|
---|
100 | throw new ArgumentException("Cube, Root, Power Symbols are not supported.");
|
---|
101 | tree = DerivativeCalculator.Derive(tree, constraint.Variable);
|
---|
102 | }
|
---|
103 |
|
---|
104 | resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
|
---|
105 | }
|
---|
106 |
|
---|
107 | var satisfied = constraint.Interval.Contains(resultInterval);
|
---|
108 | return satisfied;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public static bool ConstraintsSatisfied(IEnumerable<IntervalConstraint> constraints,
|
---|
112 | IReadOnlyDictionary<string, Interval> variableRanges,
|
---|
113 | ISymbolicExpressionTree solution) {
|
---|
114 | var intervalInterpreter = new IntervalInterpreter();
|
---|
115 | foreach (var constraint in constraints) {
|
---|
116 | if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
|
---|
117 | throw new
|
---|
118 | ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.",
|
---|
119 | nameof(IntervalConstraintsParser));
|
---|
120 |
|
---|
121 | var satisfied = ConstraintSatisfied(constraint, intervalInterpreter, variableRanges, solution);
|
---|
122 | if (!satisfied) return false;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | } |
---|