Edit: It's implemented like an add-on. Thus you get it from the global service manager |
REM Basic Code
smgr = GetProcessServiceManager()
solv = smgr.createInstance("com.sun.star.sheet.Solver")
Dim Variables(2) as Object
Dim Constraint_0 as New com.sun.star.sheet.SolverConstraint
Dim Constraint_1 as New com.sun.star.sheet.SolverConstraint
Dim Constraint_2 as New com.sun.star.sheet.SolverConstraint
Dim Constraints(2) as Object
smgr = GetProcessServiceManager()
solv = smgr.createInstance("com.sun.star.sheet.Solver")
ConstrOperator1 = com.sun.star.sheet.SolverConstraintOperator.LESS_EQUAL
ConstrOperator2 = com.sun.star.sheet.SolverConstraintOperator.GREATER_EQUAL
oDoc = ThisComponent
solv.Document = oDoc
Sheet = oDoc.Sheets(0)
ObjectiveCell = Sheet.getCellByPosition(3,0) 'This cell has the formula =A1 +B1 - C1
solv.Objective = ObjectiveCell.CellAddress
VariCell0 = Sheet.getCellByPosition(0,0)
Variables(0) = VariCell0.CellAddress
VariCell1 = Sheet.getCellByPosition(1,0)
Variables(1) = VariCell1.CellAddress
VariCell2 = Sheet.getCellByPosition(2,0)
Variables(2) = VariCell2.CellAddress
solv.Variables = Variables()
Constraint_0.Left = Sheet.getCellByPosition(0,0).CellAddress
Constraint_0.Operator = ConstrOperator1
Constraint_0.Right = 6
Constraints(0) = Constraint_0
Constraint_1.Left = Sheet.getCellByPosition(1,0).CellAddress
Constraint_1.Operator = ConstrOperator1
Constraint_1.Right = 8
Constraints(1) = Constraint_1
Constraint_2.Left = Sheet.getCellByPosition(2,0).CellAddress
Constraint_2.Operator = ConstrOperator2
Constraint_2.Right = 4
Constraints(2) = Constraint_2
solv.Constraints = Constraints()
solv.Maximize = True
solv.Solve()
Print solv.ResultValue
Olle E wrote:I'm running OpenOffice 3.0.1. on Ubuntu Intrepid.
FJCC wrote:I built a trivial example to show how code might look. I avoided loops to show every step explicitly. The problem I used is to have the formula =A1+B1-C1 in cell D1. I want to maximize D1 with the constraints that A1 must be <= 6, B1 must be <= 8 and C1 must be >= 4. Take a look at this in conjunction with the documentation for Interface XSolver, Struct SolverConstraint, and Enum SolverConstraintOperator. Thanks to Villeroy for showing how to get the Solver service.
- Code: Select all Expand viewCollapse view
Dim Variables(2) as Object
Dim Constraint_0 as New com.sun.star.sheet.SolverConstraint
Dim Constraint_1 as New com.sun.star.sheet.SolverConstraint
Dim Constraint_2 as New com.sun.star.sheet.SolverConstraint
Dim Constraints(2) as Object
smgr = GetProcessServiceManager()
solv = smgr.createInstance("com.sun.star.sheet.Solver")
ConstrOperator1 = com.sun.star.sheet.SolverConstraintOperator.LESS_EQUAL
ConstrOperator2 = com.sun.star.sheet.SolverConstraintOperator.GREATER_EQUAL
oDoc = ThisComponent
solv.Document = oDoc
Sheet = oDoc.Sheets(0)
ObjectiveCell = Sheet.getCellByPosition(3,0) 'This cell has the formula =A1 +B1 - C1
solv.Objective = ObjectiveCell.CellAddress
VariCell0 = Sheet.getCellByPosition(0,0)
Variables(0) = VariCell0.CellAddress
VariCell1 = Sheet.getCellByPosition(1,0)
Variables(1) = VariCell1.CellAddress
VariCell2 = Sheet.getCellByPosition(2,0)
Variables(2) = VariCell2.CellAddress
solv.Variables = Variables()
Constraint_0.Left = Sheet.getCellByPosition(0,0).CellAddress
Constraint_0.Operator = ConstrOperator1
Constraint_0.Right = 6
Constraints(0) = Constraint_0
Constraint_1.Left = Sheet.getCellByPosition(1,0).CellAddress
Constraint_1.Operator = ConstrOperator1
Constraint_1.Right = 8
Constraints(1) = Constraint_1
Constraint_2.Left = Sheet.getCellByPosition(2,0).CellAddress
Constraint_2.Operator = ConstrOperator2
Constraint_2.Right = 4
Constraints(2) = Constraint_2
solv.Constraints = Constraints()
solv.Maximize = True
solv.Solve()
Print solv.ResultValue
Users browsing this forum: No registered users and 2 guests