' --------------------------------------------------------------------------
' File: examples/src/LPex3.vb
' Version 10.1  
' --------------------------------------------------------------------------
'  Copyright (C) 1999-2006 by ILOG.
'  All Rights Reserved.
'  Permission is expressly granted to use this example in the
'  course of developing applications that use ILOG products.
' --------------------------------------------------------------------------
'
' LPex3.vb, example of adding constraints to solve a problem
'
' Modified example from Chvatal, "Linear Programming", Chapter 26.
'   minimize  c*x
'   subject to  Hx = d
'               Ax = b
'               l <= x <= u
'   where
'
'   H = ( -1  0  1  0  1  0  0  0 )  d = ( -3 )
'       (  1 -1  0  1  0  0  0  0 )      (  1 )
'       (  0  1 -1  0  0  1 -1  0 )      (  4 )
'       (  0  0  0 -1  0 -1  0  1 )      (  3 )
'       (  0  0  0  0 -1  0  1 -1 )      ( -5 )
'
'   A = (  2  1 -2 -1  2 -1 -2 -3 )  b = (  4 )
'       (  1 -3  2  3 -1  2  1  1 )      ( -2 )
'
'   c = ( -9  1  4  2 -8  2  8 12 ) 
'   l = (  0  0  0  0  0  0  0  0 )
'   u = ( 50 50 50 50 50 50 50 50 )
'
'
'
'  Treat the constraints with A as the complicating constraints, and
'  the constraints with H as the "simple" problem.
'  
'  The idea is to solve the simple problem first, and then add the
'  constraints for the complicating constraints, and solve with dual.
Imports ILOG.Concert
Imports ILOG.CPLEX

 _


Public Class LPex3
   
   Public Overloads Shared Sub Main(ByVal args() As String)
      Try
         Dim ncols As Integer = 8

         Dim cplex As New Cplex()
         Dim lp As ILPMatrix = cplex.AddLPMatrix()

         ' add empty corresponding to new variables columns to lp
         Dim x As INumVar() = cplex.NumVarArray(cplex.ColumnArray(lp, ncols), 0, 50)

         ' add rows to lp
         Dim rhs0 As Double() = {-3.0, 1.0, 4.0, 3.0, -5.0}
         Dim val0 As Double()() = {New Double() {-1.0, 1.0, 1.0}, _
                                   New Double() {1.0, -1.0, 1.0}, _
                                   New Double() {1.0, -1.0, 1.0, -1.0},  _
                                   New Double() {-1.0, -1.0, 1.0}, _
                                   New Double() {-1.0, 1.0, -1.0}}
         Dim ind0 As Integer()() = {New Integer() {0, 2, 4}, _
                                    New Integer() {0, 1, 3}, _
                                    New Integer() {1, 2, 5, 6}, _
                                    New Integer() {3, 5, 7},  _
                                    New Integer() {4, 6, 7}}
         lp.AddRows(rhs0, rhs0, ind0, val0)

         ' add the objective function
         Dim objvals As Double() = {-9, 1, 4, 2, -8, 2, 8, 12}
         cplex.AddMinimize(cplex.ScalProd(x, objvals))

         ' Solve initial problem with the network optimizer
         cplex.SetParam(cplex.IntParam.RootAlg, cplex.Algorithm.Network)
         cplex.Solve()
         System.Console.WriteLine(("After network optimization, objective is " & cplex.ObjValue))

         ' add rows from matrix A to lp
         Dim rhs1 As Double() = {4, -2}
         Dim val1 As Double()() = {New Double() {2.0, 1.0, -2.0, -1.0, 2.0, -1.0, -2.0, -3.0}, _
                                   New Double() {1.0, -3.0, 2.0, 3.0, -1.0, 2.0, 1.0, 1.0}}
         Dim ind1 As Integer()() = {New Integer() {0, 1, 2, 3, 4, 5, 6, 7}, _
                                    New Integer() {0, 1, 2, 3, 4, 5, 6, 7}}
         lp.AddRows(rhs1, rhs1, ind1, val1)

         ' Because the problem is dual feasible with the rows added, using
         ' the dual simplex method is indicated.
         cplex.SetParam(cplex.IntParam.RootAlg, cplex.Algorithm.Dual)
         If cplex.Solve() Then
            System.Console.WriteLine(("Solution status = " + cplex.GetStatus().ToString))
            System.Console.WriteLine(("Solution value  = " + cplex.ObjValue.ToString))

            Dim sol As Double() = cplex.GetValues(lp)
            Dim j As Integer

            For j = 0 To ncols - 1
               System.Console.WriteLine(("Column: " & j & " Value = " & sol(j)))
            Next j
         End If
         cplex.End()
      Catch e As ILOG.Concert.Exception
         System.Console.WriteLine(("Concert exception '" + e.ToString + "' caught"))
      End Try
   End Sub 'Main
End Class 'LPex3
