Java Code Examples for org.apache.commons.math3.geometry.euclidean.oned.Interval#getInf()

The following examples show how to use org.apache.commons.math3.geometry.euclidean.oned.Interval#getInf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static List<Interval> refineOnLinearScale(final Interval interval, final int maxNumberOfSubIntervals, final double minimumLengthOfIntervals) {
	double min = interval.getInf();
	double max = interval.getSup();
	double length = max - min;
	List<Interval> intervals = new ArrayList<>();

	/* if no refinement is possible, return just the interval itself */
	if (length <= minimumLengthOfIntervals) {
		intervals.add(interval);
		return intervals;
	}

	/* otherwise compute the sub-intervals */
	int numberOfIntervals = Math.min((int) Math.ceil(length / minimumLengthOfIntervals), maxNumberOfSubIntervals);
	double stepSize = length / numberOfIntervals;
	for (int i = 0; i < numberOfIntervals; i++) {
		intervals.add(new Interval(min + i * stepSize, min + ((i + 1) * stepSize)));
	}
	return intervals;
}
 
Example 2
Source File: ExtendedRandomTreeTest.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
private double singleOptimaRun(final int plusMinus, final double startX, final Interval range) {
	double lower = range.getInf();
	double upper = range.getSup();
	double currentX = startX;
	double currentGrad = this.grad.apply(currentX);
	double nextX = currentX;
	double nextGrad = currentGrad;
	int runs = 0;
	while (runs < maxRuns && this.nextRunFitsANegativeUpdate(nextX, lower) && this.nextRunFitsAPositiveUpdate(nextX, upper) && !this.gradIsCloseToZero(nextGrad)) {
		currentX = nextX;
		currentGrad = nextGrad;
		int gradientSignum = currentGrad < 0 ? -1 : +1;
		nextX = currentX + (plusMinus * gradientSignum * gradientDescentStepSize);
		nextGrad = this.grad.apply(currentX);
		runs++;
	}
	// either of the conditions is met
	return this.fun.apply(currentX);
}
 
Example 3
Source File: ExtendedM5TreeTest.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
private double singleOptimaRun(final int plusMinus, final double startX, final Interval range) {
	double lower = range.getInf();
	double upper = range.getSup();
	double currentX = startX;
	double currentGrad = this.grad.apply(currentX);
	double nextX = currentX;
	double nextGrad = currentGrad;
	int runs = 0;
	// System.out.println("Random start point is " + startX + ", seraching for max:"
	// + (plusMinus == +1));
	while (runs < maxRuns && this.nextRunFitsANegativeUpdate(nextX, lower) && this.nextRunFitsAPositiveUpdate(nextX, upper) && !this.gradIsCloseToZero(nextGrad)) {
		currentX = nextX;
		currentGrad = nextGrad;
		int gradientSignum = currentGrad < 0 ? -1 : +1;
		nextX = currentX + (plusMinus * gradientSignum * gradientDescentStepSize);
		nextGrad = this.grad.apply(currentX);
		runs++;
	}
	// System.out.println("Returning with X:" + currentX + ", grad:" + currentGrad +
	// " f(X):" + fun.apply(currentX));
	// either of the conditions is met
	return this.fun.apply(currentX);
}
 
