Initializing help system before first use

Data input/output via I/O drivers


Type: Programming
Rating: 3 (intermediate)
Description: Use I/O drivers to handle Mosel output with a callback function, compile a model from memory to memory, load a bim file from memory, initialise arrays in the Model program from .NET objects and retrieve information from the model through memory.
  • ExDrvsCallback.vb: using 'dotnet:' I/O driver for data exchange via callbacks
  • ExDrvsRaw.vb: using 'dotnetraw:' I/O driver for data exchange in memory
  • ExDrvsStream.vb: using 'dotnetstream:' I/O driver working with streams for data exchange in memory
These .vb files can be run from the VB.NET project Mosel-VB.NET.vbproj (required auxiliary files: AssemblyInfo.vb, frmMain.vb, frmMain.resx, OptimizerLog.vb).
File(s): ExDrvsCallback.vb, ExDrvsRaw.vb, ExDrvsStream.vb

ExDrvsCallback.vb
'********************************************************
'*  Mosel Library Examples                              *
'*  ======================                              *
'*                                                      *
'*  file ExDrvsCallback.vb                              *
'*  ```````````````````                                 *
'*  Example for the use of the Mosel libraries          *
'*  (using dotnet: I/O driver for data exchange via     *
'*  callbacks                                           *
'*                                                      *
'*  (c) 2011 Fair Isaac Corporation                     *
'*      author: J.Farmer, Y. Colombani, 2011            *
'********************************************************

Imports System
Imports System.IO
Imports Mosel

