0 / 0
Code snippets for building models
Last updated: Mar 05, 2025
Code snippets for building models in a Decision Optimization experiment

For Python DOcplex or OPL models, you can select code snippets to help you build a model. Using code snippets can make model building faster, as you can add and edit code without having to enter all the lines of code from scratch.

In the Build model view, click the code snippets icon Code snippets icon to open the code snippets pane. You can then choose to preview and add different code snippets to your model. The types of code snippets that are available depend on the modeling language (Python DOcplex or OPL) and the model type (mathematical programming or constraint programming models) that you are using. For mathematical programming models, the Optimizer is IBM CPLEX, and for constraint programming it is CP Optimizer.

You can preview the code snippet and then insert it into your model wherever your cursor is, or copy and paste it into a specific place in your model.

Model skeleton

The Model skeleton code snippet provides you with a model structure to get you started quickly. When you open a Model skeleton, you must select the appropriate Optimizer for your model type. Other options are available depending on your modeling language.

Python models

The Data frames code snippet automatically uses the correct table names that are found in the Prepare data view tables to define inputs. An empty output data frame is also provided for you to define your output for the Explore solution view.

You can use the Progress listener code snippet to create a listener to monitor the solution progress, capture intermediate solutions, and stop the solve based on your own criteria. This code snippet also provides you with the code necessary to create output solution tables in the Explore solution view.

OPL models

The Input tuples code snippet automatically uses the correct table names and data types that are found in the Prepare data view tables, so you can define your model input more quickly and easily. You can also define the Tuple name prefix that you want to use in your data definitions.

Learn more