Skip to content

Luna Model

An extended version of the luna_model.Model with additional methods (upload and download functionality) for integration with LunaSolve and the Luna Platform.

A symbolic optimization model combining an objective and constraints.

The Model class represents a structured symbolic optimization problem. It contains an objective, a collection of Constraint objects, and an Environment that scopes all variables used in the model.

Models can be constructed implicitly by allowing the model to create its own private environment, or explicitly by passing an environment. If constructed inside an active Environment context, that context is used automatically.

Parameters:

  • name (str, default: None ) –

    An optional name assigned to the model for identification and debugging.

  • sense (Sense, default: Sense.MIN ) –

    The optimization sense - Sense.MIN to minimize or Sense.MAX to maximize the objective function.

  • env (Environment, default: None ) –

    The environment in which variables and expressions are created. If not provided, the model will either use the current context (if active), or create a new private environment.

Attributes:

  • name (str) –

    The name of the model.

  • sense (Sense) –

    The optimization sense (MIN or MAX).

  • objective (Expression) –

    The objective function to optimize.

  • constraints (ConstraintCollection) –

    Collection of constraints that must be satisfied.

  • environment (Environment) –

    The environment containing all variables.

  • num_variables (int) –

    Number of variables in the model.

  • num_constraints (int) –

    Number of constraints in the model.

Examples:

Basic usage:

>>> from luna_model import Model, Variable, Sense
>>> model = Model("MyModel", sense=Sense.MAX)
>>> x = model.add_variable("x")
>>> y = model.add_variable("y")
>>> model.objective = x * y + x
>>> model.constraints += x >= 0
>>> model.constraints += y <= 5
>>> print(model)
Model: MyModel
Maximize
  x * y + x
Subject To
  c0: x >= 0
  c1: y <= 5
Binary
  x y

With explicit environment:

>>> from luna_model import Environment
>>> env = Environment()
>>> model = Model("ScopedModel", env=env)
>>> x = model.add_variable("x")
>>> model.objective = x * x
>>> print(model)
Model: ScopedModel
Minimize
  x
Binary
  x

Serialization:

>>> blob = model.encode()
>>> restored = Model.decode(blob)
>>> print(restored.name)
ScopedModel