Module ExDrvsCallback

    Private OutputLog As TextWriter

    ' Define the Mosel source of our model
    Private source_of_model As String = _
      "model tstcb" & vbCrLf & _
      "uses 'mmsystem'" & vbCrLf & _
      "parameters" & vbCrLf & _
      " ICB_INITFROM=''" & vbCrLf & _
      " ICB_INITTO=''" & vbCrLf & _
      "end-parameters" & vbCrLf & _
 _
      "declarations" & vbCrLf & _
      " v_i:integer" & vbCrLf & _
      " v_r:real" & vbCrLf & _
      " v_s:string" & vbCrLf & _
      " v_b:boolean" & vbCrLf & _
      " v_d:date" & vbCrLf & _
 _
      " s_i:set of integer" & vbCrLf & _
      " l_i:list of integer" & vbCrLf & _
 _
      " s_d:set of date" & vbCrLf & _
      " l_d:list of date" & vbCrLf & _
 _
      " a_i:array(range) of integer" & vbCrLf & _
      " Rx:range" & vbCrLf & _
      " a_s:array(Rx) of string" & vbCrLf & _
      " a_r:array(Rx) of real" & vbCrLf & _
 _
      " R=record" & vbCrLf & _
      "    i:integer" & vbCrLf & _
      "    s:set of integer" & vbCrLf & _
      "   end-record" & vbCrLf & _
      " r:R" & vbCrLf & _
      " a_R:array(range) of R" & vbCrLf & _
      "end-declarations" & vbCrLf & _
 _
      "initialisations from ICB_INITFROM" & vbCrLf & _
      " v_i" & vbCrLf & _
      " v_r" & vbCrLf & _
      " v_s" & vbCrLf & _
      " v_b" & vbCrLf & _
      " v_d" & vbCrLf & _
 _
      " s_i" & vbCrLf & _
      " l_i" & vbCrLf & _
 _
      " s_d" & vbCrLf & _
      " l_d" & vbCrLf & _
 _
      " a_i" & vbCrLf & _
      " [a_s,a_r] as 'ax'" & vbCrLf & _
 _
      " r" & vbCrLf & _
      " a_R" & vbCrLf & _
      "end-initialisations" & vbCrLf & _
 _
      " writeln('v_i=',v_i)" & vbCrLf & _
      " writeln('v_r=',v_r)" & vbCrLf & _
      " writeln('v_s=',v_s)" & vbCrLf & _
      " writeln('v_b=',v_b)" & vbCrLf & _
      " writeln('v_d=',v_d)" & vbCrLf & _
      " writeln('s_i=',s_i)" & vbCrLf & _
      " writeln('l_i=',l_i)" & vbCrLf & _
      " writeln('s_d=',s_d)" & vbCrLf & _
      " writeln('l_d=',l_d)" & vbCrLf & _
      " writeln('a_i=',a_i)" & vbCrLf & _
      " writeln('a_r=',a_r)" & vbCrLf & _
      " writeln('a_s=',a_s)" & vbCrLf & _
      " writeln('r=',r)" & vbCrLf & _
      " writeln('a_R=',a_R)" & vbCrLf & _
 _
      "initialisations to ICB_INITTO" & vbCrLf & _
      " v_i" & vbCrLf & _
      " v_r" & vbCrLf & _
      " v_s" & vbCrLf & _
      " v_b" & vbCrLf & _
      " v_d" & vbCrLf & _
 _
      " s_i" & vbCrLf & _
      " l_i" & vbCrLf & _
 _
      " s_d" & vbCrLf & _
      " l_d" & vbCrLf & _
 _
      " a_i" & vbCrLf & _
 _
      " r" & vbCrLf & _
      " a_R" & vbCrLf & _
      "end-initialisations" & vbCrLf & _
      "end-model"


    ' A function to initialize the Mosel data-structures via callback
    Private Function initializeFrom(ByVal ictx As XPRMInitializeContext, ByVal label As String, ByVal type As XPRMTyped) As Boolean

        Try

            Select Case label
                Case "v_i" ' v_i:999
                    ictx.Send(999)
                    Return True

                Case "v_r" ' v_r:999.99
                    ictx.Send(999.99)
                    Return True

                Case "v_b" ' v_b:false
                    ictx.Send(False)
                    Return True

                Case "v_s" ' v_s:"tralala"
                    ictx.Send("tralala")
                    Return True

                Case "v_d" ' v_d:"2012-12-12"
                    ictx.Send("2012-12-12")
                    Return True

                Case "s_i", "l_i" ' s_d:[10 20 30 ... ]
                    ictx.Send(XPRMInitializeControl.OpenList)
                    For i As Integer = 1 To 10
                        ictx.Send(i * 10)
                    Next
                    ictx.Send(XPRMInitializeControl.CloseList)
                    Return True

                Case "s_d", "l_d" ' s_d:["2001-01-11" "2002-02-21" ... ]
                    ictx.Send(XPRMInitializeControl.OpenList)
                    For i As Integer = 1 To 10
                        ictx.Send(String.Format("{0}-{1}-{2}", 2000 + i, i, i + 1))
                    Next
                    ictx.Send(XPRMInitializeControl.CloseList)
                    Return True

                Case "a_i" '  // a_i:[ (1) 10 (2) 20 ... ]
                    ictx.Send(XPRMInitializeControl.OpenList)
                    For i As Integer = 1 To 10
                        ictx.Send(XPRMInitializeControl.OpenIndices)
                        ictx.Send(i)
                        ictx.Send(XPRMInitializeControl.CloseIndices)
                        ictx.Send(i * 10)
                    Next
                    ictx.Send(XPRMInitializeControl.CloseList)
                    Return True

                Case "ax" '   ax:[ (1) [ "aa1" 1.23 ] (2) [ "aa2" 2.46 ] ... ]
                    ictx.Send(XPRMInitializeControl.OpenList)
                    For i As Integer = 1 To 10
                        ictx.Send(XPRMInitializeControl.OpenIndices)
                        ictx.Send(i)
                        ictx.Send(XPRMInitializeControl.CloseIndices)
                        ictx.Send(XPRMInitializeControl.OpenList)
                        ictx.Send(String.Format("aa{0}", i))
                        ictx.Send(CType(i * 1.23, Double))
                        ictx.Send(XPRMInitializeControl.CloseList)
                    Next
                    ictx.Send(XPRMInitializeControl.CloseList)
                    Return True

                Case "r" '   r:[ 123 [ 10 20 30 ] ]
                    ictx.Send(XPRMInitializeControl.OpenList)
                    ictx.Send(123)
                    ictx.Send(XPRMInitializeControl.OpenList)
                    For i As Integer = 1 To 3
                        ictx.Send(i * 10)
                    Next
                    ictx.Send(XPRMInitializeControl.CloseList)
                    ictx.Send(XPRMInitializeControl.CloseList)
                    Return True

                Case "a_R" '   a_R:[ (1) [10 [10 20 30] ] (1) [20 [20 40 60] ] ... ]
                    ictx.Send(XPRMInitializeControl.OpenList)
                    For i As Integer = 1 To 10
                        ictx.Send(XPRMInitializeControl.OpenIndices)
                        ictx.Send(i)
                        ictx.Send(XPRMInitializeControl.CloseIndices)
                        ictx.Send(XPRMInitializeControl.OpenList)
                        ictx.Send(i * 10)
                        ictx.Send(XPRMInitializeControl.OpenList)
                        For j As Integer = 1 To 3
                            ictx.Send(j * i * 10)
                        Next
                        ictx.Send(XPRMInitializeControl.CloseList)
                        ictx.Send(XPRMInitializeControl.CloseList)
                    Next
                    ictx.Send(XPRMInitializeControl.CloseList)
                    Return True

                Case Else
                    OutputLog.WriteLine("Label '{0}' not found", label)
                    Return False
            End Select

        Catch e As Exception
            OutputLog.WriteLine("Label '{0}' could not be initialized - {1}", label, e.Message)
            Return False
        End Try
    End Function


    ' A method to retrieve data from Mosel
    Private Function initializeTo(ByVal label As String, ByVal val As XPRMValue) As Boolean
        OutputLog.WriteLine(".NET: {0} = {1}", label, val)
        Return True
    End Function




    ' Main function
    Public Sub RunExDrvsCallback(ByVal Log As TextWriter)
        ' Initialize mosel
        Dim mosel As XPRM = XPRM.Init
        ' Set default output stream to stdout
        mosel.SetDefaultStream(XPRMStreamType.F_OUTPUT_LINEBUF, Log)
        mosel.SetDefaultStream(XPRMStreamType.F_ERROR, Log)
        OutputLog = Log

        ' Compile and load the model
        Dim model As XPRMModel = mosel.CompileAndLoad(New StringReader(source_of_model))

        ' Set the execution parameters and bind the variables
        model.SetExecParam("ICB_INITFROM", "dotnet:cbinitfrom")
        model.SetExecParam("ICB_INITTO", "dotnet:cbinitto")
        model.Bind("cbinitfrom", New XPRMInitializationFrom(AddressOf initializeFrom))
        model.Bind("cbinitto", New XPRMInitializationTo(AddressOf initializeTo))

        ' Run the model
        model.Run()

    End Sub

