Solvor your optimization needs.
Project description
Solvor
Solvor your optimization needs..
What's in the box?
| Category | Solvers | Use Case |
|---|---|---|
| Linear/Integer | solve_lp, solve_milp |
Resource allocation, scheduling |
| Constraint | solve_sat |
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 |
| Exact Cover | solve_exact_cover |
Sudoku, N-Queens, tiling puzzles |
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
For resource allocation, blending, production planning. Finds the exact optimum for linear objectives with linear constraints.
# 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
When some variables must be integers. Diet problems, scheduling with discrete slots, set covering.
# 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
For "is this configuration valid?" problems. Dependencies, exclusions, implications - anything that boils down to boolean constraints.
# (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)
For puzzles and scheduling with "all different", arithmetic, and logical constraints. Sudoku, N-Queens, timetabling.
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
Greedy local search with memory. Prevents cycling back to recent solutions, forcing exploration of new territory. More deterministic than anneal.
result = tabu_search(
initial=initial_solution,
objective_fn=cost_function,
neighbors=get_neighbors, # returns [(move, solution), ...]
cooldown=10
)
evolve
Population-based search. More overhead than anneal/tabu, but better diversity and parallelizable.
result = evolve(
objective_fn=fitness,
population=initial_pop,
crossover=my_crossover,
mutate=my_mutate,
max_gen=100
)
Continuous Optimization
gradient_descent / momentum / adam
Follow the slope downhill. Great for polishing solutions from other methods if your objective is differentiable. Adam adapts learning rates per parameter - usually the default choice.
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
When each evaluation is expensive (think hyperparameter tuning, simulations). Builds a surrogate model to guess where to sample next instead of brute-forcing.
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
"How much can I push through this network?" Assigning workers to tasks, finding bottlenecks. The max-flow min-cut theorem gives you bottleneck analysis for free.
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
"What's the cheapest way to route X units?" Transportation, logistics, matching with costs.
# 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
Exact Cover
solve_exact_cover
For "place these pieces without overlap" or "fill this grid with exactly one of each" problems. Sudoku, pentomino tiling, scheduling where every slot must be filled exactly once.
# Tiling problem: cover all columns with non-overlapping rows
matrix = [
[1, 1, 0, 0], # row 0 covers columns 0, 1
[0, 1, 1, 0], # row 1 covers columns 1, 2
[0, 0, 1, 1], # row 2 covers columns 2, 3
[1, 0, 0, 1], # row 3 covers columns 0, 3
]
result = solve_exact_cover(matrix)
# result.solution = (0, 2) or (1, 3) - rows that cover all columns exactly once
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 |
| Exact cover, tiling, N-Queens | solve_exact_cover |
Philosophy
- Pure Python - no numpy, no scipy, no compiled extensions
- Readable - each solvor fits in one file you can actually read
- Consistent - same Result format, same minimize/maximize convention
- Practical - solves real problems, or AoC puzzles
Contributing
See CONTRIBUTING.md for development setup and guidelines.
License
Apache 2.0 License - free for personal and commercial use.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file solvor-0.3.3.tar.gz.
File metadata
- Download URL: solvor-0.3.3.tar.gz
- Upload date:
- Size: 28.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d557a1c17ccf0109adfe4ed903f76a3e2d3484526702e30adc782bca76ab726
|
|
| MD5 |
fad53fac609be3844de228132e573357
|
|
| BLAKE2b-256 |
f5d09e4d99bf33663f0c338228c8af4bea294431ef99d079eaa371adaac8b416
|
Provenance
The following attestation bundles were made for solvor-0.3.3.tar.gz:
Publisher:
publish.yml on StevenBtw/solvor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
solvor-0.3.3.tar.gz -
Subject digest:
4d557a1c17ccf0109adfe4ed903f76a3e2d3484526702e30adc782bca76ab726 - Sigstore transparency entry: 771778067
- Sigstore integration time:
-
Permalink:
StevenBtw/solvor@4e673b50eba5c25629120f6a7c5ae8e45637d6a2 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/StevenBtw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4e673b50eba5c25629120f6a7c5ae8e45637d6a2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file solvor-0.3.3-py3-none-any.whl.
File metadata
- Download URL: solvor-0.3.3-py3-none-any.whl
- Upload date:
- Size: 30.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa90a8ece28a77c052a6652346a3edf8441d185090e52521faeba49ffe9e519f
|
|
| MD5 |
08e43b926762cbd2f64cb6cb7f3cac53
|
|
| BLAKE2b-256 |
1d159721c2c6c1919a44ba7167359785d0a202f8906c11a69335ed7ea1c74e3f
|
Provenance
The following attestation bundles were made for solvor-0.3.3-py3-none-any.whl:
Publisher:
publish.yml on StevenBtw/solvor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
solvor-0.3.3-py3-none-any.whl -
Subject digest:
fa90a8ece28a77c052a6652346a3edf8441d185090e52521faeba49ffe9e519f - Sigstore transparency entry: 771778081
- Sigstore integration time:
-
Permalink:
StevenBtw/solvor@4e673b50eba5c25629120f6a7c5ae8e45637d6a2 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/StevenBtw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4e673b50eba5c25629120f6a7c5ae8e45637d6a2 -
Trigger Event:
release
-
Statement type: