Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17311


Ignore:
Timestamp:
10/04/19 09:34:18 (5 years ago)
Author:
gkronber
Message:

#2994: add names for constraints and derivatives, fix a problem: variableRanges are reset when creating a new solution. fix smaller bugs

Location:
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLS.cs

    r17214 r17311  
    174174      int idx = 0;
    175175      var formatter = new InfixExpressionFormatter();
     176      var constraintDescriptions = state.ConstraintDescriptions.ToArray();
    176177      foreach(var constraintTree in state.constraintTrees) {
    177178        // HACK to remove parameter nodes which occurr multiple times
    178179        var reparsedTree = parser.Parse(formatter.Format(constraintTree));
    179         Results.AddOrUpdateResult($"Constraint {idx++}", reparsedTree);
     180        Results.AddOrUpdateResult($"{constraintDescriptions[idx++]}", reparsedTree);
    180181      }
    181182
     
    191192
    192193      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);
    194197
    195198
     
    215218      var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
    216219      // 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;
    218230    }
    219231  }
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLSInternal.cs

    r17240 r17311  
    4141    public double[] BestConstraintValues => bestConstraintValues;
    4242
     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
    4359    public bool CheckGradient { get; internal set; }
    4460
     
    4763    private SymbolicDataAnalysisExpressionTreeLinearInterpreter interpreter;
    4864    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;
    5066    private readonly IntPtr[] constraintDataPtr; // must hold the objects to prevent GC
    5167    private readonly NLOpt.nlopt_func[] calculateConstraintDelegates; // must hold the delegates to prevent GC
     
    217233        // throw new InvalidOperationException($"NLOpt failed {res} {NLOpt.nlopt_get_errmsg(nlopt)}");
    218234        return;
    219       } else if(minf <= bestError) {
     235      } else if (minf <= bestError) {
    220236        bestSolution = x;
    221237        bestError = minf;
     
    302318
    303319      if (double.IsNaN(sse)) {
    304         if(grad!=null) Array.Clear(grad, 0, grad.Length);
     320        if (grad != null) Array.Clear(grad, 0, grad.Length);
    305321        return double.MaxValue;
    306322      }
     
    367383      UpdateThetaValues(curX);
    368384      var intervalEvaluator = new IntervalEvaluator();
     385      var refIntervalEvaluator = new IntervalInterpreter();
     386
    369387      var constraintData = Marshal.PtrToStructure<ConstraintData>(data);
    370388
     
    373391      var interval = intervalEvaluator.Evaluate(constraintData.Tree, dataIntervals, constraintData.ParameterNodes,
    374392        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) * 1e-4) throw new InvalidProgramException($"Intervals don't match. {interval.LowerBound} <> {refInterval.LowerBound}");
     396      if (Math.Abs(interval.UpperBound - refInterval.UpperBound) > Math.Abs(interval.UpperBound) * 1e-4) throw new InvalidProgramException($"Intervals don't match. {interval.UpperBound} <> {refInterval.UpperBound}");
    375397
    376398      // we transformed this to a constraint c(x) <= 0, so only the upper bound is relevant for us
     
    405427      UpdateConstraintViolations(constraintData.Idx, interval.UpperBound);
    406428      if (double.IsNaN(interval.UpperBound)) {
    407         Array.Clear(grad, 0, grad.Length);
     429        if(grad!=null)Array.Clear(grad, 0, grad.Length);
    408430        return double.MaxValue;
    409431      } else return interval.UpperBound;
Note: See TracChangeset for help on using the changeset viewer.