Skip to content

Binary Integer Linear Programming (BILP) Example

Download Notebook


Binary Integer Linear Programming (BILP) is a general-purpose optimization framework with binary decision variables, linear constraints, and a linear objective. It models a wide range of combinatorial problems from logistics to scheduling.

import getpass
import os

import numpy as np
from dotenv import load_dotenv
from luna_quantum.algorithms import SCIP

from luna_usecases.binary_integer_linear_programming import (
    BilpData,
    BilpFormulation,
    BilpInstance,
)

load_dotenv()
if "LUNA_API_KEY" not in os.environ:
    os.environ["LUNA_API_KEY"] = getpass.getpass("Enter your Luna API key: ")

Create Data

Define a binary integer linear program with 4 variables and 3 constraints.

constraint_matrix = np.array(
    [
        [1, 0, 2, 0],
        [0, 1, 1, 3],
        [1, 4, 0, 1],
    ]
)
rhs = [1.0, 2.0, 3.0]
objective_coeffs = [3.0, 2.0, 5.0, 4.0]
constraint_senses = ["==", "<=", ">="]

data = BilpData.from_arrays(
    constraint_matrix=constraint_matrix,
    rhs=rhs,
    objective_coeffs=objective_coeffs,
    constraint_senses=constraint_senses,
)

print(data.to_string())
BILP Data:
  Number of variables: 4
  Number of constraints: 3
  Objective coefficients: [3.0, 2.0, 5.0, 4.0]
  RHS: [1.0, 2.0, 3.0]
  Constraint types: ['==', '<=', '>=']

Plot Data

Visualize the constraint structure.

data.plot()

<Axes: title={'center': 'BILP Constraint Matrix'}, xlabel='Variables', ylabel='Constraints'>
png

Create Formulation

Maximize the linear objective subject to linear inequality constraints with binary variables.

formulation = BilpFormulation()
print(formulation.to_string(data))
Binary Integer Linear Programming (BILP) Formulation:
  Variables: 4 (binary)
  Constraints: 3

Decision Variables:
  x[i] in {0,1} for i = 0, ..., 3

Objective:
  maximize sum_i c[i] * x[i]  for i = 0, ..., 3

Constraints:
  Constraint 0: sum_i S[0,i] * x[i] == 1.0
  Constraint 1: sum_i S[1,i] * x[i] <= 2.0
  Constraint 2: sum_i S[2,i] * x[i] >= 3.0

Create Instance

Combine data and formulation into a solvable instance.

instance = BilpInstance(data=data, formulation=formulation)
print(instance.to_string())
Data:BILP Data:
  Number of variables: 4
  Number of constraints: 3
  Objective coefficients: [3.0, 2.0, 5.0, 4.0]
  RHS: [1.0, 2.0, 3.0]
  Constraint types: ['==', '<=', '>=']
Formulation:Binary Integer Linear Programming (BILP) Formulation:
  Variables: 4 (binary)
  Constraints: 3

Decision Variables:
  x[i] in {0,1} for i = 0, ..., 3

Objective:
  maximize sum_i c[i] * x[i]  for i = 0, ..., 3

Constraints:
  Constraint 0: sum_i S[0,i] * x[i] == 1.0
  Constraint 1: sum_i S[1,i] * x[i] <= 2.0
  Constraint 2: sum_i S[2,i] * x[i] >= 3.0

Formulate Model

Translate the instance into a mathematical optimization model.

model = instance.formulate()

Solve and Interpret

Solve the model with SCIP and interpret the raw result into a use-case-specific solution.

scip = SCIP()
job = scip.run(model)
sol = job.result()
uc_solution = instance.interpret(sol)
print(uc_solution.to_string())
/Users/maximilianjanetschek/PycharmProjects/luna-usecases/.venv/lib/python3.13/site-packages/rich/live.py:260:
UserWarning: install "ipywidgets" for Jupyter support
  warnings.warn('install "ipywidgets" for Jupyter support')






2026-05-29 11:33:28 INFO     Sleeping for 5.0 seconds. Waiting and checking a function in a loop.


2026-05-29 11:33:35 INFO     Sleeping for 10.0 seconds. Waiting and checking a function in a loop.


2026-05-29 11:33:46 INFO     Sleeping for 15.0 seconds. Waiting and checking a function in a loop.




BILP Solution:
  Solution vector: [1, 1, 0, 0]
  Objective value: 5.0
  Valid: True

Plot Solution

Visualize the optimal solution.

uc_solution.plot(data)

array([<Axes: title={'center': 'BILP Solution — Objective: 5.0'}, xlabel='Variables', ylabel='Value'>,
       <Axes: title={'center': 'Constraint Satisfaction'}>], dtype=object)
png