Files
odfstudio/sc/source/solver/CoinMPModel.hxx
T
Tomaž Vajngerl 2c87d392dc solvers: extract the CoinMP problem build and solve into a helper
CoinMPSolver did the whole job in one function: sample the model by
perturbing each variable, build the CoinMP matrix from those
coefficients, solve, and read the result back. Move the matrix
build and solve into a standalone helper, coinmpSolveLinearModel,
that  takes the sampled coefficients plus the variable bounds and
column  types and returns the solution and status.

The helper treats the first entry of each cell's coefficient vector
as the value of an affine model at the origin and the rest as the
per variable slopes, which is what the existing sampling already
produces. That keeps the same convention while letting a future
caller supply coefficients sampled around a different point and its
own bounds, which is what a sequential linear programming loop
needs.

No behaviour change. CoinMPSolver still samples and sets up bounds
and column types exactly as before, then calls the helper. The
CoinMP solver unit test passes unchanged.

Signed-off-by: Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>
Change-Id: I6d373a48caa9d67a932a599068368bae978f1f22
Reviewed-on: https://gerrit.collaboraoffice.com/c/online/+/4648
Tested-by: Tomaž Vajngerl <tomaz.vajngerl@collabora.com>
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
(cherry picked from commit 197f790a93880f56dd53db43e6bbb61c22ccfdc2)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/208337
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
Tested-by: Xisco Fauli <xiscofauli@libreoffice.org>
2026-07-21 11:39:34 +02:00

47 lines
1.9 KiB
C++

/* -*- 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/.
*
*/
#pragma once
#include <com/sun/star/sheet/SolverConstraint.hpp>
#include <com/sun/star/table/CellAddress.hpp>
#include <com/sun/star/uno/Sequence.hxx>
#include "SolverComponent.hxx"
#include <vector>
// Result of solving one linear problem with CoinMP.
struct CoinMpSolveResult
{
bool bSuccess = false;
// CoinGetSolutionStatus when the solve did not succeed: 1 infeasible,
// 2 unbounded, other values for the remaining cases.
int nStatus = 0;
std::vector<double> aSolution;
double fObjective = 0.0;
};
// Build a linear problem from already sampled coefficients and solve it with
// CoinMP. For the objective and for each constraint cell, rCells holds a vector
// whose first entry is the value of an affine model of that cell at the origin
// (all variables zero) and whose remaining entries are the slope of each
// variable. The caller supplies the variable bounds and column types (which
// encode integer and binary variables), so the same routine serves both a
// one-shot linear solve and a linearized step of an outer nonlinear loop.
CoinMpSolveResult coinmpSolveLinearModel(
const css::uno::Sequence<css::table::CellAddress>& rVariables,
const css::uno::Sequence<css::sheet::SolverConstraint>& rConstraints,
const css::table::CellAddress& rObjective, const ScSolverCellHashMap& rCells,
const std::vector<double>& rLowerBounds, const std::vector<double>& rUpperBounds,
const std::vector<char>& rColumnType, bool bMaximize, double fTimeoutSeconds);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */