Changeset 17792 for branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 12/15/20 14:52:22 (4 years ago)
- Location:
- branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintEvaluator.cs
r17769 r17792 25 25 private const string UseSoftConstraintsParameterName = "Use Soft Constraints Evaluation"; 26 26 27 private const string PenaltyMultiplierParameterName = "Constraints Penalty Multiplier";27 //private const string PenaltyMultiplierParameterName = "Constraints Penalty Multiplier"; 28 28 29 29 private const string BoundsEstimatorParameterName = "Bounds estimator"; 30 30 31 private const string MaximumPenaltyFactorParamterName = "Maximum Penalty Factor"; 32 31 33 32 34 public IFixedValueParameter<BoolValue> UseConstantOptimizationParameter => 33 (IFixedValueParameter<BoolValue>) 35 (IFixedValueParameter<BoolValue>)Parameters[UseConstantOptimizationParameterName]; 34 36 35 37 public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter => 36 (IFixedValueParameter<IntValue>) 38 (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; 37 39 38 40 public IFixedValueParameter<BoolValue> UseSoftConstraintsParameter => 39 (IFixedValueParameter<BoolValue>) 40 41 public IFixedValueParameter<DoubleValue> PenaltyMultiplierParameter =>42 (IFixedValueParameter<DoubleValue>)Parameters[PenaltyMultiplierParameterName];41 (IFixedValueParameter<BoolValue>)Parameters[UseSoftConstraintsParameterName]; 42 43 //public IFixedValueParameter<DoubleValue> PenaltyMultiplierParameter => 44 //(IFixedValueParameter<DoubleValue>)Parameters[PenaltyMultiplierParameterName]; 43 45 44 46 public IValueParameter<IBoundsEstimator> BoundsEstimatorParameter => 45 (IValueParameter<IBoundsEstimator>) Parameters[BoundsEstimatorParameterName]; 47 (IValueParameter<IBoundsEstimator>)Parameters[BoundsEstimatorParameterName]; 48 public IFixedValueParameter<DoubleValue> MaximumPenaltyFactorParamter => 49 (IFixedValueParameter<DoubleValue>)Parameters[MaximumPenaltyFactorParamterName]; 46 50 47 51 public bool UseConstantOptimization { … … 61 65 62 66 public double PenaltyMultiplier { 63 get => PenaltyMultiplierParameter.Value.Value;64 set => PenaltyMultiplierParameter.Value.Value = value;67 get => 1.0;//PenaltyMultiplierParameter.Value.Value; 68 //set => PenaltyMultiplierParameter.Value.Value = value; 65 69 } 66 70 … … 68 72 get => BoundsEstimatorParameter.Value; 69 73 set => BoundsEstimatorParameter.Value = value; 74 } 75 76 public double MaximumPenaltyFactor { 77 get => MaximumPenaltyFactorParamter.Value.Value; 78 set => MaximumPenaltyFactorParamter.Value.Value = value; 70 79 } 71 80 … … 91 100 Parameters.Add(new FixedValueParameter<BoolValue>(UseSoftConstraintsParameterName, 92 101 "Define whether the constraints are penalized by soft or hard constraints.", new BoolValue(false))); 93 Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyMultiplierParameterName,94 "Specify how hard constraints violations should be punished", new DoubleValue(1.0)));95 102 Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName, 96 103 "Select the Boundsestimator.", new IABoundsEstimator())); 104 Parameters.Add(new FixedValueParameter<DoubleValue>(MaximumPenaltyFactorParamterName, 105 "Specify how hard constraints violations should be punished", new DoubleValue(1.5))); 97 106 } 98 107 … … 114 123 } 115 124 116 if (!Parameters.ContainsKey(PenaltyMultiplierParameterName)) {117 Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyMultiplierParameterName,118 "Specify how hard constraints violations should be punished", new DoubleValue(1.0)));119 }125 //if (!Parameters.ContainsKey(PenaltyMultiplierParameterName)) { 126 // Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyMultiplierParameterName, 127 // "Specify how hard constraints violations should be punished", new DoubleValue(1.0))); 128 //} 120 129 121 130 if (!Parameters.ContainsKey(BoundsEstimatorParameterName)) 122 131 Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName, 123 132 "Select the Boundsestimator.", new IABoundsEstimator())); 133 134 if (!Parameters.ContainsKey(MaximumPenaltyFactorParamterName)) { 135 Parameters.Add(new FixedValueParameter<DoubleValue>(MaximumPenaltyFactorParamterName, 136 "Specify how hard constraints violations should be punished", new DoubleValue(1.5))); 137 } 124 138 } 125 139 … … 166 180 if (node.Symbol.Name == "Offset") { 167 181 node.RemoveSubtree(1); 168 var alphaNode = new ConstantTreeNode(new Constant()) { Value = alpha};182 var alphaNode = new ConstantTreeNode(new Constant()) { Value = alpha }; 169 183 node.AddSubtree(alphaNode); 170 184 } else if (node.Symbol.Name == "Scaling") { 171 185 node.RemoveSubtree(1); 172 var betaNode = new ConstantTreeNode(new Constant()) { Value = beta};186 var betaNode = new ConstantTreeNode(new Constant()) { Value = beta }; 173 187 node.AddSubtree(betaNode); 174 188 } … … 177 191 178 192 var quality = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, 179 PenaltyMultiplier, BoundsEstimator );193 PenaltyMultiplier, BoundsEstimator, UseSoftConstraints, MaximumPenaltyFactor); 180 194 QualityParameter.ActualValue = new DoubleValue(quality); 181 195 … … 188 202 double upperEstimationLimit, 189 203 IRegressionProblemData problemData, IEnumerable<int> rows, 190 double penaltyMultiplier, IBoundsEstimator estimator) { 204 double penaltyMultiplier, IBoundsEstimator estimator, 205 bool useSoftConstraints, double maximumPenaltyFactor) { 206 191 207 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 192 208 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); … … 202 218 } 203 219 204 //if (!SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, solution, 205 // out var error)) { 206 // if (useSoftConstraints) { 207 // if (double.IsNaN(error) || double.IsInfinity(error)) { 208 // nmse += penaltyMultiplier * 1.0; 209 // } else { 210 // nmse += penaltyMultiplier * error; 211 // } 212 213 // nmse = Math.Min(1.0, nmse); 214 // } else { 215 // nmse = 1.0; 216 // } 217 //} 218 219 220 if (IntervalUtil.IntervalConstraintsViolation(constraints, estimator, intervalCollection, solution) 221 .Any(x => x != 0.0)) { 220 var constraintResults = IntervalUtil.IntervalConstraintsViolation(constraints, estimator, intervalCollection, solution); 221 222 if (constraintResults.Any(x => double.IsNaN(x) || double.IsInfinity(x))) { 223 return 1.0; 224 } 225 226 227 if (useSoftConstraints) { 228 if (maximumPenaltyFactor < 0.0) 229 throw new ArgumentException("The parameter 'Maximum Penalty Factor' has to be greater or equal 0.0!"); 230 231 var zip = constraints.Zip(constraintResults, (c, e) => new { Constraint = c, Error = e }); 232 double sum = 0.0; 233 234 foreach (var x in zip) { 235 if (x.Constraint.Weight <= 0) 236 throw new ArgumentException("Constraint weights <= 0 are not allowed!"); 237 238 double e = x.Error / x.Constraint.Weight; 239 240 e = double.IsNaN(e) ? 0.0 : e; 241 e = e > 1.0 ? 1.0 : e; 242 243 sum += Math.Sqrt(e) * maximumPenaltyFactor; 244 } 245 246 double avgError = sum / zip.Count(); 247 nmse = nmse * avgError + nmse; 248 nmse = Math.Min(1.0, nmse); 249 } else if (constraintResults.Any(x => x != 0.0)) { 222 250 nmse = 1.0; 223 251 } … … 235 263 var nmse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, 236 264 EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, 237 problemData, rows, PenaltyMultiplier, BoundsEstimator );265 problemData, rows, PenaltyMultiplier, BoundsEstimator, UseSoftConstraints, MaximumPenaltyFactor); 238 266 239 267 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; -
branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionConstraintAnalyzer.cs
r17769 r17792 25 25 private const string MaximumStepsParameterName = "Maximum Steps"; 26 26 private const string StartPenalityParameterName = "Start Penality"; 27 private const string ConstraintUnsatisfiedSolutionsParameterName = "Constraint Unsatisfied Solutions"; 27 28 28 29 #region parameter properties … … 49 50 (IValueParameter<DoubleValue>) Parameters[StartPenalityParameterName]; 50 51 52 public IResultParameter<DataTable> ConstraintUnsatisfiedSolutionsParameter => 53 (IResultParameter<DataTable>)Parameters[ConstraintUnsatisfiedSolutionsParameterName]; 54 51 55 #endregion 52 56 53 57 public override bool EnabledByDefault => false; 54 58 public static int Iterations { get; set; } = 0; 59 60 public IBoundsEstimator BoundsEstimator { get; set; } = new IABoundsEstimator(); 55 61 56 62 [StorableConstructor] … … 79 85 Parameters.Add(new ValueParameter<DoubleValue>(StartPenalityParameterName, 80 86 "The start value for the penality multiplier.", new DoubleValue(0.5))); 87 Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName, 88 "Shows the number of solutions with unsatisfied constraints.")); 81 89 82 90 … … 92 100 XAxisTitle = "Generations", 93 101 YAxisTitle = "Penality Multiplier" 102 } 103 }; 104 105 ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) { 106 VisualProperties = { 107 XAxisTitle = "Generations", 108 YAxisTitle = "Constraint Unsatisfied Solutions" 94 109 } 95 110 }; … … 137 152 Parameters.Add(new ValueParameter<DoubleValue>(StartPenalityParameterName, 138 153 "The start value for the penality multiplier.", new DoubleValue(0.5))); 154 155 156 if(!Parameters.ContainsKey(ConstraintUnsatisfiedSolutionsParameterName)) { 157 Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName, 158 "Shows the number of solutions with unsatisfied constraints.")); 159 160 ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) { 161 VisualProperties = { 162 XAxisTitle = "Generations", 163 YAxisTitle = "Constraint Unsatisfied Solutions" 164 } 165 }; 166 } 139 167 } 140 168 … … 158 186 var constraints = problemData.IntervalConstraints.EnabledConstraints; 159 187 var variableRanges = problemData.VariableRanges.GetReadonlyDictionary(); 188 var intervalCollection = problemData.VariableRanges; 160 189 var newDataTable = ConstraintViolationParameter.ActualValue; 190 var solutions = this.SymbolicExpressionTree.ToArray(); 161 191 162 192 if (newDataTable.Rows.Count == 0) … … 176 206 penalityDataTable.Rows.Add(new DataRow(rowName)); 177 207 penalityDataTable.Rows[rowName].Values.Add(penalityMultiplier.Value); 208 209 var constraintUnsatisfiedSolutionsDataTable = ConstraintUnsatisfiedSolutionsParameter.ActualValue; 210 if (constraintUnsatisfiedSolutionsDataTable.Rows.Count == 0) 211 constraintUnsatisfiedSolutionsDataTable.Rows.Add(new DataRow(ConstraintUnsatisfiedSolutionsParameterName)); 212 213 constraintUnsatisfiedSolutionsDataTable.Rows[ConstraintUnsatisfiedSolutionsParameterName] 214 .Values 215 .Add( 216 solutions 217 .Where(s => IntervalUtil.IntervalConstraintsViolation(constraints, BoundsEstimator, intervalCollection, s).Any(x => x != 0.0)) 218 .Count()); 178 219 179 220 return base.Apply(); -
branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSingleObjectiveMetaModelAnalyzer.cs
r17776 r17792 10 10 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 11 11 using HeuristicLab.Optimization; 12 using HeuristicLab.Parameters; 12 13 13 14 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { … … 21 22 22 23 #region parameter properties 23 public IResultParameter<I SymbolicRegressionSolution> BestMetaModelParameter =>24 (IResultParameter<I SymbolicRegressionSolution>)Parameters[BestMetaModelParameterName];24 public IResultParameter<ItemList<ISymbolicRegressionSolution>> BestMetaModelParameter => 25 (IResultParameter<ItemList<ISymbolicRegressionSolution>>)Parameters[BestMetaModelParameterName]; 25 26 #endregion 26 27 … … 32 33 33 34 public SymbolicRegressionSingleObjectiveMetaModelAnalyzer() { 34 Parameters.Add(new ResultParameter<I SymbolicRegressionSolution>(BestMetaModelParameterName,35 " The best meta model found."));35 Parameters.Add(new ResultParameter<ItemList<ISymbolicRegressionSolution>>(BestMetaModelParameterName, 36 "A list with the meta model for all problems.")); 36 37 } 37 38 38 39 [StorableHook(HookType.AfterDeserialization)] 39 40 private void AfterDeserialization() { 41 Parameters.Remove(BestMetaModelParameterName); 42 40 43 if (!Parameters.ContainsKey(BestMetaModelParameterName)) 41 Parameters.Add(new ResultParameter<I SymbolicRegressionSolution>(BestMetaModelParameterName,42 " The best meta model found."));44 Parameters.Add(new ResultParameter<ItemList<ISymbolicRegressionSolution>>(BestMetaModelParameterName, 45 "A list with all meta models for the problems.")); 43 46 } 44 47 … … 52 55 // init 53 56 var solutions = this.SymbolicExpressionTree.ToArray(); 57 var bestQualityWithConstantOpt = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue; 58 var bestQualityWithoutConstantOpt = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue; 54 59 var evaluator = baseProblem.Evaluator; 55 var bestQuality = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue; 56 SymbolicRegressionSolution bestMetaModel = null; 57 60 var interpreter = baseProblem.SymbolicExpressionTreeInterpreter; 61 ISymbolicExpressionTree bestSolutionWithConstantOpt = null; 62 ISymbolicExpressionTree bestSolutionWithoutConstantOpt = null; 63 var metaModels = new ItemList<ISymbolicRegressionSolution>(); 58 64 // iterate solutions 59 65 foreach (var solution in solutions) { 60 double qualityAvg = CalculateAverageQuality(solution, evaluator, problems); 66 // calculate with constant optimization 67 var tmpSolution = (ISymbolicExpressionTree) solution.Clone(); 68 double qualityAvg = CalculateAverageQuality(tmpSolution, evaluator, problems, interpreter, true); 69 // check if this solution is the best 70 bool isBest = baseProblem.Maximization.Value ? (bestQualityWithConstantOpt < qualityAvg) : (bestQualityWithConstantOpt > qualityAvg); 71 if (isBest) { 72 bestQualityWithConstantOpt = qualityAvg; 73 bestSolutionWithConstantOpt = tmpSolution; 74 } 61 75 76 // calculate it again without constant optimization to have a comparison 77 tmpSolution = (ISymbolicExpressionTree) solution.Clone(); 78 qualityAvg = CalculateAverageQuality(tmpSolution, evaluator, problems, interpreter, false); 62 79 // check if this solution is the best 63 bool isBest = baseProblem.Maximization.Value ? (bestQuality < qualityAvg) : (bestQuality> qualityAvg);80 isBest = baseProblem.Maximization.Value ? (bestQualityWithoutConstantOpt < qualityAvg) : (bestQualityWithoutConstantOpt > qualityAvg); 64 81 if (isBest) { 65 bestQuality = qualityAvg;66 best MetaModel = BuildSolution(solution, targetVariable, baseProblem);82 bestQualityWithoutConstantOpt = qualityAvg; 83 bestSolutionWithoutConstantOpt = tmpSolution; 67 84 } 68 85 } 69 BestMetaModelParameter.ActualValue = bestMetaModel; 86 87 foreach(var problem in problems) { 88 metaModels.Add(BuildSolution(bestSolutionWithConstantOpt, targetVariable, problem, "withConstantOpt")); 89 metaModels.Add(BuildSolution(bestSolutionWithoutConstantOpt, targetVariable, problem, "withoutConstantOpt")); 90 } 91 92 BestMetaModelParameter.ActualValue = metaModels; 70 93 } 71 94 … … 73 96 ISymbolicExpressionTree solution, 74 97 string targetVariable, 75 SymbolicRegressionSingleObjectiveProblem baseProblem) { 98 SymbolicRegressionSingleObjectiveProblem problem, 99 string suffix) { 76 100 var model = new SymbolicRegressionModel( 77 101 targetVariable, 78 102 (ISymbolicExpressionTree)solution.Clone(), 79 103 new SymbolicDataAnalysisExpressionTreeInterpreter()); 80 return new SymbolicRegressionSolution(model, baseProblem.ProblemData);104 return new SymbolicRegressionSolution(model, problem.ProblemData) { Name = $"{problem.Name}_solution_{suffix}" }; 81 105 } 82 106 83 107 private double CalculateAverageQuality( 84 108 ISymbolicExpressionTree solution, 85 ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> evaluator, 86 IEnumerable<SymbolicRegressionSingleObjectiveProblem> problems) { 109 ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> evaluator, 110 IEnumerable<SymbolicRegressionSingleObjectiveProblem> problems, 111 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 112 bool useConstantOptimization) { 87 113 double qualitySum = 0.0; 88 114 // iterate problems 89 115 foreach (var problem in problems) { 90 IDataset dataset = problem.ProblemData.Dataset; 91 IEnumerable<int> rows = Enumerable.Range(0, dataset.Rows); 92 // evalute problem with the evaluator of the base problem 93 qualitySum += evaluator.Evaluate(ExecutionContext, solution, problem.ProblemData, rows); 116 var problemData = problem.ProblemData; 117 118 if (useConstantOptimization) { 119 SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants( 120 interpreter, 121 solution, 122 problemData, 123 problemData.TrainingIndices, 124 false, 10, true); 125 } 126 127 qualitySum += evaluator.Evaluate( 128 ExecutionContext, 129 solution, 130 problemData, 131 problemData.TrainingIndices); 94 132 } 133 95 134 // calculate the average quality 96 135 return qualitySum / problems.Count();
Note: See TracChangeset
for help on using the changeset viewer.