Referencing imported data and defining solution output in a model
Last updated: Aug 06, 2024
Referencing imported data in a model in a Decision Optimization experiment
For Python DOcplex or OPL models, you can refer to imported data by
using the following syntax.
Python models
Copy link to section
For Python DOcplex models, the data that you imported in the Prepare dataview in the experiment UI is accessible from the input dictionary. To
access your imported data you must define each table with the following syntax
inputs['tablename']. For example, in this line of code, food is an entity that is
defined from the table called diet_food:
food = inputs['diet_food']
Copy to clipboardCopied to clipboard
Similarly, to show tables in the Explore solution view of the experiment UI you must specify them using the syntax
outputs['tablename']. For example,
outputs['solution'] = solution_df
Copy to clipboardCopied to clipboard
defines an output table that is called
solution. The entity solution_df in the Python model defines this
table.
In an OPL model you must declare a tupleset, for each table that you imported in
the Prepare dataview and with the same names. The schema for
each tupleset must have the same number of columns as the table and use the same field names. For
example, if you have an input table in your Prepare dataview called Product with the
attributes name, demand, insideCost, and outsideCost, your OPL
model must contain the following definition:
The limitation on only using tuples and tuple sets as OPL input is to facilitate
integration with data sources. For example, SQL data sources can be accessed and data-streamed with
a minimum of effort; NoSQL data sources can be accessed and data can be transformed automatically to
tables. If necessary, the optimization model developer can reformulate the data to populate other
data structures during the optimization, but this manipulation must not affect the input or output
data.
Similarly, if you want to display a table in the Explore solutionview, you must define a
tupleset for this output table in your OPL model. For example, this code produces
an output table with 3 columns in the
solution.
/// solution
tuple TPlannedProduction {
key string productId;
float insideProduction;
float outsideProduction;
}
{TPlannedProduction} plan = {<p.name, Inside[p], Outside[p]> | p in Products};
Copy to clipboardCopied to clipboard
You can find this example OPL model for a pasta production problem in the
Model_Builder folder of the DO-samples. You can
download and extract all the samples. Select the relevant product and version subfolder.