mplShortPath.vb

VBNet Example:


Imports Maximal.OptiMax


Public Class MplShortPath

   Private _resultString As String
   Private _totalDistance As Double
   Private _path() As Double


   Public Sub New()
      _resultString = ""
      _totalDistance = 0.0
      ReDim _path(0)
   End Sub


   Private Function loadModel(ByVal model As Model, ByVal startCity As String, ByVal endCity As String, _
                        ByVal cityNames As String(), ByVal routeDistData As ArrayList) As ResultType
      Const flow As Double = 1.0
      Try
         model.IndexSets.Add("node", cityNames)
         model.IndexSets.Add("FromNode[node]", cityNames)
         model.IndexSets.Add("ToNode[node]", cityNames)

         model.DataVectors.Add("Distance[FromNode,ToNode]", routeDistData)
         model.DataStrings.Add("StartCity", startCity)
         model.DataStrings.Add("EndCity", endCity)

         model.VariableVectors.Add("Path[FromNode, ToNode] WHERE (Distance > 0)")
         model.Objectives.Add("SUM(FromNode,ToNode: Distance*Path)")

         Dim conFlowBal As ConstraintVector = model.ConstraintVectors.Add("FlowBalance[node]", _
                "SUM(FromNode: Path[FromNode, ToNode:=node]) = SUM(ToNode: Path[FromNode:=node, ToNode])")
         conFlowBal(startCity).RHSValue = -flow
         conFlowBal(endCity).RHSValue = flow

      Catch ex As Exception
         Console.WriteLine(ex.Message)
      End Try
      Return model.LastResult
   End Function


   Public Function SolveModel(ByVal solverName As String, ByVal startCity As String, ByVal endCity As String, _
                        ByVal cityNames() As String, ByVal routeDistData As ArrayList) As Boolean
      Dim mpl As OptiMax = New OptiMax()
      Dim model As Model = mpl.Models.Add("ShortPath")
      Dim solver As Solver = mpl.Solvers.Add(solverName)
      If solver Is Nothing Then
         _resultString = "Solver " & solverName & " was not found."
         Return False
      End If
      Dim result As ResultType = loadModel(model, startCity, endCity, cityNames, routeDistData)
      If Not result = ResultType.Success Then
         _resultString = model.Error.ToString()
         Return False
      End If
      model.UseExceptions = False
      result = model.Solve(solver)
      If Not result = ResultType.Success Then
         _resultString = "Solver " & solver.Name & ": " & model.ResultString & vbCrLf & model.Solution.ResultString
         Return False
      End If
      _resultString = model.Solution.ResultString
      getSolutionValues(model)
      Return True
   End Function


   Private Function getSolutionValues(ByVal model As Model) As Integer
      Dim varPath As VariableVector = model.VariableVectors("Path")
      Dim solPath As List(Of Double) = New List(Of Double)
      For Each var As Variable In varPath
         solPath.Add(var.Activity)
      Next var
      _path = solPath.ToArray()
      _totalDistance = model.Solution.ObjectValue
      Return solPath.Count
   End Function



   Public ReadOnly Property ResultString() As String
      Get
         Return _resultString
      End Get
   End Property

   Public ReadOnly Property Path() As Double()
      Get
         Return _path
      End Get
   End Property

   Public ReadOnly Property TotalDistance() As Double
      Get
         Return _totalDistance
      End Get
   End Property

   Public ReadOnly Property DataDir() As String
      Get
         Return GetDataDir()
      End Get
   End Property


   Public Shared Function GetDataDir() As String
      Dim tempMpl As OptiMax = New OptiMax()
      Dim dataDir As String = tempMpl.ModelDirectory
      tempMpl = Nothing
      Return dataDir
   End Function


End Class

        

Back To Top | Maximal Home Page | List of Samples | Previous Page | Next Page