1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Collections.ObjectModel;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Threading;
|
---|
6 | using HEAL.Attic;
|
---|
7 | using HeuristicLab.Analysis;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Core;
|
---|
10 | using HeuristicLab.Data;
|
---|
11 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
12 | using HeuristicLab.Optimization;
|
---|
13 | using HeuristicLab.Optimization.Operators;
|
---|
14 | using HeuristicLab.Parameters;
|
---|
15 |
|
---|
16 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
17 | [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
|
---|
18 | public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
|
---|
19 | private const string ProblemDataParameterName = "ProblemData";
|
---|
20 | private const string ConstraintViolationParameterName = "ConstraintViolations";
|
---|
21 | private const string ConstraintUnsatisfiedSolutionsParameterName = "Constraint Unsatisfied Solutions";
|
---|
22 |
|
---|
23 | #region parameter properties
|
---|
24 |
|
---|
25 | public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter =>
|
---|
26 | (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName];
|
---|
27 |
|
---|
28 | public IResultParameter<DataTable> ConstraintViolationParameter =>
|
---|
29 | (IResultParameter<DataTable>) Parameters[ConstraintViolationParameterName];
|
---|
30 |
|
---|
31 | public IResultParameter<DataTable> ConstraintUnsatisfiedSolutionsParameter =>
|
---|
32 | (IResultParameter<DataTable>)Parameters[ConstraintUnsatisfiedSolutionsParameterName];
|
---|
33 |
|
---|
34 | #endregion
|
---|
35 |
|
---|
36 | public override bool EnabledByDefault => false;
|
---|
37 | public static int Iterations { get; set; } = 0;
|
---|
38 |
|
---|
39 | public IBoundsEstimator BoundsEstimator { get; set; } = new IABoundsEstimator();
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
43 |
|
---|
44 | protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) :
|
---|
45 | base(original, cloner) { }
|
---|
46 |
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
48 | return new SymbolicRegressionConstraintAnalyzer(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public SymbolicRegressionConstraintAnalyzer() {
|
---|
52 | Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName,
|
---|
53 | "The problem data of the symbolic data analysis problem."));
|
---|
54 | Parameters.Add(new ResultParameter<DataTable>(ConstraintViolationParameterName,
|
---|
55 | "Shows the number of constraint violations!"));
|
---|
56 | Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName,
|
---|
57 | "Shows the number of solutions with unsatisfied constraints."));
|
---|
58 |
|
---|
59 |
|
---|
60 | ConstraintViolationParameter.DefaultValue = new DataTable(ConstraintViolationParameterName) {
|
---|
61 | VisualProperties = {
|
---|
62 | XAxisTitle = "Generations",
|
---|
63 | YAxisTitle = "Constraint Violations"
|
---|
64 | }
|
---|
65 | };
|
---|
66 |
|
---|
67 | ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) {
|
---|
68 | VisualProperties = {
|
---|
69 | XAxisTitle = "Generations",
|
---|
70 | YAxisTitle = "Constraint Unsatisfied Solutions"
|
---|
71 | }
|
---|
72 | };
|
---|
73 | }
|
---|
74 |
|
---|
75 | public override void InitializeState() {
|
---|
76 | Iterations = 0;
|
---|
77 | base.InitializeState();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override void ClearState() {
|
---|
81 | Iterations = 0;
|
---|
82 | base.ClearState();
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | [StorableHook(HookType.AfterDeserialization)]
|
---|
87 | private void AfterDeserialization() {
|
---|
88 | if (Parameters.ContainsKey(ConstraintUnsatisfiedSolutionsParameterName)) return;
|
---|
89 | Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName,
|
---|
90 | "Shows the number of solutions with unsatisfied constraints."));
|
---|
91 |
|
---|
92 | ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) {
|
---|
93 | VisualProperties = {
|
---|
94 | XAxisTitle = "Generations",
|
---|
95 | YAxisTitle = "Constraint Unsatisfied Solutions"
|
---|
96 | }
|
---|
97 | };
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override IOperation Apply() {
|
---|
101 | Iterations++;
|
---|
102 |
|
---|
103 | var problemData = RegressionProblemDataParameter.ActualValue;
|
---|
104 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
105 |
|
---|
106 | var results = ResultCollectionParameter.ActualValue;
|
---|
107 | var constraints = problemData.IntervalConstraints.EnabledConstraints;
|
---|
108 | var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
|
---|
109 | var intervalCollection = problemData.VariableRanges;
|
---|
110 | var newDataTable = ConstraintViolationParameter.ActualValue;
|
---|
111 | var solutions = this.SymbolicExpressionTree.ToArray();
|
---|
112 |
|
---|
113 | if (newDataTable.Rows.Count == 0)
|
---|
114 | foreach (var constraint in constraints)
|
---|
115 | newDataTable.Rows.Add(new DataRow(constraint.Expression));
|
---|
116 |
|
---|
117 | foreach (var constraint in constraints) {
|
---|
118 | var violations = trees.Count(tree => IntervalUtil.IntervalConstraintViolation(constraint, BoundsEstimator, intervalCollection, tree) > 0.0);
|
---|
119 |
|
---|
120 | newDataTable.Rows[constraint.Expression].Values.Add(violations);
|
---|
121 | }
|
---|
122 |
|
---|
123 | var constraintUnsatisfiedSolutionsDataTable = ConstraintUnsatisfiedSolutionsParameter.ActualValue;
|
---|
124 | if (constraintUnsatisfiedSolutionsDataTable.Rows.Count == 0)
|
---|
125 | constraintUnsatisfiedSolutionsDataTable.Rows.Add(new DataRow(ConstraintUnsatisfiedSolutionsParameterName));
|
---|
126 |
|
---|
127 | constraintUnsatisfiedSolutionsDataTable.Rows[ConstraintUnsatisfiedSolutionsParameterName]
|
---|
128 | .Values
|
---|
129 | .Add(solutions.Count(s => IntervalUtil.IntervalConstraintsViolation(constraints, BoundsEstimator, intervalCollection, s).Any(x => x != 0.0)));
|
---|
130 |
|
---|
131 | return base.Apply();
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | }
|
---|
136 | } |
---|