Alleria Source (private)

THE EXAMPLE NOTEBOOK

Alleria examples

Three complete examples you can read and adapt. Running the Python examples requires installation from the currently private source repository with authorized access. The interactive browser demos work without installing the solver.

01 / CONTINUOUS LP

Continuous linear program

Choose quantities of two products. Each uses one unit of a shared resource; product x earns 3 and product y earns 2. There are 4 resource units, and x is limited to 3.

maximize3x + 2ysubject to
x + y ≤ 4
x ≤ 3
0 ≤ x, y ≤ 10

The optimum is x = 3, y = 1, with an objective of 11. This example requires the native Python backend.

Download continuous_lp.py ↓
continuous_lp.py
"""Maximize production value subject to two capacity limits."""

from alleria import solve_model


model = {
    "name": "production",
    "objective_sense": "maximize",
    "columns": [
        {
            "name": "x",
            "lower_bound": 0.0,
            "upper_bound": 10.0,
            "obj_coeff": 3.0,
            "col_type": "continuous",
        },
        {
            "name": "y",
            "lower_bound": 0.0,
            "upper_bound": 10.0,
            "obj_coeff": 2.0,
            "col_type": "continuous",
        },
    ],
    "constraints": [
        {
            "name": "capacity",
            "sense": "<=",
            "rhs": 4.0,
            "coeffs": [[0, 1.0], [1, 1.0]],
        },
        {
            "name": "x_limit",
            "sense": "<=",
            "rhs": 3.0,
            "coeffs": [[0, 1.0]],
        },
    ],
}

result = solve_model(
    model, backend="inprocess", preset="exact", include_primal=True
)
print(f"status: {result.status}")
if result.status != "optimal" or result.incumbent_primal is None:
    raise RuntimeError("This small example should have an optimal solution.")
print(f"objective: {result.objective:g}")
for name, value in zip(result.column_names, result.incumbent_primal):
    print(f"{name}: {value:g}")
EXPECTED RESULTstatus: optimal
objective: 11
x: 3
y: 1

EXPLORE THE GEOMETRY

Explore a linear program

Change the shared capacity, the limit on x, or the value of y. The shaded region shows feasible plans; the marked corner gives the best objective. This variant caps y at 6 and starts at the same optimum as the Python example.

Educational demo computed locally in your browser. Export the current model as JSON to use with Alleria’s Python model API.

Browser demo

More capacity can expand the feasible region. Range: 0 to 10.

This moves the vertical boundary. Range: 0 to 6.

Maximize 3x + this value × y. Above 3, y becomes more valuable per resource unit.

Best objective11

x = 3, y = 1

The best plan earns 11.

Feasible production plans
Feasible regionCapacity boundaryObjective levelOne optimum

Both quantities are nonnegative, and y ≤ 6. A linear objective reaches its best value at a feasible corner; equal-value corners can share an optimal edge.

Use this model with Alleria

Copy the dictionary below, load it with json.loads(...), then call alleria.solve_model(model, backend="inprocess", include_primal=True) in your Python environment. See native setup.

Current LP model · JSON
{"name":"browser_production","objective_sense":"maximize","columns":[{"name":"x","lower_bound":0,"upper_bound":3,"obj_coeff":3,"col_type":"continuous"},{"name":"y","lower_bound":0,"upper_bound":6,"obj_coeff":2,"col_type":"continuous"}],"constraints":[{"name":"capacity","sense":"<=","rhs":4,"coeffs":[[0,1],[1,1]]}]}

02 / BINARY MILP

Binary knapsack

Select a subset of four items to maximize their total value without exceeding a capacity of 7. Each item is a binary decision: take it or leave it.

Knapsack data
ItemWeightValue
item_023
item_134
item_245
item_358

The best selection is item_0 + item_3: weight 7, value 11. Requires the native backend.

Download binary_knapsack.py ↓
binary_knapsack.py
"""Choose items with maximum value while staying within a weight limit."""

from alleria import solve_model


weights = [2, 3, 4, 5]
values = [3, 4, 5, 8]
model = {
    "name": "knapsack",
    "objective_sense": "maximize",
    "columns": [
        {
            "name": f"item_{i}",
            "lower_bound": 0.0,
            "upper_bound": 1.0,
            "obj_coeff": value,
            "col_type": "binary",
        }
        for i, value in enumerate(values)
    ],
    "constraints": [
        {
            "name": "weight_limit",
            "sense": "<=",
            "rhs": 7.0,
            "coeffs": [[i, weight] for i, weight in enumerate(weights)],
        },
    ],
}

result = solve_model(
    model, backend="inprocess", preset="exact", include_primal=True
)
print(f"status: {result.status}")
if result.status != "optimal" or result.incumbent_primal is None:
    raise RuntimeError("This small example should have an optimal solution.")
print(f"objective: {result.objective:g}")
chosen = [
    name
    for name, value in zip(result.column_names, result.incumbent_primal)
    if value > 0.5
]
print(f"chosen: {', '.join(chosen)}")
EXPECTED RESULTstatus: optimal
objective: 11
chosen: item_0, item_3

CHANGE ONE THING

Explore the budget

Move the capacity slider and watch the best combination change.

0 units14 units

Educational browser demo: checks all 16 combinations with JavaScript. The downloadable Python example runs the Rust solver.

✓ITEM 0weight 2 · value 3
✓ITEM 1weight 3 · value 4
✓ITEM 2weight 4 · value 5
✓ITEM 3weight 5 · value 8
Best total value11
Capacity used7 / 7

Take item_0 and item_3.

FOLLOW THE SEARCH

Branch-and-bound walkthrough

Step through a small branch-and-bound search using the same four items. Every split takes or leaves one item. The search visits the open node with the largest upper bound, and drops branches that cannot improve the best feasible selection.

Educational search computed locally in your browser. The downloadable Python example runs Alleria’s Rust solver; its internal search can differ from this teaching algorithm.

Browser demo

Changing capacity starts a new search. Try 6 to see a fractional bound.

Step 1
Incumbent · best feasible value0
Remaining upper bound11

Best feasible plan: the empty selection (weight 0).

Start with the feasible empty selection, worth 0.

Branch-and-bound search tree
OpenBranchedPrunedIncumbent

Node labels show an upper bound (≤). Take branches go left; leave branches go right. Item order is 3, 0, 1, 2, sorted by value per unit of weight.

Inspect a node

Select a node below to read its decisions and bound. A question mark is an undecided item.

  1. Root: upper bound 11.

Root: no decisions made yet.

Relaxing binary choices to fractions gives an upper bound.

Why is the bound safe?

For each node, take its fixed items, then fill remaining capacity in value-to-weight order, allowing a fraction of the last item. This relaxation contains every valid integer completion, so its value is an upper bound. The incumbent uses whole items only. When no open bound can beat it, the optimum is proved for this four-item model.

03 / FILES + CLI

LP files and the command line

The same two-variable production model can live in an LP file. Read it from Python or pass it directly to the CLI.

Place both downloads in the same folder and run python file_workflow.py. Build the CLI and install the wrapper first; the native extension is optional for this example.

Installation instructions →
model.lp
Maximize
 obj: 3 x + 2 y
Subject To
 capacity: x + y <= 4
 x_limit: x <= 3
Bounds
 0 <= x <= 10
 0 <= y <= 10
End
file_workflow.py
"""Solve an LP file through the CLI backend from Python."""

from pathlib import Path

from alleria import solve_file


path = Path(__file__).with_name("model.lp")
result = solve_file(
    path, backend="cli", preset="exact", include_primal=True
)
print(f"status: {result.status}")
if result.status != "optimal" or result.incumbent_primal is None:
    raise RuntimeError("This small example should have an optimal solution.")
print(f"objective: {result.objective:g}")
# File parsers can reorder columns. Pair values with the returned names.
solution = dict(zip(result.column_names, result.incumbent_primal))
for name in sorted(solution):
    print(f"{name}: {solution[name]:g}")
CLI · macOS / Linux
./target/release/alleria_api --input website/examples/model.lp --preset exact --include-primal

Run the CLI command from the repository root. On Windows, add .exe to the executable name. Expected optimum: x = 3, y = 1, objective = 11.

Ready to change the model?

Explore the model schema