← Back to Handbooks

Knapsack (Binary Variables) - Handbook

Type: BIP (Binary Integer Programming)

This handbook explains the Knapsack sample problem in the LP Black Box platform.

This is one of the most famous optimization problems and demonstrates the use of Binary variables.


The Problem

Scenario

Select items to pack in a knapsack with a weight limit. Each item has a value and weight.

ItemValue ($)Weight (kg)
Item 16010
Item 210020
Item 312015
Item 4808
Item 5505

Weight Limit: 30 kg

Your Goal

Maximize total value while staying within weight limit.

The Key: Binary Variables

Each item is either:

This is why we use Binary variable type - you can only choose 0 or 1, not partial quantities.

The Variables

VariableTypeMeaning
item1Binary (0 or 1)Is item 1 selected?
item2Binary (0 or 1)Is item 2 selected?
item3Binary (0 or 1)Is item 3 selected?
item4Binary (0 or 1)Is item 4 selected?
item5Binary (0 or 1)Is item 5 selected?

The Constraints

  1. Weight Limit: Total weight cannot exceed 30 kg

    Formula: 10×item1 + 20×item2 + 15×item3 + 8×item4 + 5×item5 ≤ 30


How to Use

Step 1: Load the Sample

Select Knapsack (Binary Variables) from the dropdown.

Step 2: Solve

The solver determines which items to select for maximum value within the weight limit.

Step 3: Interpret Results

The solution will show:


Try It Yourself


Why Binary Matters

Binary variables are used for yes/no decisions:

This makes LP capable of solving discrete optimization problems, not just continuous ones.


This demonstrates how Linear Programming with Binary variables can solve classic combinatorial optimization problems.