[10596] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[16844] | 4 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10596] | 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 |
|
---|
[16851] | 24 | using System;
|
---|
[16592] | 25 | using System.Collections.Generic;
|
---|
[16747] | 26 | using System.Linq;
|
---|
[16713] | 27 | using HEAL.Attic;
|
---|
[10596] | 28 | using HeuristicLab.Analysis;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
[16590] | 31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[10596] | 32 | using HeuristicLab.Optimization;
|
---|
| 33 | using HeuristicLab.Parameters;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[16628] | 36 | [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
|
---|
[16851] | 37 | public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
|
---|
[16592] | 38 | private const string ConstraintViolationsResultName = "Constraint Violations";
|
---|
| 39 | private const string ProblemDataParameterName = "ProblemData";
|
---|
[16590] | 40 |
|
---|
[16592] | 41 | #region parameter properties
|
---|
[16851] | 42 | public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter {
|
---|
| 43 | get { return (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName]; }
|
---|
[10596] | 44 | }
|
---|
[16592] | 45 | #endregion
|
---|
[16800] | 46 |
|
---|
| 47 | public override bool EnabledByDefault => false;
|
---|
[10596] | 48 |
|
---|
| 49 | [StorableConstructor]
|
---|
[16628] | 50 | protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) {
|
---|
[16590] | 51 | }
|
---|
| 52 |
|
---|
[16800] | 53 | protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) : base(original, cloner) {
|
---|
[16590] | 54 | }
|
---|
| 55 |
|
---|
[10596] | 56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[16590] | 57 | return new SymbolicRegressionConstraintAnalyzer(this, cloner);
|
---|
[10596] | 58 | }
|
---|
| 59 |
|
---|
[16851] | 60 | public SymbolicRegressionConstraintAnalyzer() : base(){
|
---|
| 61 | Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem."));
|
---|
[10596] | 62 | }
|
---|
| 63 |
|
---|
[13582] | 64 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 65 | private void AfterDeserialization() {
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[16851] | 68 | public override IOperation Apply() {
|
---|
| 69 | var problemData = RegressionProblemDataParameter.ActualValue;
|
---|
| 70 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 71 |
|
---|
| 72 | var results = ResultCollectionParameter.ActualValue;
|
---|
[16800] | 73 | var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled);
|
---|
| 74 | var variableRanges = problemData.VariableRanges.VariableIntervals;
|
---|
[16851] | 75 |
|
---|
| 76 | if (!results.ContainsKey(ConstraintViolationsResultName)) {
|
---|
| 77 | var newDataTable = new DataTable(ConstraintViolationsResultName);
|
---|
| 78 | foreach (var constraint in constraints) {
|
---|
| 79 | newDataTable.Rows.Add(new DataRow(constraint.Expression));
|
---|
| 80 | }
|
---|
| 81 | results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.", newDataTable));
|
---|
| 82 | }
|
---|
| 83 | var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
|
---|
| 84 |
|
---|
| 85 | var interpreter = new IntervalInterpreter();
|
---|
[16747] | 86 | foreach (var constraint in constraints) {
|
---|
[16851] | 87 | int violations = 0;
|
---|
| 88 | foreach (var tree in trees) {
|
---|
| 89 | var satisfied = SymbolicRegressionConstraintAnalyzer.ConstraintSatisfied(constraint, interpreter, variableRanges, tree);
|
---|
| 90 | if (!satisfied) violations++;
|
---|
[16747] | 91 | }
|
---|
[16851] | 92 | dataTable.Rows[constraint.Expression].Values.Add(violations);
|
---|
[16747] | 93 | }
|
---|
[16851] | 94 |
|
---|
[16747] | 95 |
|
---|
[16851] | 96 | return base.Apply();
|
---|
[16747] | 97 | }
|
---|
| 98 |
|
---|
[16851] | 99 | public static bool ConstraintSatisfied(IntervalConstraint constraint, IntervalInterpreter intervalInterpreter,
|
---|
| 100 | IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
|
---|
[16747] | 101 |
|
---|
[16851] | 102 | if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
|
---|
| 103 | throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
|
---|
| 104 |
|
---|
| 105 | Interval resultInterval;
|
---|
| 106 |
|
---|
[16800] | 107 | if (!constraint.IsDerivation) {
|
---|
[16851] | 108 | resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
|
---|
[16800] | 109 | } else {
|
---|
[16851] | 110 | var tree = solution;
|
---|
[16800] | 111 | for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
|
---|
[16851] | 112 | tree = DerivativeCalculator.Derive(tree, constraint.Variable);
|
---|
[16800] | 113 | }
|
---|
[16851] | 114 | resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
|
---|
[16747] | 115 | }
|
---|
[16800] | 116 |
|
---|
[16851] | 117 | var satisfied = constraint.Interval.Contains(resultInterval, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound);
|
---|
| 118 | return satisfied;
|
---|
[16747] | 119 | }
|
---|
| 120 |
|
---|
[16851] | 121 | public static bool ConstraintsSatisfied(IEnumerable<IntervalConstraint> constraints, IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
|
---|
| 122 | var intervalInterpreter = new IntervalInterpreter();
|
---|
[16592] | 123 | foreach (var constraint in constraints) {
|
---|
[16851] | 124 | if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
|
---|
| 125 | throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
|
---|
[16592] | 126 |
|
---|
[16851] | 127 | var satisfied = ConstraintSatisfied(constraint, intervalInterpreter, variableRanges, solution);
|
---|
| 128 | if (!satisfied) return false;
|
---|
[16592] | 129 | }
|
---|
[16851] | 130 | return true;
|
---|
[16590] | 131 | }
|
---|
[10596] | 132 | }
|
---|
| 133 | }
|
---|