/** * Gets the block diagonal matrix D of the decomposition. * D is a block diagonal matrix. * Real eigenvalues are on the diagonal while complex values are on * 2x2 blocks { {real +imaginary}, {-imaginary, real} }. * * @return the D matrix. * * @see #getRealEigenvalues() * @see #getImagEigenvalues() */ public RealMatrix getD() { if (cachedD == null) { // cache the matrix for subsequent calls cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues); for (int i = 0; i < imagEigenvalues.length; i++) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) { cachedD.setEntry(i, i+1, imagEigenvalues[i]); } else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { cachedD.setEntry(i, i-1, imagEigenvalues[i]); } } } return cachedD; }
/** {@inheritDoc} */ public int compare(final Vector2D o1, final Vector2D o2) { final double tolerance = getTolerance(); // need to take the tolerance value into account, otherwise collinear points // will not be handled correctly when building the upper/lower hull final int diff = Precision.compareTo(o1.getX(), o2.getX(), tolerance); if (diff == 0) { return Precision.compareTo(o1.getY(), o2.getY(), tolerance); } else { return diff; } } });
/** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { for (int i = getNumObjectiveFunctions(); i < getWidth() - 1; i++) { final double entry = tableau.getEntry(0, i); if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; }
/** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { final double[] objectiveFunctionRow = getRow(0); final int end = getRhsOffset(); for (int i = getNumObjectiveFunctions(); i < end; i++) { final double entry = objectiveFunctionRow[i]; if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; }
/** * Checks whether the given column is valid pivot column, i.e. will result * in a valid pivot row. * <p> * When applying Bland's rule to select the pivot column, it may happen that * there is no corresponding pivot row. This method will check if the selected * pivot column will return a valid pivot row. * * @param tableau simplex tableau for the problem * @param col the column to test * @return {@code true} if the pivot column is valid, {@code false} otherwise */ private boolean isValidPivotColumn(SimplexTableau tableau, int col) { for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { final double entry = tableau.getEntry(i, col); // do the same check as in getPivotRow if (Precision.compareTo(entry, 0d, cutOff) > 0) { return true; } } return false; }
if (Precision.compareTo(entry, 0d, epsilon) > 0) { columnsToDrop.add(i);
if (Precision.compareTo(entry, 0d, epsilon) > 0) { columnsToDrop.add(i);
if (Precision.compareTo(entry, 0d, cutOff) > 0) { final double ratio = FastMath.abs(rhs / entry);
final double entry = tableau.getEntry(i, col); if (Precision.compareTo(entry, 0d, maxUlps) > 0) { final double ratio = rhs / entry;
/** * Checks whether the given hull vertices form a convex hull. * @param hullVertices the hull vertices * @return {@code true} if the vertices form a convex hull, {@code false} otherwise */ private boolean isConvex(final Vector2D[] hullVertices) { if (hullVertices.length < 3) { return true; } int sign = 0; for (int i = 0; i < hullVertices.length; i++) { final Vector2D p1 = hullVertices[i == 0 ? hullVertices.length - 1 : i - 1]; final Vector2D p2 = hullVertices[i]; final Vector2D p3 = hullVertices[i == hullVertices.length - 1 ? 0 : i + 1]; final Vector2D d1 = p2.subtract(p1); final Vector2D d2 = p3.subtract(p2); final double crossProduct = MathArrays.linearCombination(d1.getX(), d2.getY(), -d1.getY(), d2.getX()); final int cmp = Precision.compareTo(crossProduct, 0.0, tolerance); // in case of collinear points the cross product will be zero if (cmp != 0.0) { if (sign != 0.0 && cmp != sign) { return false; } sign = cmp; } } return true; }
final double[] coeff = solution.getPoint(); for (int i = 0; i < coeff.length; i++) { if (Precision.compareTo(coeff[i], 0, epsilon) < 0) { throw new NoFeasibleSolutionException();
/** {@inheritDoc} */ public int compare(final Vector2D o1, final Vector2D o2) { final double tolerance = getTolerance(); // need to take the tolerance value into account, otherwise collinear points // will not be handled correctly when building the upper/lower hull final int diff = Precision.compareTo(o1.getX(), o2.getX(), tolerance); if (diff == 0) { return Precision.compareTo(o1.getY(), o2.getY(), tolerance); } else { return diff; } } });
/** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { for (int i = getNumObjectiveFunctions(); i < getWidth() - 1; i++) { final double entry = tableau.getEntry(0, i); if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; }
/** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { final double[] objectiveFunctionRow = getRow(0); final int end = getRhsOffset(); for (int i = getNumObjectiveFunctions(); i < end; i++) { final double entry = objectiveFunctionRow[i]; if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; }
/** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { final double[] objectiveFunctionRow = getRow(0); final int end = getRhsOffset(); for (int i = getNumObjectiveFunctions(); i < end; i++) { final double entry = objectiveFunctionRow[i]; if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; }
/** * Checks whether the given column is valid pivot column, i.e. will result * in a valid pivot row. * <p> * When applying Bland's rule to select the pivot column, it may happen that * there is no corresponding pivot row. This method will check if the selected * pivot column will return a valid pivot row. * * @param tableau simplex tableau for the problem * @param col the column to test * @return {@code true} if the pivot column is valid, {@code false} otherwise */ private boolean isValidPivotColumn(SimplexTableau tableau, int col) { for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { final double entry = tableau.getEntry(i, col); // do the same check as in getPivotRow if (Precision.compareTo(entry, 0d, cutOff) > 0) { return true; } } return false; }
/** * Checks whether the given column is valid pivot column, i.e. will result * in a valid pivot row. * <p> * When applying Bland's rule to select the pivot column, it may happen that * there is no corresponding pivot row. This method will check if the selected * pivot column will return a valid pivot row. * * @param tableau simplex tableau for the problem * @param col the column to test * @return {@code true} if the pivot column is valid, {@code false} otherwise */ private boolean isValidPivotColumn(SimplexTableau tableau, int col) { for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { final double entry = tableau.getEntry(i, col); // do the same check as in getPivotRow if (Precision.compareTo(entry, 0d, cutOff) > 0) { return true; } } return false; }
@Test public void testGridSearch() { Interval interval = Solver.gridSearch(new Parabola(0.25), 0.0, 1.0, 0.1); assertEquals(Precision.compareTo(interval.getInf(), 0.1, 1), 0); assertEquals(Precision.compareTo(interval.getSup(), 0.3, 1), 0); interval = Solver.gridSearch(new Parabola(1.2), 0.0, 1.0, 0.1); assertEquals(Precision.compareTo(interval.getInf(), 0.9, 1), 0); assertEquals(Precision.compareTo(interval.getSup(), 1.0, 1), 0); interval = Solver.gridSearch(new Parabola(1.2), 0.0, 1.0, 0.3); assertEquals(Precision.compareTo(interval.getInf(), 0.9, 1), 0); assertEquals(Precision.compareTo(interval.getSup(), 1.0, 1), 0); }
@Test public void testMaximize() { assertEquals(Precision.compareTo(Solver.maximize(new Parabola(0.47), 0.0, 1.0, 0.1, 0.00001, 0.00001, 100, 100), 0.47, 0.00001), 0); }