xref: /aoo42x/main/sccomp/source/solver/solver.cxx (revision 1f56509e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include <CoinMP.h>
24 
25 #include "solver.hxx"
26 #include "solver.hrc"
27 
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XIndexAccess.hpp>
30 #include <com/sun/star/frame/XModel.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
33 #include <com/sun/star/sheet/XSpreadsheet.hpp>
34 #include <com/sun/star/table/CellAddress.hpp>
35 #include <com/sun/star/table/CellRangeAddress.hpp>
36 #include <com/sun/star/text/XTextRange.hpp>
37 
38 #include <rtl/math.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <cppuhelper/factory.hxx>
41 #include <vector>
42 #include <hash_map>
43 
44 #include <tools/resmgr.hxx>
45 
46 using namespace com::sun::star;
47 
48 using ::rtl::OUString;
49 
50 #define C2U(constAsciiStr) (::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) ))
51 
52 #define STR_NONNEGATIVE   "NonNegative"
53 #define STR_INTEGER       "Integer"
54 #define STR_TIMEOUT       "Timeout"
55 #define STR_EPSILONLEVEL  "EpsilonLevel"
56 #define STR_LIMITBBDEPTH  "LimitBBDepth"
57 #define STR_NONLINEARTEST "NonLinearTest"
58 
59 // -----------------------------------------------------------------------
60 //  Resources from tools are used for translated strings
61 
62 static ResMgr* pSolverResMgr = NULL;
63 
lcl_GetResourceString(sal_uInt32 nId)64 OUString lcl_GetResourceString( sal_uInt32 nId )
65 {
66     if (!pSolverResMgr)
67         pSolverResMgr = CREATEVERSIONRESMGR( solver );
68 
69     return String( ResId( nId, *pSolverResMgr ) );
70 }
71 
72 // -----------------------------------------------------------------------
73 
74 namespace
75 {
76     enum
77     {
78         PROP_NONNEGATIVE,
79         PROP_INTEGER,
80         PROP_TIMEOUT,
81         PROP_EPSILONLEVEL,
82         PROP_LIMITBBDEPTH,
83         PROP_NONLINEARTEST
84     };
85 }
86 
87 // -----------------------------------------------------------------------
88 
89 // hash map for the coefficients of a dependent cell (objective or constraint)
90 // The size of each vector is the number of columns (variable cells) plus one, first entry is initial value.
91 
92 struct ScSolverCellHash
93 {
operator ()ScSolverCellHash94     size_t operator()( const table::CellAddress& rAddress ) const
95     {
96         return ( rAddress.Sheet << 24 ) | ( rAddress.Column << 16 ) | rAddress.Row;
97     }
98 };
99 
AddressEqual(const table::CellAddress & rAddr1,const table::CellAddress & rAddr2)100 inline bool AddressEqual( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 )
101 {
102     return rAddr1.Sheet == rAddr2.Sheet && rAddr1.Column == rAddr2.Column && rAddr1.Row == rAddr2.Row;
103 }
104 
105 struct ScSolverCellEqual
106 {
operator ()ScSolverCellEqual107     bool operator()( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 ) const
108     {
109         return AddressEqual( rAddr1, rAddr2 );
110     }
111 };
112 
113 typedef std::hash_map< table::CellAddress, std::vector<double>, ScSolverCellHash, ScSolverCellEqual > ScSolverCellHashMap;
114 
115 // -----------------------------------------------------------------------
116 
lcl_GetCell(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos)117 uno::Reference<table::XCell> lcl_GetCell( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
118                                           const table::CellAddress& rPos )
119 {
120     uno::Reference<container::XIndexAccess> xSheets( xDoc->getSheets(), uno::UNO_QUERY );
121     uno::Reference<sheet::XSpreadsheet> xSheet( xSheets->getByIndex( rPos.Sheet ), uno::UNO_QUERY );
122     return xSheet->getCellByPosition( rPos.Column, rPos.Row );
123 }
124 
lcl_SetValue(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos,double fValue)125 void lcl_SetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
126                    const table::CellAddress& rPos, double fValue )
127 {
128     lcl_GetCell( xDoc, rPos )->setValue( fValue );
129 }
130 
lcl_GetValue(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos)131 double lcl_GetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
132                      const table::CellAddress& rPos )
133 {
134     return lcl_GetCell( xDoc, rPos )->getValue();
135 }
136 
137 // -------------------------------------------------------------------------
138 
SolverComponent(const uno::Reference<uno::XComponentContext> &)139 SolverComponent::SolverComponent( const uno::Reference<uno::XComponentContext>& /* rSMgr */ ) :
140     OPropertyContainer( GetBroadcastHelper() ),
141     mbMaximize( sal_True ),
142     mbNonNegative( sal_False ),
143     mbInteger( sal_False ),
144     mnTimeout( 120 ),
145     mnEpsilonLevel( 0 ),
146     mbLimitBBDepth( sal_True ),
147     mbNonLinearTest( sal_True ),
148     mbSuccess( sal_False ),
149     mfResultValue( 0.0 )
150 {
151     // for XPropertySet implementation:
152     registerProperty( C2U(STR_NONNEGATIVE),  PROP_NONNEGATIVE,  0, &mbNonNegative,  getCppuType( &mbNonNegative )  );
153     registerProperty( C2U(STR_INTEGER),      PROP_INTEGER,      0, &mbInteger,      getCppuType( &mbInteger )      );
154     registerProperty( C2U(STR_TIMEOUT),      PROP_TIMEOUT,      0, &mnTimeout,      getCppuType( &mnTimeout )      );
155     registerProperty( C2U(STR_EPSILONLEVEL), PROP_EPSILONLEVEL, 0, &mnEpsilonLevel, getCppuType( &mnEpsilonLevel ) );
156     registerProperty( C2U(STR_LIMITBBDEPTH), PROP_LIMITBBDEPTH, 0, &mbLimitBBDepth, getCppuType( &mbLimitBBDepth ) );
157     registerProperty( C2U(STR_NONLINEARTEST), PROP_NONLINEARTEST, 0, &mbNonLinearTest, getCppuType( &mbNonLinearTest ) );
158 }
159 
~SolverComponent()160 SolverComponent::~SolverComponent()
161 {
162 }
163 
IMPLEMENT_FORWARD_XINTERFACE2(SolverComponent,SolverComponent_Base,OPropertyContainer)164 IMPLEMENT_FORWARD_XINTERFACE2( SolverComponent, SolverComponent_Base, OPropertyContainer )
165 IMPLEMENT_FORWARD_XTYPEPROVIDER2( SolverComponent, SolverComponent_Base, OPropertyContainer )
166 
167 cppu::IPropertyArrayHelper* SolverComponent::createArrayHelper() const
168 {
169     uno::Sequence<beans::Property> aProps;
170     describeProperties( aProps );
171     return new cppu::OPropertyArrayHelper( aProps );
172 }
173 
getInfoHelper()174 cppu::IPropertyArrayHelper& SAL_CALL SolverComponent::getInfoHelper()
175 {
176     return *getArrayHelper();
177 }
178 
getPropertySetInfo()179 uno::Reference<beans::XPropertySetInfo> SAL_CALL SolverComponent::getPropertySetInfo() throw(uno::RuntimeException)
180 {
181     return createPropertySetInfo( getInfoHelper() );
182 }
183 
184 // XSolverDescription
185 
getComponentDescription()186 OUString SAL_CALL SolverComponent::getComponentDescription() throw (uno::RuntimeException)
187 {
188     return lcl_GetResourceString( RID_SOLVER_COMPONENT );
189 }
190 
getStatusDescription()191 OUString SAL_CALL SolverComponent::getStatusDescription() throw (uno::RuntimeException)
192 {
193     return maStatus;
194 }
195 
getPropertyDescription(const OUString & rPropertyName)196 OUString SAL_CALL SolverComponent::getPropertyDescription( const OUString& rPropertyName ) throw (uno::RuntimeException)
197 {
198     sal_uInt32 nResId = 0;
199 	sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
200     switch (nHandle)
201     {
202         case PROP_NONNEGATIVE:
203             nResId = RID_PROPERTY_NONNEGATIVE;
204             break;
205         case PROP_INTEGER:
206             nResId = RID_PROPERTY_INTEGER;
207             break;
208         case PROP_TIMEOUT:
209             nResId = RID_PROPERTY_TIMEOUT;
210             break;
211         case PROP_EPSILONLEVEL:
212             nResId = RID_PROPERTY_EPSILONLEVEL;
213             break;
214         case PROP_LIMITBBDEPTH:
215             nResId = RID_PROPERTY_LIMITBBDEPTH;
216             break;
217         case PROP_NONLINEARTEST:
218             nResId = RID_PROPERTY_NONLINEARTEST;
219             break;
220         default:
221             {
222                 // unknown - leave empty
223             }
224     }
225     OUString aRet;
226     if ( nResId )
227         aRet = lcl_GetResourceString( nResId );
228     return aRet;
229 }
230 
231 // XSolver: settings
232 
getDocument()233 uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocument() throw(uno::RuntimeException)
234 {
235     return mxDoc;
236 }
237 
setDocument(const uno::Reference<sheet::XSpreadsheetDocument> & _document)238 void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document )
239                                 throw(uno::RuntimeException)
240 {
241     mxDoc = _document;
242 }
243 
getObjective()244 table::CellAddress SAL_CALL SolverComponent::getObjective() throw(uno::RuntimeException)
245 {
246     return maObjective;
247 }
248 
setObjective(const table::CellAddress & _objective)249 void SAL_CALL SolverComponent::setObjective( const table::CellAddress& _objective ) throw(uno::RuntimeException)
250 {
251     maObjective = _objective;
252 }
253 
getVariables()254 uno::Sequence<table::CellAddress> SAL_CALL SolverComponent::getVariables() throw(uno::RuntimeException)
255 {
256     return maVariables;
257 }
258 
setVariables(const uno::Sequence<table::CellAddress> & _variables)259 void SAL_CALL SolverComponent::setVariables( const uno::Sequence<table::CellAddress>& _variables )
260                                 throw(uno::RuntimeException)
261 {
262     maVariables = _variables;
263 }
264 
getConstraints()265 uno::Sequence<sheet::SolverConstraint> SAL_CALL SolverComponent::getConstraints() throw(uno::RuntimeException)
266 {
267     return maConstraints;
268 }
269 
setConstraints(const uno::Sequence<sheet::SolverConstraint> & _constraints)270 void SAL_CALL SolverComponent::setConstraints( const uno::Sequence<sheet::SolverConstraint>& _constraints )
271                                 throw(uno::RuntimeException)
272 {
273     maConstraints = _constraints;
274 }
275 
getMaximize()276 sal_Bool SAL_CALL SolverComponent::getMaximize() throw(uno::RuntimeException)
277 {
278     return mbMaximize;
279 }
280 
setMaximize(sal_Bool _maximize)281 void SAL_CALL SolverComponent::setMaximize( sal_Bool _maximize ) throw(uno::RuntimeException)
282 {
283     mbMaximize = _maximize;
284 }
285 
286 // XSolver: get results
287 
getSuccess()288 sal_Bool SAL_CALL SolverComponent::getSuccess() throw(uno::RuntimeException)
289 {
290     return mbSuccess;
291 }
292 
getResultValue()293 double SAL_CALL SolverComponent::getResultValue() throw(uno::RuntimeException)
294 {
295     return mfResultValue;
296 }
297 
getSolution()298 uno::Sequence<double> SAL_CALL SolverComponent::getSolution() throw(uno::RuntimeException)
299 {
300     return maSolution;
301 }
302 
303 // -------------------------------------------------------------------------
304 
solve()305 void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException)
306 {
307     uno::Reference<frame::XModel> xModel( mxDoc, uno::UNO_QUERY );
308     if ( !xModel.is() )
309         throw uno::RuntimeException();
310 
311     maStatus = OUString();
312     mbSuccess = false;
313 
314     xModel->lockControllers();
315 
316     // collect variables in vector (?)
317 
318     std::vector<table::CellAddress> aVariableCells;
319     for (sal_Int32 nPos=0; nPos<maVariables.getLength(); nPos++)
320         aVariableCells.push_back( maVariables[nPos] );
321     size_t nVariables = aVariableCells.size();
322     size_t nVar = 0;
323 
324     // collect all dependent cells
325 
326     ScSolverCellHashMap aCellsHash;
327     aCellsHash[maObjective].reserve( nVariables + 1 );                  // objective function
328 
329     for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
330     {
331         table::CellAddress aCellAddr = maConstraints[nConstrPos].Left;
332         aCellsHash[aCellAddr].reserve( nVariables + 1 );                // constraints: left hand side
333 
334         if ( maConstraints[nConstrPos].Right >>= aCellAddr )
335             aCellsHash[aCellAddr].reserve( nVariables + 1 );            // constraints: right hand side
336     }
337 
338     // set all variables to zero
339     //! store old values?
340     //! use old values as initial values?
341     std::vector<table::CellAddress>::const_iterator aVarIter;
342     for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter )
343     {
344         lcl_SetValue( mxDoc, *aVarIter, 0.0 );
345     }
346 
347     // read initial values from all dependent cells
348     ScSolverCellHashMap::iterator aCellsIter;
349     for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
350     {
351         double fValue = lcl_GetValue( mxDoc, aCellsIter->first );
352         aCellsIter->second.push_back( fValue );                         // store as first element, as-is
353     }
354 
355     // loop through variables
356     for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter )
357     {
358         lcl_SetValue( mxDoc, *aVarIter, 1.0 );      // set to 1 to examine influence
359 
360         // read value change from all dependent cells
361         for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
362         {
363             double fChanged = lcl_GetValue( mxDoc, aCellsIter->first );
364             double fInitial = aCellsIter->second.front();
365             aCellsIter->second.push_back( fChanged - fInitial );
366         }
367 
368         lcl_SetValue( mxDoc, *aVarIter, 2.0 );      // minimal test for linearity
369 
370         for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
371         {
372             double fInitial = aCellsIter->second.front();
373             double fCoeff   = aCellsIter->second.back();       // last appended: coefficient for this variable
374             double fTwo     = lcl_GetValue( mxDoc, aCellsIter->first );
375 
376           if ( mbNonLinearTest )
377           {
378               bool bLinear ( sal_True );
379               bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) ||
380               rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff );
381             // second comparison is needed in case fTwo is zero
382               if ( !bLinear )
383                   maStatus = lcl_GetResourceString( RID_ERROR_NONLINEAR );
384            }
385         }
386 
387         lcl_SetValue( mxDoc, *aVarIter, 0.0 );      // set back to zero for examining next variable
388     }
389 
390     xModel->unlockControllers();
391 
392     if ( maStatus.getLength() )
393         return;
394 
395     //
396     // build parameter arrays for CoinMP
397     //
398 
399     // set objective function
400 
401     const std::vector<double>& rObjCoeff = aCellsHash[maObjective];
402     double* pObjectCoeffs = new double[nVariables];
403     for (nVar=0; nVar<nVariables; nVar++)
404         pObjectCoeffs[nVar] = rObjCoeff[nVar+1];
405     double nObjectConst = rObjCoeff[0];             // constant term of objective
406 
407     // add rows
408 
409     size_t nRows = maConstraints.getLength();
410     size_t nCompSize = nVariables * nRows;
411     double* pCompMatrix = new double[nCompSize];    // first collect all coefficients, row-wise
412     for (size_t i=0; i<nCompSize; i++)
413         pCompMatrix[i] = 0.0;
414 
415     double* pRHS = new double[nRows];
416     char* pRowType = new char[nRows];
417     for (size_t i=0; i<nRows; i++)
418     {
419         pRHS[i] = 0.0;
420         pRowType[i] = 'N';
421     }
422 
423     for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
424     {
425         // integer constraints are set later
426         sheet::SolverConstraintOperator eOp = maConstraints[nConstrPos].Operator;
427         if ( eOp == sheet::SolverConstraintOperator_LESS_EQUAL ||
428              eOp == sheet::SolverConstraintOperator_GREATER_EQUAL ||
429              eOp == sheet::SolverConstraintOperator_EQUAL )
430         {
431             double fDirectValue = 0.0;
432             bool bRightCell = false;
433             table::CellAddress aRightAddr;
434             const uno::Any& rRightAny = maConstraints[nConstrPos].Right;
435             if ( rRightAny >>= aRightAddr )
436                 bRightCell = true;                  // cell specified as right-hand side
437             else
438                 rRightAny >>= fDirectValue;         // constant value
439 
440             table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left;
441 
442             const std::vector<double>& rLeftCoeff = aCellsHash[aLeftAddr];
443             double* pValues = &pCompMatrix[nConstrPos * nVariables];
444             for (nVar=0; nVar<nVariables; nVar++)
445                 pValues[nVar] = rLeftCoeff[nVar+1];
446 
447             // if left hand cell has a constant term, put into rhs value
448             double fRightValue = -rLeftCoeff[0];
449 
450             if ( bRightCell )
451             {
452                 const std::vector<double>& rRightCoeff = aCellsHash[aRightAddr];
453                 // modify pValues with rhs coefficients
454                 for (nVar=0; nVar<nVariables; nVar++)
455                     pValues[nVar] -= rRightCoeff[nVar+1];
456 
457                 fRightValue += rRightCoeff[0];      // constant term
458             }
459             else
460                 fRightValue += fDirectValue;
461 
462             switch ( eOp )
463             {
464                 case sheet::SolverConstraintOperator_LESS_EQUAL:    pRowType[nConstrPos] = 'L'; break;
465                 case sheet::SolverConstraintOperator_GREATER_EQUAL: pRowType[nConstrPos] = 'G'; break;
466                 case sheet::SolverConstraintOperator_EQUAL:         pRowType[nConstrPos] = 'E'; break;
467                 default:
468                     OSL_ENSURE( false, "unexpected enum type" );
469             }
470             pRHS[nConstrPos] = fRightValue;
471         }
472     }
473 
474     // Find non-zero coefficients, column-wise
475 
476     int* pMatrixBegin = new int[nVariables+1];
477     int* pMatrixCount = new int[nVariables];
478     double* pMatrix = new double[nCompSize];    // not always completely used
479     int* pMatrixIndex = new int[nCompSize];
480     int nMatrixPos = 0;
481     for (nVar=0; nVar<nVariables; nVar++)
482     {
483         int nBegin = nMatrixPos;
484         for (size_t nRow=0; nRow<nRows; nRow++)
485         {
486             double fCoeff = pCompMatrix[ nRow * nVariables + nVar ];    // row-wise
487             if ( fCoeff != 0.0 )
488             {
489                 pMatrix[nMatrixPos] = fCoeff;
490                 pMatrixIndex[nMatrixPos] = nRow;
491                 ++nMatrixPos;
492             }
493         }
494         pMatrixBegin[nVar] = nBegin;
495         pMatrixCount[nVar] = nMatrixPos - nBegin;
496     }
497     pMatrixBegin[nVariables] = nMatrixPos;
498     delete[] pCompMatrix;
499     pCompMatrix = NULL;
500 
501     // apply settings to all variables
502 
503     double* pLowerBounds = new double[nVariables];
504     double* pUpperBounds = new double[nVariables];
505     for (nVar=0; nVar<nVariables; nVar++)
506     {
507         pLowerBounds[nVar] = mbNonNegative ? 0.0 : -DBL_MAX;
508         pUpperBounds[nVar] = DBL_MAX;
509 
510         // bounds could possibly be further restricted from single-cell constraints
511     }
512 
513     char* pColType = new char[nVariables];
514     for (nVar=0; nVar<nVariables; nVar++)
515         pColType[nVar] = mbInteger ? 'I' : 'C';
516 
517     // apply single-var integer constraints
518 
519     for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
520     {
521         sheet::SolverConstraintOperator eOp = maConstraints[nConstrPos].Operator;
522         if ( eOp == sheet::SolverConstraintOperator_INTEGER ||
523              eOp == sheet::SolverConstraintOperator_BINARY )
524         {
525             table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left;
526             // find variable index for cell
527             for (nVar=0; nVar<nVariables; nVar++)
528                 if ( AddressEqual( aVariableCells[nVar], aLeftAddr ) )
529                 {
530                     if ( eOp == sheet::SolverConstraintOperator_INTEGER )
531                         pColType[nVar] = 'I';
532                     else
533                     {
534                         pColType[nVar] = 'B';
535                         pLowerBounds[nVar] = 0.0;
536                         pUpperBounds[nVar] = 1.0;
537                     }
538                 }
539         }
540     }
541 
542     int nObjectSense = mbMaximize ? SOLV_OBJSENS_MAX : SOLV_OBJSENS_MIN;
543 
544     HPROB hProb = CoinCreateProblem("");
545     int nResult = CoinLoadProblem( hProb, nVariables, nRows, nMatrixPos, 0,
546                     nObjectSense, nObjectConst, pObjectCoeffs,
547                     pLowerBounds, pUpperBounds, pRowType, pRHS, NULL,
548                     pMatrixBegin, pMatrixCount, pMatrixIndex, pMatrix,
549                     NULL, NULL, NULL );
550     nResult = CoinLoadInteger( hProb, pColType );
551 
552     delete[] pColType;
553     delete[] pMatrixIndex;
554     delete[] pMatrix;
555     delete[] pMatrixCount;
556     delete[] pMatrixBegin;
557     delete[] pUpperBounds;
558     delete[] pLowerBounds;
559     delete[] pRowType;
560     delete[] pRHS;
561     delete[] pObjectCoeffs;
562 
563     CoinSetRealOption( hProb, COIN_REAL_MAXSECONDS, mnTimeout );
564     CoinSetRealOption( hProb, COIN_REAL_MIPMAXSEC, mnTimeout );
565 
566     // TODO: handle (or remove) settings: epsilon, B&B depth
567 
568     // solve model
569 
570     nResult = CoinCheckProblem( hProb );
571     nResult = CoinOptimizeProblem( hProb, 0 );
572 
573     mbSuccess = ( nResult == SOLV_CALL_SUCCESS );
574     if ( mbSuccess )
575     {
576         // get solution
577 
578         maSolution.realloc( nVariables );
579         CoinGetSolutionValues( hProb, maSolution.getArray(), NULL, NULL, NULL );
580         mfResultValue = CoinGetObjectValue( hProb );
581     }
582     else
583     {
584         int nSolutionStatus = CoinGetSolutionStatus( hProb );
585         if ( nSolutionStatus == 1 )
586             maStatus = lcl_GetResourceString( RID_ERROR_INFEASIBLE );
587         else if ( nSolutionStatus == 2 )
588             maStatus = lcl_GetResourceString( RID_ERROR_UNBOUNDED );
589         // TODO: detect timeout condition and report as RID_ERROR_TIMEOUT
590         // (currently reported as infeasible)
591     }
592 
593     CoinUnloadProblem( hProb );
594 }
595 
596 // -------------------------------------------------------------------------
597 
598 // XServiceInfo
599 
SolverComponent_getSupportedServiceNames()600 uno::Sequence< OUString > SolverComponent_getSupportedServiceNames()
601 {
602     uno::Sequence< OUString > aServiceNames( 1 );
603     aServiceNames[ 0 ] = OUString::createFromAscii( "com.sun.star.sheet.Solver" );
604     return aServiceNames;
605 }
606 
SolverComponent_getImplementationName()607 OUString SolverComponent_getImplementationName()
608 {
609     return OUString::createFromAscii( "com.sun.star.comp.Calc.Solver" );
610 }
611 
getImplementationName()612 OUString SAL_CALL SolverComponent::getImplementationName() throw(uno::RuntimeException)
613 {
614     return SolverComponent_getImplementationName();
615 }
616 
supportsService(const OUString & rServiceName)617 sal_Bool SAL_CALL SolverComponent::supportsService( const OUString& rServiceName ) throw(uno::RuntimeException)
618 {
619     const uno::Sequence< OUString > aServices = SolverComponent_getSupportedServiceNames();
620     const OUString* pArray = aServices.getConstArray();
621     const OUString* pArrayEnd = pArray + aServices.getLength();
622     return ::std::find( pArray, pArrayEnd, rServiceName ) != pArrayEnd;
623 }
624 
getSupportedServiceNames()625 uno::Sequence<OUString> SAL_CALL SolverComponent::getSupportedServiceNames() throw(uno::RuntimeException)
626 {
627     return SolverComponent_getSupportedServiceNames();
628 }
629 
SolverComponent_createInstance(const uno::Reference<uno::XComponentContext> & rSMgr)630 uno::Reference<uno::XInterface> SolverComponent_createInstance( const uno::Reference<uno::XComponentContext>& rSMgr )
631     throw(uno::Exception)
632 {
633 	return (cppu::OWeakObject*) new SolverComponent( rSMgr );
634 }
635 
636 // -------------------------------------------------------------------------
637 
638 extern "C"
639 {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)640     SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
641         const sal_Char ** ppEnvTypeName, uno_Environment ** )
642     {
643         *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
644     }
645 
646     // -------------------------------------------------------------------------
647 
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)648     SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
649     {
650         OUString    aImplName( OUString::createFromAscii( pImplName ) );
651         void*       pRet = 0;
652 
653         if( pServiceManager )
654         {
655             uno::Reference< lang::XSingleComponentFactory > xFactory;
656             if( aImplName.equals( SolverComponent_getImplementationName() ) )
657                 xFactory = cppu::createSingleComponentFactory(
658                         SolverComponent_createInstance,
659                         OUString::createFromAscii( pImplName ),
660                         SolverComponent_getSupportedServiceNames() );
661 
662             if( xFactory.is() )
663             {
664                 xFactory->acquire();
665                 pRet = xFactory.get();
666             }
667         }
668         return pRet;
669     }
670 }
671 
672