Package cc.mallet.optimize.tests
Class TestOptimizable
- java.lang.Object
-
- junit.framework.Assert
-
- junit.framework.TestCase
-
- cc.mallet.optimize.tests.TestOptimizable
-
- All Implemented Interfaces:
junit.framework.Test
public class TestOptimizable extends junit.framework.TestCase
Contains static methods for testing subclasses of Maximizable and Maximizable.ByGradient. Especially useful are methods that verify the consistency of the value and gradient functions of an instance of Maximizable.ByGradient.
-
-
Constructor Summary
Constructors Constructor Description TestOptimizable(java.lang.String name)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
main(java.lang.String[] args)
static void
setNumComponents(int n)
Sets the number of gradient components that will be checked.protected void
setUp()
static junit.framework.Test
suite()
static boolean
testGetSetParameters(Optimizable maxable)
Tests that parameters set by setParameters can be retrieved by getParameters.void
testTestValueAndGradient()
static boolean
testValueAndGradient(Optimizable.ByGradientValue maxable)
Tests that getValue and getValueGradient are consistent.static double
testValueAndGradientCurrentParameters(Optimizable.ByGradientValue maxable)
Tests that the value and gradient function are consistent at the current parameters.static double
testValueAndGradientInDirection(Optimizable.ByGradientValue maxable, double[] direction)
static boolean
testValueAndGradientRandomParameters(Optimizable.ByGradientValue maxable, java.util.Random r)
Tests that getValue and getValueGradient are consistent at a random parameter setting.-
Methods inherited from class junit.framework.TestCase
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runBare, runTest, setName, tearDown, toString
-
-
-
-
Method Detail
-
setNumComponents
public static void setNumComponents(int n)
Sets the number of gradient components that will be checked. If negative, all will be checked.
-
testGetSetParameters
public static boolean testGetSetParameters(Optimizable maxable)
Tests that parameters set by setParameters can be retrieved by getParameters.- Parameters:
maxable
- Instance of a Maximizable that should be tested. Its current parameters will be overwritten.
-
testValueAndGradientInDirection
public static double testValueAndGradientInDirection(Optimizable.ByGradientValue maxable, double[] direction)
-
testValueAndGradientCurrentParameters
public static double testValueAndGradientCurrentParameters(Optimizable.ByGradientValue maxable)
Tests that the value and gradient function are consistent at the current parameters. Computes both the analytic gradient (the one given by maxable.getValueGradient) and the empirical gradient, which is (if x are the current parameters and f the function computed by maxable) f(x + epsilon) - f(x). Verifies that the angle between the empirical and analytic gradients are close to 0.- Throws:
java.lang.IllegalStateException
- If the angle is above the tolerance- See Also:
testValueAndGradient
,testValueAndGradientRandomParameters
-
testValueAndGradient
public static boolean testValueAndGradient(Optimizable.ByGradientValue maxable)
Tests that getValue and getValueGradient are consistent. Tests for consistency at params = 0 and at params = -0.0001 * grad(f)- Throws:
java.lang.IllegalStateException
- If the test fails.- See Also:
testValueAndGradientCurrentParameters
-
testValueAndGradientRandomParameters
public static boolean testValueAndGradientRandomParameters(Optimizable.ByGradientValue maxable, java.util.Random r)
Tests that getValue and getValueGradient are consistent at a random parameter setting.- Throws:
java.lang.IllegalStateException
- If the test fails.- See Also:
testValueAndGradientCurrentParameters
-
testTestValueAndGradient
public void testTestValueAndGradient()
-
suite
public static junit.framework.Test suite()
-
setUp
protected void setUp()
- Overrides:
setUp
in classjunit.framework.TestCase
-
main
public static void main(java.lang.String[] args)
-
-