Example 4
Source File: IsValidParameterRangeRefinementPredicate.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<Interval> refineOnLinearScale(final Interval interval, final int maxNumberOfSubIntervals, final double minimumLengthOfIntervals, final boolean wasInitiallyLogarithmic, final boolean createPointIntervalsForExtremalValues) {
	double min = interval.getInf();
	double max = interval.getSup();
	double length = max - min;
	double logLength = max / min - 1;
	double relevantLength = wasInitiallyLogarithmic ? logLength : length;
	List<Interval> intervals = new ArrayList<>();
	this.logger.debug("Refining interval [{}, {}] in a linear fashion. Was initially refined on log-scale: {}", min, max, wasInitiallyLogarithmic);

	/* if no refinement is possible, return just the interval itself */
	if (relevantLength <= minimumLengthOfIntervals) {
		intervals.add(interval);
		if (createPointIntervalsForExtremalValues) {
			intervals.add(0, new Interval(min, min));
			intervals.add(new Interval(max, max));
		}
		return intervals;
	}
	/* otherwise compute the sub-intervals */
	int numberOfIntervals = Math.min((int) Math.ceil(relevantLength / minimumLengthOfIntervals), maxNumberOfSubIntervals);
	if (createPointIntervalsForExtremalValues) {
		numberOfIntervals -= 2;
	}
	numberOfIntervals = Math.max(numberOfIntervals, 1);
	this.logger.trace("Splitting interval of length {} and log-length {} into {} sub-intervals.", length, logLength, numberOfIntervals);
	double stepSize = length / numberOfIntervals;
	for (int i = 0; i < numberOfIntervals; i++) {
		intervals.add(new Interval(min + i * stepSize, min + ((i + 1) * stepSize)));
	}
	if (createPointIntervalsForExtremalValues) {
		intervals.add(0, new Interval(min, min));
		intervals.add(new Interval(max, max));
	}
	this.logger.trace("Derived sub-intervals {}", intervals.stream().map(i -> "[" + i.getInf() + ", " + i.getSup() + "]").collect(Collectors.toList()));
	return intervals;
}
 
Example 5
Source File: ExtendedRandomTreeTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
private double getOptima(final int plusMinus, final Interval xInterval) {
	// strategy: gradient descent pick 10 random start points got into the negative
	// direction until either a local optima has been reached(gradient close enough
	// to 0), or, an upper/lower bound has been reached.
	double[] randomStart = new double[randomStarts];
	for (int i = 0; i < randomStarts; i++) {
		randomStart[i] = Math.random() * (xInterval.getSup() - xInterval.getInf()) + xInterval.getInf();
	}
	if (plusMinus == +1) {
		return Arrays.stream(randomStart).mapToObj(x -> this.singleOptimaRun(plusMinus, x, xInterval)).max(Double::compare).orElseThrow(() -> new IllegalStateException());
	} else {
		return Arrays.stream(randomStart).mapToObj(x -> this.singleOptimaRun(plusMinus, x, xInterval)).min(Double::compare).orElseThrow(() -> new IllegalStateException());
	}

}
 
Example 6
Source File: ExtendedM5TreeTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
private double getOptima(final int plusMinus, final Interval xInterval) {
	// strategy: gradient descent pick 10 random start points got into the negative
	// direction until either a local optima has been reached(gradient close enough
	// to 0), or, an upper/lower bound has been reached.
	double[] randomStart = new double[randomStarts];
	for (int i = 0; i < randomStarts; i++) {
		randomStart[i] = Math.random() * (xInterval.getSup() - xInterval.getInf()) + xInterval.getInf();
	}
	if (plusMinus == +1) {
		return Arrays.stream(randomStart).mapToObj(x -> this.singleOptimaRun(plusMinus, x, xInterval)).max(Double::compare).orElseThrow(() -> new IllegalStateException());
	} else {
		return Arrays.stream(randomStart).mapToObj(x -> this.singleOptimaRun(plusMinus, x, xInterval)).min(Double::compare).orElseThrow(() -> new IllegalStateException());
	}

}
 
