Changeset 17311
 Timestamp:
 10/04/19 09:34:18 (2 years ago)
 Location:
 branches/2994AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

branches/2994AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLS.cs
r17214 r17311 174 174 int idx = 0; 175 175 var formatter = new InfixExpressionFormatter(); 176 var constraintDescriptions = state.ConstraintDescriptions.ToArray(); 176 177 foreach(var constraintTree in state.constraintTrees) { 177 178 // HACK to remove parameter nodes which occurr multiple times 178 179 var reparsedTree = parser.Parse(formatter.Format(constraintTree)); 179 Results.AddOrUpdateResult($" Constraint {idx++}", reparsedTree);180 Results.AddOrUpdateResult($"{constraintDescriptions[idx++]}", reparsedTree); 180 181 } 181 182 … … 191 192 192 193 Results.AddOrUpdateResult("Best solution", CreateSolution((ISymbolicExpressionTree)state.BestTree.Clone(), problem.ProblemData)); 193 Results.AddOrUpdateResult("Best solution constraint values", new DoubleArray(state.BestConstraintValues)); 194 var bestConstraintValues = new DoubleArray(state.BestConstraintValues); 195 bestConstraintValues.ElementNames = constraintDescriptions; 196 Results.AddOrUpdateResult("Best solution constraint values", bestConstraintValues); 194 197 195 198 … … 215 218 var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeLinearInterpreter()); 216 219 // model.Scale(problemData); 217 return model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone()); 220 // var sol = model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone()); 221 // model.CreateRegressionSolution produces a new ProblemData and recalculates intervals 222 223 var sol = new SymbolicRegressionSolution(model, (IRegressionProblemData)problemData.Clone()); 224 225 // ==> set variable ranges to same range as in original problemData 226 // foreach(var interval in problemData.VariableRanges.GetIntervals()) { 227 // sol.ProblemData.VariableRanges.SetInterval 228 // } 229 return sol; 218 230 } 219 231 } 
branches/2994AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLSInternal.cs
r17240 r17311 41 41 public double[] BestConstraintValues => bestConstraintValues; 42 42 43 44 // for debugging (must be in the same order as processed below) 45 public IEnumerable<string> ConstraintDescriptions { 46 get { 47 foreach (var elem in problemData.IntervalConstraints.Constraints) { 48 if (!elem.Enabled) continue; 49 if (elem.Interval.UpperBound < double.PositiveInfinity) { 50 yield return elem.Expression + " < " + elem.Interval.UpperBound; 51 } 52 if (elem.Interval.LowerBound > double.NegativeInfinity) { 53 yield return "" + elem.Expression + " < " + (1) * elem.Interval.LowerBound; 54 } 55 } 56 } 57 } 58 43 59 public bool CheckGradient { get; internal set; } 44 60 … … 47 63 private SymbolicDataAnalysisExpressionTreeLinearInterpreter interpreter; 48 64 private readonly NLOpt.nlopt_func calculateObjectiveDelegate; // must hold the delegate to prevent GC 49 private readonly NLOpt.nlopt_precond preconditionDelegate;65 // private readonly NLOpt.nlopt_precond preconditionDelegate; 50 66 private readonly IntPtr[] constraintDataPtr; // must hold the objects to prevent GC 51 67 private readonly NLOpt.nlopt_func[] calculateConstraintDelegates; // must hold the delegates to prevent GC … … 217 233 // throw new InvalidOperationException($"NLOpt failed {res} {NLOpt.nlopt_get_errmsg(nlopt)}"); 218 234 return; 219 } else if (minf <= bestError) {235 } else if (minf <= bestError) { 220 236 bestSolution = x; 221 237 bestError = minf; … … 302 318 303 319 if (double.IsNaN(sse)) { 304 if (grad!=null) Array.Clear(grad, 0, grad.Length);320 if (grad != null) Array.Clear(grad, 0, grad.Length); 305 321 return double.MaxValue; 306 322 } … … 367 383 UpdateThetaValues(curX); 368 384 var intervalEvaluator = new IntervalEvaluator(); 385 var refIntervalEvaluator = new IntervalInterpreter(); 386 369 387 var constraintData = Marshal.PtrToStructure<ConstraintData>(data); 370 388 … … 373 391 var interval = intervalEvaluator.Evaluate(constraintData.Tree, dataIntervals, constraintData.ParameterNodes, 374 392 out double[] lowerGradient, out double[] upperGradient); 393 394 var refInterval = refIntervalEvaluator.GetSymbolicExpressionTreeInterval(constraintData.Tree, dataIntervals); 395 if (Math.Abs(interval.LowerBound  refInterval.LowerBound) > Math.Abs(interval.LowerBound) * 1e4) throw new InvalidProgramException($"Intervals don't match. {interval.LowerBound} <> {refInterval.LowerBound}"); 396 if (Math.Abs(interval.UpperBound  refInterval.UpperBound) > Math.Abs(interval.UpperBound) * 1e4) throw new InvalidProgramException($"Intervals don't match. {interval.UpperBound} <> {refInterval.UpperBound}"); 375 397 376 398 // we transformed this to a constraint c(x) <= 0, so only the upper bound is relevant for us … … 405 427 UpdateConstraintViolations(constraintData.Idx, interval.UpperBound); 406 428 if (double.IsNaN(interval.UpperBound)) { 407 Array.Clear(grad, 0, grad.Length);429 if(grad!=null)Array.Clear(grad, 0, grad.Length); 408 430 return double.MaxValue; 409 431 } else return interval.UpperBound;
Note: See TracChangeset
for help on using the changeset viewer.