mplCutStock.py

Python Example:


from mplpy import *


class MplCutStock(object):

   def __init__(self):
      self._totalCost = 0.0
      self._resultString = ""
      self._solution = "No solution yet"


   def _loadModel(self, model, cutNames, patternNames, priceSheet, sheetsAvail, cutDemand, cutsInPattern):
      try:
         model.IndexSets.AddNameSet("cuts", cutNames)
         model.IndexSets.AddNameSet("patterns", patternNames)
         model.DataConstants.Add("PriceSheet", priceSheet)
         model.DataConstants.Add("SheetsAvail", sheetsAvail)
         model.DataVectors.AddDense("CutDemand[cuts]", cutDemand)
         model.DataVectors.Add("CutsInPattern[patterns, cuts]", cutsInPattern)
         model.PlainVariables.Add("SheetsCut", "-> T1")
         model.PlainVariables.Add("TotalCost", "-> TC")
         model.VariableVectors.Add("PatternCount[patterns]", "-> \"\"")
         model.VariableVectors.Add("ExcessCuts[cuts]", "-> X")
         model.Objectives.Add("TotalCost", ObjectSense.Minimize)
         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]")
      except Exception as ex:
         print(str(ex))
      result = model.LastResult
      return result


   def SolveModel(self, solverName, cutNames, patternNames, priceSheet, sheetsAvail, cutDemand, cutsInPattern):
      solver = mpl.Solvers.Add(solverName)
      if solver == None:
         self._resultString = "Solver " + solverName + " + was not found."
         return False
      model = mpl.model
      model.UseExceptions = True
      model.ClearModel()
      model.Name = "CutStock"
      result = self._loadModel(model, cutNames, patternNames, priceSheet, sheetsAvail, cutDemand, cutsInPattern)
      if not result == ResultType.Success:
         if result == ResultType.AddFailed:
            self._resultString = model.ResultString
         else:
            self._resultString = str(model.Error)
         model.ClearModel()
         return False

      try:
         model.Solve(solver)
      except Exception as ex:
         print(str(ex))
      result = model.LastResult
      if not result == ResultType.Success:
         self._resultString = "Solver " + solver.Name + ": " + model.ResultString + "\n" + model.Solution.ResultString
         model.ClearModel()
         return False
      self._resultString = model.Solution.ResultString
      self._totalCost = model.Solution.ObjectValue
      self._solution = str(model.Solution)
      model.ClearModel()
      return True

   @property
   def ResultString(self):
      return self._resultString

   @property
   def TotalCost(self):
      return self._totalCost

   @property
   def Solution(self):
      return self._solution

   @property
   def DataDir(self):
      return mpl.ModelDirectory

        

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