Top > OOoBasic > Calc > Solver
* ソルバー [#qa1226b0]

デフォルトの線形ソルバーは com.sun.star.sheet.Solver サービスを利用します。

その他のソルバーの実装をマクロから利用するには特定の実装名からコンポーネントを探します。オプションはソルバーインスタンスのプロパティで設定できます。

#code(basic){{
Sub solver_1
  ' find solver component implementation
  Dim oSolverComp As Object
  sSolverImplName = "com.sun.star.comp.Calc.Solver" ' default solver
  'implementation names for http://extensions.services.openoffice.org/project/NLPSolver
  'com.sun.star.comp.Calc.NLPSolver.SCOSolverImpl
  'com.sun.star.comp.Calc.NLPSolver.DEPSSolverImpl
  oEnume = GetProcessServiceManager().createContentEnumeration("com.sun.star.sheet.Solver")
  While oEnume.hasMoreElements()
    oObj = oEnume.nextElement()
    If oObj.getImplementationName() = sSolverImplName Then
      oSolverComp = oObj
    End If
  WEnd
  
  If IsNull(oSolverComp) Then
    msgbox "Could not find solver: " & sSolverImplName
    Exit Sub
  End If
  
  ' instantiate solver
  oSolver = CreateUnoService(sSolverImplName)
  ' unable to call createInstance method for solver's implemented by NLPSolver
  'oSolver = oSolverComp.createInstance() 
  mri oSolver
  oDoc = ThisComponent
  nOP = com.sun.star.sheet.SolverConstraintOperator
  With oSolver
    .Document = oDoc
    .Objective = makeCellAddress(0, 1, 6)
    .Variables = Array(makeCellAddress(0, 2, 3), makeCellAddress(0, 2, 4))
    .Constraints = Array( _
        makeSolverConstraint(makeCellAddress(0, 1, 6), nOP.LESS_EQUAL, 2500), _
        makeSolverConstraint(makeCellAddress(0, 1, 7), nOP.EQUAL, 8))
    .Maximize = True
  End With
  
  oSolver.solve()
  If oSolver.Success Then
    ' default solver does not set solutions to cells
    'nRes = oSolver.Solution
    'oSheet = oDoc.getSheets().getByIndex(0)
    'oSheet.getCellByPosition(2, 3).setValue(nRes(0))
    'oSheet.getCellByPosition(2, 4).setValue(nRes(1))
    
    'msgbox oSolver.ResultValue
  Else
    msgbox "no solution."
  End If
End Sub

Function makeCellAddress(nsheet As Integer, ncol As Long, nrow As Long) _
     As com.sun.star.table.CellAddress
  aCellAddr = CreateUnoStruct("com.sun.star.table.CellAddress")
  With aCellAddr
    .Sheet = nsheet
    .Column = ncol
    .Row = nrow
  End With
  makeCellAddress = aCellAddr
End Function


Function makeSolverConstraint( _
    aLeft As com.sun.star.table.CellAddress, nOp As Integer, vRight As Variant ) _
       As com.sun.star.sheet.SolverConstraint
  aSolverConstraint = CreateUnoStruct("com.sun.star.sheet.SolverConstraint")
  With aSolverConstraint
    .Left = aLeft
    .Operator = nOp
    .Right = vRight
  End With
  makeSolverConstraint = aSolverConstraint
End Function
}}

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes