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