Example #1
0
  /*
   * in some cases we could suspect that some point inside the box is an optimum
   * that we are looking for. (For example when a point algorithm found it).
   * Than we want to check this. To do this we cut the box on a small box
   * around this point and everything else
   */
  public Box[] cutOutBoxAroundThisPoint(double[] potentialOptPoint) {
    final double epsilon = 5e-4;

    final int dim = getDimension();
    assert (potentialOptPoint.length == dim);
    if (!this.contains(potentialOptPoint)) {
      // probably it is due to rounding error and it is close
      // save original point for diagnostic
      double[] origPoint = potentialOptPoint.clone();
      if (setToClosestAreaPoint(potentialOptPoint) < epsilon * dim) {
        // let it be "close enough"
        // continue
      } else {
        Box arr[] = new Box[1];
        arr[0] = this;
        // possible bug. Lets fail in debug. See origPoint[].
        assert (false);
        return arr;
      }
    }
    ArrayList<Box> parts = new ArrayList<>();
    Box boxOfInterest = this;
    Box boxes[] = null;
    for (int i = 0; i < dim; i++) {
      double cutPoint = potentialOptPoint[i] - epsilon;
      if (boxOfInterest.getInterval(i).contains(cutPoint)) {
        boxes = boxOfInterest.splitSideByPoint(i, cutPoint);
        assert (boxes[1].contains(potentialOptPoint)); // "our" box is right
        parts.add(boxes[0]);
        boxOfInterest = boxes[1];
      }
      cutPoint = potentialOptPoint[i] + epsilon; // <
      if (boxOfInterest.getInterval(i).contains(cutPoint)) {
        boxes = boxOfInterest.splitSideByPoint(i, cutPoint);
        assert (boxes[0].contains(potentialOptPoint)); // "our" box is left now
        parts.add(boxes[1]);
        boxOfInterest = boxes[0];
      }
    }
    assert (boxOfInterest.contains(potentialOptPoint));
    parts.add(boxOfInterest);
    return parts.toArray(new Box[parts.size()]);
  }