' Header unit for Gipals32.dll.
'
' There are two possible ways to work with the library Gipals32.dll.
'
' 1) First way is a linear program construction by the provided set of functions,
'    such as specification of the variables and constraints count and assignment
'    of the constraint matrix elements. This way provides a complete flexibility
'    of the linear programming modeling.
'
' 2) The second way is importing linear program from a text file in MPS format.
'    It can be done by one function LoadFromMPS. This way can be suitable if there
'    is a set of prepared MPS files that need to be calculated.
'
'
'   Optimalon Software, 2005
'

' Constraint sign
  Const sign_Less   As Integer = 0 ' <=
  Const sign_Equal As Integer = 1  ' =
  Const sign_More   As Integer = 2 ' >=

' Preprocessor flags
  Const prep_Disable           As Integer = 0  ' Disable preprocessor
  Const prep_RemoveFixedVars   As Integer = 1  ' Remove all fixed variables
  Const prep_RemoveEmptyCols   As Integer = 2  ' Remove empty columns
  Const prep_RemoveSingular    As Integer = 4  ' Remove singular rows and columns
  Const prep_RemoveLinearRows  As Integer = 8   ' Remove linear-dependent rows
  Const prep_RemoveLinearCols  As Integer = 16  ' Remove linear-dependent columns
  Const prep_ForcedRows        As Integer = 32 ' Remove forced rows

' Calculation result state (negative - unsuccessful, positive - successful)}
  Const calc_None           As Integer = 0  ' Calculation has not been provided yet
  Const calc_Optimal        As Integer = 1  ' Finished with optimal solution
  Const calc_Infeasible     As Integer = -1 ' Failed with primal infeasibility
  Const calc_DblInfeasible  As Integer = -2 ' Failed with dual infeasibility
  Const calc_Unknown        As Integer = 2  ' Finished because reached the max. of iterations
  Const calc_Suboptimal     As Integer = 3  ' Finished due to slow convergence
  Const calc_Stoped         As Integer = 4  ' Stoped by the user
  Const calc_Error          As Integer = -3 ' Failed due to numerical eror
  Const calc_Unbounded      As Integer = -4 ' Failed because the linear program is unbounded

' Flags returned by the user-defined callback function
  Const res_Abort As Integer = -1  ' Calculation is canceled
  Const res_None   As Integer = 0  ' Calculation continues
  Const res_Stop   As Integer = 1  ' Calculation is stoped and the results are ready

' The user-defined callback function must have the following format:
'
' Function CallBack(ByVal IterCount  As Integer, ' Current iteration
'                   ByVal PrimalObj  As Double,  ' Primal objective function
'                   ByVal DualObj    As Double,  ' Dual objective function
'                   ByVal InfPrimal  As Double,  ' Primal infeasibility
'                   ByVal InfDual    As Double,  ' Dual infeasibility
'                   ByVal Optimality As Double   ' Optimality
'                   ) As Integer
'


' Set number of conastraints (rows)
Private Declare Sub SetConstraintCount Lib "gipals32.dll" Alias "SetConstraintCount@4" (ByVal ACount As Integer)

' Set number of decision (primal) variables
Private Declare Sub SetVariableCount Lib "gipals32.dll" Alias "SetVariableCount@4" (ByVal ACount As Integer)

' Returns number of constraints
Private Declare Function GetConstraintCount Lib "gipals32.dll" Alias "GetConstraintCount@0" () As Integer

' Returns number of decision (primal) variables
Private Declare Function GetVariableCount Lib "gipals32.dll" Alias "GetVariableCount@0" () As Integer

' Clears the linear program. Deletes all existing data and set number of
' constraints and variables to 1
Private Declare Sub ClearLinearProgram Lib "gipals32.dll" Alias "ClearLinearProgram@0" ()

' Set the objective to minimize (1) or maximize (0)
Private Declare Sub SetObjectiveType Lib "gipals32.dll" Alias "SetObjectiveType@4" (ByVal Minimization As Integer)

' Set the cost and bounds for the variable Index. Returns 0 if variable does not exist.
'  Index    As Integer - Variable index
'  Cost     As Double  - Objective function coefficient
'  Lower    As Double  - Lower bound of variable (ignored if LowerInf <> 0)
'  Upper    As Double  - Upper bound of variable (ignored if UpperInf <> 0)
'  LowerInf As Integer - If <> 0 then indicates variable has no lower bound
'  UpperInf As Integer - If <> 0 then indicates variable has no upper bound
Private Declare Function SetVariable Lib "gipals32.dll" Alias "SetVariable@36" (ByVal Index As Integer, ByVal Cost As Double, ByVal Lower As Double, ByVal Upper As Double, ByVal LowerInf As Integer, ByVal UpperInf As Integer)As Integer

' Add a new variable and set the cost and bounds for the variable Index.
' Returns a new variable index if succeeded and -1 if the maximum size reached.
'   Cost     As Double  - Objective function coefficient
'   Lower    As Double  - Lower bound of variable (ignored if LowerInf <> 0)
'   Upper    As Double  - Upper bound of variable (ignored if UpperInf <> 0)
'   LowerInf As Integer - If <> 0 then indicates variable has no lower bound
'   UpperInf As Integer - If <> 0 then indicates variable has no upper bound
Private Declare Function AddVariable Lib "gipals32.dll" Alias "AddVariable@32" (ByVal Cost As Double, ByVal Lower As Double, ByVal Upper As Double, ByVal LowerInf As Integer, ByVal UpperInf As Integer) As Integer

' Set the right side and the sign of the specified constraint Index.
' Returns 0 if constraint does not exist.
'   Index As Integer - Index of the constraint
'   Right As Double  - Right side of the constraint
'   Sign  As Integer - Sign (sign_Less, sign_Equal, sign_More)
Private Declare Function SetConstraint Lib "gipals32.dll" Alias "SetConstraint@16" (ByVal Index As Integer, ByVal Right As Double, ByVal Sign  As Integer) As Integer

' Add a new constraint and set its right side and the sign.
' Returns index of the constraint or -1 if the maximum size reached.
'   Right As Double  - Right side of the constraint
'   Sign  As Integer - Sign (sign_Less, sign_Equal, sign_More)
Private Declare Function AddConstraint Lib "gipals32.dll" Alias "AddConstraint@12" (ByVal Right As Double, ByVal Sign As Integer) As Integer

' Set the coefficient for the specified variable VarIndex in the specified
' constraint RowIndex. Returns 1 if succeeded.
'   RowIndex As Integer - constraint index
'   VarIndex As Integer - variable index
'   Value    As Double  - coefficient value
Private Declare Function SetConstraintElement Lib "gipals32.dll" Alias "SetConstraintElement@16" (ByVal RowIndex As Integer, ByVal VarIndex As Integer, ByVal Value As Double) As Integer

' The following two routines allow to fill the constraint matrix directly
' with much higher speed than SetConstraintElement function

' Selects the constraints to fill. Returns 0 if the constraints does not exist
Private Declare Function DirectSelectConstraint Lib "gipals32.dll" Alias "DirectSelectConstraint@4" (ByVal Index As Integer) As Integer

' Add a new constraint element in fast way. Variable index MUST be in an
' ascending order. Returns 1 if succeeded.
'   Index As Integer  - variable index
'   Value As Double   - variable coefficient
Private Declare Function DirectAddConstraintElement Lib "gipals32.dll" Alias "DirectAddConstraintElement@12" (ByVal Index As Integer, ByVal Value As Double) As Integer

' Load Linear program from the specified file in MPS format.
' Returns: 1 if succeeded.
'           0 if the maximum allowed number of variables or constraint reached
'          -1 if the file is in wrong format
Private Declare Function LoadFromMPS Lib "gipals32.dll" Alias "LoadFromMPS@4" (ByVal AFile As String) As Integer

' The main function. Calculates the linear program and returns result.
'   The result is defined in "Calculation result" section above.
'   ProcessFunc is callbac function provided to control iteration and cancel
'   or stop the calculation
Private Declare Function Calculate Lib "gipals32.dll" Alias "Calculate@4" (ByVal ProcessFunc As Object) As Integer

' GET RESULTS
' Returns primal objective function value
Private Declare Function PrimalObjective Lib "gipals32.dll" Alias "PrimalObjective@0" () As Double

' Returns dual objective function value
Private Declare Function DualObjective Lib "gipals32.dll" Alias "DualObjective@0" () As Double

' Returns value of the specified decision (primal) variable (X)
Private Declare Function PrimalValue Lib "gipals32.dll" Alias "PrimalValue@4" (ByVal Index As Integer) As Double

' Returns value of the specified dual variable (Y)
Private Declare Function DualValue Lib "gipals32.dll" Alias "DualValue@4" (ByVal Index As Integer) As Double

' Returns value of the specified reduced cost variable (S)
Private Declare Function ReducedCostValue Lib "gipals32.dll" Alias "ReducedCostValue@4" (ByVal Index As Integer) As Double


'///////////////////////////////////////////////////////////////////////////////////////////
' The following routines are optional and should be used to tune the calculation engine.  //
'///////////////////////////////////////////////////////////////////////////////////////////

' Flags are combination of Preprocessor flags or prep_Disable to disable
Private Declare Sub SetPreprocessor Lib "gipals32.dll" Alias "SetPreprocessor@4" (ByVal Flags As Integer)

' Set tolerances. Defaults are: Primal=1e-8, Dual=1e-8, Optimal=1e-10
Private Declare Sub SetTolerances Lib "gipals32.dll" Alias "SetTolerances@24" (ByVal Primal As Double, ByVal Dual As Double, ByVal Optimal As Double)

' Set maximum of iterations (default is 100)
Private Declare Sub SetMaxIterationCount Lib "gipals32.dll" Alias "SetMaxIterationCount@4" (ByVal Value As Integer)

' Enable/Disable the LP scaling. 1 - enable (default), 0 - disable.
Private Declare Sub SetScaling Lib "gipals32.dll" Alias "SetScaling@4" (ByVal Value As Integer)

' Enable/Disable the iterative refinement. 1 - enable (default), 0 - disable.
Private Declare Sub SetRefinement Lib "gipals32.dll" Alias "SetRefinement@4" (ByVal Value As Integer)

' Use the Gondzio correction. 1 - use (default), 0 - don't.
Private Declare Sub SetGondzio Lib "gipals32.dll" Alias "SetGondzio@4" (ByVal Value As Integer)

