Model Module¶
This section documents the model components of the Nextmv Python SDK.
model
¶
Model module for creating and saving decision models in Nextmv Cloud.
This module provides the base classes and functionality for creating decision models that can be deployed and run in Nextmv Cloud. The main components are:
CLASS | DESCRIPTION |
---|---|
Model |
Base class for defining decision models. |
ModelConfiguration |
Configuration for packaging and deploying models. |
Models defined using this module can be packaged with their dependencies and |
|
deployed to Nextmv Cloud for execution. |
|
Model
¶
Base class for defining decision models that run in Nextmv Cloud.
You can import the Model
class directly from nextmv
:
This class serves as a foundation for creating decision models that can be
deployed to Nextmv Cloud. Subclasses must implement the solve
method,
which is the main entry point for processing inputs and producing decisions.
METHOD | DESCRIPTION |
---|---|
solve |
Process input data and produce a decision output. |
save |
Save the model to the filesystem for deployment. |
Examples:
>>> import nextroute
>>> import nextmv
>>>
>>> class DecisionModel(nextmv.Model):
... def solve(self, input: nextmv.Input) -> nextmv.Output:
... nextroute_input = nextroute.schema.Input.from_dict(input.data)
... nextroute_options = nextroute.Options.extract_from_dict(input.options.to_dict())
... nextroute_output = nextroute.solve(nextroute_input, nextroute_options)
...
... return nextmv.Output(
... options=input.options,
... solution=nextroute_output.solutions[0].to_dict(),
... statistics=nextroute_output.statistics.to_dict(),
... )
save
¶
save(
model_self,
model_dir: str,
configuration: ModelConfiguration,
) -> None
Save the model to the local filesystem for deployment.
This method packages the model according to the provided configuration, creating all necessary files and dependencies for deployment to Nextmv Cloud.
PARAMETER | DESCRIPTION |
---|---|
|
The directory where the model will be saved.
TYPE:
|
|
The configuration of the model, which defines how the model is saved and loaded.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ImportError
|
If mlflow is not installed, which is required for model packaging. |
Notes
This method uses mlflow for model packaging, creating the necessary files and directory structure for deployment.
Examples:
>>> model = MyDecisionModel()
>>> config = ModelConfiguration(
... name="routing_model",
... requirements=["pandas", "numpy"]
... )
>>> model.save("/tmp/my_model", config)
Source code in nextmv/nextmv/model.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
|
solve
¶
solve(input: Input) -> Output
Process input data and produce a decision output.
This is the main entry point of your model that you must implement in subclasses. It receives input data and should process it to produce an output containing the solution to the decision problem.
PARAMETER | DESCRIPTION |
---|---|
|
The input data that the model will use to make a decision.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Output
|
The output of the model, which is the solution to the decision model/problem. |
RAISES | DESCRIPTION |
---|---|
NotImplementedError
|
When called on the base Model class, as this method must be implemented by subclasses. |
Examples:
>>> def solve(self, input: Input) -> Output:
... # Process input data
... result = self._process_data(input.data)
...
... # Return formatted output
... return Output(
... options=input.options,
... solution=result,
... statistics={"processing_time": 0.5}
... )
Source code in nextmv/nextmv/model.py
ModelConfiguration
dataclass
¶
ModelConfiguration(
name: str,
requirements: Optional[list[str]] = None,
options: Optional[Options] = None,
)
Configuration class for Nextmv models.
You can import the ModelConfiguration
class directly from nextmv
:
This class holds the configuration for a model, defining how a Python model is encoded and loaded for use in Nextmv Cloud.
PARAMETER | DESCRIPTION |
---|---|
|
A personalized name for the model. This is required.
TYPE:
|
|
A list of Python dependencies that the decision model requires, formatted as they would appear in a requirements.txt file.
TYPE:
|
|
Options that the decision model requires.
TYPE:
|
Examples:
>>> from nextmv import ModelConfiguration, Options
>>> config = ModelConfiguration(
... name="my_routing_model",
... requirements=["nextroute>=1.0.0"],
... options=Options({"max_time": 60})
... )