diff options
Diffstat (limited to 'sccomp/qa')
-rw-r--r-- | sccomp/qa/unit/SwarmSolverTest.cxx | 354 | ||||
-rw-r--r-- | sccomp/qa/unit/data/MultiVariable.ods | bin | 0 -> 7754 bytes | |||
-rw-r--r-- | sccomp/qa/unit/data/Simple.ods | bin | 0 -> 21518 bytes | |||
-rw-r--r-- | sccomp/qa/unit/data/TwoVariables.ods | bin | 0 -> 7449 bytes | |||
-rw-r--r-- | sccomp/qa/unit/solver.cxx | 125 |
5 files changed, 479 insertions, 0 deletions
diff --git a/sccomp/qa/unit/SwarmSolverTest.cxx b/sccomp/qa/unit/SwarmSolverTest.cxx new file mode 100644 index 000000000..250309051 --- /dev/null +++ b/sccomp/qa/unit/SwarmSolverTest.cxx @@ -0,0 +1,354 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <com/sun/star/sheet/XSolver.hpp> +#include <com/sun/star/sheet/XSpreadsheetDocument.hpp> +#include <com/sun/star/sheet/XSpreadsheet.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> + +#include <test/calc_unoapi_test.hxx> + +using namespace css; + +namespace +{ +class SwarmSolverTest : public CalcUnoApiTest +{ + uno::Reference<lang::XComponent> mxComponent; + void testUnconstrained(); + void testVariableBounded(); + void testVariableConstrained(); + void testTwoVariables(); + void testMultipleVariables(); + +public: + SwarmSolverTest() + : CalcUnoApiTest("sccomp/qa/unit/data") + { + } + + virtual void tearDown() override; + + CPPUNIT_TEST_SUITE(SwarmSolverTest); + CPPUNIT_TEST(testUnconstrained); + CPPUNIT_TEST(testVariableBounded); + CPPUNIT_TEST(testVariableConstrained); + CPPUNIT_TEST(testMultipleVariables); + CPPUNIT_TEST(testTwoVariables); + CPPUNIT_TEST_SUITE_END(); +}; + +void SwarmSolverTest::tearDown() +{ + if (mxComponent.is()) + closeDocument(mxComponent); +} + +void SwarmSolverTest::testUnconstrained() +{ + CPPUNIT_ASSERT(!mxComponent.is()); + + OUString aFileURL; + createFileURL(u"Simple.ods", aFileURL); + mxComponent = loadFromDesktop(aFileURL); + + uno::Reference<sheet::XSpreadsheetDocument> xDocument(mxComponent, uno::UNO_QUERY_THROW); + uno::Reference<container::XIndexAccess> xIndex(xDocument->getSheets(), uno::UNO_QUERY_THROW); + uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW); + + uno::Reference<sheet::XSolver> xSolver; + + xSolver.set(m_xContext->getServiceManager()->createInstanceWithContext( + "com.sun.star.comp.Calc.SwarmSolver", m_xContext), + uno::UNO_QUERY_THROW); + + table::CellAddress aObjective(0, 1, 1); + + // "changing cells" - unknown variables + uno::Sequence<table::CellAddress> aVariables{ { 0, 1, 0 } }; + + // constraints + uno::Sequence<sheet::SolverConstraint> aConstraints; + + // initialize solver + xSolver->setDocument(xDocument); + xSolver->setObjective(aObjective); + xSolver->setVariables(aVariables); + xSolver->setConstraints(aConstraints); + xSolver->setMaximize(false); + + // test results + xSolver->solve(); + CPPUNIT_ASSERT(xSolver->getSuccess()); + uno::Sequence<double> aSolution = xSolver->getSolution(); + + CPPUNIT_ASSERT_EQUAL(aVariables.getLength(), aSolution.getLength()); + // It happens that the unconstrained test does not find a solution in the + // timeframe or number of generations it has available as the search space is + // too big and the values might not converge to solution. So for now just run + // the test so we know for sure the algorithm is guaranteed to finish + // and doesn't cause any seg faults. + //CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, aSolution[0], .9); +} + +void SwarmSolverTest::testVariableBounded() +{ + CPPUNIT_ASSERT(!mxComponent.is()); + + OUString aFileURL; + createFileURL(u"Simple.ods", aFileURL); + mxComponent = loadFromDesktop(aFileURL); + + uno::Reference<sheet::XSpreadsheetDocument> xDocument(mxComponent, uno::UNO_QUERY_THROW); + uno::Reference<container::XIndexAccess> xIndex(xDocument->getSheets(), uno::UNO_QUERY_THROW); + uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW); + + uno::Reference<sheet::XSolver> xSolver; + + xSolver.set(m_xContext->getServiceManager()->createInstanceWithContext( + "com.sun.star.comp.Calc.SwarmSolver", m_xContext), + uno::UNO_QUERY_THROW); + + table::CellAddress aObjective(0, 1, 1); + + // "changing cells" - unknown variables + uno::Sequence<table::CellAddress> aVariables{ { 0, 1, 0 } }; + + // constraints + uno::Sequence<sheet::SolverConstraint> aConstraints{ + { /* [0] Left */ table::CellAddress(0, 1, 0), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(100.0) }, + { /* [1] Left */ table::CellAddress(0, 1, 0), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(-100.0) } + }; + + // initialize solver + xSolver->setDocument(xDocument); + xSolver->setObjective(aObjective); + xSolver->setVariables(aVariables); + xSolver->setConstraints(aConstraints); + xSolver->setMaximize(false); + + // test results + xSolver->solve(); + CPPUNIT_ASSERT(xSolver->getSuccess()); + uno::Sequence<double> aSolution = xSolver->getSolution(); + + CPPUNIT_ASSERT_EQUAL(aVariables.getLength(), aSolution.getLength()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, aSolution[0], 1E-5); +} + +void SwarmSolverTest::testVariableConstrained() +{ + CPPUNIT_ASSERT(!mxComponent.is()); + + OUString aFileURL; + createFileURL(u"Simple.ods", aFileURL); + mxComponent = loadFromDesktop(aFileURL); + + uno::Reference<sheet::XSpreadsheetDocument> xDocument(mxComponent, uno::UNO_QUERY_THROW); + uno::Reference<container::XIndexAccess> xIndex(xDocument->getSheets(), uno::UNO_QUERY_THROW); + uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW); + + uno::Reference<sheet::XSolver> xSolver; + + xSolver.set(m_xContext->getServiceManager()->createInstanceWithContext( + "com.sun.star.comp.Calc.SwarmSolver", m_xContext), + uno::UNO_QUERY_THROW); + + table::CellAddress aObjective(0, 1, 1); + + // "changing cells" - unknown variables + uno::Sequence<table::CellAddress> aVariables{ { 0, 1, 0 } }; + + // constraints + uno::Sequence<sheet::SolverConstraint> aConstraints{ + { /* [0] Left */ table::CellAddress(0, 1, 0), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(-50000.0) }, + { /* [1] Left */ table::CellAddress(0, 1, 0), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(0.0) }, + { /* [2] Left */ table::CellAddress(0, 1, 1), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(10.0) } + }; + + // initialize solver + xSolver->setDocument(xDocument); + xSolver->setObjective(aObjective); + xSolver->setVariables(aVariables); + xSolver->setConstraints(aConstraints); + xSolver->setMaximize(false); + + // test results + xSolver->solve(); + CPPUNIT_ASSERT(xSolver->getSuccess()); + uno::Sequence<double> aSolution = xSolver->getSolution(); + + CPPUNIT_ASSERT_EQUAL(aVariables.getLength(), aSolution.getLength()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.741657, aSolution[0], 1E-5); +} + +void SwarmSolverTest::testTwoVariables() +{ + CPPUNIT_ASSERT(!mxComponent.is()); + + OUString aFileURL; + createFileURL(u"TwoVariables.ods", aFileURL); + mxComponent = loadFromDesktop(aFileURL); + + uno::Reference<sheet::XSpreadsheetDocument> xDocument(mxComponent, uno::UNO_QUERY_THROW); + uno::Reference<container::XIndexAccess> xIndex(xDocument->getSheets(), uno::UNO_QUERY_THROW); + uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW); + + uno::Reference<sheet::XSolver> xSolver; + + xSolver.set(m_xContext->getServiceManager()->createInstanceWithContext( + "com.sun.star.comp.Calc.SwarmSolver", m_xContext), + uno::UNO_QUERY_THROW); + + table::CellAddress aObjective(0, 1, 5); + + // "changing cells" - unknown variables + uno::Sequence<table::CellAddress> aVariables{ { 0, 1, 2 }, { 0, 1, 3 } }; + + // constraints + uno::Sequence<sheet::SolverConstraint> aConstraints{ + { /* [0] Left */ table::CellAddress(0, 1, 2), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(-100.0) }, + { /* [1] Left */ table::CellAddress(0, 1, 3), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(-100.0) }, + { /* [2] Left */ table::CellAddress(0, 1, 2), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(100.0) }, + { /* [3] Left */ table::CellAddress(0, 1, 3), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(100.0) } + }; + + // initialize solver + xSolver->setDocument(xDocument); + xSolver->setObjective(aObjective); + xSolver->setVariables(aVariables); + xSolver->setConstraints(aConstraints); + xSolver->setMaximize(true); + + // test results + xSolver->solve(); + CPPUNIT_ASSERT(xSolver->getSuccess()); + uno::Sequence<double> aSolution = xSolver->getSolution(); + + CPPUNIT_ASSERT_EQUAL(aVariables.getLength(), aSolution.getLength()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.666667, aSolution[0], 1E-5); + CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.666667, aSolution[1], 1E-5); +} + +void SwarmSolverTest::testMultipleVariables() +{ + CPPUNIT_ASSERT(!mxComponent.is()); + + OUString aFileURL; + createFileURL(u"MultiVariable.ods", aFileURL); + mxComponent = loadFromDesktop(aFileURL); + + uno::Reference<sheet::XSpreadsheetDocument> xDocument(mxComponent, uno::UNO_QUERY_THROW); + uno::Reference<container::XIndexAccess> xIndex(xDocument->getSheets(), uno::UNO_QUERY_THROW); + uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW); + + uno::Reference<sheet::XSolver> xSolver; + + xSolver.set(m_xContext->getServiceManager()->createInstanceWithContext( + "com.sun.star.comp.Calc.SwarmSolver", m_xContext), + uno::UNO_QUERY_THROW); + + uno::Reference<beans::XPropertySet> xPropSet(xSolver, uno::UNO_QUERY_THROW); + xPropSet->setPropertyValue("Integer", uno::Any(true)); + + table::CellAddress aObjective(0, 5, 7); + + // "changing cells" - unknown variables + uno::Sequence<table::CellAddress> aVariables{ + { 0, 6, 1 }, { 0, 6, 2 }, { 0, 6, 3 }, { 0, 6, 4 } + }; + + // constraints + uno::Sequence<sheet::SolverConstraint> aConstraints{ + { /* [ 0] Left */ table::CellAddress(0, 1, 5), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(table::CellAddress(0, 1, 6)) }, + { /* [ 1] Left */ table::CellAddress(0, 2, 5), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(table::CellAddress(0, 2, 6)) }, + { /* [ 2] Left */ table::CellAddress(0, 3, 5), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(table::CellAddress(0, 3, 6)) }, + { /* [ 3] Left */ table::CellAddress(0, 4, 5), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(table::CellAddress(0, 4, 6)) }, + { /* [ 4] Left */ table::CellAddress(0, 6, 1), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(0.0) }, + { /* [ 5] Left */ table::CellAddress(0, 6, 2), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(0.0) }, + { /* [ 6] Left */ table::CellAddress(0, 6, 3), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(0.0) }, + { /* [ 7] Left */ table::CellAddress(0, 6, 4), + /* Operator */ sheet::SolverConstraintOperator_GREATER_EQUAL, + /* Right */ uno::Any(0.0) }, + { /* [ 8] Left */ table::CellAddress(0, 6, 1), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(10000.0) }, + { /* [ 9] Left */ table::CellAddress(0, 6, 2), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(10000.0) }, + { /* [10] Left */ table::CellAddress(0, 6, 3), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(10000.0) }, + { /* [11] Left */ table::CellAddress(0, 6, 4), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(10000.0) } + }; + + // initialize solver + xSolver->setDocument(xDocument); + xSolver->setObjective(aObjective); + xSolver->setVariables(aVariables); + xSolver->setConstraints(aConstraints); + xSolver->setMaximize(false); + + // test results + xSolver->solve(); + CPPUNIT_ASSERT(xSolver->getSuccess()); + uno::Sequence<double> aSolution = xSolver->getSolution(); + + CPPUNIT_ASSERT_EQUAL(aVariables.getLength(), aSolution.getLength()); +#if 0 + // Disable for now, needs algorithm stability improvements + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, aSolution[0], 1E-5); + CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, aSolution[1], 1E-5); + CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, aSolution[2], 1E-5); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, aSolution[3], 1E-5); +#endif +} + +CPPUNIT_TEST_SUITE_REGISTRATION(SwarmSolverTest); +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sccomp/qa/unit/data/MultiVariable.ods b/sccomp/qa/unit/data/MultiVariable.ods Binary files differnew file mode 100644 index 000000000..1e9db736a --- /dev/null +++ b/sccomp/qa/unit/data/MultiVariable.ods diff --git a/sccomp/qa/unit/data/Simple.ods b/sccomp/qa/unit/data/Simple.ods Binary files differnew file mode 100644 index 000000000..2ac8224c0 --- /dev/null +++ b/sccomp/qa/unit/data/Simple.ods diff --git a/sccomp/qa/unit/data/TwoVariables.ods b/sccomp/qa/unit/data/TwoVariables.ods Binary files differnew file mode 100644 index 000000000..c27e362e0 --- /dev/null +++ b/sccomp/qa/unit/data/TwoVariables.ods diff --git a/sccomp/qa/unit/solver.cxx b/sccomp/qa/unit/solver.cxx new file mode 100644 index 000000000..c4df7b644 --- /dev/null +++ b/sccomp/qa/unit/solver.cxx @@ -0,0 +1,125 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/sheet/XSolver.hpp> +#include <com/sun/star/sheet/XSolverDescription.hpp> +#include <test/bootstrapfixture.hxx> + +using namespace css; + +namespace { + +class LpSolverTest: public test::BootstrapFixture +{ + uno::Reference<sheet::XSpreadsheetDocument> m_xDocument; + +#ifdef ENABLE_LPSOLVE + void testLpSolver(); +#endif +#ifdef ENABLE_COINMP + void testCoinMPSolver(); +#endif + +#if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP) + void testSolver(OUString const & rName); +#endif + +public: + virtual void setUp() override; + virtual void tearDown() override; + + CPPUNIT_TEST_SUITE(LpSolverTest); +#ifdef ENABLE_LPSOLVE + CPPUNIT_TEST(testLpSolver); +#endif +#ifdef ENABLE_COINMP + CPPUNIT_TEST(testCoinMPSolver); +#endif + CPPUNIT_TEST_SUITE_END(); +}; + +void LpSolverTest::setUp() +{ + test::BootstrapFixture::setUp(); + uno::Reference<frame::XDesktop2> xComponentLoader = frame::Desktop::create(m_xContext); + uno::Reference<lang::XComponent> xComponent(xComponentLoader->loadComponentFromURL( + "private:factory/scalc", "_blank", 0, + uno::Sequence < css::beans::PropertyValue >())); + m_xDocument.set(xComponent, uno::UNO_QUERY_THROW); +} + +void LpSolverTest::tearDown() +{ + uno::Reference<lang::XComponent>(m_xDocument, uno::UNO_QUERY_THROW)->dispose(); + m_xDocument.clear(); + test::BootstrapFixture::tearDown(); +} + +#ifdef ENABLE_LPSOLVE +void LpSolverTest::testLpSolver() +{ + testSolver("com.sun.star.comp.Calc.LpsolveSolver"); +} +#endif + +#ifdef ENABLE_COINMP +void LpSolverTest::testCoinMPSolver() +{ + testSolver("com.sun.star.comp.Calc.CoinMPSolver"); +} +#endif + +#if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP) +void LpSolverTest::testSolver(OUString const & rName) +{ + uno::Reference<sheet::XSolver> xSolver(m_xContext->getServiceManager()-> + createInstanceWithContext(rName, m_xContext), uno::UNO_QUERY_THROW); + + table::CellAddress aObjective(0, 0, 0); + + // "changing cells" - unknown variables + uno::Sequence<table::CellAddress> aVariables { {0, 0, 0 } }; + + // constraints + uno::Sequence<sheet::SolverConstraint> aConstraints{ + { /* Left */ table::CellAddress(0, 0, 0), + /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL, + /* Right */ uno::Any(5.0) } + }; + + // initialize solver + xSolver->setDocument( m_xDocument ); + xSolver->setObjective( aObjective ); + xSolver->setVariables( aVariables ); + xSolver->setConstraints( aConstraints ); + xSolver->setMaximize( true ); + + // test results + xSolver->solve(); + CPPUNIT_ASSERT(xSolver->getSuccess()); + uno::Sequence<double> aSolution = xSolver->getSolution(); + CPPUNIT_ASSERT_EQUAL(aSolution.getLength(), aVariables.getLength()); + CPPUNIT_ASSERT_EQUAL(aSolution[0], 5.0); + + uno::Reference<sheet::XSolverDescription> xDesc(xSolver, uno::UNO_QUERY_THROW); + const OString sMessage("Empty description for " + OUStringToOString(rName, RTL_TEXTENCODING_UTF8)); + CPPUNIT_ASSERT_MESSAGE(sMessage.getStr(), !xDesc->getComponentDescription().isEmpty()); +} +#endif + +CPPUNIT_TEST_SUITE_REGISTRATION(LpSolverTest); + +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |