Skip to main content

Pure Python optimization solvers. No dependencies, no nonsense.

Project description

Solvor

Python 3.14+ License: MIT

Pure Python optimization solvers. No dependencies, no nonsense.

What's in the box?

Category Solvers Use Case
Linear/Integer solve_lp, solve_milp Resource allocation, scheduling
Constraint solve_sat, Model Sudoku, configuration, puzzles
Local Search anneal, tabu_search TSP, combinatorial optimization
Population evolve When you want nature to do the work
Continuous gradient_descent, momentum, adam ML, curve fitting
Black-box bayesian_opt Hyperparameter tuning, expensive functions
Graph max_flow, min_cost_flow, solve_assignment Matching, transportation

Quickstart

uv add solvor
from solvor import solve_lp, solve_tsp, anneal, Model

# Linear Programming
result = solve_lp(c=[1, 2], A=[[1, 1], [2, 1]], b=[4, 5])
print(result.solution)  # optimal x

# TSP with tabu search
distances = [[0, 10, 15], [10, 0, 20], [15, 20, 0]]
result = solve_tsp(distances)
print(result.solution)  # best tour found

# Constraint satisfaction
m = Model()
x = m.int_var(1, 9, 'x')
y = m.int_var(1, 9, 'y')
m.add(m.all_different([x, y]))
m.add(m.sum_eq([x, y], 10))
result = m.solve()
print(result.solution)  # {'x': 3, 'y': 7}

Solvers

Linear & Integer Programming

solve_lp

Two-phase simplex for linear programming.

# minimize 2x + 3y subject to x + y >= 4, x <= 3
result = solve_lp(
    c=[2, 3],
    A=[[-1, -1], [1, 0]],  # constraints as Ax <= b
    b=[-4, 3]
)

solve_milp

Branch and bound for mixed-integer problems.

# same as above, but x must be integer
result = solve_milp(c=[2, 3], A=[[-1, -1], [1, 0]], b=[-4, 3], integers=[0])
Constraint Programming

solve_sat

DPLL with unit propagation and clause learning.

# (x1 OR x2) AND (NOT x1 OR x3) AND (NOT x2 OR NOT x3)
result = solve_sat([[1, 2], [-1, 3], [-2, -3]])
print(result.solution)  # {1: True, 2: False, 3: True}

Model (CP-SAT)

Constraint programming via SAT encoding.

m = Model()
cells = [[m.int_var(1, 9, f'c{i}{j}') for j in range(9)] for i in range(9)]

# All different in each row
for row in cells:
    m.add(m.all_different(row))

result = m.solve()
Metaheuristics

anneal

Simulated annealing - accepts worse solutions probabilistically.

result = anneal(
    initial=initial_solution,
    objective_fn=cost_function,
    neighbors=random_neighbor,
    temperature=1000,
    cooling=0.9995
)

tabu_search

Maintains a "tabu list" of recent moves to escape local optima.

result = tabu_search(
    initial=initial_solution,
    objective_fn=cost_function,
    neighbors=get_neighbors,  # returns [(move, solution), ...]
    cooldown=10
)

evolve

Genetic algorithm with selection, crossover, mutation.

result = evolve(
    objective_fn=fitness,
    population=initial_pop,
    crossover=my_crossover,
    mutate=my_mutate,
    max_gen=100
)
Continuous Optimization

gradient_descent / momentum / adam

First-order methods for differentiable functions.

def grad_fn(x):
    return [2 * x[0], 2 * x[1]]  # gradient of x^2 + y^2

result = adam(grad_fn, x0=[5.0, 5.0])
print(result.solution)  # [~0, ~0]

bayesian_opt

Gaussian process surrogate for expensive black-box functions.

def expensive_fn(x):
    # imagine this takes 10 minutes to evaluate
    return (x[0] - 0.3)**2 + (x[1] - 0.7)**2

result = bayesian_opt(expensive_fn, bounds=[(0, 1), (0, 1)], max_iter=30)
Network Flow

max_flow

Ford-Fulkerson with BFS (Edmonds-Karp).

graph = {
    's': [('a', 10, 0), ('b', 5, 0)],
    'a': [('b', 15, 0), ('t', 10, 0)],
    'b': [('t', 10, 0)],
    't': []
}
result = max_flow(graph, 's', 't')
print(result.objective)  # total flow
print(result.solution)   # edge flows dict

min_cost_flow / solve_assignment

Successive shortest paths for minimum cost flow.

# Assignment problem: 3 workers, 3 tasks
costs = [
    [10, 5, 13],
    [3, 9, 18],
    [10, 6, 12]
]
result = solve_assignment(costs)
# result.solution[i] = task assigned to worker i
# result.objective = total cost

Result Format

All solvers return a consistent Result namedtuple:

Result(
    solution,     # best solution found
    objective,    # objective value
    iterations,   # solver iterations (pivots, generations, etc.)
    evaluations,  # function evaluations
    status        # OPTIMAL, FEASIBLE, INFEASIBLE, UNBOUNDED, MAX_ITER
)

When to use what?

Problem Solver
Linear constraints, continuous variables solve_lp
Linear constraints, some integers solve_milp
Boolean satisfiability solve_sat
Discrete variables, complex constraints Model
Combinatorial, good initial solution tabu_search, anneal
Combinatorial, no clue where to start evolve
Smooth, differentiable adam
Expensive black-box bayesian_opt
Assignment, matching, flow max_flow, solve_assignment

Philosophy

  1. Pure Python - no numpy, no scipy, no compiled extensions
  2. Readable - each solvor fits in one file you can actually read
  3. Consistent - same Result format, same minimize/maximize convention
  4. Practical - solves real problems, or AoC puzzles

Contributing

See CONTRIBUTING.md for development setup and guidelines.

License

MIT License - free for personal and commercial use.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

solvor-0.3.1.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

solvor-0.3.1-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file solvor-0.3.1.tar.gz.

File metadata

  • Download URL: solvor-0.3.1.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for solvor-0.3.1.tar.gz
Algorithm Hash digest
SHA256 469fc9f0d64b6ae66b70f8a2eaef552a4edc8d124fe2304abeab6499bac577a7
MD5 cad3c793dddb5cbe2f50a7bcb10a43d9
BLAKE2b-256 e2e2ebe83516deb6634871906946f34b327a391e5ab31e4e9f00e2e47cd79af9

See more details on using hashes here.

Provenance

The following attestation bundles were made for solvor-0.3.1.tar.gz:

Publisher: publish.yml on StevenBtw/solvor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file solvor-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: solvor-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 27.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for solvor-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e1f51c9e719e4413866025317f54415c7b7f22ab80c9a3468874db58adef37a
MD5 43b16c41b542b813682629c4ee1dab94
BLAKE2b-256 042e4b74e41e05ca90b2209fa1c504456c701e35b72f5e5df65fe88f6d36237d

See more details on using hashes here.

Provenance

The following attestation bundles were made for solvor-0.3.1-py3-none-any.whl:

Publisher: publish.yml on StevenBtw/solvor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page