Skip to content

Archived LunaModel 0.5.9 documentation

You are reading the old LunaModel 0.5.9 documentation. The regular documentation describes LunaModel >=0.6.0; use the latest documentation unless you explicitly need the old release.

Model Translators

Model translators convert optimization models between LunaModel and various external formats. This enables integration with different solvers and optimization platforms.

Overview

LunaModel provides translators for:

  • LP files
  • MPS files
  • QUBO
  • BQM
  • CQM

LpTranslator

The LpTranslator converts models to and from LP file format.

Basic Usage

Python
from luna_model import Model, Vtype, Sense
from luna_model.utils import quicksum
from luna_model.translator import LpTranslator

# Create a model
model = Model(name="Production", sense=Sense.MAX)

# Add variables
x = model.add_variable("x", vtype=Vtype.INTEGER, lower=0, upper=100)
y = model.add_variable("y", vtype=Vtype.INTEGER, lower=0, upper=50)

# Set objective
model.objective = 3 * x + 2 * y

# Add constraints
model.constraints += 2 * x + y <= 150
model.constraints += x + 2 * y <= 120

# Translate to LP format
lp_string = LpTranslator.from_lm(model)

print(lp_string)

Output:

\Problem name: Production

Maximize
 obj: 3 x + 2 y

Subject To
 c0: 2 x + y <= 150
 c1: x + 2 y <= 120

Bounds
 0 <= x <= 100
 0 <= y <= 50

General
 x y

End

Importing from LP

Python
# Read LP file and convert to LunaModel
from pathlib import Path

model = LpTranslator.to_lm(Path("path/to/lp/file.lp"))

# Use the imported model
print(f"Imported model: {model.name}")
print(f"Variables: {model.num_variables()}")
print(f"Constraints: {model.num_constraints()}")

Exporting to File

Python
# Export to LP file
LpTranslator.from_lm(model, filepath=Path("path/to/lp/file.lp"))

Supported Features

  • Linear and quadratic objectives
  • Linear and quadratic constraints
  • Binary, integer, and continuous variables
  • Minimization and maximization
  • Variable bounds

Limitations

  • Does not support higher-order (degree > 2) expressions
  • Spin variables are converted to binary

MpsTranslator

The MpsTranslator converts models to and from the MPS file format.

Basic Usage

Python
from luna_model import Model, Vtype, Sense
from luna_model.utils import quicksum
from luna_model.translator import MpsTranslator

# Create a model
model = Model(name="Production", sense=Sense.MAX)

# Add variables
x = model.add_variable("x", vtype=Vtype.INTEGER, lower=0, upper=100)
y = model.add_variable("y", vtype=Vtype.INTEGER, lower=0, upper=50)

# Set objective
model.objective = 3 * x + 2 * y

# Add constraints
model.constraints += 2 * x + y <= 150
model.constraints += x + 2 * y <= 120

# Translate to MPS format
mps_string = MpsTranslator.from_lm(model)

print(mps_string)

Output:

``NAME          Production
OBJSENSE
 MAX
ROWS
 N  OBJ
 L  c0
 L  c1
COLUMNS
    MARK0000  'MARKER'                 'INTORG'
    x  OBJ  3.0
    x  c0  2.0
    x  c1  1.0
    y  OBJ  2.0
    y  c0  1.0
    y  c1  2.0
    MARK0000  'MARKER'                 'INTEND'
RHS
    RHS1      c0  150.0
    RHS1      c1  120.0
BOUNDS
 LI BND1      x  0.0
 UI BND1      x  100.0
 LI BND1      y  0.0
 UI BND1      y  50.0
ENDATA

Importing from MPS

Python
from pathlib import Path
# Read MPS file and convert to LunaModel
model = MpsTranslator.to_lm(Path("path/to/mps/file.mps"))

# Use the imported model
print(f"Imported model: {model.name}")
print(f"Variables: {model.num_variables()}")
print(f"Constraints: {model.num_constraints()}")

Exporting to File

Python
# Export to MPS file
MpsTranslator.from_lm(model, filepath=Path("path/to/mps/file.mps"))

Supported Features

  • Linear and quadratic objectives
  • Linear and quadratic constraints
  • Binary, integer, and continuous variables
  • Minimization and maximization
  • Variable bounds

Limitations

  • Does not support higher-order (degree > 2) expressions
  • Spin variables are converted to binary

QuboTranslator

The QuboTranslator converts models to and from QUBO (Quadratic Unconstrained Binary Optimization) format.

Basic Usage

Python
from luna_model import Model, Vtype
from luna_model.translator import QuboTranslator

# Create a binary quadratic model
model = Model()

# Binary variables only
x = model.add_variable("x", vtype=Vtype.BINARY)
y = model.add_variable("y", vtype=Vtype.BINARY)
z = model.add_variable("z", vtype=Vtype.BINARY)

# Quadratic objective
model.objective = x * y + 2 * y * z - x - 2 * y

# Translate to QUBO
qubo = QuboTranslator.from_lm(model)

print(qubo)
# {(0, 1): 1.0, (1, 2): 2.0, (0, 0): -1.0, (1, 1): -2.0}

QUBO Format

