/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /************************************************************************* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Copyright 2000, 2010 Oracle and/or its affiliates. * * OpenOffice.org - a multi-platform office productivity suite * * This file is part of OpenOffice.org. * * OpenOffice.org is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * only, as published by the Free Software Foundation. * * OpenOffice.org is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License version 3 for more details * (a copy is included in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see * * for a copy of the LGPLv3 License. * * 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 #undef LANGUAGE_NONE #if defined _WIN32 #define WINAPI __stdcall #endif #define LoadInverseLib FALSE #define LoadLanguageLib FALSE #ifdef SYSTEM_LPSOLVE #include #else #include #endif #undef LANGUAGE_NONE #include "SolverComponent.hxx" #include #include #include #include #include #include #include namespace com::sun::star::uno { class XComponentContext; } using namespace com::sun::star; namespace { class LpsolveSolver : public SolverComponent { public: LpsolveSolver() {} private: virtual void SAL_CALL solve() override; virtual OUString SAL_CALL getImplementationName() override { return "com.sun.star.comp.Calc.LpsolveSolver"; } virtual OUString SAL_CALL getComponentDescription() override { return SolverComponent::GetResourceString( RID_SOLVER_COMPONENT ); } }; } void SAL_CALL LpsolveSolver::solve() { uno::Reference xModel( mxDoc, uno::UNO_QUERY_THROW ); maStatus.clear(); mbSuccess = false; if ( mnEpsilonLevel < EPS_TIGHT || mnEpsilonLevel > EPS_BAGGY ) { maStatus = SolverComponent::GetResourceString( RID_ERROR_EPSILONLEVEL ); return; } xModel->lockControllers(); // collect variables in vector (?) const auto & aVariableCells = maVariables; size_t nVariables = aVariableCells.size(); size_t nVar = 0; // collect all dependent cells ScSolverCellHashMap aCellsHash; aCellsHash[maObjective].reserve( nVariables + 1 ); // objective function for (const auto& rConstr : maConstraints) { table::CellAddress aCellAddr = rConstr.Left; aCellsHash[aCellAddr].reserve( nVariables + 1 ); // constraints: left hand side if ( rConstr.Right >>= aCellAddr ) aCellsHash[aCellAddr].reserve( nVariables + 1 ); // constraints: right hand side } // set all variables to zero //! store old values? //! use old values as initial values? for ( const auto& rVarCell : aVariableCells ) { SolverComponent::SetValue( mxDoc, rVarCell, 0.0 ); } // read initial values from all dependent cells for ( auto& rEntry : aCellsHash ) { double fValue = SolverComponent::GetValue( mxDoc, 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 // read value change from all dependent cells for ( auto& rEntry : aCellsHash ) { double fChanged = SolverComponent::GetValue( mxDoc, rEntry.first ); double fInitial = rEntry.second.front(); rEntry.second.push_back( fChanged - fInitial ); } SolverComponent::SetValue( mxDoc, 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 ); 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, rVarCell, 0.0 ); // set back to zero for examining next variable } xModel->unlockControllers(); if ( !maStatus.isEmpty() ) return; // build lp_solve model lprec* lp = make_lp( 0, nVariables ); if ( !lp ) return; set_outputfile( lp, const_cast( "" ) ); // no output // set objective function const std::vector& rObjCoeff = aCellsHash[maObjective]; std::unique_ptr pObjVal(new REAL[nVariables+1]); pObjVal[0] = 0.0; // ignored for (nVar=0; nVar>= aRightAddr ) bRightCell = true; // cell specified as right-hand side else rRightAny >>= fDirectValue; // constant value table::CellAddress aLeftAddr = rConstr.Left; const std::vector& rLeftCoeff = aCellsHash[aLeftAddr]; std::unique_ptr pValues(new REAL[nVariables+1] ); pValues[0] = 0.0; // ignored? for (nVar=0; nVar& rRightCoeff = aCellsHash[aRightAddr]; // modify pValues with rhs coefficients for (nVar=0; nVar const &) { return cppu::acquire(new LpsolveSolver()); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */