mplCirclePack.py

Python Example:


from mplpy import *

class MplCirclePack(object):

   def __init__(self):
      self._resultString = ""
      self._solRadius = 0.0
      self._solXVal = []
      self._solYVal = []

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

   @property
   def SolRadius(self):
      return self._solRadius

   @property
   def SolXVal(self):
      return self._solXVal

   @property
   def SolYVal(self):
      return self._solYVal


   def _loadModel(self, mod, circleCount):
      mod.UseExceptions = True
      try:
         mod.IndexSets.AddNumRange("i", 1, circleCount)
         mod.IndexSets.AddAlias("j", "i")

         varRadius = mod.PlainVariables.Add("Radius")
         varX = mod.VariableVectors.Add("x[i]", VariableType.Free)
         varY = mod.VariableVectors.Add("y[i]", VariableType.Free)

         varRadius.UpperBound = 1.0
         varX.LowerBound = -1.0
         varX.UpperBound = 1.0
         varY.LowerBound = -1.0
         varY.UpperBound = 1.0

         mod.Objectives.Add("Radius", ObjectSense.Maximize)
         mod.ConstraintVectors.Add("NoOverlap[i,j]",
            "2 * Radius <= ((x[i] - x[i:=j])^2 + (y[i] - y[i:=j])^2)^0.5", "WHERE(i < j)")
         mod.ConstraintVectors.Add("InBoundsA[i]", "x + Radius <= 1")
         mod.ConstraintVectors.Add("InBoundsB[i]", "x - Radius >= -1")
         mod.ConstraintVectors.Add("InBoundsC[i]", "y + Radius <= 1")
         mod.ConstraintVectors.Add("InBoundsD[i]", "y - Radius >= -1")
      except Exception as ex:
         print(str(ex))
      return mod.LastResult

   def _getSolutionXValues(self, model):
      solXVal = []
      xVar = model.VariableVectors["x"]
      for var in xVar:
         solXVal.append(var.Activity)
      return solXVal

   def _getSolutionYValues(self, model):
      solYVal = []
      yVar = model.VariableVectors["y"]
      for var in yVar:
         solYVal.append(var.Activity)
      return solYVal


   def SolveModel(self, solverName, circleCount):
      model = mpl.Models.Add("CirclePacking")
      solver = mpl.Solvers.Add(solverName)
      if solver == None:
         self._resultString = "Solver " + solverName + " + was not found."
         return False
      mpl.Options["ModelType"].ValueStr = "Nonlinear"
      mpl.Options["ParserType"].ValueStr = "Extended"
      result = self._loadModel(model, circleCount)
      if not result == ResultType.Success:
         if result == ResultType.AddFailed:
            self._resultString = model.ResultString
         else:
            self._resultString = str(model.Error)
         model.ClearModel()
         return False
      model.UseExceptions = False
      result = model.Solve(solver)
      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._solRadius = model.Solution.ObjectValue
      self._solXVal = self._getSolutionXValues(model)
      self._solYVal = self._getSolutionYValues(model)
      model.ClearModel()
      return True


        

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