| Imports System.IO
Imports Mosel
' Example of accessing sets in Mosel
Module ExSet
    Public Sub RunExSet(ByVal Log As TextWriter)
        Dim mosel As XPRM
        Dim model As XPRMModel
        Dim mySet As XPRMSet
        Dim first, last, i As Integer
        ' Initialise Mosel
        mosel = XPRM.Init
        ' Load a BIM file
        model = mosel.LoadModel("Models/burglari.bim")
        ' And run it
        model.Run()
        ' Get the model object named 'ITEMS'
        ' (it must be a set)
        mySet = model.FindIdentifier("ITEMS")
        If (Not mySet.IsEmpty) Then
            ' Items in a set are indexed by numbers
            ' So get the first and last indexes
            first = mySet.FirstIndex
            last = mySet.LastIndex
            Log.WriteLine("Elements of set ITEMS:")
            For i = first To last
                Log.Write(" {0}, ", mySet.GetAsString(i))
            Next
            Log.WriteLine()
            ' We've written this out explicitly to demonstrate set access, but the
            ' set actually knows how to output itself.  Uncomment the following line
            ' to see this in action
            ' Log.WriteLine(mySet.ToString)
            If (mySet.GetIndex("CD player") < 0) Then
                Log.WriteLine("'CD player' is not contained in 'ITEMS'")
            End If
        End If
    End Sub
End Module
 | 
| Imports System.IO
Imports Mosel
' Example of using arrays with index sets; different ways of enumerating arrays
Module ExAs
    Public Sub RunExAs(ByVal Log As TextWriter)
        Dim mosel As XPRM
        Dim model As XPRMModel
        Dim varr As XPRMArray
        Dim sets() As XPRMSet
        Dim vindex() As XPRMValue
        Dim indices() As Integer
        Dim dimensions As Integer
        Dim i As Integer
        ' Initialise Mosel
        mosel = XPRM.Init
        ' Load a BIM file
        model = mosel.LoadModel("Models/trans.bim")
        ' Run the model
        model.Run()
        ' Get the model object named 'x'
        ' It must be an array
        varr = model.FindIdentifier("x")
        ' Get the number of dimensions of the array
        dimensions = varr.Dim
        ' Get the indexing sets
        sets = varr.IndexSets
        ' We could use varr.ToString to obtain a summary of the array contents,
        ' but instead we'll demonstrate how to interate over the array's content
        ' directly
        Log.WriteLine("1. Logic entries:")
        ' Get the first entry of varr
        For Each indices In varr.Indices
            Log.Write("x(")
            ' Get the values for this index
            vindex = varr.DereferenceIndex(indices)
            ' Now, output them.  Note that we could call the utility method
            ' varr.IndexToString(indices) instead of doing all this, but again
            ' this wouldn't be such a useful demonstration
            If (dimensions > 1) Then
                For i = 0 To dimensions - 2
                    Log.Write(vindex(i).AsString & ",")
                Next
            End If
            Log.Write(vindex(dimensions - 1).AsString & "), ")
        Next
        Log.WriteLine()
        ' Now enumerate over the true entries - in a dense array this would
        ' be no different from the previous demonstration, so only do it for
        ' a dynamic array
        If (varr.IsDynamic) Then
            Log.WriteLine("2. True entries:")
            For Each indices In varr.TEIndices
                ' Functionally the same as above, but this time we'll demonstrate
                ' using IndexToStringrasExAs
                Log.Write("x" & varr.IndexToString(indices) & ", ")
            Next
        End If
        Log.WriteLine()
        model.Reset()
    End Sub
End Module
 | 
| Imports System.IO
Imports Mosel
' Example of accessing problems and solution information
Module ExProb
    Public Sub RunExProb(ByVal Log As TextWriter)
        Dim mosel As XPRM
        Dim model As XPRMModel
        Dim varr, darr As XPRMArray
        Dim lgrade As XPRMLinCtr
        ' Initialise Mosel
        mosel = XPRM.Init
        ' Load a BIM file
        model = mosel.LoadModel("Models/blend2.bim")
        ' Run the model (it includes optimization)
        model.Run()
        ' Export the problem to a file in LP format (maximization)
        model.ExportProblem("p", "blend")
        ' Test whether optimal is found
        If (model.ProblemStatus = XPRMProblemStatus.PB_OPTIMAL) Then
            Log.WriteLine("Solution is optimal.")
        End If
        ' Print out the objective function value
        Log.WriteLine("Objective value: {0}", model.ObjectiveValue)
        ' Get the model objects 'x' and 'COST' (both arrays)
        varr = model.FindIdentifier("x")
        darr = model.FindIdentifier("COST")
        ' For each entry in array var, display solution value and corresponding
        ' cost
        Dim indices() As Integer
        For Each indices In varr.Indices
            Log.WriteLine( _
                "x{0}={1} (COST: {2})", _
                varr.IndexToString(indices), _
                varr.Get(indices).AsMPVar.Solution, _
                darr.GetAsReal(indices) _
            )
        Next
        ' Get the model object 'LoGrade'
        ' It must be a reference to a linear constraint
        lgrade = CType(model.FindIdentifier("LoGrade"), XPRMReference).Value.AsLinCtr
        Log.WriteLine( _
            "LoGrade: activity={0}, dual={1}", _
            lgrade.Activity, _
            lgrade.Dual _
        )
        model.Reset()
    End Sub
End Module
 |