Package elki.math.linearalgebra
Class ConstrainedQuadraticProblemSolver
- java.lang.Object
-
- elki.math.linearalgebra.ConstrainedQuadraticProblemSolver
-
public class ConstrainedQuadraticProblemSolver extends java.lang.ObjectSolve a constrained quadratic equation in the form \( \tfrac12 x^T A x + b^T x + c \) constrained by a bounding box.Works by recursion over the dimensions, searches the different possible outcomes until it finds the best solution.
- Since:
- 0.8.0
- Author:
- Robert Gehde
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classConstrainedQuadraticProblemSolver.DimensionStateDescribes the calculation state of a Dimensionprivate static classConstrainedQuadraticProblemSolver.ProblemDataContains arrays for a specific size needed for the problem calculation using this object saves the creation of all those arrays, because we can just reuse them.
-
Field Summary
Fields Modifier and Type Field Description private ConstrainedQuadraticProblemSolver.ProblemData[]cacheCache object
-
Constructor Summary
Constructors Constructor Description ConstrainedQuadraticProblemSolver(int dim)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcalculateLinearDerivativeLimits(double[][] a, double[] b, double[] min, double[] max, int dim, double[] buf)Calculates the min and max value of a linear derivative of a quadratic function in the bounding box.private doublecomputeMaximumPossibleFuncValue(double[][] a, double[] b, double c, double[] min, double[] max)Calculates the maximum possible function value for a constrained quadratic function.private static booleancontains(double[] min, double[] max, double[] point)Checks if the point is inside or on the bounds of this bounding boxprivate doubleevaluateConstrainedQuadraticFunction(double[][] a, double[] b, double c, double[] min, double[] max, ConstrainedQuadraticProblemSolver.DimensionState[] dimensionStates, boolean cutoffCheck, double[] result, double resultValue)Main recursive function.private doubleevaluateConstrainedQuadraticFunction1D(double a, double b, double c, double lowerBound, double upperBound, double[] result, double resultValue)Finds the maximum for a 1d constrained quadratic function.private static doubleevaluateQuadraticFormula(double[][] a, double[] b, double c, double[] point)calculate \( \tfrac12 x^T A x + x^t b + c \) for the given values.private voidexpandNewSolution(double[] outbox, double[] inbox, int insert, double insertValue)Expands the redRes to a problem with dim+1 and saves it into resultprivate intfindLimitedDimensionWithDerivative(double[][] a, double[] b, double[] min, double[] max)Find the first dimension where the partial derivative wrt. that dimension is nonnegative or nonpositive.private double[]findMaximumWithFunctionValue(double[][] a, double[] b)Finds the argmax for \( \tfrac12 A x^2 + b x \).private static voidreduceConstraints(double[] inmin, double[] inmax, double[] outmin, double[] outmax, ConstrainedQuadraticProblemSolver.DimensionState[] instates, ConstrainedQuadraticProblemSolver.DimensionState[] outstates, int omit)Reduces the constrains to a problem with dim-1private static doublereduceEquation(double[][] a, double[] b, double c, double[][] redA, double[] redB, int reducedDim, double reduceToValue)Reduces the equation/function the a problem with dim-1private double[]reduceSolution(double[] result, int omit)Reduce the solution to a problem with dim-1doublesolve(double[][] a, double[] b, double c, double[] min, double[] max, double[] argmaxPoint)Calculate \( 0.5 x^T A x + b + c \) we use it in KDTrees with \( b = (0)^d \) and \( c = 0 \).private doublestartReducedProblem(double[][] a, double[] b, double c, double[] min, double[] max, ConstrainedQuadraticProblemSolver.DimensionState[] dimStates, int reducedDim, ConstrainedQuadraticProblemSolver.DimensionState reducedTo, double[] result, double resultValue)This function reduces the quadratic problem (given with a,b,c, bounds and dimState) with the information given in reducedDim and reducedTo.
-
-
-
Field Detail
-
cache
private ConstrainedQuadraticProblemSolver.ProblemData[] cache
Cache object
-
-
Method Detail
-
solve
public double solve(double[][] a, double[] b, double c, double[] min, double[] max, double[] argmaxPoint)Calculate \( 0.5 x^T A x + b + c \) we use it in KDTrees with \( b = (0)^d \) and \( c = 0 \). If you use it like this you can multiply the result with 2 to get \( x^T A x + b + c \).- Parameters:
a- a coefficient matrix of the functionb- b coefficient vector of the functionc- c coefficient of the functionmin- Lower constraintsmax- Upper constraintsargmaxPoint- point with the maximum
-
findLimitedDimensionWithDerivative
private int findLimitedDimensionWithDerivative(double[][] a, double[] b, double[] min, double[] max)Find the first dimension where the partial derivative wrt. that dimension is nonnegative or nonpositive. If the derivative is nonnegative (lo ≥0) it will be driven to the upper limit and vice versa.This does NOT mean that the non-found dimensions are not driven to a limit. This is just an implication, not an equivalence
- Parameters:
a- a coefficient matrix of the functionb- b coefficient vector of the functionmin- Lower constraintsmax- Upper constraints- Returns:
- 0 if none, dim + 1 if lo, -dim-1 if hi
-
calculateLinearDerivativeLimits
private void calculateLinearDerivativeLimits(double[][] a, double[] b, double[] min, double[] max, int dim, double[] buf)Calculates the min and max value of a linear derivative of a quadratic function in the bounding box. The function is of the form \( 0.5 A x^2 + b x + c \).- Parameters:
a- gradient/slopeb- y axis crossingmin- Lower constraintsmax- Upper constraintsdim- dimension of interestbuf- Return buffer
-
computeMaximumPossibleFuncValue
private double computeMaximumPossibleFuncValue(double[][] a, double[] b, double c, double[] min, double[] max)Calculates the maximum possible function value for a constrained quadratic function. This function value does not need to exist, it is just an upper limit.- Parameters:
a- a coefficient matrix of the functionb- b coefficient vector of the functionc- c coefficient of the functionmin- Lower constraintsmax- Upper constraints- Returns:
- The maximum Function value
-
evaluateConstrainedQuadraticFunction
private double evaluateConstrainedQuadraticFunction(double[][] a, double[] b, double c, double[] min, double[] max, ConstrainedQuadraticProblemSolver.DimensionState[] dimensionStates, boolean cutoffCheck, double[] result, double resultValue)Main recursive function. We calculate \( \frac12 x^T A x + bx + c \) and only update the result array if we have a better result- Parameters:
a- a coefficient matrix of the functionb- b coefficient vector of the functionc- c coefficient of the functionmin- Lower constraintsmax- Upper constraintsdimensionStates- Current calculation state of the dimensionscutoffCheck- Flag if a cutoff check should be performed
-
evaluateConstrainedQuadraticFunction1D
private double evaluateConstrainedQuadraticFunction1D(double a, double b, double c, double lowerBound, double upperBound, double[] result, double resultValue)Finds the maximum for a 1d constrained quadratic function. If a new maximum is found, the argmax contained in result will be overwritten with the new argmax.- Parameters:
a- a coefficient matrix of the functionb- b coefficient vector of the functionc- c coefficient of the functionlowerBound- lower bound of valid inputupperBound- upper bound of valid inputresult- the - so far - argmaxresultValue- the - so far - max- Returns:
- the new max
-
startReducedProblem
private double startReducedProblem(double[][] a, double[] b, double c, double[] min, double[] max, ConstrainedQuadraticProblemSolver.DimensionState[] dimStates, int reducedDim, ConstrainedQuadraticProblemSolver.DimensionState reducedTo, double[] result, double resultValue)This function reduces the quadratic problem (given with a,b,c, bounds and dimState) with the information given in reducedDim and reducedTo. It will then callevaluateConstrainedQuadraticFunction(double[][],double[],double,double[],double[],elki.math.linearalgebra.ConstrainedQuadraticProblemSolver.DimensionState[],boolean,double[],double)to continue evaluation.- Parameters:
a- a coefficient matrix of the functionb- b coefficient vector of the functionc- c coefficient of the functionmin- Lower constraintsmax- Upper constraintsdimStates- Current calculation state of the dimensionsreducedDim- the dimension to reducereducedTo- the state the dimension is reduced toresult- the current argmaxresultValue- the current max- Returns:
- the new max value
-
expandNewSolution
private void expandNewSolution(double[] outbox, double[] inbox, int insert, double insertValue)Expands the redRes to a problem with dim+1 and saves it into result- Parameters:
outbox- the resultinbox- the the reduced resultinsert- the dimension that was reduced to gain redResinsertValue- the value the dimension was reduced to to gain redRes
-
reduceSolution
private double[] reduceSolution(double[] result, int omit)Reduce the solution to a problem with dim-1- Parameters:
result- result of the arrayomit- dimension to omit- Returns:
- reduced result
-
reduceConstraints
private static void reduceConstraints(double[] inmin, double[] inmax, double[] outmin, double[] outmax, ConstrainedQuadraticProblemSolver.DimensionState[] instates, ConstrainedQuadraticProblemSolver.DimensionState[] outstates, int omit)Reduces the constrains to a problem with dim-1- Parameters:
inmin- Input minimainmax- Input maximaoutmin- Output minimaoutmax- Output maximainstates- dimension states of the problemoutstates- result array for dimension statesomit- dimension to omit
-
reduceEquation
private static double reduceEquation(double[][] a, double[] b, double c, double[][] redA, double[] redB, int reducedDim, double reduceToValue)Reduces the equation/function the a problem with dim-1- Parameters:
a- a coefficient matrix of the functionb- b coefficient vector of the functionc- c coefficient of the functionredA- reduced a result arrayredB- reduced b result arrayreducedDim- dimension to reducereduceToValue- state to reduce the dimension to- Returns:
- reduced c
-
findMaximumWithFunctionValue
private double[] findMaximumWithFunctionValue(double[][] a, double[] b)Finds the argmax for \( \tfrac12 A x^2 + b x \).- Parameters:
a- coefficient matrix A of the functionb- coefficient vector b of the function- Returns:
- the argmax of the given function
-
evaluateQuadraticFormula
private static double evaluateQuadraticFormula(double[][] a, double[] b, double c, double[] point)calculate \( \tfrac12 x^T A x + x^t b + c \) for the given values.- Parameters:
a- coefficient matrix A of the functionb- coefficient vector b of the functionc- coefficient scalar c of the functionpoint- x to calculate the function at- Returns:
- function value
-
contains
private static boolean contains(double[] min, double[] max, double[] point)Checks if the point is inside or on the bounds of this bounding box- Parameters:
min- Lower constraintsmax- Upper constraintspoint- point to check- Returns:
- true if point is weakly inside bounds
-
-