End Module


ExDrvsRaw.vb
Imports System.IO
Imports Mosel

' Example for use of Mosel libraries (using 'dotnetstream' IOdriver for data exchange)

Module ExDrvsRaw
    ' Defines the Mosel source of our model 
    Private source_of_model As String = _
      "model drivers" & vbCrLf & _
 _
      "parameters" & vbCrLf & _
      " DATA=''" & vbCrLf & _
      " SOL=''" & vbCrLf & _
      "end-parameters" & vbCrLf & _
 _
      "declarations" & vbCrLf & _
      " S:set of string" & vbCrLf & _
      " R:range" & vbCrLf & _
      " data:array(S,R) of real" & vbCrLf & _
      " sol:array(1..10) of real" & vbCrLf & _
      "end-declarations" & vbCrLf & _
 _
      "initialisations from 'dotnetraw:'" & vbCrLf & _
      " data as 'DATA(s,r,v)'" & vbCrLf & _
      "end-initialisations" & vbCrLf & _
 _
      "writeln('set   S=',S)" & vbCrLf & _
      "writeln('range R=',R)" & vbCrLf & _
      "writeln('array data=',data)" & vbCrLf & _
 _
      "forall(i in 1..10) sol(i):=i^2" & vbCrLf & _
 _
      "initialisations to 'dotnetraw:noindex'" & vbCrLf & _
      " sol as 'SOL'" & vbCrLf & _
      "end-initialisations" & vbCrLf & _
 _
      "end-model"

    ' Define a structure to store initial values for the array 'data'
    Public Class ModelDataElement
        Public s As String
        Public r As Integer
        Public v As Double

        Public Sub New(ByVal s As String, ByVal r As Integer, ByVal v As Double)
            Me.s = s
            Me.r = r
            Me.v = v
        End Sub

    End Class

    Public Sub RunExDrvsRaw(ByVal Log As TextWriter)
        ' Initialize Mosel
        Dim moselRT As XPRM = XPRM.Init

        ' Use a StringReader to compile and load the Mosel model directly from a .NET string
        Log.WriteLine("Compiling from memory...")
        Dim modelSourceReader As New StringReader(source_of_model)
        Dim model As XPRMModel = moselRT.CompileAndLoad(modelSourceReader)

        ' Send the model's output directly to the log
        model.SetDefaultStream(XPRMStreamType.F_OUTPUT_LINEBUF, Log)

        ' Create an array containing the initialization data
        Dim ModelData(3) As ModelDataElement
        ModelData(0) = New ModelDataElement("one", 2, 12.5)
        ModelData(1) = New ModelDataElement("two", 1, 15)
        ModelData(2) = New ModelDataElement("three", 16, 9)
        ModelData(3) = New ModelDataElement("hundred", 2, 17)
        ' Bind this array to the name "DATA"
        moselRT.Bind("DATA", ModelData)

        ' Create an array to receive solution data and bind it to the name 'SOL'
        Dim Solution(9) As Double
        moselRT.Bind("SOL", Solution)

        ' Run the model
        model.Run()

        ' Print the solution
        Log.WriteLine()
        Log.WriteLine()
        Log.Write("Solution values: ")
        Dim i As Integer
        For i = 0 To 9
            Log.Write(" {0}", Solution(i))
        Next
        Log.WriteLine()
        Log.WriteLine()

    End Sub

