@Test public void should_fail_if_actual_has_duplicates() { double[] actual = {1d, 2d, 2d, 3d, 2d, 4d}; ArrayWrapperList wrapped = wrap(actual); Collection<Object> duplicates = duplicatesFrom(wrapped); try { arrays.assertDoesNotHaveDuplicates(description, actual); } catch (AssertionError e) { verify(failures).failure(description, shouldNotHaveDuplicates(actual, duplicates)); return; } throw expectedAssertionErrorNotThrown(); }
public double[] minimize(DifferentiableFunction function, double[] initial, double tolerance) { boolean printProgress = true; BacktrackingLineSearcher lineSearcher = new BacktrackingLineSearcher(); double[] guess = DoubleArrays.clone(initial); // this grows linearly as the iterations go on (if stepSizeGrowthAmount // > 0.0) // but gets scaled back geometrically by lineSearcher double stepSize = initialStepSize; for (int iteration = 0; iteration < maxIterations; iteration++) { if (stepSizeGrowthAmount == 0.0) stepSize = initialStepSize; else { stepSize *= stepSizeGrowthAmount; } double[] subgradient = function.derivativeAt(guess); double value = function.valueAt(guess); double[] direction = subgradient; DoubleArrays.scale(direction, -1.0); if (iteration == 0) lineSearcher.stepSizeMultiplier = initialStepSizeMultiplier; else lineSearcher.stepSizeMultiplier = stepSizeMultiplier; lineSearcher.initialStepSize = stepSize; double[] nextGuess = doLineSearch(function, lineSearcher, guess, direction); stepSize = lineSearcher.getFinalStepSize(); double[] nextDerivative = function.derivativeAt(nextGuess); double nextValue = function.valueAt(nextGuess); if (printProgress) { Logger.i().logs("[Subgradient] Iteration %d: %.6f", iteration, nextValue); } if (iteration >= minIterations && converged(value, nextValue, tolerance)) { return nextGuess; } guess = nextGuess; value = nextValue; subgradient = nextDerivative; } return guess; }
protected Pair<Double, double[]> calculate(double[] x) { for (ObjectiveItemDifferentiableFunction<I> itemFn : itemFns) { itemFn.setWeights(x); } List<Mapper> mappers = getMappers(); AsynchronousMapper.doMapping(items, mappers); double objVal = 0.0; double[] grad = new double[dimension()]; for (Mapper mapper : mappers) { objVal += mapper.objVal; DoubleArrays.addInPlace(grad, mapper.localGrad); } if (regularizer != null) { objVal += regularizer.update(x, grad, 1.0); } return Pair.newPair(objVal, grad); }
@Test public void should_pass_if_actual_is_empty() { double[] actual = {}; arrays.assertDoesNotHaveDuplicates(description, actual); }
@Test public void should_pass_if_actual_does_not_have_duplicates() { double[] actual = {1d, 2d, 3d}; arrays.assertDoesNotHaveDuplicates(description, actual); }
@Test public void should_fail_if_actual_is_null() { String message = ShouldNotBeNull.shouldNotBeNull().create(description); thrown.expect(AssertionError.class, message); arrays.assertDoesNotHaveDuplicates(description, null); }