@Test public void testRandom() { Random rnd = new Random(42); int n = 1000; int f = 0; for (int i = 0; i < n; i++) { IVector2 botPos = Vector2.fromXY( (rnd.nextDouble() * 4000) - 2000, (rnd.nextDouble() * 4000) - 2000); IVector2 botVel = Vector2.fromXY(rnd.nextDouble() * 2, rnd.nextDouble() * 2); IVector2 ballVel = Vector2.fromXY(rnd.nextDouble() * 2000, rnd.nextDouble() * 2000); ITrackedBall ball = genBall(ballVel); ITrackedBot tBot = TrackedBot.stubBuilder(BotID.createBotId(0, ETeamColor.YELLOW), 0) .withPos(botPos) .withVel(botVel) .build(); double optTime = optimalTime(ball, tBot); double optRefTime = optimalTimeRef(ball, botPos, botVel); if (Math.abs(optRefTime - optTime) > 0.05) { f++; } } assertThat((double) f / n) .withFailMessage("Fail rate is greater than 10 percent") .isLessThanOrEqualTo(0.1); }
public double optimalTime() { UnivariateOptimizer optimizer = new BrentOptimizer(0.0001, 0.01, new Checker()); double tMax = Math.max(1e-5, ballTrajectory.getTimeByVel(0)); Optional<IVector2> fieldIntersection = ballTrajectory.getPlanarCurve() .getIntersectionsWithRectangle(Geometry.getField()) .stream() .filter(p -> ballTrajectory.getTravelLineSegment().isPointOnLine(p)) .findFirst(); if (fieldIntersection.isPresent()) { tMax = Math.min(tMax, ballTrajectory.getTimeByPos(fieldIntersection.get())); } double tClosest = getTimeForDestClosestToBallLine(); if (tClosest >= tMax) { return tMax; } try { UnivariatePointValuePair result = optimizer.optimize( GoalType.MINIMIZE, new MaxEval(20), new MaxIter(20), new SearchInterval(0, tMax, tClosest), new UnivariateObjectiveFunction(this::absSlackTime)); return result.getPoint(); } catch (TooManyEvaluationsException | TooManyIterationsException e) { log.debug("Optimization failed.", e); return tMax; } }
BallInterceptor { public double optimalTime() { UnivariateOptimizer optimizer = new BrentOptimizer(0.0001, 0.01, new Checker()); double tMax = Math.max(1e-5, ballTrajectory.getTimeByVel(0)); Optional<IVector2> fieldIntersection = ballTrajectory.getPlanarCurve() .getIntersectionsWithRectangle(Geometry.getField()) .stream() .filter(p -> ballTrajectory.getTravelLineSegment().isPointOnLine(p)) .findFirst(); if (fieldIntersection.isPresent()) { tMax = Math.min(tMax, ballTrajectory.getTimeByPos(fieldIntersection.get())); } double tClosest = getTimeForDestClosestToBallLine(); if (tClosest >= tMax) { return tMax; } try { UnivariatePointValuePair result = optimizer.optimize( GoalType.MINIMIZE, new MaxEval(20), new MaxIter(20), new SearchInterval(0, tMax, tClosest), new UnivariateObjectiveFunction(this::absSlackTime)); return result.getPoint(); } catch (TooManyEvaluationsException | TooManyIterationsException e) { log.debug("Optimization failed.", e); return tMax; } } }
BallInterceptor { public double optimalTime() { UnivariateOptimizer optimizer = new BrentOptimizer(0.0001, 0.01, new Checker()); double tMax = Math.max(1e-5, ballTrajectory.getTimeByVel(0)); Optional<IVector2> fieldIntersection = ballTrajectory.getPlanarCurve() .getIntersectionsWithRectangle(Geometry.getField()) .stream() .filter(p -> ballTrajectory.getTravelLineSegment().isPointOnLine(p)) .findFirst(); if (fieldIntersection.isPresent()) { tMax = Math.min(tMax, ballTrajectory.getTimeByPos(fieldIntersection.get())); } double tClosest = getTimeForDestClosestToBallLine(); if (tClosest >= tMax) { return tMax; } try { UnivariatePointValuePair result = optimizer.optimize( GoalType.MINIMIZE, new MaxEval(20), new MaxIter(20), new SearchInterval(0, tMax, tClosest), new UnivariateObjectiveFunction(this::absSlackTime)); return result.getPoint(); } catch (TooManyEvaluationsException | TooManyIterationsException e) { log.debug("Optimization failed.", e); return tMax; } } private  BallInterceptor(final Builder builder); }
BallInterceptor { public double optimalTime() { UnivariateOptimizer optimizer = new BrentOptimizer(0.0001, 0.01, new Checker()); double tMax = Math.max(1e-5, ballTrajectory.getTimeByVel(0)); Optional<IVector2> fieldIntersection = ballTrajectory.getPlanarCurve() .getIntersectionsWithRectangle(Geometry.getField()) .stream() .filter(p -> ballTrajectory.getTravelLineSegment().isPointOnLine(p)) .findFirst(); if (fieldIntersection.isPresent()) { tMax = Math.min(tMax, ballTrajectory.getTimeByPos(fieldIntersection.get())); } double tClosest = getTimeForDestClosestToBallLine(); if (tClosest >= tMax) { return tMax; } try { UnivariatePointValuePair result = optimizer.optimize( GoalType.MINIMIZE, new MaxEval(20), new MaxIter(20), new SearchInterval(0, tMax, tClosest), new UnivariateObjectiveFunction(this::absSlackTime)); return result.getPoint(); } catch (TooManyEvaluationsException | TooManyIterationsException e) { log.debug("Optimization failed.", e); return tMax; } } private  BallInterceptor(final Builder builder); static Builder aBallInterceptor(); double optimalTime(); Optional<Double> optimalTimeIfReasonable(); static double getMinAcc(); static double getMaxAcc(); }
BallInterceptor { public double optimalTime() { UnivariateOptimizer optimizer = new BrentOptimizer(0.0001, 0.01, new Checker()); double tMax = Math.max(1e-5, ballTrajectory.getTimeByVel(0)); Optional<IVector2> fieldIntersection = ballTrajectory.getPlanarCurve() .getIntersectionsWithRectangle(Geometry.getField()) .stream() .filter(p -> ballTrajectory.getTravelLineSegment().isPointOnLine(p)) .findFirst(); if (fieldIntersection.isPresent()) { tMax = Math.min(tMax, ballTrajectory.getTimeByPos(fieldIntersection.get())); } double tClosest = getTimeForDestClosestToBallLine(); if (tClosest >= tMax) { return tMax; } try { UnivariatePointValuePair result = optimizer.optimize( GoalType.MINIMIZE, new MaxEval(20), new MaxIter(20), new SearchInterval(0, tMax, tClosest), new UnivariateObjectiveFunction(this::absSlackTime)); return result.getPoint(); } catch (TooManyEvaluationsException | TooManyIterationsException e) { log.debug("Optimization failed.", e); return tMax; } } private  BallInterceptor(final Builder builder); static Builder aBallInterceptor(); double optimalTime(); Optional<Double> optimalTimeIfReasonable(); static double getMinAcc(); static double getMaxAcc();  }