The entire model for our knapsack example and excerpts from its solution are listed below. The model formulation file may be found in your SAMPLES subdirectory off the main LINGO directory under the name KNAPSACK.

MODEL:

 

SETS:

  ITEMS: INCLUDE, WEIGHT, RATING;

ENDSETS

 

DATA:

  ITEMS          WEIGHT RATING =

   ANT_REPEL        1      2

   BEER             3      9

   BLANKET          4      3

   BRATWURST        3      8

   BROWNIES         3     10

   FRISBEE          1      6

   SALAD            5      4

   WATERMELON      10     10;

 

  KNAPSACK_CAPACITY = 15;

ENDDATA

 

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

 

@SUM( ITEMS: WEIGHT * INCLUDE) <=

KNAPSACK_CAPACITY;

 

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

 

END

Model: KNAPSACK

Global optimal solution found.

Objective value:                              38.00000

Objective bound:                              38.00000

Infeasibilities:                              0.000000

Extended solver steps:                               0

Total solver iterations:                             0

Elapsed runtime seconds:                          0.03

 

 Model Class:                                      PILP

 

           Variable            Value        Reduced Cost

 INCLUDE( ANT_REPEL)        1.000000           -2.000000

      INCLUDE( BEER)        1.000000           -9.000000

   INCLUDE( BLANKET)        1.000000           -3.000000

 INCLUDE( BRATWURST)        1.000000           -8.000000

  INCLUDE( BROWNIES)        1.000000           -10.00000

   INCLUDE( FRISBEE)        1.000000           -6.000000

     INCLUDE( SALAD)        0.000000           -4.000000

INCLUDE( WATERMELON)        0.000000           -10.00000

Solution to KNAPSACK

Your knapsack is fully packed at 15 pounds, and we take along everything but the salad and watermelon. Your lunch of beer, bratwurst, and brownies may not be very healthy, but at least you will be happy!