public static boolean testCostAndGradient(Minimizable.ByGradient minable) { Matrix parameters = minable.getNewMatrix(); parameters.setAll(0.0); minable.setParameters(parameters); testCostAndGradientCurrentParameters(minable); parameters.setAll(0.0); Matrix delta = minable.getNewMatrix(); minable.getCostGradient(delta); delta.timesEquals(-0.0001); parameters.plusEquals(delta); minable.setParameters(parameters); testCostAndGradientCurrentParameters(minable); return true; }
public static boolean testGetSetParameters(Minimizable minable) { System.out.println("TestMinimizable testGetSetParameters"); // Set all the parameters to unique values using setParameters() Matrix parameters = minable.getNewMatrix(); minable.getParameters(parameters); for (int i = 0; i < parameters.singleSize(); i++) parameters.setSingleValue(i, (double) i); minable.setParameters(parameters); // Test to make sure those parameters are there parameters.setAll(0.0); minable.getParameters(parameters); for (int i = 0; i < parameters.singleSize(); i++) assertTrue(parameters.singleValue(i) == (double) i); // Set all the parameters to unique values using setParameter() parameters.setAll(0.0); minable.setParameters(parameters); int[] indices = new int[parameters.getNumDimensions()]; for (int i = 0; i < parameters.singleSize(); i++) { parameters.singleToIndices(i, indices); minable.setParameter(indices, (double) i); } // Test to make sure those parameters are there parameters.setAll(0.0); minable.getParameters(parameters); for (int i = 0; i < parameters.singleSize(); i++) { // System.out.println ("Got "+parameters.getSingle(i)+", expecting "+((double)i)); assertTrue(parameters.singleValue(i) == (double) i); } // Test to make sure they are also there when we look individually for (int i = 0; i < parameters.singleSize(); i++) { parameters.singleToIndices(i, indices); assertTrue(minable.getParameter(indices) == (double) i); } return true; }