Welcome to LunaModel
High-performance symbolic modeling for optimization problems
LunaModel is a powerful Python library for defining, translating, and transforming optimization problems. Built with Rust for exceptional performance, it provides an intuitive Python API for creating symbolic mathematical models that can be solved using various optimization backends.
Why LunaModel?
- High Performance: Rust-powered core for lightning-fast model operations
- Expressive Syntax: Natural Python syntax for defining optimization models
- Universal Translation: Convert between LP, QUBO, BQM, CQM, and other formats
- Flexible Modeling: Support for expressions of arbitrary degree (linear, quadratic, and higher-order)
- Extensible: Transform models between different representations
- Portable: Built-in serialization for seamless model sharing
Quick Start
Installation
LunaModel can be installed with:
Your First Model
Here's a simple example that demonstrates the core concepts:
from luna_model import Model, Sense, Vtype
from luna_model.utils import quicksum
# Create a model
model = Model(name="Knapsack", sense=Sense.MAX)
# Define problem data
n_items = 5
max_weight = 25
weights = [1.5, 10.0, 5.2, 3.5, 8.32]
values = [10.0, 22.0, 3.2, 1.99, 6.25]
# Add binary variables (1 = take item, 0 = don't take)
items = [
model.add_variable(f"item_{i}", vtype=Vtype.BINARY)
for i in range(n_items)
]
# Set objective: maximize total value
model.objective = quicksum(values[i] * items[i] for i in range(n_items))
# Add constraint: don't exceed weight limit
model.constraints += quicksum(weights[i] * items[i] for i in range(n_items)) <= max_weight
print(model)
That's it! You've created your first optimization model.
Core Concepts
LunaModel is built around four fundamental concepts:
Variables
Decision variables represent unknowns that the optimizer will determine.
# Binary variable (0 or 1)
x = model.add_variable("x", vtype=Vtype.BINARY)
# Integer variable with bounds
y = model.add_variable("y", vtype=Vtype.INTEGER, lower=0, upper=10)
# Continuous variable
z = model.add_variable("z", vtype=Vtype.REAL, lower=-1.5, upper=1.5)
# Spin variable (-1 or +1)
s = model.add_variable("s", vtype=Vtype.SPIN)
Expressions
Mathematical combinations of variables and constants.
Constraints
Restrictions that valid solutions must satisfy.
Documentation Structure
Getting Started
Everything you need to get up and running with LunaModel:
- Installation instructions
- Your first optimization model
- Quick reference guide
User Guide
Comprehensive guides for building optimization models:
- Core Concepts - Understanding models, variables, expressions, and constraints
- Modeling Basics - Building your first complete models
- Working with Expressions - Creating and manipulating expressions
- Working with Solutions - Handling optimization results
Translators
Convert models between different optimization formats:
- Model Translators - LP, QUBO, BQM, CQM format conversion
- Solution Translators - Converting solver results
Transformations
Transform models between different representations:
- Pass Manager - Managing transformation pipelines
- Built-in Passes - Available transformation passes
- Custom Passes - Creating your own transformations
- Advanced Topics - Advanced transformation techniques
API Reference
Complete API documentation for all classes and functions:
- Model - Model, Sense, ModelSpecs, Ctype
- Variable - Variable types and properties
- Expression - Expression building and manipulation
- Constraint - Constraint creation
- Solution - Solution handling
- Translators - Translation API
- Transformations - Transformation API
- Utils - Utility functions
- Environment - Environment configuration
Key Features
Multiple Variable Types
LunaModel supports four variable types to match your problem domain:
| Type | Values | Use Case |
|---|---|---|
Vtype.BINARY |
0 or 1 | Yes/no decisions, selection problems |
Vtype.SPIN |
-1 or +1 | Ising models, quantum optimization |
Vtype.INTEGER |
..., -1, 0, 1, ... | Counting, discrete quantities |
Vtype.REAL |
Real numbers | Continuous optimization |
Universal Model Translation
Seamlessly convert between different optimization formats:
from luna_model.translator import BqmTranslator
from dimod import BinaryQuadraticModel
bqm = BinaryQuadraticModel({"x": -1, "y": 2}, {("x", "y"): 1}, 0.5, "BINARY")
model = BqmTranslator.to_lm(bqm, name="example")
This translator requires luna-model to be installed with the dimod extra.
from luna_model.translator import CqmTranslator
from dimod import ConstrainedQuadraticModel, Binary, Integer
cqm = ConstrainedQuadraticModel()
x = Binary("x")
y = Integer("y", lower_bound=0, upper_bound=10)
cqm.set_objective(x + 2 * y)
cqm.add_constraint(x + y >= 3, label="min_sum")
model = CqmTranslator.to_lm(cqm, name="example")
This translator requires luna-model to be installed with the dimod extra.
Solution Handling
Work with optimization results from any solver:
from luna_model import Solution, Environment, Variable
env = Environment()
with env:
_ = Variable("x")
_ = Variable("y")
# Create solution from solver results
solution = Solution(
samples=[{"x": 1, "y": 0}],
obj_values=[3.0],
feasible=[True],
env=env,
)
# Access solution data
print(f"Best solution: {solution.best()}")
print(f"Objective value: {solution.obj_values[0]}")
Integration with Luna Platform
LunaModel integrates seamlessly with the Luna Platform through luna-quantum:
This gives you access to powerful optimization solvers and quantum computing resources to solve your models.
Common Use Cases
Optimize item selection with constraints:
Assign tasks to resources optimally:
Optimize manufacturing schedules:
Performance Tips
- Use
quicksum()instead of Python'ssum()for large expressions - Set appropriate bounds on INTEGER and REAL variables
- Use the right variable type - BINARY is more efficient than INTEGER with bounds [0, 1]
- Build expressions incrementally for large models
- Leverage transformations to convert to solver-native formats
Getting Help
- Documentation: You're reading it! Explore the sections above
- Discussions: GitHub Discussions for questions and ideas
- Issues: Report bugs or request features
- Platform Docs: Luna Platform Documentation
License
LunaModel is licensed under the Apache License 2.0. See LICENSE for details.
What's Next?
Ready to dive deeper? Here are some suggested paths:
- New to optimization modeling? Start with Getting Started
- Want to understand the fundamentals? Read Core Concepts
- Ready to build models? Try Modeling Basics
- Need to convert models? Check out Model Translators
- Looking for reference docs? Browse the API Reference