sc: read and write solver cells through ScDocument, not the UNO API
The solver components reached every cell through the spreadsheet UNO API: getSheets, then getByIndex for the sheet, then getCellByPosition, each with a QueryInterface. That indirection is cheap once but expensive when it runs for every variable on every evaluation. Now that the solvers live in sc, recover the ScDocument from the document through the UNO tunnel (ScModelObj) in setDocument and read and write cells directly with ScDocument::SetValue and GetValue. Automatic recalculation is turned on there as well, so writing the input cells and then reading the recalculated objective and constraint cells behaves exactly as before. Both the shared SolverComponent base (used by the CoinMP solver) and the standalone SwarmSolver are converted. The results are unchanged because the recalculation engine is the same. Only the cost of getting values in and out of the document drops. The swarm solver evaluates the objective for a whole population every generation, so this is its hot path: on a 100-variable model its evaluation loop runs about four times faster. CoinMP touches the cells only once, in a single pass that reads off the linear coefficients, so it gains less, about 1.7 times faster on the same model. Library_solver now links sc and includes sc/inc. SwarmSolver keeps the UNO cell lookup only for the uncommon case where a constraint's right-hand side is given as a cell address. Signed-off-by: Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> Change-Id: I03ccec74cdc4d6cde30c86e048bf4314c3ca1397 Reviewed-on: https://gerrit.collaboraoffice.com/c/online/+/4047 Reviewed-by: Miklos Vajna <vmiklos@collabora.com> Tested-by: Tomaž Vajngerl <tomaz.vajngerl@collabora.com> (cherry picked from commit 929c17cf8e51a1510c32399de4a94ba946bb9919) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/208281 Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org> Tested-by: Jenkins
This commit is contained in:
committed by
Xisco Fauli
parent
fc54f170b8
commit
72da83e66f
@@ -29,6 +29,7 @@ $(eval $(call gb_Library_use_sdk_api,solver))
|
||||
|
||||
$(eval $(call gb_Library_set_include,solver,\
|
||||
$$(INCLUDE) \
|
||||
-I$(SRCDIR)/sc/inc \
|
||||
-I$(SRCDIR)/sc/source/solver \
|
||||
))
|
||||
|
||||
@@ -37,11 +38,13 @@ $(eval $(call gb_Library_use_libraries,solver,\
|
||||
cppu \
|
||||
cppuhelper \
|
||||
sal \
|
||||
sc \
|
||||
utl \
|
||||
i18nlangtag \
|
||||
))
|
||||
|
||||
$(eval $(call gb_Library_use_externals,solver,\
|
||||
boost_headers \
|
||||
coinmp \
|
||||
))
|
||||
|
||||
|
||||
@@ -90,36 +90,36 @@ void SAL_CALL CoinMPSolver::solve()
|
||||
//! use old values as initial values?
|
||||
for ( const auto& rVarCell : aVariableCells )
|
||||
{
|
||||
SolverComponent::SetValue( mxDoc, rVarCell, 0.0 );
|
||||
SetValue(rVarCell, 0.0);
|
||||
}
|
||||
|
||||
// read initial values from all dependent cells
|
||||
for ( auto& rEntry : aCellsHash )
|
||||
{
|
||||
double fValue = SolverComponent::GetValue( mxDoc, rEntry.first );
|
||||
double fValue = GetValue(rEntry.first);
|
||||
rEntry.second.push_back( fValue ); // store as first element, as-is
|
||||
}
|
||||
|
||||
// loop through variables
|
||||
for ( const auto& rVarCell : aVariableCells )
|
||||
{
|
||||
SolverComponent::SetValue( mxDoc, rVarCell, 1.0 ); // set to 1 to examine influence
|
||||
SetValue(rVarCell, 1.0); // set to 1 to examine influence
|
||||
|
||||
// read value change from all dependent cells
|
||||
for ( auto& rEntry : aCellsHash )
|
||||
{
|
||||
double fChanged = SolverComponent::GetValue( mxDoc, rEntry.first );
|
||||
double fChanged = GetValue(rEntry.first);
|
||||
double fInitial = rEntry.second.front();
|
||||
rEntry.second.push_back( fChanged - fInitial );
|
||||
}
|
||||
|
||||
SolverComponent::SetValue( mxDoc, rVarCell, 2.0 ); // minimal test for linearity
|
||||
SetValue(rVarCell, 2.0); // minimal test for linearity
|
||||
|
||||
for ( const auto& rEntry : aCellsHash )
|
||||
{
|
||||
double fInitial = rEntry.second.front();
|
||||
double fCoeff = rEntry.second.back(); // last appended: coefficient for this variable
|
||||
double fTwo = SolverComponent::GetValue( mxDoc, rEntry.first );
|
||||
double fTwo = GetValue(rEntry.first);
|
||||
|
||||
bool bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) ||
|
||||
rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff );
|
||||
@@ -128,7 +128,7 @@ void SAL_CALL CoinMPSolver::solve()
|
||||
maStatus = SolverComponent::GetResourceString( RID_ERROR_NONLINEAR );
|
||||
}
|
||||
|
||||
SolverComponent::SetValue( mxDoc, rVarCell, 0.0 ); // set back to zero for examining next variable
|
||||
SetValue(rVarCell, 0.0); // set back to zero for examining next variable
|
||||
}
|
||||
|
||||
xModel->unlockControllers();
|
||||
|
||||
@@ -26,9 +26,14 @@
|
||||
#include <com/sun/star/table/CellAddress.hpp>
|
||||
|
||||
#include <cppuhelper/supportsservice.hxx>
|
||||
#include <comphelper/servicehelper.hxx>
|
||||
|
||||
#include <unotools/resmgr.hxx>
|
||||
|
||||
#include <docuno.hxx>
|
||||
#include <document.hxx>
|
||||
#include <address.hxx>
|
||||
|
||||
using namespace com::sun::star;
|
||||
|
||||
|
||||
@@ -72,24 +77,21 @@ namespace
|
||||
};
|
||||
}
|
||||
|
||||
uno::Reference<table::XCell> SolverComponent::GetCell( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
|
||||
const table::CellAddress& rPos )
|
||||
void SolverComponent::SetValue(const table::CellAddress& rPosition, double fValue)
|
||||
{
|
||||
uno::Reference<container::XIndexAccess> xSheets( xDoc->getSheets(), uno::UNO_QUERY );
|
||||
uno::Reference<sheet::XSpreadsheet> xSheet( xSheets->getByIndex( rPos.Sheet ), uno::UNO_QUERY );
|
||||
return xSheet->getCellByPosition( rPos.Column, rPos.Row );
|
||||
if (mpDocument)
|
||||
mpDocument->SetValue(ScAddress(SCCOL(rPosition.Column),
|
||||
SCROW(rPosition.Row),
|
||||
SCTAB(rPosition.Sheet)), fValue);
|
||||
}
|
||||
|
||||
void SolverComponent::SetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
|
||||
const table::CellAddress& rPos, double fValue )
|
||||
double SolverComponent::GetValue(const table::CellAddress& rPosition)
|
||||
{
|
||||
SolverComponent::GetCell( xDoc, rPos )->setValue( fValue );
|
||||
}
|
||||
|
||||
double SolverComponent::GetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
|
||||
const table::CellAddress& rPos )
|
||||
{
|
||||
return SolverComponent::GetCell( xDoc, rPos )->getValue();
|
||||
if (!mpDocument)
|
||||
return 0.0;
|
||||
return mpDocument->GetValue(ScAddress(SCCOL(rPosition.Column),
|
||||
SCROW(rPosition.Row),
|
||||
SCTAB(rPosition.Sheet)));
|
||||
}
|
||||
|
||||
SolverComponent::SolverComponent() :
|
||||
@@ -178,6 +180,15 @@ uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocumen
|
||||
void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document )
|
||||
{
|
||||
mxDoc = _document;
|
||||
mpDocument = nullptr;
|
||||
if (ScModelObj* pModel = comphelper::getFromUnoTunnel<ScModelObj>(mxDoc))
|
||||
{
|
||||
mpDocument = pModel->GetDocument();
|
||||
// The solver writes input cells and reads the recalculated objective and
|
||||
// constraint cells, which needs automatic recalculation to be on.
|
||||
if ( mpDocument )
|
||||
mpDocument->SetAutoCalc( true );
|
||||
}
|
||||
}
|
||||
|
||||
table::CellAddress SAL_CALL SolverComponent::getObjective()
|
||||
|
||||
@@ -32,6 +32,8 @@
|
||||
|
||||
namespace com::sun::star::table { class XCell; }
|
||||
|
||||
class ScDocument;
|
||||
|
||||
// hash map for the coefficients of a dependent cell (objective or constraint)
|
||||
// The size of each vector is the number of columns (variable cells) plus one, first entry is initial value.
|
||||
|
||||
@@ -63,6 +65,9 @@ class SolverComponent
|
||||
protected:
|
||||
// settings
|
||||
css::uno::Reference< css::sheet::XSpreadsheetDocument > mxDoc;
|
||||
// The ScDocument behind mxDoc, recovered through the UNO tunnel. Null until
|
||||
// setDocument runs.
|
||||
ScDocument* mpDocument = nullptr;
|
||||
css::table::CellAddress maObjective;
|
||||
css::uno::Sequence< css::table::CellAddress > maVariables;
|
||||
css::uno::Sequence< css::sheet::SolverConstraint > maConstraints;
|
||||
@@ -93,15 +98,10 @@ protected:
|
||||
css::sheet::SensitivityReport m_aSensitivityReport;
|
||||
|
||||
static OUString GetResourceString(TranslateId aId);
|
||||
static css::uno::Reference<css::table::XCell> GetCell(
|
||||
const css::uno::Reference<css::sheet::XSpreadsheetDocument>& xDoc,
|
||||
const css::table::CellAddress& rPos );
|
||||
static void SetValue(
|
||||
const css::uno::Reference<css::sheet::XSpreadsheetDocument>& xDoc,
|
||||
const css::table::CellAddress& rPos, double fValue );
|
||||
static double GetValue(
|
||||
const css::uno::Reference<css::sheet::XSpreadsheetDocument>& xDoc,
|
||||
const css::table::CellAddress& rPos );
|
||||
|
||||
// Direct cell access through mpDocument.
|
||||
void SetValue(const css::table::CellAddress& rPosition, double fValue);
|
||||
double GetValue(const css::table::CellAddress& rPosition);
|
||||
|
||||
public:
|
||||
SolverComponent();
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <com/sun/star/table/CellContentType.hpp>
|
||||
#include <com/sun/star/table/XCell.hpp>
|
||||
#include <com/sun/star/lang/XServiceInfo.hpp>
|
||||
#include <com/sun/star/uno/RuntimeException.hpp>
|
||||
|
||||
#include <rtl/math.hxx>
|
||||
#include <cppuhelper/implbase.hxx>
|
||||
@@ -34,6 +35,11 @@
|
||||
#include <random>
|
||||
|
||||
#include <unotools/resmgr.hxx>
|
||||
#include <comphelper/servicehelper.hxx>
|
||||
|
||||
#include <docuno.hxx>
|
||||
#include <document.hxx>
|
||||
#include <address.hxx>
|
||||
|
||||
#include "DifferentialEvolution.hxx"
|
||||
#include "ParticelSwarmOptimization.hxx"
|
||||
@@ -103,6 +109,8 @@ class SwarmSolver
|
||||
{
|
||||
private:
|
||||
uno::Reference<sheet::XSpreadsheetDocument> mxDocument;
|
||||
// The ScDocument implementation behind mxDocument
|
||||
ScDocument* mpDocument = nullptr;
|
||||
table::CellAddress maObjective;
|
||||
uno::Sequence<table::CellAddress> maVariables;
|
||||
uno::Sequence<sheet::SolverConstraint> maConstraints;
|
||||
@@ -168,6 +176,15 @@ public:
|
||||
setDocument(const uno::Reference<sheet::XSpreadsheetDocument>& rDocument) override
|
||||
{
|
||||
mxDocument = rDocument;
|
||||
mpDocument = nullptr;
|
||||
if (ScModelObj* pModel = comphelper::getFromUnoTunnel<ScModelObj>(mxDocument))
|
||||
{
|
||||
mpDocument = pModel->GetDocument();
|
||||
// The solver writes input cells and reads the recalculated objective
|
||||
// and constraint cells, which needs automatic recalculation on.
|
||||
if (mpDocument)
|
||||
mpDocument->SetAutoCalc(true);
|
||||
}
|
||||
}
|
||||
|
||||
virtual table::CellAddress SAL_CALL getObjective() override { return maObjective; }
|
||||
@@ -281,14 +298,29 @@ uno::Reference<table::XCell> SwarmSolver::getCell(const table::CellAddress& rPos
|
||||
return xSheet->getCellByPosition(rPosition.Column, rPosition.Row);
|
||||
}
|
||||
|
||||
// Build a document cell address from the API address, throwing if its sheet is
|
||||
// outside the document. Access through the spreadsheet UNO objects used to throw
|
||||
// on a missing sheet. Direct ScDocument access does not, so validate here to keep
|
||||
// an invalid variable cell an error rather than a silently ignored write.
|
||||
static ScAddress lcl_cellAddress(const ScDocument& rDocument, const table::CellAddress& rPosition)
|
||||
{
|
||||
if (rPosition.Sheet < 0 || rPosition.Sheet >= rDocument.GetTableCount())
|
||||
throw uno::RuntimeException(u"solver: cell address is outside the document"_ustr);
|
||||
return ScAddress(static_cast<SCCOL>(rPosition.Column), static_cast<SCROW>(rPosition.Row),
|
||||
static_cast<SCTAB>(rPosition.Sheet));
|
||||
}
|
||||
|
||||
void SwarmSolver::setValue(const table::CellAddress& rPosition, double fValue)
|
||||
{
|
||||
getCell(rPosition)->setValue(fValue);
|
||||
if (mpDocument)
|
||||
mpDocument->SetValue(lcl_cellAddress(*mpDocument, rPosition), fValue);
|
||||
}
|
||||
|
||||
double SwarmSolver::getValue(const table::CellAddress& rPosition)
|
||||
{
|
||||
return getCell(rPosition)->getValue();
|
||||
if (!mpDocument)
|
||||
return 0.0;
|
||||
return mpDocument->GetValue(lcl_cellAddress(*mpDocument, rPosition));
|
||||
}
|
||||
|
||||
void SwarmSolver::applyVariables(std::vector<double> const& rVariables)
|
||||
|
||||
@@ -180,7 +180,6 @@ gb_MERGE_LIBRARY_LIST += \
|
||||
$(if $(filter WNT,$(OS)), \
|
||||
smplmail \
|
||||
) \
|
||||
solver \
|
||||
storagefd \
|
||||
svgfilter \
|
||||
t602filter \
|
||||
|
||||
Reference in New Issue
Block a user