FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browser

Working with models, data and dynamic libraries in Mosel

Description
ExSet.vb: Using sets in Mosel (requires burglari.mos)
  • retrieve a set by its model name
  • get the set size
  • get first and last set element
  • get the name or index of a set element
ExAs.vb: Using arrays with index sets (requires trans.mos)
  • get indexing sets of an array
  • get array type
  • enumerate array entries in usual and transposed order
  • enumerate true array entries
ExProb.vb: Accessing problems and solution information with Mosel (requires blend2.mos)
  • export problem to a file (MPS or LP format)
  • get problem status
  • get objective function value
  • get primal/dual solution values, and constraint activity
ExLib.vb: Working with models and accessing dynamic libraries in Mosel
  • load and unload BIM models
  • run a model in Mosel
  • display information about loaded models
  • display information about additional libraries required by the loaded models
DispMod.vb: Display the contents of a model; the information is read from a bim file
  • display run-time parameters, requirements, symbols, package/module dependencies, annotations
DispDso.vb: Display the contents of a module
  • display constants, types, control paramters, subroutines, I/O drivers
ExDrvs*.cb.: 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: frmMain.vb, frmMain.resx, OptimizerLog.vb).

mmexdatavb.zip[download all files]

Source Files

Data Files





DispDso.vb

Imports System.IO
Imports Mosel

' Displays information about a Mosel DSO module

Module DispDso
    Public Sub RunDispDso(ByVal ModName As String, ByVal log As TextWriter)
        log.WriteLine("Will interrogate module '{0}'", ModName)

        Dim mosel As XPRM
        Dim ourModule As XPRMModule

        ' Initialise Mosel
        mosel = XPRM.Init

        ' Load the module
        ourModule = mosel.LoadModule(ModName)

        ' Output basic information about the module
        log.WriteLine("Module '{0}', version {1}", ourModule.Name, ourModule.Version)
        If ((Not ourModule.Certificate Is Nothing) And (ourModule.Certificate.Length > 0)) Then
            log.WriteLine(" ({0})", ourModule.Certificate)
        Else
            log.WriteLine("")
        End If
        log.WriteLine()

        ' Output a list of types defined within the module
        log.WriteLine("Types:")
        Dim nt As XPRMNativeType
        For Each nt In ourModule.Types
            log.WriteLine(" {0} (", nt.Name)
            If (nt.HasCreate) Then
                log.Write("create")
            End If
            If (nt.HasDelete) Then
                log.Write(",delete")
            End If
            If (nt.HasToString) Then
                log.Write(",tostring")
            End If
            If (nt.HasPRTBL) Then
                log.Write("+")
            End If
            If (nt.HasFromString) Then
                log.Write(",fromstring")
            End If
            log.WriteLine()
        Next
        log.WriteLine()

        ' List of control parameters
        log.WriteLine("Control Parameters:")
        Dim p As XPRMParameter
        For Each p In ourModule.Parameters
            log.Write(" {0}: {1} (", p.Name, p.TypeName)
            If (Not p.Description Is Nothing And p.Description.Length > 0) Then
                log.Write("{0},", p.Description)
            End If
            log.WriteLine(rwstatus(p) + ")")
        Next
        log.WriteLine()

        ' List of subroutines
        log.WriteLine("Procedures and Functions:")
        Dim proc As XPRMProcedure
        For Each proc In ourModule.Procedures
            dispProcFct(proc, log)
        Next
        log.WriteLine()

        ' List of IO drivers
        log.WriteLine("I/O drivers:")
        Dim iod As XPRMIODriver
        For Each iod In ourModule.IODrivers
            log.WriteLine(" {0}:{1}", iod.Name, IIf(Not iod.Info Is Nothing, iod.Info, ""))
        Next

    End Sub

    ' Return the r/w status of a control parameter
    Private Function rwstatus(ByVal p As XPRMParameter) As String
        If (p.IsReadable) Then
            If (p.IsWriteable) Then
                Return "r/w"
            Else
                Return "r"
            End If
        Else
            If (p.IsWriteable) Then
                Return "w"
            Else
                Return "?"
            End If
        End If
    End Function


    ' Display a prototype from a signature
    Private Function dispProcFct(ByVal proc As XPRMProcedure, ByVal log As TextWriter)
        Dim parms() As Char
        Dim i As Integer

        If (proc.TypeCode <> XPRMVarType.NOT) Then
            log.Write(" function {0}", proc.Name)
        Else
            log.Write(" procedure {0}", proc.Name)
        End If

        If (proc.NbParameters > 0) Then
            log.Write("(")
            parms = proc.ParameterTypes.ToCharArray
            i = 0
            Do While (i < parms.Length)
                If (i > 0) Then
                    log.Write(",")
                End If
                i = dispType(i, parms, log) + 1
            Loop
            log.Write(")")
        End If

        If (proc.TypeCode <> XPRMVarType.NOT) Then
            log.Write(":{0}", proc.TypeName)
        End If
        log.WriteLine()
    End Function


    ' Display a type name from a signature
    Private Function dispType(ByVal i As Integer, ByVal parms As Char(), ByVal log As TextWriter)
        Dim j As Integer
        Select Case parms(i)
            Case "i"
                log.Write("integer")
            Case "r"
                log.Write("real")
            Case "s"
                log.Write("string")
            Case "b"
                log.Write("boolean")
            Case "v"
                log.Write("mpvar")
            Case "c"
                log.Write("linctr")
            Case "I"
                log.Write("range")
            Case "a"
                log.Write("array")
            Case "e"
                log.Write("set")
            Case "|"
                i = i + 1
                Do
                    log.Write(parms(i))
                    i = i + 1
                Loop While (parms(i) <> "|")
            Case "!"
                i = i + 1
                Do
                    log.Write(parms(i))
                    i = i + 1
                Loop While (parms(i) <> "!")
            Case "A"
                log.Write("array (")
                i = i + 1
                j = i
                Do While (parms(i) <> ".")
                    If (j <> i) Then
                        log.Write(",")
                    End If
                    i = dispType(i, parms, log) + 1
                Loop
                log.Write(") of ")
                i = dispType(i + 1, parms, log)
            Case "E"
                log.Write("set of ")
                i = i + 1
                i = dispType(i, parms, log)
            Case Else
                log.Write("?")
        End Select
        Return i
    End Function
End Module

Back to examples browser