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(),
... metrics=nextroute_output.metrics.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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 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 | |
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,
... metrics={"processing_time": 0.5}
... )