End Module

ExDrvsStream.vb
Imports System.IO
Imports Mosel

' Example for use of Mosel libraries (using 'dotnetstream' IOdriver for data exchange)

Module ExDrvsStream
    ' Defines the Mosel source of our model 
    Private source_of_model As String = _
      "model Blend" & vbCrLf & _
      "uses ""mmxprs""" & vbCrLf & _
      "declarations" & vbCrLf & _
      " ROres = 1..2" & vbCrLf & _
      " REV = 125" & vbCrLf & _
      " MINGRADE = 4" & vbCrLf & _
      " MAXGRADE = 5" & vbCrLf & _
      " COST: array(ROres) of real" & vbCrLf & _
      " AVAIL: array(ROres) of real" & vbCrLf & _
      " GRADE: array(ROres) of real" & vbCrLf & _
      " x: array(ROres) of mpvar" & vbCrLf & _
      "end-declarations" & vbCrLf & _
      "" & vbCrLf & _
      "initializations from 'dotnetstream:BlendIni'" & vbCrLf & _
      " COST" & vbCrLf & _
      " AVAIL" & vbCrLf & _
      " GRADE" & vbCrLf & _
      "end-initializations" & vbCrLf & _
      vbCrLf & _
      "Profit:= sum(o in ROres) (REV-COST(o))*x(o)" & vbCrLf & _
      "LoGrade:= sum(o in ROres) (GRADE(o)-MINGRADE) * x(o) >= 0" & vbCrLf & _
      "UpGrade:= sum(o in ROres) (MAXGRADE-GRADE(o)) * x(o) >= 0" & vbCrLf & _
      "" & vbCrLf & _
      "forall(o in ROres) x(o)<=AVAIL(o)" & vbCrLf & _
      "" & vbCrLf & _
      "maximize(Profit)" & vbCrLf & _
      "writeln(""Objective:"", getobjval)" & vbCrLf & _
      "end-model"

    ' Defines the initialisation data for our model
    Dim init_data As String = _
      "COST: [85 93]" & vbCrLf & _
      "AVAIL: [60 45]" & vbCrLf & _
      "GRADE: [2.1 6.3]" & vbCrLf


    Public Sub RunExDrvsStream(ByVal Log As TextWriter)
        ' Initialize Mosel
        Dim moselRT As XPRM = XPRM.Init

        ' Use a StringReader to compile and load the Mosel model directly from a .NET string
        Log.WriteLine("Compiling from memory...")
        Dim modelSourceReader As New StringReader(source_of_model)
        Dim model As XPRMModel = moselRT.CompileAndLoad(modelSourceReader)

        ' Bind a stream to read the initialization data to the name 'BlendIni'
        model.Bind("BlendIni", New StringReader(init_data))

        ' Send the model's output directly to the log
        model.SetDefaultStream(XPRMStreamType.F_OUTPUT_LINEBUF, Log)

        ' And finally run the model
        model.Run()
    End Sub

End Module