In order to illustrate the K-Best feature, we will be using a variant of the knapsack model discussed above in the Binary Integer Variables section.  You may want to refer back to the earlier discussion if you are not familiar with the knapsack model.

Here's our model:

MODEL:

 

SETS:

  ITEMS: INCLUDE, WEIGHT, RATING;

  MYFAVORITES( ITEMS);

ENDSETS

 

DATA:

  KNAPSACK_CAPACITY = 15;

 

  ITEMS      WEIGHT  RATING =

    BRATS       3      1

    BROWNIES    3      1

    BEER        3      1

    ANT_REPEL   7      1

    BLANKET     4      6

    FRISBEE     1      6

    SALAD       5     10

    WATERMELON  7      9;

 

  MYFAVORITES = BRATS BROWNIES BEER;

ENDDATA

 

MAX = @SUM( ITEMS: RATING * INCLUDE);

 

@SUM( ITEMS: WEIGHT * INCLUDE) <=

KNAPSACK_CAPACITY;

 

@FOR( ITEMS: @BIN( INCLUDE));

 

NUMFAVE = @SUM( MYFAVORITES: INCLUDE);

 

END

Model: KBEST

In this example, we are packing a picnic basket for a picnic we will be taking with a friend.  Our friend's ratings of the candidate picnic items is given in the data section above.  It turns out that our friend is health conscious and does not care much for bratwurst, brownies nor beer.  This is unfortunate, because these happen to be our favorite items, which we indicate with a new subset of ITEMS called MYFAVORITES.

If we solve the model as is, thus solely maximizing our friend's preferences, we get the following solution:

Global optimal solution found.

Objective value:          25.00000

 

           Variable        Value

            NUMFAVE      0.000000

    INCLUDE( BRATS)      0.000000

 INCLUDE( BROWNIES)      0.000000

     INCLUDE( BEER)      0.000000

INCLUDE( ANT_REPEL)      0.000000

  INCLUDE( BLANKET)      0.000000

  INCLUDE( FRISBEE)      1.000000

    INCLUDE( SALAD)      1.000000

INCLUDE( WATERMELON)      1.000000

As indicated by the NUMFAVE variable, none of our favorite items are included in the optimal basket.  Now, we like our friend a lot, and we want him to be happy.  However, we are wondering if there isn't another combination of items that our friend might like almost as much that includes at least one of our favorite items.  To investigate this question, we set the Desired Number parameter of the K-Best Solutions box on the Solver|Options Integer Solver tab to 4:

kbest4sol

This means that we would like LINGO to generate the 4 best solutions to the model.  We then click OK and then run the Solver|Solve command.  At which point, the integer solver sees that the K-Best feature is being requested, and it automatically generates the 4 best solutions to the model.  At which point, we are presented with the following dialog box:

kbestdb

In the Candidate Solutions window we see that the solver was able to find 4 feasible next-best solutions to the model.  The solutions are ranked in order by their objective values.

There is also a column labeled Tradeoff, which lists the value in each solution of a designated tradeoff variable.  Any scalar variable in a model can be selected as the tradeoff variable.  In this example, there is only one scalar variable, NUMFAVE, so it is automatically selected as the tradeoff variable.  The idea behind the tradeoff variable is that it allows you to weigh the tradeoffs in a model's objective value with a secondary goal.  In this case, our secondary goal is the number of our favorite items in the picnic basket.  In particular, we see that there are three solutions with slightly worse objective values (23 vs. 25) that include one of our favorite items.  For example, if we selected solution 2 and pressed the View button, we'd see the following solution containing one of our favorite items, bratwurst:

Objective value:               23.00000

 

              Variable           Value

     KNAPSACK_CAPACITY        15.00000

               NUMFAVE        1.000000

       INCLUDE( BRATS)        1.000000

    INCLUDE( BROWNIES)        0.000000

        INCLUDE( BEER)        0.000000

   INCLUDE( ANT_REPEL)        0.000000

     INCLUDE( BLANKET)        1.000000

     INCLUDE( FRISBEE)        1.000000

       INCLUDE( SALAD)        1.000000

  INCLUDE( WATERMELON)        0.000000

The following buttons are available along the bottom edge of the K-Best dialog box:

Help Displays online help regarding the K-Best feature.
Cancel Cancels out of K-Best mode, closing the dialog box.
View Displays any solutions selected in the Candidate Solutions box.
Select Allow you to select one of the candidate solutions as the final solution to the model.

These buttons allow you to examine selected solutions returned by the K-Best solver.  Once you find a solution you believe to be the best, you can select it as the final solution.  Once a final solution is selected, all subsequent solution reports will be based on that particular solution.