FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Load an LP and modify it by adding an extra constraint

Description
The problem
               Maximize
                   2x + y
               subject to
                   c1:  x + 4y <= 24
                   c2:       y <=  5
                   c3: 3x +  y <= 20
                   c4:  x +  y <=  9
               and
                   0 <= x,y <= +infinity
and the extra constraint
               c5: 6x + y <= 20
are first stored in the user's data structures. The LP is then loaded into Optimizer, using loadprob, and solved using the primal simplex algorithm. Next, the extra constraint is added to the problem matrix, using addrows, and the revised problem solved using the dual algorithm. In each case, the problem matrix is output to a file, the objective function value displayed on screen, and the problem statistics are are stored in a log file.

loadlp_vbdnet.zip[download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
LoadLP.vb[download]





LoadLP.vb

Imports System
Imports Microsoft.VisualBasic
Imports System.IO
Imports Optimizer

Module LoadLP
    Public Sub RunLoadLP(ByVal Log As TextWriter)
        ' Store the problem
        ' Row data
        Dim nRow As Integer = 4
        Dim sRowType() As Char = {"L", "L", "L", "L"}
        Dim dRHS() As Double = {24.0, 5.0, 20.0, 9.0}
        Dim sRowName() As String = {"c1", "c2", "c3", "c4"}

        ' Column data
        Dim nCol As Integer = 2
        Dim dObj() As Double = {2.0, 1.0}
        Dim dLowerBd() As Double = {0, 0}
        Dim dUpperBd() As Double = {XPRS.PLUSINFINITY, XPRS.PLUSINFINITY}

        Dim sColName() As String = {"x", "y"}

        ' Matrix data
        Dim nColStart() As Integer = {0, 3, 7}
        Dim nRowInd() As Integer = {0, 2, 3, 0, 1, 2, 3}
        Dim dMatElem() As Double = {1, 3, 1, 4, 1, 1, 1}

        ' Store extra constraint
        Dim nNewRow As Integer = 1
        Dim nNewElem As Integer = 2
        Dim sNewRowType() As Char = {"L"}
        Dim sNewRowName() As String = {"c5"}

        Dim dNewRHS() As Double = {20}
        Dim dNewRowElem() As Double = {6, 1}
        Dim nNewRowStart() As Integer = {0, 2}
        Dim nNewColInd() As Integer = {0, 1}

        Dim dObjValue As Double

        Const sLogFile As String = "loadlp.log"
        Const sProblem1 As String = "lp"
        Const sProblem2 As String = "revised"

        Dim prob As XPRSprob
        prob = Nothing

        Try
            XPRS.Init("")

            prob = New XPRSprob
            prob.SetLogFile(sLogFile)

            ' Tell the Optimizer to call OptimizerMsg whenever a message is output
            prob.AddMessageCallback(New Optimizer.MessageCallback(AddressOf HandleOptimizerMessage), Log)

            prob.LoadLP( _
                sProblem1, _
                nCol, _
                nRow, _
                sRowType, _
                dRHS, _
                Nothing, _
                dObj, _
                nColStart, _
                Nothing, _
                nRowInd, _
                dMatElem, _
                dLowerBd, _
                dUpperBd _
            )

            ' Add row names
            prob.AddNames(1, sRowName, 0, nRow - 1)

            ' Add column names
            prob.AddNames(2, sColName, 0, nCol - 1)

            ' Output the matrix
            prob.WriteProb(sProblem1, "")
            Log.WriteLine("Matrix file {0}.mat has been created", sProblem1)

            ' Solve the LP problem
            prob.LpOptimize()

            ' Get and display the value of the objective function
            dObjValue = prob.LPObjVal
            Log.WriteLine("The optimal objective value is {0}", dObjValue)

            ' Add the extra constraint and solve again

            ' Add new row
            prob.AddRows( _
                nNewRow, _
                nNewElem, _
                sNewRowType, _
                dNewRHS, _
                Nothing, _
                nNewRowStart, _
                nNewColInd, _
                dNewRowElem _
            )

            ' Add new row name
            prob.AddNames(1, sNewRowName, nRow, nRow)

            ' Output the revised matrix
            prob.WriteProb(sProblem2, "")
            Log.WriteLine("Matrix file {0}.mat has been created", sProblem2)

            ' Solve with dual - since the revised problem inherits dual feasibility from the
            ' original
            prob.LpOptimize("d")

            ' Get and display the value of the objective function
            dObjValue = prob.LPObjVal
            Log.WriteLine("The revised optimal objective value is {0}", dObjValue)

        Catch ex As Exception
            Log.WriteLine(ex.ToString)

        Finally
            If (Not prob Is Nothing) Then
                prob.Destroy()
            End If
            XPRS.Free()
        End Try

    End Sub

    Private Sub HandleOptimizerMessage(ByVal prob As Optimizer.XPRSprob, ByVal data As Object, _
                                       ByVal message As String, ByVal len As Integer, _
                                       ByVal msglvl As Integer)
        Dim log As TextWriter
        log = data
        If (msglvl = 3 Or msglvl = 4) Then
            log.WriteLine(message)
        End If
    End Sub

End Module

Back to examples browserPrevious exampleNext example