1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | using System;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.Linq;
|
---|
27 | using HEAL.Attic;
|
---|
28 | using HeuristicLab.Analysis;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
36 | [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
|
---|
37 | public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
|
---|
38 | private const string ConstraintViolationsResultName = "Constraint Violations";
|
---|
39 | private const string ProblemDataParameterName = "ProblemData";
|
---|
40 | private const string ConstraintViolationParameterName = "ConstraintViolations";
|
---|
41 |
|
---|
42 | #region parameter properties
|
---|
43 | public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter {
|
---|
44 | get { return (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IResultParameter<DataTable> ConstraintViolationParameter =>
|
---|
48 | (IResultParameter<DataTable>) Parameters[ConstraintViolationParameterName];
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | public override bool EnabledByDefault => false;
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) {
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) : base(original, cloner) {
|
---|
58 | }
|
---|
59 |
|
---|
60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
61 | return new SymbolicRegressionConstraintAnalyzer(this, cloner);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public SymbolicRegressionConstraintAnalyzer() : base(){
|
---|
65 | Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem."));
|
---|
66 | Parameters.Add(new ResultParameter<DataTable>(ConstraintViolationParameterName, "Shows the number of constraint violations!"));
|
---|
67 | ConstraintViolationParameter.DefaultValue = new DataTable(ConstraintViolationParameterName) {
|
---|
68 | VisualProperties = {
|
---|
69 | XAxisTitle = "Generations",
|
---|
70 | YAxisTitle = "Constraint Violations",
|
---|
71 | }
|
---|
72 | };
|
---|
73 | }
|
---|
74 |
|
---|
75 | [StorableHook(HookType.AfterDeserialization)]
|
---|
76 | private void AfterDeserialization() {
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override IOperation Apply() {
|
---|
80 | var problemData = RegressionProblemDataParameter.ActualValue;
|
---|
81 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
82 |
|
---|
83 | var results = ResultCollectionParameter.ActualValue;
|
---|
84 | var constraints = problemData.IntervalConstraints.EnabledConstraints;
|
---|
85 | var variableRanges = problemData.VariableRanges.GetIntervals();
|
---|
86 | var newDataTable = ConstraintViolationParameter.ActualValue;
|
---|
87 |
|
---|
88 | if(newDataTable.Rows.Count == 0) {
|
---|
89 | foreach (var constraint in constraints) {
|
---|
90 | newDataTable.Rows.Add(new DataRow(constraint.Expression));
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | var interpreter = new IntervalInterpreter();
|
---|
95 | foreach (var constraint in constraints) {
|
---|
96 | var violations = trees.Select(tree => SymbolicRegressionConstraintAnalyzer.ConstraintSatisfied(constraint, interpreter, variableRanges, tree)).Count(satisfied => !satisfied);
|
---|
97 | newDataTable.Rows[constraint.Expression].Values.Add(violations);
|
---|
98 | }
|
---|
99 |
|
---|
100 | return base.Apply();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public static bool ConstraintSatisfied(IntervalConstraint constraint, IntervalInterpreter intervalInterpreter,
|
---|
104 | IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
|
---|
105 |
|
---|
106 | if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
|
---|
107 | throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
|
---|
108 |
|
---|
109 | Interval resultInterval;
|
---|
110 |
|
---|
111 | if (!constraint.IsDerivative) {
|
---|
112 | resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
|
---|
113 | } else {
|
---|
114 | var tree = solution;
|
---|
115 | for (var i = 0; i < constraint.NumberOfDerivations; ++i) {
|
---|
116 | if (!IntervalInterpreter.IsCompatible(tree) || !DerivativeCalculator.IsCompatible(tree))
|
---|
117 | throw new ArgumentException("Cube, Root, Power Symbols are not supported.");
|
---|
118 | tree = DerivativeCalculator.Derive(tree, constraint.Variable);
|
---|
119 | }
|
---|
120 | resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
|
---|
121 | }
|
---|
122 |
|
---|
123 | var satisfied = constraint.Interval.Contains(resultInterval);
|
---|
124 | return satisfied;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static bool ConstraintsSatisfied(IEnumerable<IntervalConstraint> constraints, IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
|
---|
128 | var intervalInterpreter = new IntervalInterpreter();
|
---|
129 | foreach (var constraint in constraints) {
|
---|
130 | if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
|
---|
131 | throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
|
---|
132 |
|
---|
133 | var satisfied = ConstraintSatisfied(constraint, intervalInterpreter, variableRanges, solution);
|
---|
134 | if (!satisfied) return false;
|
---|
135 | }
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|