Search code examples
optaplanner

OptaPlanner terminates immediately if I add constructionHeuristic config


I'm learning the OptaPlanner library. My very simple test seems to work pretty well. The planning run gets terminated after 20 sec as I specify in my XML config.

I then add

<constructionHeuristic>
<constructionHeuristicType>FIRST_FIT_DECREASING</constructionHeuristicType>
</constructionHeuristic>

and the planning terminates almost immediately with very bad result that breaks even hard constraints.

In the manual I see:

Construction heuristics terminate automatically, so there's usually no need to configure a Termination on the construction heuristic phase specifically.

Does that relate to the whole planning run? If yes, then why does it need to terminate? I thought the point of constructionHeuristic is to construct good initial starting position the then start planning. That's not the case that I observe.

Am I missing something? What could be the reasons for premature termination?


Solution

  • The optimization algorithms configuration is optional. That means if you have no <constructionHeuristic> AND no <localSearch>, OptaPlanner will use a sensible default configuration (which happens to consist of one Construction Heuristic phase and one Local Search phase. That's why planning runs and gives a result in your simple test with no algorithms (phases) configured.

    When you add the <constructionHeuristic> yourself, OptaPlanner no longer uses the default configuration. At this point you have configured a single Construction Heuristic phase which runs the First Fit Decreasing algorithm. When it terminates the planning ends because there is no other phase to continue with.

    The automatic Construction Heuristic termination means that this phase terminates as soon as all entities' planning variables are initialized. Therefore you don't have to configure <termination> for this phase.

    What you need to do now is to add the <localSearch> phase after the <constructionHeuristic> phase and start with some basic configuration of one of the Local Search algorithms, for example the Hill Climbing. See the Local Search chapter for configuration examples.

    So for example:

    <?xml version="1.0" encoding="UTF-8"?>
    <solver>
      <!-- Define the model -->
      <scanAnnotatedClasses/>
    
      <!-- Define the score function -->
      <scoreDirectorFactory>
        ...
      </scoreDirectorFactory>
    
      <!-- Configure solver (global) termination -->
      <termination>
        <secondsSpentLimit>20</secondsSpentLimit>
      </termination>
    
      <!-- Configure the optimization algorithms (optional) -->
      <constructionHeuristic>
        <constructionHeuristicType>FIRST_FIT_DECREASING</constructionHeuristicType>
      </constructionHeuristic>
      <localSearch>
        <localSearchType>HILL_CLIMBING</localSearchType>
        <acceptor>
          <acceptorType>HILL_CLIMBING</acceptorType>
        </acceptor>
        <forager>
          <acceptedCountLimit>1</acceptedCountLimit>
        </forager>
    
        <!-- You can also configure phase termination -->
        <termination>
          <stepCountLimit>100</stepCountLimit>
        </termination>
      </localSearch>
    </solver>