LibreOffice Module sc (master) 1
SolverSettings.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11#pragma once
12
13#include <memory>
14#include <utility>
15#include <variant>
16#include <rtl/ustring.hxx>
17#include <com/sun/star/beans/PropertyValue.hpp>
18
19class ScTable;
20class ScDocShell;
21
22namespace sc
23{
24// These values are MS compatible
26{
29 OT_VALUE = 3
30};
31
33{
34 SP_OBJ_CELL, // Objective cell
35 SP_OBJ_TYPE, // Objective type (max, min, value)
36 SP_OBJ_VAL, // Value (used when objective is of type "value")
37 SP_VAR_CELLS, // Variable cells
38 SP_CONSTR_COUNT, // Number of constraints (MSO only)
39 SP_LO_ENGINE, // Engine name used in LO
40 SP_MS_ENGINE, // Engine ID used in MSO
41 SP_INTEGER, // Assume all variables are integer (0: no, 1: yes)
42 SP_NON_NEGATIVE, // Assume non negativity (1: yes, 2: no)
43 SP_EPSILON_LEVEL, // Epsilon level
44 SP_LIMIT_BBDEPTH, // Branch and bound depth
45 SP_TIMEOUT, // Time limit to return a solution
46 SP_ALGORITHM // Algorithm used by the SwarmSolver (1, 2 or 3)
47};
48
49// Starts at 1 to maintain MS compatibility
51{
56 CO_BINARY = 5
57};
58
59// Parts of a constraint
61{
65};
66
67// Stores the information of a single constraint (condition)
69{
70 OUString aLeftStr;
72 OUString aRightStr;
73
76 {
77 }
78 bool IsDefault() const
79 {
80 return aLeftStr.isEmpty() && aRightStr.isEmpty() && nOperator == CO_LESS_EQUAL;
81 }
82};
83
84/* Class SolverSettings
85 *
86 * This class is used to load/save and manipulate solver settings in a Calc tab.
87 *
88 * During initialization, (see Initialize() method) all settings stored in the tab are loaded onto
89 * the object. Settings that are not defined use default values.
90 *
91 * Read/Write methods are private and are used internally to load/write solver settings from
92 * named ranges associated with the sheet.
93 *
94 * Get/Set methods are public methods used to change object properties (they do not save data
95 * to the file).
96 *
97 * The method SaveSolverSettings() is used to create the named ranges containing the current
98 * property values into the file.
99 *
100 */
101
103{
104private:
108
109 // Used to read/write the named ranges in the tab
111
112 OUString m_sObjCell;
114 OUString m_sObjVal;
118
119 // Solver engine options
120 OUString m_sInteger;
124 OUString m_sTimeout;
125 OUString m_sAlgorithm;
126 css::uno::Sequence<css::beans::PropertyValue> m_aEngineOptions;
127
128 std::vector<ModelConstraint> m_aConstraints;
129
130 void Initialize();
131
132 // Used to create or read a single solver parameter based on its named range
133 bool ReadParamValue(SolverParameter eParam, OUString& rValue, bool bRemoveQuotes = false);
134 void WriteParamValue(SolverParameter eParam, OUString sValue, bool bQuoted = false);
135
136 // Creates or reads all constraints stored in named ranges
137 void ReadConstraints();
138 void WriteConstraints();
139
140 // Used to create or get a single constraint part
141 bool ReadConstraintPart(ConstraintPart ePart, tools::Long nIndex, OUString& rValue);
142 void WriteConstraintPart(ConstraintPart ePart, tools::Long nIndex, OUString sValue);
143
144 // Creates or reads all named ranges associated with solver engine options
145 void ReadEngine();
146 void WriteEngine();
147
149
150 // Maps solver parameters to named ranges
151 std::map<SolverParameter, OUString> m_mNamedRanges
152 = { { SP_OBJ_CELL, "solver_opt" }, { SP_OBJ_TYPE, "solver_typ" },
153 { SP_OBJ_VAL, "solver_val" }, { SP_VAR_CELLS, "solver_adj" },
154 { SP_CONSTR_COUNT, "solver_num" }, { SP_LO_ENGINE, "solver_lo_eng" },
155 { SP_MS_ENGINE, "solver_eng" }, { SP_INTEGER, "solver_int" },
156 { SP_NON_NEGATIVE, "solver_neg" }, { SP_EPSILON_LEVEL, "solver_eps" },
157 { SP_LIMIT_BBDEPTH, "solver_bbd" }, { SP_TIMEOUT, "solver_tim" },
158 { SP_ALGORITHM, "solver_alg" } };
159
160 // Maps LO solver implementation names to MS engine codes
161 std::map<OUString, OUString> SolverNamesToExcelEngines = {
162 { "com.sun.star.comp.Calc.CoinMPSolver", "2" }, // Simplex LP
163 { "com.sun.star.comp.Calc.LpsolveSolver", "2" }, // Simplex LP
164 { "com.sun.star.comp.Calc.SwarmSolver", "1" } // GRG Nonlinear
165 };
166
167 // Maps MS solver engine codes to LO solver implementation names
168 std::map<OUString, OUString> SolverCodesToLOEngines = {
169 { "1", "com.sun.star.comp.Calc.SwarmSolver" }, // GRG Nonlinear
170 { "2", "com.sun.star.comp.Calc.CoinMPSolver" }, // Simplex LP
171 { "3", "com.sun.star.comp.Calc.SwarmSolver" } // Evolutionary
172 };
173
174 // Maps LO solver parameters to named ranges to be used
175 // NonNegative: for MS compatibility, use 1 for selected and 2 for not selected
176 typedef std::vector<std::variant<OUString, SolverParameter>> TParamInfo;
177 std::map<OUString, TParamInfo> SolverParamNames
178 = { { "Integer", { SP_INTEGER, "solver_int", "bool" } },
179 { "NonNegative", { SP_NON_NEGATIVE, "solver_neg", "bool" } },
180 { "EpsilonLevel", { SP_EPSILON_LEVEL, "solver_eps", "int" } },
181 { "LimitBBDepth", { SP_LIMIT_BBDEPTH, "solver_bbd", "bool" } },
182 { "Timeout", { SP_TIMEOUT, "solver_tim", "int" } },
183 { "Algorithm", { SP_ALGORITHM, "solver_alg", "int" } } };
184
185 // Stores the roots used for named ranges of constraint parts
186 // Items here must be in the same order as in ConstraintPart enum
187 std::vector<OUString> m_aConstraintParts{ "solver_lhs", "solver_rel", "solver_rhs" };
188
189public:
190 /* A SolverSettings object is linked to the ScTable where solver parameters
191 * are located and saved to */
192 SolverSettings(ScTable& pTable);
193
195 SC_DLLPUBLIC void SetParameter(SolverParameter eParam, OUString sValue);
198 SC_DLLPUBLIC void GetEngineOptions(css::uno::Sequence<css::beans::PropertyValue>& aOptions);
199 SC_DLLPUBLIC void SetEngineOptions(css::uno::Sequence<css::beans::PropertyValue>& aOptions);
200 SC_DLLPUBLIC std::vector<ModelConstraint> GetConstraints() { return m_aConstraints; }
201 SC_DLLPUBLIC void SetConstraints(std::vector<ModelConstraint> aConstraints);
202
205};
206
207} // namespace sc
208
209/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SC_DLLPUBLIC void GetEngineOptions(css::uno::Sequence< css::beans::PropertyValue > &aOptions)
SC_DLLPUBLIC std::vector< ModelConstraint > GetConstraints()
bool ReadConstraintPart(ConstraintPart ePart, tools::Long nIndex, OUString &rValue)
std::map< OUString, TParamInfo > SolverParamNames
std::vector< OUString > m_aConstraintParts
ScRangeName * m_pRangeName
SolverSettings(ScTable &pTable)
css::uno::Sequence< css::beans::PropertyValue > m_aEngineOptions
std::map< OUString, OUString > SolverNamesToExcelEngines
SC_DLLPUBLIC OUString GetParameter(SolverParameter eParam)
SC_DLLPUBLIC void ResetToDefaults()
std::map< SolverParameter, OUString > m_mNamedRanges
SC_DLLPUBLIC ObjectiveType GetObjectiveType()
SC_DLLPUBLIC void SetObjectiveType(ObjectiveType eType)
SC_DLLPUBLIC void SetConstraints(std::vector< ModelConstraint > aConstraints)
void WriteConstraintPart(ConstraintPart ePart, tools::Long nIndex, OUString sValue)
bool ReadParamValue(SolverParameter eParam, OUString &rValue, bool bRemoveQuotes=false)
SC_DLLPUBLIC void SetEngineOptions(css::uno::Sequence< css::beans::PropertyValue > &aOptions)
ScDocShell * m_pDocShell
ObjectiveType m_eObjType
std::vector< std::variant< OUString, SolverParameter > > TParamInfo
SC_DLLPUBLIC void SetParameter(SolverParameter eParam, OUString sValue)
SC_DLLPUBLIC void SaveSolverSettings()
void WriteParamValue(SolverParameter eParam, OUString sValue, bool bQuoted=false)
std::vector< ModelConstraint > m_aConstraints
std::map< OUString, OUString > SolverCodesToLOEngines
CAUTION! The following defines must be in the same namespace as the respective type.
Definition: broadcast.cxx:15
SolverParameter
@ SP_OBJ_VAL
@ SP_MS_ENGINE
@ SP_OBJ_CELL
@ SP_CONSTR_COUNT
@ SP_TIMEOUT
@ SP_OBJ_TYPE
@ SP_LO_ENGINE
@ SP_EPSILON_LEVEL
@ SP_LIMIT_BBDEPTH
@ SP_VAR_CELLS
@ SP_ALGORITHM
@ SP_INTEGER
@ SP_NON_NEGATIVE
@ OT_VALUE
@ OT_MAXIMIZE
@ OT_MINIMIZE
ConstraintOperator
@ CO_EQUAL
@ CO_GREATER_EQUAL
@ CO_INTEGER
@ CO_LESS_EQUAL
@ CO_BINARY
ConstraintPart
@ CP_OPERATOR
@ CP_LEFT_HAND_SIDE
@ CP_RIGHT_HAND_SIDE
long Long
#define SC_DLLPUBLIC
Definition: scdllapi.h:27
bool IsDefault() const
ConstraintOperator nOperator