Java Code Examples for org.apache.commons.math3.util.Precision#compareTo()

The following examples show how to use org.apache.commons.math3.util.Precision#compareTo() . 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: EigenDecomposition.java    From astor with GNU General Public License v2.0 8 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: EigenDecomposition.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: Math_33_SimplexTableau_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 5
Source File: ConvexHull2D.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 7
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 8
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 10
Source File: Math_33_SimplexTableau_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 13
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: JGenProg2017_00112_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns the row with the minimum ratio as given by the minimum ratio test (MRT).
 * @param tableau simple tableau for the problem
 * @param col the column to test the ratio of.  See {@link #getPivotColumn(SimplexTableau)}
 * @return row with the minimum ratio
 */
private Integer getPivotRow(SimplexTableau tableau, final int col) {
    // create a list of all the rows that tie for the lowest score in the minimum ratio test
    List<Integer> minRatioPositions = new ArrayList<Integer>();
    double minRatio = Double.MAX_VALUE;
    for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) {
        final double rhs = tableau.getEntry(i, tableau.getWidth() - 1);
        final double entry = tableau.getEntry(i, col);

        if (Precision.compareTo(entry, 0d, maxUlps) > 0) {
            final double ratio = rhs / entry;
            // check if the entry is strictly equal to the current min ratio
            // do not use a ulp/epsilon check
            final int cmp = Double.compare(ratio, minRatio);
            if (cmp == 0) {
                minRatioPositions.add(i);
            } else if (cmp < 0) {
                minRatio = ratio;
                minRatioPositions = new ArrayList<Integer>();
                minRatioPositions.add(i);
            }
        }
    }

    if (minRatioPositions.size() == 0) {
        return null;
    } 
    return minRatioPositions.get(0);
}
 
Example 15
Source File: Cardumen_0041_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 17
Source File: 1_SimplexTableau.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: 1_SimplexTableau.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 19
Source File: DungeonMath.java    From dungeon with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Compares two doubles with the default tolerance margin.
 */
static int fuzzyCompare(double first, double second) {
  return Precision.compareTo(first, second, DEFAULT_DOUBLE_TOLERANCE);
}
 
Example 20
Source File: SimplexSolver.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public PointValuePair doOptimize()
    throws TooManyIterationsException,
           UnboundedSolutionException,
           NoFeasibleSolutionException {

    // reset the tableau to indicate a non-feasible solution in case
    // we do not pass phase 1 successfully
    if (solutionCallback != null) {
        solutionCallback.setTableau(null);
    }

    final SimplexTableau tableau =
        new SimplexTableau(getFunction(),
                           getConstraints(),
                           getGoalType(),
                           isRestrictedToNonNegative(),
                           epsilon,
                           maxUlps);

    solvePhase1(tableau);
    tableau.dropPhase1Objective();

    // after phase 1, we are sure to have a feasible solution
    if (solutionCallback != null) {
        solutionCallback.setTableau(tableau);
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // check that the solution respects the nonNegative restriction in case
    // the epsilon/cutOff values are too large for the actual linear problem
    // (e.g. with very small constraint coefficients), the solver might actually
    // find a non-valid solution (with negative coefficients).
    final PointValuePair solution = tableau.getSolution();
    if (isRestrictedToNonNegative()) {
        final double[] coeff = solution.getPoint();
        for (int i = 0; i < coeff.length; i++) {
            if (Precision.compareTo(coeff[i], 0, epsilon) < 0) {
                throw new NoFeasibleSolutionException();
            }
        }
    }
    return solution;
}