/* -*- 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/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ #include #include #include "SolverComponent.hxx" #include "solver.hrc" #include #include #include #include #include #include using namespace com::sun::star; class CoinMPSolver : public SolverComponent { public: CoinMPSolver() {} virtual ~CoinMPSolver() {} private: virtual void SAL_CALL solve() throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE; virtual OUString SAL_CALL getImplementationName() throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE { return OUString("com.sun.star.comp.Calc.CoinMPSolver"); } virtual OUString SAL_CALL getComponentDescription() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return SolverComponent::GetResourceString( RID_COINMP_SOLVER_COMPONENT ); } }; void SAL_CALL CoinMPSolver::solve() throw(uno::RuntimeException, std::exception) { uno::Reference xModel( mxDoc, uno::UNO_QUERY ); if ( !xModel.is() ) throw uno::RuntimeException(); maStatus.clear(); mbSuccess = false; xModel->lockControllers(); // collect variables in vector (?) std::vector aVariableCells; for (sal_Int32 nPos=0; nPos>= aCellAddr ) aCellsHash[aCellAddr].reserve( nVariables + 1 ); // constraints: right hand side } // set all variables to zero //! store old values? //! use old values as initial values? std::vector::const_iterator aVarIter; for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter ) { SolverComponent::SetValue( mxDoc, *aVarIter, 0.0 ); } // read initial values from all dependent cells ScSolverCellHashMap::iterator aCellsIter; for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) { double fValue = SolverComponent::GetValue( mxDoc, aCellsIter->first ); aCellsIter->second.push_back( fValue ); // store as first element, as-is } // loop through variables for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter ) { SolverComponent::SetValue( mxDoc, *aVarIter, 1.0 ); // set to 1 to examine influence // read value change from all dependent cells for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) { double fChanged = SolverComponent::GetValue( mxDoc, aCellsIter->first ); double fInitial = aCellsIter->second.front(); aCellsIter->second.push_back( fChanged - fInitial ); } SolverComponent::SetValue( mxDoc, *aVarIter, 2.0 ); // minimal test for linearity for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) { double fInitial = aCellsIter->second.front(); double fCoeff = aCellsIter->second.back(); // last appended: coefficient for this variable double fTwo = SolverComponent::GetValue( mxDoc, aCellsIter->first ); bool bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) || rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff ); // second comparison is needed in case fTwo is zero if ( !bLinear ) maStatus = SolverComponent::GetResourceString( RID_ERROR_NONLINEAR ); } SolverComponent::SetValue( mxDoc, *aVarIter, 0.0 ); // set back to zero for examining next variable } xModel->unlockControllers(); if ( !maStatus.isEmpty() ) return; // // build parameter arrays for CoinMP // // set objective function const std::vector& rObjCoeff = aCellsHash[maObjective]; double* pObjectCoeffs = new double[nVariables]; for (nVar=0; nVar>= aRightAddr ) bRightCell = true; // cell specified as right-hand side else rRightAny >>= fDirectValue; // constant value table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left; const std::vector& rLeftCoeff = aCellsHash[aLeftAddr]; double* pValues = &pCompMatrix[nConstrPos * nVariables]; for (nVar=0; nVar& rRightCoeff = aCellsHash[aRightAddr]; // modify pValues with rhs coefficients for (nVar=0; nVar const &) { return cppu::acquire(new CoinMPSolver()); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */