mplCutStock.vb

VBNet Example:


Imports Maximal.OptiMax


Public Class MplCutStock

   Private _resultString As String
   Private _totalCost As Double
   Private _sheetsCut As Integer
   Private _totalSheets As Integer
   Private _totalWaste As Integer
   Private _solPatternCount() As Integer
   Private _solPatternNames() As String


   Public Sub New()
      _resultString = ""
      _totalCost = 0.0
      _sheetsCut = 0
      _totalSheets = 0
      _totalWaste = 0
      ReDim _solPatternCount(0)
      ReDim _solPatternNames(0)
   End Sub


   Private Function loadModel(ByVal model As Model, ByVal sheetPrice As Double, ByVal sheetsAvail As Integer, _
                        ByVal numWidths As Integer, ByVal cutWidths() As Integer, ByVal cutDemands() As Integer, _
                        ByVal numPatterns As Integer, ByVal patternWaste() As Integer, _
                        ByVal patternTable(,) As Integer) As ResultType
      Try
         model.IndexSets.Add("patterns", "p", 1, numPatterns)
         model.IndexSets.Add("cuts", "w", 1, numWidths)

         model.DataConstants.Add("PriceSheet", sheetPrice)
         model.DataConstants.Add("SheetsAvail", sheetsAvail)

         model.DataVectors.Add("CutWidths[cuts]", cutWidths)
         model.DataVectors.Add("CutDemand[cuts]", cutDemands)
         model.DataVectors.Add("PatWaste[patterns]", patternWaste)
         model.DataVectors.Add("CutsInPattern[patterns, cuts]", patternTable)

         model.PlainVariables.Add("SheetsCut", "-> T1")
         model.PlainVariables.Add("TotalCost", "-> TC")

         model.VariableVectors.Add("PatternCount[patterns]", "-> """"")
         model.VariableVectors.Add("ExcessCuts[cuts]", "-> X")

         model.Objectives.Add("Obj", ObjectSense.Minimize, "TotalCost")

         model.PlainConstraints.Add("TotCost", "TotalCost = PriceSheet*SheetsCut")
         model.PlainConstraints.Add("RawAvail", "SheetsCut < SheetsAvail")
         model.PlainConstraints.Add("Sheets", "SheetsCut = SUM(patterns: PatternCount[patterns])")

         model.ConstraintVectors.Add("CutReq[cuts]", _
            "SUM(patterns: CutsInPattern[patterns, cuts] * PatternCount[patterns]) = CutDemand[cuts] + ExcessCuts[cuts]")

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


   Public Function SolveModel(ByVal solverName As String, ByVal priceSheet As Double, ByVal sheetsAvail As Integer, _
                        ByVal numWidths As Integer, ByVal cutWidths() As Integer, ByVal cutDemands() As Integer, _
                        ByVal numPatterns As Integer, ByVal patternWaste() As Integer, _
                        ByVal patternTable(,) As Integer) As Boolean
      Dim mpl As OptiMax = New OptiMax()
      Dim model As Model = mpl.Models.Add("CutStock")
      Dim solver As Solver = mpl.Solvers.Add(solverName)
      If solver Is Nothing Then
         _resultString = "Solver " & solverName & " was not found."
         Return False
      End If
      model.WorkingDirectory = mpl.ModelDirectory
      Dim result As ResultType = loadModel(model, priceSheet, sheetsAvail, numWidths, cutWidths, _
                                  cutDemands, numPatterns, patternWaste, patternTable)
      If Not result = ResultType.Success Then
         _resultString = model.Error.ToString()
         Return False
      End If
      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)
      computeSolutionTotals(_solPatternCount, patternWaste)
      Return True
   End Function


   Private Sub getSolutionValues(ByVal model As Model)
      _totalCost = model.Solution.ObjectValue
      Dim sheetsCutVar As Variable = model.PlainVariables("SheetsCut")
      _sheetsCut = Convert.ToInt32(sheetsCutVar.Activity)

      Dim solPatternNames As List(Of String) = New List(Of String)
      Dim solPatterns As List(Of Integer) = New List(Of Integer)
      Dim PatternCountVect As VariableVector = model.VariableVectors("PatternCount")
      For Each var As Variable In PatternCountVect
         solPatternNames.Add(PatternCountVect.Subscripts(1).ValueStr)
         solPatterns.Add(Convert.ToInt32(var.Activity))
      Next var
      _solPatternNames = solPatternNames.ToArray()
      _solPatternCount = solPatterns.ToArray()
   End Sub


   Private Sub computeSolutionTotals(ByVal patternCount() As Integer, ByVal patternWaste() As Integer)
      _totalSheets = 0
      _totalWaste = 0
      For p As Integer = 0 To patternCount.Length - 1
         If patternCount(p) > 0 Then
            _totalSheets = _totalSheets + patternCount(p)
            _totalWaste = _totalWaste + patternWaste(p) * patternCount(p)
         End If
      Next p
   End Sub


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

   Public ReadOnly Property TotalCost() As Double
      Get
         Return _totalCost
      End Get
   End Property

   Public ReadOnly Property SheetsCut() As Integer
      Get
         Return _sheetsCut
      End Get
   End Property

   Public ReadOnly Property TotalSheets() As Integer
      Get
         Return _totalSheets
      End Get
   End Property

   Public ReadOnly Property TotalWaste() As Integer
      Get
         Return _totalWaste
      End Get
   End Property

   Public ReadOnly Property SolPatternCount() As Integer()
      Get
         Return _solPatternCount
      End Get
   End Property

   Public ReadOnly Property SolPatternNames() As String()
      Get
         Return _solPatternNames
      End Get
   End Property

End Class


        

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