Custom data sources

Describes how to provide custom data sources for your model by extending the class IloOplDataSourceBaseII

You can provide OPL models with custom data sources by extending the class IloOplDataSourceBaseI . All you need to do is implement the read method, which will be called by the OPL interpreter as necessary. A custom data source uses the interface IloOplDataHandler to transfer data to the model, and the method getDataHandler to access the associated data handler and send events. The handler API is event-driven, similar to SAX interfaces for XML.

Here are two examples of how these handlers could be used to initialize custom data sources:

Initialization of a multi-dimensional array custom data source


            handler.StartElement("cost") 
            handler.StartIndexedArray() 
            For i = 1 To NUMDEMAND 
                handler.SetItemStringIndex(dtin.Rows(i - 1)(0)) 
                handler.StartIndexedArray() 
                For j = 1 To NUMSUPPLY 
                    handler.SetItemStringIndex(dtin.Columns(j).ColumnName) 
                    handler.AddNumItem(dtin.Rows(i - 1)(j)) 
                Next j 
                handler.EndIndexedArray() 
            Next i 
            handler.EndIndexedArray() 
            handler.EndElement() 

Initialization of a string set custom data source


            handler.StartElement("Plants") 
            handler.StartSet() 
            For j = 1 To NUMSUPPLY 
                handler.AddStringItem(dtin.Columns(j).ColumnName) 
            Next j 
            handler.EndSet() 
            handler.EndElement() 

C++

class MyParams: public IloOplDataSourceBaseI {
    int _nbWarehouses;
    int _nbStores;
    int _fixed;
    IloBool _disaggregate;

    void usage();
public:
    MyParams(IloEnv& env, int argc, char* argv[]);
    void read() const;
};

C++: Providing custom data sources

void MyParams::read() const {
    IloOplDataHandler handler = getDataHandler();

    handler.startElement("nbWarehouses");
    handler.addIntItem(_nbWarehouses);
    handler.endElement();

    handler.startElement("nbStores");
    handler.addIntItem(_nbStores);
    handler.endElement();

    handler.startElement("fixed");
    handler.addIntItem(_fixed);
    handler.endElement();

    handler.startElement("disaggregate");
    handler.addIntItem(_disaggregate);
    handler.endElement();
}

Java

Java: Providing custom data sources

    static class MyParams extends IloCustomOplDataSource
    {
        int _nbWarehouses;
        int _nbStores;
        int _fixed;
        int _disaggregate;

        MyParams(IloOplFactory oplF,int nbWarehouses,int nbStores,int fixed,int disaggregate)
        {
            super(oplF);
            _nbWarehouses = nbWarehouses;
            _nbStores = nbStores;
            _fixed = fixed;
            _disaggregate = disaggregate;
        }

        public void customRead()
        {
            IloOplDataHandler handler = getDataHandler();

            handler.startElement("nbWarehouses");
            handler.addIntItem(_nbWarehouses);
            handler.endElement();

            handler.startElement("nbStores");
            handler.addIntItem(_nbStores);
            handler.endElement();

            handler.startElement("fixed");
            handler.addIntItem(_fixed);
            handler.endElement();

            handler.startElement("disaggregate");
            handler.addIntItem(_disaggregate);
            handler.endElement();
        }
    };

.NET (C#)

Accessing the values of a decision variable within a solution

    internal class MyParams : CustomOplDataSource
    {
        int _nbWarehouses;
        int _nbStores;
        int _fixed;
        int _disaggregate;

        internal MyParams(OplFactory oplF, int nbWarehouses, int nbStores, int fixedP, int disaggregate)
            : base(oplF)
        {
            _nbWarehouses = nbWarehouses;
            _nbStores = nbStores;
            _fixed = fixedP;
            _disaggregate = disaggregate;
        }