Example 7
Source File: ExtendedM5Tree.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Interval predictInterval(final IntervalAndHeader intervalAndHeader) {
	Interval[] queriedInterval = intervalAndHeader.getIntervals();
	// the stack of elements that still have to be processed.
	Deque<Entry<Interval[], RuleNode>> stack = new ArrayDeque<>();
	// initially, the root and the queried interval
	stack.push(RQPHelper.getEntry(queriedInterval, this.getM5RootNode()));

	// the list of all leaf values
	ArrayList<Double> list = new ArrayList<>();
	while (stack.peek() != null) {
		// pick the next node to process
		Entry<Interval[], RuleNode> toProcess = stack.pop();
		RuleNode nextTree = toProcess.getValue();
		double threshold = nextTree.splitVal();
		int attribute = nextTree.splitAtt();
		// process node
		if (nextTree.isLeaf()) {
			this.predictLeaf(list, toProcess, nextTree, intervalAndHeader.getHeaderInformation());
		} else {
			Interval intervalForAttribute = queriedInterval[attribute];
			// no leaf node...
			RuleNode leftChild = nextTree.leftNode();
			RuleNode rightChild = nextTree.rightNode();
			// traverse the tree

			if (intervalForAttribute.getInf() <= threshold) {

				if (threshold <= intervalForAttribute.getSup()) {
					// scenario: x_min <= threshold <= x_max
					// query [x_min, threshold] on the left child
					// query [threshold, x_max] on the right child
					Interval[] leftInterval = RQPHelper.substituteInterval(toProcess.getKey(), new Interval(intervalForAttribute.getInf(), threshold), attribute);
					stack.push(RQPHelper.getEntry(leftInterval, leftChild));
					Interval[] rightInterval = RQPHelper.substituteInterval(toProcess.getKey(), new Interval(threshold, intervalForAttribute.getSup()), attribute);
					stack.push(RQPHelper.getEntry(rightInterval, rightChild));
				} else {
					// scenario: x_min <= x_max < threshold
					// query [x_min, x_max] on the left child
					stack.push(RQPHelper.getEntry(toProcess.getKey(), leftChild));
				}
			} else {
				stack.push(RQPHelper.getEntry(toProcess.getKey(), rightChild));
			}
		}
	}
	return this.intervalAggregator.aggregate(list);
}
 
Example 8
Source File: ExtendedRandomTree.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Interval predictInterval(final IntervalAndHeader intervalAndHeader) {
	Interval[] queriedInterval = intervalAndHeader.getIntervals();
	// the stack of elements that still have to be processed.
	Deque<Entry<Interval[], Tree>> stack = new ArrayDeque<>();
	// initially, the root and the queried interval
	stack.push(RQPHelper.getEntry(queriedInterval, this.m_Tree));

	// the list of all leaf values
	ArrayList<Double> list = new ArrayList<>();
	while (stack.peek() != null) {
		// pick the next node to process
		Entry<Interval[], Tree> toProcess = stack.pop();
		Tree nextTree = toProcess.getValue();
		double threshold = nextTree.getM_SplitPoint();
		int attribute = nextTree.getM_Attribute();
		Tree[] children = nextTree.getM_Successors();
		double[] classDistribution = nextTree.getM_Classdistribution();
		// process node
		if (attribute == -1) {
			// node is a leaf
			// for now, assume that we have regression!
			list.add(classDistribution[0]);
		} else {
			Interval intervalForAttribute = queriedInterval[attribute];
			// no leaf node...
			Tree leftChild = children[0];
			Tree rightChild = children[1];
			// traverse the tree

			if (intervalForAttribute.getInf() <= threshold) {

				if (threshold <= intervalForAttribute.getSup()) {
					// scenario: x_min <= threshold <= x_max
					// query [x_min, threshold] on the left child
					// query [threshold, x_max] right
					Interval[] newInterval = RQPHelper.substituteInterval(toProcess.getKey(), new Interval(intervalForAttribute.getInf(), threshold), attribute);
					Interval[] newMaxInterval = RQPHelper.substituteInterval(toProcess.getKey(), new Interval(threshold, intervalForAttribute.getSup()), attribute);
					stack.push(RQPHelper.getEntry(newInterval, leftChild));
					stack.push(RQPHelper.getEntry(newMaxInterval, rightChild));
				} else {
					// scenario: threshold <= x_min <= x_max
					// query [x_min, x_max] on the left child
					stack.push(RQPHelper.getEntry(toProcess.getKey(), leftChild));
				}
			}
			// analogously...
			if (intervalForAttribute.getSup() > threshold) {
				stack.push(RQPHelper.getEntry(toProcess.getKey(), rightChild));
			}
		}
	}
	return this.intervalAggregator.aggregate(list);
}