QUBO is represented as a dictionary where: - Keys are tuples (i, j) representing variable indices - Values are coefficients - Diagonal terms (i, i) are linear coefficients - Off-diagonal terms (i, j) where i < j are quadratic coefficients

Importing from QUBO

Python
# QUBO dictionary
qubo_dict = {
    (0, 0): -1.0,  # Linear term for variable 0
    (1, 1): -2.0,  # Linear term for variable 1
    (0, 1): 1.0,   # Quadratic term x0*x1
}

# Variable names
var_names = ["x", "y"]

# Convert to LunaModel
model = QuboTranslator.to_lm(qubo_dict, variable_names=var_names)

Constraint Handling

QUBO is unconstrained, so constraints must be penalized into the objective:

Python
# Model with constraint
model = Model()
x = model.add_variable("x", vtype=Vtype.BINARY)
y = model.add_variable("y", vtype=Vtype.BINARY)

model.objective = x + y
model.constraints += x + y <= 1  # Constraint

# Translate with penalty
translator = QuboTranslator(penalty_weight=10.0)
qubo = translator.from_lm(model)

# Constraint is penalized in objective

Supported Features

  • Binary variables only
  • Quadratic objectives
  • Constraint penalization

Limitations

  • Only binary variables supported
  • Constraints converted to penalties
  • Higher-order terms not supported

BqmTranslator

The BqmTranslator converts to and from D-Wave's Binary Quadratic Model format (requires dimod).

Installation

pip install luna-model[dimod]

Basic Usage

Python
from luna_model import Model, Vtype
from luna_model.translator import BqmTranslator

# Create model
model = Model()
x = model.add_variable("x", vtype=Vtype.BINARY)
y = model.add_variable("y", vtype=Vtype.BINARY)

model.objective = x * y - x - 2 * y + 3

# Convert to BQM
bqm = BqmTranslator.from_lm(model)

print(f"BQM type: {type(bqm)}")
print(f"Linear: {bqm.linear}")
print(f"Quadratic: {bqm.quadratic}")
print(f"Offset: {bqm.offset}")

Integration with D-Wave

Python
from dimod import ExactSolver
from luna_model.translator import BqmTranslator, DwaveTranslator

# Create and translate model
model = Model()
# ... define model ...

# To BQM
bqm = BqmTranslator.from_lm(model)

# Solve with D-Wave sampler
sampler = ExactSolver()
sampleset = sampler.sample(bqm)

# Convert solution back
solution = DwaveTranslator.to_lm(sampleset)

print(f"Best solution: {solution.best()}")
print(f"Best energy: {min(solution.obj_values)}")

Importing from BQM

Python
from dimod import BinaryQuadraticModel

# Create a BQM
bqm = BinaryQuadraticModel(
    {'x': -1, 'y': -2},      # Linear terms
    {('x', 'y'): 1},         # Quadratic terms
    0.0,                      # Offset
    'BINARY'
)

# Convert to LunaModel
model = BqmTranslator.to_lm(bqm)

Supported Features

  • Binary and spin variables
  • Quadratic objectives
  • Full dimod BQM compatibility

Limitations

  • Requires dimod package
  • Constraints not supported (use CQM for constraints)

CqmTranslator

The CqmTranslator converts to and from D-Wave's Constrained Quadratic Model format.

Installation

pip install luna-model[dimod]

Basic Usage

Python
from luna_model import Model, Vtype, Sense
from luna_model.utils import quicksum
from luna_model.translator import CqmTranslator

# Create model with constraints
model = Model(sense=Sense.MIN)

# Variables
x = [model.add_variable(f"x_{i}", vtype=Vtype.BINARY) for i in range(5)]

# Objective
model.objective = quicksum(i * x[i] for i in range(5))

# Constraints
model.constraints += quicksum(x) <= 2
model.constraints += x[0] + x[1] >= 1

# Convert to CQM
cqm = CqmTranslator.from_lm(model)

print(f"CQM variables: {len(cqm.variables)}")
print(f"CQM constraints: {len(cqm.constraints)}")

Integration with D-Wave Hybrid

Python
from dwave.system import LeapHybridCQMSampler
from luna_model.translator import CqmTranslator, DwaveTranslator

# Create model
model = Model()
# ... define constrained model ...

# Convert to CQM
cqm = CqmTranslator.from_lm(model)

# Solve with hybrid sampler
sampler = LeapHybridCQMSampler()
sampleset = sampler.sample_cqm(cqm)

# Convert solution
solution = DwaveTranslator.to_lm(sampleset)

Importing from CQM

Python
from dimod import ConstrainedQuadraticModel, Binary

# Create CQM
cqm = ConstrainedQuadraticModel()
x = Binary('x')
y = Binary('y')

cqm.set_objective(x + 2*y)
cqm.add_constraint(x + y <= 1, label="c0")

# Convert to LunaModel
model = CqmTranslator.to_lm(cqm)

Supported Features

  • Binary, integer, and continuous variables
  • Quadratic objectives
  • Quadratic constraints
  • Full dimod CQM compatibility

Limitations

  • Requires dimod package
  • Higher-order terms not supported

Handle Errors

Python
from luna_model.errors import TranslationError

try:
    qubo = QuboTranslator.from_lm(model)
except TranslationError as e:
    print(f"Translation failed: {e}")
    print("Model may have unsupported features")

See Also