1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <CoinError.hpp>
23 #include "SolverComponent.hxx"
24 #include <strings.hrc>
26 #include <com/sun/star/frame/XModel.hpp>
27 #include <com/sun/star/table/CellAddress.hpp>
29 #include <rtl/math.hxx>
34 namespace com::sun::star::uno
{ class XComponentContext
; }
36 using namespace com::sun::star
;
38 class CoinMPSolver
: public SolverComponent
44 virtual void SAL_CALL
solve() override
;
45 virtual OUString SAL_CALL
getImplementationName() override
47 return "com.sun.star.comp.Calc.CoinMPSolver";
49 virtual OUString SAL_CALL
getComponentDescription() override
51 return SolverComponent::GetResourceString( RID_COINMP_SOLVER_COMPONENT
);
55 void SAL_CALL
CoinMPSolver::solve()
57 uno::Reference
<frame::XModel
> xModel( mxDoc
, uno::UNO_QUERY_THROW
);
62 xModel
->lockControllers();
64 // collect variables in vector (?)
66 auto aVariableCells
= comphelper::sequenceToContainer
<std::vector
<table::CellAddress
>>(maVariables
);
67 size_t nVariables
= aVariableCells
.size();
70 // collect all dependent cells
72 ScSolverCellHashMap aCellsHash
;
73 aCellsHash
[maObjective
].reserve( nVariables
+ 1 ); // objective function
75 for (const auto& rConstr
: std::as_const(maConstraints
))
77 table::CellAddress aCellAddr
= rConstr
.Left
;
78 aCellsHash
[aCellAddr
].reserve( nVariables
+ 1 ); // constraints: left hand side
80 if ( rConstr
.Right
>>= aCellAddr
)
81 aCellsHash
[aCellAddr
].reserve( nVariables
+ 1 ); // constraints: right hand side
84 // set all variables to zero
86 //! use old values as initial values?
87 for ( const auto& rVarCell
: aVariableCells
)
89 SolverComponent::SetValue( mxDoc
, rVarCell
, 0.0 );
92 // read initial values from all dependent cells
93 for ( auto& rEntry
: aCellsHash
)
95 double fValue
= SolverComponent::GetValue( mxDoc
, rEntry
.first
);
96 rEntry
.second
.push_back( fValue
); // store as first element, as-is
99 // loop through variables
100 for ( const auto& rVarCell
: aVariableCells
)
102 SolverComponent::SetValue( mxDoc
, rVarCell
, 1.0 ); // set to 1 to examine influence
104 // read value change from all dependent cells
105 for ( auto& rEntry
: aCellsHash
)
107 double fChanged
= SolverComponent::GetValue( mxDoc
, rEntry
.first
);
108 double fInitial
= rEntry
.second
.front();
109 rEntry
.second
.push_back( fChanged
- fInitial
);
112 SolverComponent::SetValue( mxDoc
, rVarCell
, 2.0 ); // minimal test for linearity
114 for ( const auto& rEntry
: aCellsHash
)
116 double fInitial
= rEntry
.second
.front();
117 double fCoeff
= rEntry
.second
.back(); // last appended: coefficient for this variable
118 double fTwo
= SolverComponent::GetValue( mxDoc
, rEntry
.first
);
120 bool bLinear
= rtl::math::approxEqual( fTwo
, fInitial
+ 2.0 * fCoeff
) ||
121 rtl::math::approxEqual( fInitial
, fTwo
- 2.0 * fCoeff
);
122 // second comparison is needed in case fTwo is zero
124 maStatus
= SolverComponent::GetResourceString( RID_ERROR_NONLINEAR
);
127 SolverComponent::SetValue( mxDoc
, rVarCell
, 0.0 ); // set back to zero for examining next variable
130 xModel
->unlockControllers();
132 if ( !maStatus
.isEmpty() )
136 // build parameter arrays for CoinMP
139 // set objective function
141 const std::vector
<double>& rObjCoeff
= aCellsHash
[maObjective
];
142 std::unique_ptr
<double[]> pObjectCoeffs(new double[nVariables
]);
143 for (nVar
=0; nVar
<nVariables
; nVar
++)
144 pObjectCoeffs
[nVar
] = rObjCoeff
[nVar
+1];
145 double nObjectConst
= rObjCoeff
[0]; // constant term of objective
149 size_t nRows
= maConstraints
.getLength();
150 size_t nCompSize
= nVariables
* nRows
;
151 std::unique_ptr
<double[]> pCompMatrix(new double[nCompSize
]); // first collect all coefficients, row-wise
152 for (size_t i
=0; i
<nCompSize
; i
++)
153 pCompMatrix
[i
] = 0.0;
155 std::unique_ptr
<double[]> pRHS(new double[nRows
]);
156 std::unique_ptr
<char[]> pRowType(new char[nRows
]);
157 for (size_t i
=0; i
<nRows
; i
++)
163 for (sal_Int32 nConstrPos
= 0; nConstrPos
< maConstraints
.getLength(); ++nConstrPos
)
165 // integer constraints are set later
166 sheet::SolverConstraintOperator eOp
= maConstraints
[nConstrPos
].Operator
;
167 if ( eOp
== sheet::SolverConstraintOperator_LESS_EQUAL
||
168 eOp
== sheet::SolverConstraintOperator_GREATER_EQUAL
||
169 eOp
== sheet::SolverConstraintOperator_EQUAL
)
171 double fDirectValue
= 0.0;
172 bool bRightCell
= false;
173 table::CellAddress aRightAddr
;
174 const uno::Any
& rRightAny
= maConstraints
[nConstrPos
].Right
;
175 if ( rRightAny
>>= aRightAddr
)
176 bRightCell
= true; // cell specified as right-hand side
178 rRightAny
>>= fDirectValue
; // constant value
180 table::CellAddress aLeftAddr
= maConstraints
[nConstrPos
].Left
;
182 const std::vector
<double>& rLeftCoeff
= aCellsHash
[aLeftAddr
];
183 double* pValues
= &pCompMatrix
[nConstrPos
* nVariables
];
184 for (nVar
=0; nVar
<nVariables
; nVar
++)
185 pValues
[nVar
] = rLeftCoeff
[nVar
+1];
187 // if left hand cell has a constant term, put into rhs value
188 double fRightValue
= -rLeftCoeff
[0];
192 const std::vector
<double>& rRightCoeff
= aCellsHash
[aRightAddr
];
193 // modify pValues with rhs coefficients
194 for (nVar
=0; nVar
<nVariables
; nVar
++)
195 pValues
[nVar
] -= rRightCoeff
[nVar
+1];
197 fRightValue
+= rRightCoeff
[0]; // constant term
200 fRightValue
+= fDirectValue
;
204 case sheet::SolverConstraintOperator_LESS_EQUAL
: pRowType
[nConstrPos
] = 'L'; break;
205 case sheet::SolverConstraintOperator_GREATER_EQUAL
: pRowType
[nConstrPos
] = 'G'; break;
206 case sheet::SolverConstraintOperator_EQUAL
: pRowType
[nConstrPos
] = 'E'; break;
208 OSL_ENSURE( false, "unexpected enum type" );
210 pRHS
[nConstrPos
] = fRightValue
;
214 // Find non-zero coefficients, column-wise
216 std::unique_ptr
<int[]> pMatrixBegin(new int[nVariables
+1]);
217 std::unique_ptr
<int[]> pMatrixCount(new int[nVariables
]);
218 std::unique_ptr
<double[]> pMatrix(new double[nCompSize
]); // not always completely used
219 std::unique_ptr
<int[]> pMatrixIndex(new int[nCompSize
]);
221 for (nVar
=0; nVar
<nVariables
; nVar
++)
223 int nBegin
= nMatrixPos
;
224 for (size_t nRow
=0; nRow
<nRows
; nRow
++)
226 double fCoeff
= pCompMatrix
[ nRow
* nVariables
+ nVar
]; // row-wise
229 pMatrix
[nMatrixPos
] = fCoeff
;
230 pMatrixIndex
[nMatrixPos
] = nRow
;
234 pMatrixBegin
[nVar
] = nBegin
;
235 pMatrixCount
[nVar
] = nMatrixPos
- nBegin
;
237 pMatrixBegin
[nVariables
] = nMatrixPos
;
240 // apply settings to all variables
242 std::unique_ptr
<double[]> pLowerBounds(new double[nVariables
]);
243 std::unique_ptr
<double[]> pUpperBounds(new double[nVariables
]);
244 for (nVar
=0; nVar
<nVariables
; nVar
++)
246 pLowerBounds
[nVar
] = mbNonNegative
? 0.0 : -DBL_MAX
;
247 pUpperBounds
[nVar
] = DBL_MAX
;
249 // bounds could possibly be further restricted from single-cell constraints
252 std::unique_ptr
<char[]> pColType(new char[nVariables
]);
253 for (nVar
=0; nVar
<nVariables
; nVar
++)
254 pColType
[nVar
] = mbInteger
? 'I' : 'C';
256 // apply single-var integer constraints
258 for (const auto& rConstr
: std::as_const(maConstraints
))
260 sheet::SolverConstraintOperator eOp
= rConstr
.Operator
;
261 if ( eOp
== sheet::SolverConstraintOperator_INTEGER
||
262 eOp
== sheet::SolverConstraintOperator_BINARY
)
264 table::CellAddress aLeftAddr
= rConstr
.Left
;
265 // find variable index for cell
266 for (nVar
=0; nVar
<nVariables
; nVar
++)
267 if ( AddressEqual( aVariableCells
[nVar
], aLeftAddr
) )
269 if ( eOp
== sheet::SolverConstraintOperator_INTEGER
)
270 pColType
[nVar
] = 'I';
273 pColType
[nVar
] = 'B';
274 pLowerBounds
[nVar
] = 0.0;
275 pUpperBounds
[nVar
] = 1.0;
281 int nObjectSense
= mbMaximize
? SOLV_OBJSENS_MAX
: SOLV_OBJSENS_MIN
;
283 HPROB hProb
= CoinCreateProblem("");
284 int nResult
= CoinLoadProblem( hProb
, nVariables
, nRows
, nMatrixPos
, 0,
285 nObjectSense
, nObjectConst
, pObjectCoeffs
.get(),
286 pLowerBounds
.get(), pUpperBounds
.get(), pRowType
.get(), pRHS
.get(), nullptr,
287 pMatrixBegin
.get(), pMatrixCount
.get(), pMatrixIndex
.get(), pMatrix
.get(),
288 nullptr, nullptr, nullptr );
289 if (nResult
== SOLV_CALL_SUCCESS
)
291 nResult
= CoinLoadInteger( hProb
, pColType
.get() );
295 pMatrixIndex
.reset();
297 pMatrixCount
.reset();
298 pMatrixBegin
.reset();
299 pUpperBounds
.reset();
300 pLowerBounds
.reset();
303 pObjectCoeffs
.reset();
305 CoinSetRealOption( hProb
, COIN_REAL_MAXSECONDS
, mnTimeout
);
306 CoinSetRealOption( hProb
, COIN_REAL_MIPMAXSEC
, mnTimeout
);
308 // TODO: handle (or remove) settings: epsilon, B&B depth
312 if (nResult
== SOLV_CALL_SUCCESS
)
314 nResult
= CoinCheckProblem( hProb
);
317 if (nResult
== SOLV_CALL_SUCCESS
)
321 nResult
= CoinOptimizeProblem( hProb
, 0 );
323 catch (const CoinError
& e
)
325 throw std::runtime_error(e
.message());
329 mbSuccess
= ( nResult
== SOLV_CALL_SUCCESS
);
334 maSolution
.realloc( nVariables
);
335 CoinGetSolutionValues( hProb
, maSolution
.getArray(), nullptr, nullptr, nullptr );
336 mfResultValue
= CoinGetObjectValue( hProb
);
340 int nSolutionStatus
= CoinGetSolutionStatus( hProb
);
341 if ( nSolutionStatus
== 1 )
342 maStatus
= SolverComponent::GetResourceString( RID_ERROR_INFEASIBLE
);
343 else if ( nSolutionStatus
== 2 )
344 maStatus
= SolverComponent::GetResourceString( RID_ERROR_UNBOUNDED
);
345 // TODO: detect timeout condition and report as RID_ERROR_TIMEOUT
346 // (currently reported as infeasible)
349 CoinUnloadProblem( hProb
);
352 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
353 com_sun_star_comp_Calc_CoinMPSolver_get_implementation(
354 css::uno::XComponentContext
*,
355 css::uno::Sequence
<css::uno::Any
> const &)
357 return cppu::acquire(new CoinMPSolver());
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */