Search in sources :

Example 26 with TooManyIterationsException

use of org.apache.commons.math3.exception.TooManyIterationsException in project axelor-open-suite by axelor.

the class EventServiceImpl method addRecurrentEventsByMonths.

@Override
@Transactional
public void addRecurrentEventsByMonths(Event event, int periodicity, int endType, int repetitionsNumber, LocalDate endDate, int monthRepeatType) {
    int weekNo = 1 + (event.getStartDateTime().getDayOfMonth() - 1) / 7;
    Duration duration = Duration.between(event.getStartDateTime(), event.getEndDateTime());
    Event lastEvent = event;
    BiFunction<Integer, LocalDateTime, Boolean> breakConditionFunc;
    Function<LocalDateTime, LocalDateTime> nextStartDateTimeFunc;
    LocalDateTime nextStartDateTime;
    if (endType == RecurrenceConfigurationRepository.END_TYPE_REPET) {
        breakConditionFunc = (iteration, dateTime) -> iteration >= repetitionsNumber;
    } else {
        breakConditionFunc = (iteration, dateTime) -> dateTime.toLocalDate().isAfter(endDate);
    }
    if (monthRepeatType == RecurrenceConfigurationRepository.REPEAT_TYPE_MONTH) {
        nextStartDateTimeFunc = dateTime -> dateTime.withDayOfMonth(1).plusMonths(periodicity).withDayOfMonth(event.getStartDateTime().getDayOfMonth());
    } else {
        nextStartDateTimeFunc = dateTime -> {
            LocalDateTime baseNextDateTime = dateTime.withDayOfMonth(1).plusMonths(periodicity);
            dateTime = baseNextDateTime.with(TemporalAdjusters.dayOfWeekInMonth(weekNo, event.getStartDateTime().getDayOfWeek()));
            if (!dateTime.getMonth().equals(baseNextDateTime.getMonth()) && weekNo > 1) {
                dateTime = baseNextDateTime.with(TemporalAdjusters.dayOfWeekInMonth(weekNo - 1, event.getStartDateTime().getDayOfWeek()));
            }
            return dateTime;
        };
    }
    for (int iteration = 0; ; ++iteration) {
        if (iteration > ITERATION_LIMIT) {
            throw new TooManyIterationsException(iteration);
        }
        nextStartDateTime = nextStartDateTimeFunc.apply(lastEvent.getStartDateTime());
        if (breakConditionFunc.apply(iteration, nextStartDateTime)) {
            break;
        }
        Event copy = eventRepo.copy(lastEvent, false);
        copy.setParentEvent(event);
        copy.setStartDateTime(nextStartDateTime);
        copy.setEndDateTime(nextStartDateTime.plus(duration));
        lastEvent = eventRepo.save(copy);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) TooManyIterationsException(org.apache.commons.math3.exception.TooManyIterationsException) Event(com.axelor.apps.crm.db.Event) Duration(java.time.Duration) Transactional(com.google.inject.persist.Transactional)

Example 27 with TooManyIterationsException

use of org.apache.commons.math3.exception.TooManyIterationsException in project axelor-open-suite by axelor.

the class EventServiceImpl method addRecurrentEventsByWeeks.

@Override
@Transactional
public void addRecurrentEventsByWeeks(Event event, int periodicity, int endType, int repetitionsNumber, LocalDate endDate, Map<Integer, Boolean> daysCheckedMap) {
    List<DayOfWeek> dayOfWeekList = daysCheckedMap.keySet().stream().sorted().map(DayOfWeek::of).collect(Collectors.toList());
    Duration duration = Duration.between(event.getStartDateTime(), event.getEndDateTime());
    Event lastEvent = event;
    BiFunction<Integer, LocalDateTime, Boolean> breakCondition;
    if (endType == RecurrenceConfigurationRepository.END_TYPE_REPET) {
        breakCondition = (iteration, dateTime) -> iteration >= repetitionsNumber;
    } else {
        breakCondition = (iteration, dateTime) -> dateTime.toLocalDate().isAfter(endDate);
    }
    boolean loop = true;
    for (int iteration = 0; loop; ++iteration) {
        if (iteration > ITERATION_LIMIT) {
            throw new TooManyIterationsException(iteration);
        }
        LocalDateTime nextStartDateTime = lastEvent.getStartDateTime().plusWeeks(periodicity - 1L);
        for (DayOfWeek dayOfWeek : dayOfWeekList) {
            nextStartDateTime = nextStartDateTime.with(TemporalAdjusters.next(dayOfWeek));
            if (breakCondition.apply(iteration, nextStartDateTime)) {
                loop = false;
                break;
            }
            Event copy = eventRepo.copy(lastEvent, false);
            copy.setParentEvent(event);
            copy.setStartDateTime(nextStartDateTime);
            copy.setEndDateTime(nextStartDateTime.plus(duration));
            lastEvent = eventRepo.save(copy);
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DayOfWeek(java.time.DayOfWeek) TooManyIterationsException(org.apache.commons.math3.exception.TooManyIterationsException) Event(com.axelor.apps.crm.db.Event) Duration(java.time.Duration) Transactional(com.google.inject.persist.Transactional)

Aggregations

TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)27 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)23 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)18 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)18 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)18 DiagonalMatrix (org.apache.commons.math3.linear.DiagonalMatrix)17 LeastSquaresBuilder (org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)16 PointValuePair (org.apache.commons.math3.optim.PointValuePair)14 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)12 MaxEval (org.apache.commons.math3.optim.MaxEval)12 InitialGuess (org.apache.commons.math3.optim.InitialGuess)10 OptimizationData (org.apache.commons.math3.optim.OptimizationData)10 SimpleBounds (org.apache.commons.math3.optim.SimpleBounds)10 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)10 CMAESOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer)10 MultivariateMatrixFunction (org.apache.commons.math3.analysis.MultivariateMatrixFunction)8 SimpleValueChecker (org.apache.commons.math3.optim.SimpleValueChecker)6 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)6 RealVector (org.apache.commons.math3.linear.RealVector)5 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)4