Class SolverConfigOverride<Solution_>

java.lang.Object
ai.timefold.solver.core.api.solver.SolverConfigOverride<Solution_>
Type Parameters:
Solution_ - the solution type, the class with the PlanningSolution annotation

public final class SolverConfigOverride<Solution_> extends Object
Includes settings to override default Solver configuration.

This class provides an API to override solver termination settings. The following options are available:

Important ordering constraint: If withTerminationConfig(TerminationConfig) is used, it must be called before any specific termination methods like withTerminationSpentLimit(Duration) or withTerminationUnimprovedSpentLimit(Duration). This prevents accidental override of previously set specific configurations.

  • Constructor Details

    • SolverConfigOverride

      public SolverConfigOverride()
  • Method Details

    • getTerminationConfig

      public TerminationConfig getTerminationConfig()
    • withTerminationConfig

      public @NonNull SolverConfigOverride<Solution_> withTerminationConfig(@NonNull TerminationConfig terminationConfig)
      Sets the solver TerminationConfig, providing a base configuration that can be further customized with specific termination methods.

      After calling this method, additional specific termination methods can be chained to further customize the configuration:

      
       new SolverConfigOverride<MySolution>()
               .withTerminationConfig(new TerminationConfig())
               .withTerminationSpentLimit(Duration.ofMinutes(5))
               .withTerminationUnimprovedSpentLimit(Duration.ofMinutes(2));
       

      Important: This method must be called before any specific termination methods like withTerminationSpentLimit(Duration) or withTerminationUnimprovedSpentLimit(Duration).

      Calling this method after specific termination settings have been applied will throw an exception to prevent accidental override of those settings.

      
       new SolverConfigOverride<MySolution>()
               .withTerminationSpentLimit(Duration.ofMinutes(5))
               .withTerminationConfig(new TerminationConfig()); // Will throw exception
       
      Parameters:
      terminationConfig - allows overriding the default termination config of Solver
      Returns:
      this
      Throws:
      IllegalStateException - if specific termination settings have already been applied
    • withTerminationSpentLimit

      public @NonNull SolverConfigOverride<Solution_> withTerminationSpentLimit(@NonNull Duration spentLimit)
      Sets a time limit for the solver to run, creating or updating the default termination configuration.

      This method sets the maximum duration the solver is allowed to run before terminating. It can be used independently or in combination with withTerminationUnimprovedSpentLimit(Duration).

      If no TerminationConfig has been set via withTerminationConfig(TerminationConfig), this method will create a new one. If a TerminationConfig already exists, this method will update its spent limit setting.

      Usage examples:

      
       // Set only spent limit
       new SolverConfigOverride<MySolution>()
               .withTerminationSpentLimit(Duration.ofMinutes(10));
      
       // Combine with unimproved spent limit
       new SolverConfigOverride<MySolution>()
               .withTerminationSpentLimit(Duration.ofMinutes(10))
               .withTerminationUnimprovedSpentLimit(Duration.ofMinutes(3));
      
       // Use with base config
       new SolverConfigOverride<MySolution>()
               .withTerminationConfig(new TerminationConfig())
               .withTerminationSpentLimit(Duration.ofMinutes(10));
       
      Parameters:
      spentLimit - the maximum duration the solver is allowed to run
      Returns:
      this
    • withTerminationUnimprovedSpentLimit

      public @NonNull SolverConfigOverride<Solution_> withTerminationUnimprovedSpentLimit(@NonNull Duration unimprovedSpentLimit)
      Sets a time limit for the solver to run without finding an improved solution, creating or updating the default termination configuration.

      This method sets the maximum duration the solver is allowed to run without improvement before terminating. The solver will stop if it hasn't found a better solution within this time limit, even if the total spent limit (if set) hasn't been reached yet.

      This method can be used independently or in combination with withTerminationSpentLimit(Duration). When used together, the solver will terminate when either condition is met (whichever comes first).

      If no TerminationConfig has been set via withTerminationConfig(TerminationConfig), this method will create a new one. If a TerminationConfig already exists, this method will update its unimproved spent limit setting.

      Usage examples:

      
       // Set only unimproved spent limit
       new SolverConfigOverride<MySolution>()
               .withTerminationUnimprovedSpentLimit(Duration.ofMinutes(2));
      
       // Combine with total spent limit
       new SolverConfigOverride<MySolution>()
               .withTerminationSpentLimit(Duration.ofMinutes(10))
               .withTerminationUnimprovedSpentLimit(Duration.ofMinutes(2));
      
       // Use with base config
       new SolverConfigOverride<MySolution>()
               .withTerminationConfig(new TerminationConfig())
               .withTerminationUnimprovedSpentLimit(Duration.ofMinutes(2));
       
      Parameters:
      unimprovedSpentLimit - the maximum duration the solver is allowed to run without improvement
      Returns:
      this