bump product version to 7.6.3.2-android
[LibreOffice.git] / sc / inc / SolverSettings.hxx
blobec1ef994a7b8f0ec0a2ad1462b1d358e50900583
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 */
11 #pragma once
13 #include <memory>
14 #include <utility>
15 #include <variant>
16 #include <rtl/ustring.hxx>
17 #include <com/sun/star/beans/PropertyValue.hpp>
19 class ScTable;
20 class ScDocShell;
22 namespace sc
24 // These values are MS compatible
25 enum ObjectiveType
27 OT_MAXIMIZE = 1,
28 OT_MINIMIZE = 2,
29 OT_VALUE = 3
32 enum SolverParameter
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)
49 // Starts at 1 to maintain MS compatibility
50 enum ConstraintOperator
52 CO_LESS_EQUAL = 1,
53 CO_EQUAL = 2,
54 CO_GREATER_EQUAL = 3,
55 CO_INTEGER = 4,
56 CO_BINARY = 5
59 // Parts of a constraint
60 enum ConstraintPart
62 CP_LEFT_HAND_SIDE,
63 CP_OPERATOR,
64 CP_RIGHT_HAND_SIDE
67 // Stores the information of a single constraint (condition)
68 struct ModelConstraint
70 OUString aLeftStr;
71 ConstraintOperator nOperator;
72 OUString aRightStr;
74 ModelConstraint()
75 : nOperator(CO_LESS_EQUAL)
78 bool IsDefault() const
80 return aLeftStr.isEmpty() && aRightStr.isEmpty() && nOperator == CO_LESS_EQUAL;
84 /* Class SolverSettings
86 * This class is used to load/save and manipulate solver settings in a Calc tab.
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.
91 * Read/Write methods are private and are used internally to load/write solver settings from
92 * named ranges associated with the sheet.
94 * Get/Set methods are public methods used to change object properties (they do not save data
95 * to the file).
97 * The method SaveSolverSettings() is used to create the named ranges containing the current
98 * property values into the file.
102 class SolverSettings
104 private:
105 ScTable& m_rTable;
106 ScDocument& m_rDoc;
107 ScDocShell* m_pDocShell;
109 // Used to read/write the named ranges in the tab
110 ScRangeName* m_pRangeName;
112 OUString m_sObjCell;
113 ObjectiveType m_eObjType;
114 OUString m_sObjVal;
115 OUString m_sVariableCells;
116 OUString m_sLOEngineName;
117 OUString m_sMSEngineId;
119 // Solver engine options
120 OUString m_sInteger;
121 OUString m_sNonNegative;
122 OUString m_sEpsilonLevel;
123 OUString m_sLimitBBDepth;
124 OUString m_sTimeout;
125 OUString m_sAlgorithm;
126 css::uno::Sequence<css::beans::PropertyValue> m_aEngineOptions;
128 std::vector<ModelConstraint> m_aConstraints;
130 void Initialize();
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);
136 // Creates or reads all constraints stored in named ranges
137 void ReadConstraints();
138 void WriteConstraints();
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);
144 // Creates or reads all named ranges associated with solver engine options
145 void ReadEngine();
146 void WriteEngine();
148 void DeleteAllNamedRanges();
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" } };
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
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
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" } } };
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" };
189 public:
190 /* A SolverSettings object is linked to the ScTable where solver parameters
191 * are located and saved to */
192 SolverSettings(ScTable& pTable);
194 SC_DLLPUBLIC OUString GetParameter(SolverParameter eParam);
195 SC_DLLPUBLIC void SetParameter(SolverParameter eParam, OUString sValue);
196 SC_DLLPUBLIC ObjectiveType GetObjectiveType() { return m_eObjType; }
197 SC_DLLPUBLIC void SetObjectiveType(ObjectiveType eType);
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);
203 SC_DLLPUBLIC void SaveSolverSettings();
204 SC_DLLPUBLIC void ResetToDefaults();
207 } // namespace sc
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */