1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
16 #include <rtl/ustring.hxx>
17 #include <com/sun/star/beans/PropertyValue.hpp>
24 // These values are MS compatible
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 // LpSolve, CoinMP and SwarmSolver
43 SP_NON_NEGATIVE
, // Assume non negativity (1: yes, 2: no)
44 SP_EPSILON_LEVEL
, // Epsilon level
45 SP_LIMIT_BBDEPTH
, // Branch and bound depth
46 SP_TIMEOUT
, // Time limit to return a solution
47 SP_ALGORITHM
, // Algorithm used by the SwarmSolver (1, 2 or 3)
48 // Engine options common for DEPS and SCO
49 SP_SWARM_SIZE
, // Size of Swarm
50 SP_LEARNING_CYCLES
, // Learning Cycles
51 SP_GUESS_VARIABLE_RANGE
, // Variable Bounds Guessing
52 SP_VARIABLE_RANGE_THRESHOLD
, // Variable Bounds Threshold (when guessing)
53 SP_ACR_COMPARATOR
, // Use ACR Comparator (instead of BCH)
54 SP_RND_STARTING_POINT
, // Use Random starting point
55 SP_STRONGER_PRNG
, // Use a stronger random generator (slower)
56 SP_STAGNATION_LIMIT
, // Stagnation Limit
57 SP_STAGNATION_TOLERANCE
, // Stagnation Tolerance
58 SP_ENHANCED_STATUS
, // Show enhanced solver status
60 SP_AGENT_SWITCH_RATE
, // Agent Switch Rate (DE Probability)
61 SP_SCALING_MIN
, // DE: Min Scaling Factor (0-1.2)
62 SP_SCALING_MAX
, // DE: Max Scaling Factor (0-1.2)
63 SP_CROSSOVER_PROB
, // DE: Crossover Probability (0-1)
64 SP_COGNITIVE_CONST
, // Cognitive Constant
65 SP_SOCIAL_CONST
, // Social Constant
66 SP_CONSTRICTION_COEFF
, // PS: Constriction Coefficient
67 SP_MUTATION_PROB
, // Mutation Probability (0-0.005)
69 SP_LIBRARY_SIZE
, // Size of library
72 // Starts at 1 to maintain MS compatibility
73 enum ConstraintOperator
82 // Parts of a constraint
90 // Stores the information of a single constraint (condition)
91 struct ModelConstraint
94 ConstraintOperator nOperator
;
98 : nOperator(CO_LESS_EQUAL
)
101 bool IsDefault() const
103 return aLeftStr
.isEmpty() && aRightStr
.isEmpty() && nOperator
== CO_LESS_EQUAL
;
107 /* Class SolverSettings
109 * This class is used to load/save and manipulate solver settings in a Calc tab.
111 * During initialization, (see Initialize() method) all settings stored in the tab are loaded onto
112 * the object. Settings that are not defined use default values.
114 * Read/Write methods are private and are used internally to load/write solver settings from
115 * named ranges associated with the sheet.
117 * Get/Set methods are public methods used to change object properties (they do not save data
120 * The method SaveSolverSettings() is used to create the named ranges containing the current
121 * property values into the file.
130 ScDocShell
* m_pDocShell
;
132 // Used to read/write the named ranges in the tab
133 ScRangeName
* m_pRangeName
;
136 ObjectiveType m_eObjType
;
138 OUString m_sVariableCells
;
139 OUString m_sLOEngineName
;
140 OUString m_sMSEngineId
;
142 // Solver engine options
144 OUString m_sNonNegative
;
145 OUString m_sEpsilonLevel
;
146 OUString m_sLimitBBDepth
;
148 OUString m_sAlgorithm
;
150 OUString m_sSwarmSize
;
151 OUString m_sLearningCycles
;
152 OUString m_sGuessVariableRange
;
153 OUString m_sVariableRangeThreshold
;
154 OUString m_sUseACRComparator
;
155 OUString m_sUseRandomStartingPoint
;
156 OUString m_sUseStrongerPRNG
;
157 OUString m_sStagnationLimit
;
158 OUString m_sTolerance
;
159 OUString m_sEnhancedSolverStatus
;
161 OUString m_sAgentSwitchRate
;
162 OUString m_sScalingFactorMin
;
163 OUString m_sScalingFactorMax
;
164 OUString m_sCrossoverProbability
;
165 OUString m_sCognitiveConstant
;
166 OUString m_sSocialConstant
;
167 OUString m_sConstrictionCoeff
;
168 OUString m_sMutationProbability
;
169 OUString m_sLibrarySize
;
171 css::uno::Sequence
<css::beans::PropertyValue
> m_aEngineOptions
;
173 std::vector
<ModelConstraint
> m_aConstraints
;
177 // Used to create or read a single solver parameter based on its named range
178 bool ReadParamValue(SolverParameter eParam
, OUString
& rValue
, bool bRemoveQuotes
= false);
179 bool ReadDoubleParamValue(SolverParameter eParam
, OUString
& rValue
);
180 void WriteParamValue(SolverParameter eParam
, OUString sValue
, bool bQuoted
= false);
181 void WriteDoubleParamValue(SolverParameter eParam
, std::u16string_view sValue
);
183 // Creates or reads all constraints stored in named ranges
184 void ReadConstraints();
185 void WriteConstraints();
187 // Used to create or get a single constraint part
188 bool ReadConstraintPart(ConstraintPart ePart
, tools::Long nIndex
, OUString
& rValue
);
189 void WriteConstraintPart(ConstraintPart ePart
, tools::Long nIndex
, OUString sValue
);
191 // Creates or reads all named ranges associated with solver engine options
195 void DeleteAllNamedRanges();
197 // Maps solver parameters to named ranges
198 std::map
<SolverParameter
, OUString
> m_mNamedRanges
199 = { { SP_OBJ_CELL
, "solver_opt" },
200 { SP_OBJ_TYPE
, "solver_typ" },
201 { SP_OBJ_VAL
, "solver_val" },
202 { SP_VAR_CELLS
, "solver_adj" },
203 { SP_CONSTR_COUNT
, "solver_num" },
204 { SP_LO_ENGINE
, "solver_lo_eng" },
205 { SP_MS_ENGINE
, "solver_eng" },
206 { SP_INTEGER
, "solver_int" },
207 { SP_NON_NEGATIVE
, "solver_neg" },
208 { SP_EPSILON_LEVEL
, "solver_eps" },
209 { SP_LIMIT_BBDEPTH
, "solver_bbd" },
210 { SP_TIMEOUT
, "solver_tim" },
211 { SP_ALGORITHM
, "solver_alg" },
212 { SP_SWARM_SIZE
, "solver_ssz" },
213 { SP_LEARNING_CYCLES
, "solver_lcy" },
214 { SP_GUESS_VARIABLE_RANGE
, "solver_gvr" },
215 { SP_VARIABLE_RANGE_THRESHOLD
, "solver_vrt" },
216 { SP_ACR_COMPARATOR
, "solver_acr" },
217 { SP_RND_STARTING_POINT
, "solver_rsp" },
218 { SP_STRONGER_PRNG
, "solver_prng" },
219 { SP_STAGNATION_LIMIT
, "solver_slim" },
220 { SP_STAGNATION_TOLERANCE
, "solver_stol" },
221 { SP_ENHANCED_STATUS
, "solver_enst" },
222 { SP_AGENT_SWITCH_RATE
, "solver_asr" },
223 { SP_SCALING_MIN
, "solver_smin" },
224 { SP_SCALING_MAX
, "solver_smax" },
225 { SP_CROSSOVER_PROB
, "solver_crpb" },
226 { SP_COGNITIVE_CONST
, "solver_cog" },
227 { SP_SOCIAL_CONST
, "solver_soc" },
228 { SP_CONSTRICTION_COEFF
, "solver_ccoeff" },
229 { SP_MUTATION_PROB
, "solver_mtpb" },
230 { SP_LIBRARY_SIZE
, "solver_lbsz" } };
232 // Maps LO solver implementation names to MS engine codes
233 std::map
<OUString
, OUString
> SolverNamesToExcelEngines
= {
234 { "com.sun.star.comp.Calc.CoinMPSolver", "2" }, // Simplex LP
235 { "com.sun.star.comp.Calc.LpsolveSolver", "2" }, // Simplex LP
236 { "com.sun.star.comp.Calc.SwarmSolver", "1" }, // GRG Nonlinear
237 { "com.sun.star.comp.Calc.NLPSolver.DEPSSolverImpl", "3" }, // DEPS
238 { "com.sun.star.comp.Calc.NLPSolver.SCOSolverImpl", "3" } // SCO
241 // Maps MS solver engine codes to LO solver implementation names
242 std::map
<OUString
, OUString
> SolverCodesToLOEngines
= {
243 { "1", "com.sun.star.comp.Calc.SwarmSolver" }, // GRG Nonlinear
244 { "2", "com.sun.star.comp.Calc.CoinMPSolver" }, // Simplex LP
245 { "3", "com.sun.star.comp.Calc.SwarmSolver" } // Evolutionary
248 // Maps LO solver parameters to named ranges to be used
249 // NonNegative: for MS compatibility, use 1 for selected and 2 for not selected
250 typedef std::vector
<std::variant
<OUString
, SolverParameter
>> TParamInfo
;
251 std::map
<OUString
, TParamInfo
> SolverParamNames
252 = { { "Integer", { SP_INTEGER
, "solver_int", "bool" } },
253 { "NonNegative", { SP_NON_NEGATIVE
, "solver_neg", "bool" } },
254 { "EpsilonLevel", { SP_EPSILON_LEVEL
, "solver_eps", "int" } },
255 { "LimitBBDepth", { SP_LIMIT_BBDEPTH
, "solver_bbd", "bool" } },
256 { "Timeout", { SP_TIMEOUT
, "solver_tim", "int" } },
257 { "Algorithm", { SP_ALGORITHM
, "solver_alg", "int" } },
259 { "AssumeNonNegative", { SP_NON_NEGATIVE
, "solver_neg", "bool" } },
260 { "SwarmSize", { SP_SWARM_SIZE
, "solver_ssz", "int" } },
261 { "LearningCycles", { SP_LEARNING_CYCLES
, "solver_lcy", "int" } },
262 { "GuessVariableRange", { SP_GUESS_VARIABLE_RANGE
, "solver_gvr", "bool" } },
263 { "VariableRangeThreshold", { SP_VARIABLE_RANGE_THRESHOLD
, "solver_vrt", "double" } },
264 { "UseACRComparator", { SP_ACR_COMPARATOR
, "solver_acr", "bool" } },
265 { "UseRandomStartingPoint", { SP_RND_STARTING_POINT
, "solver_rsp", "bool" } },
266 { "UseStrongerPRNG", { SP_STRONGER_PRNG
, "solver_prng", "bool" } },
267 { "StagnationLimit", { SP_STAGNATION_LIMIT
, "solver_slim", "int" } },
268 { "Tolerance", { SP_STAGNATION_TOLERANCE
, "solver_stol", "double" } },
269 { "EnhancedSolverStatus", { SP_ENHANCED_STATUS
, "solver_enst", "bool" } },
271 { "AgentSwitchRate", { SP_AGENT_SWITCH_RATE
, "solver_asr", "double" } },
272 { "DEFactorMin", { SP_SCALING_MIN
, "solver_smin", "double" } },
273 { "DEFactorMax", { SP_SCALING_MAX
, "solver_smax", "double" } },
274 { "DECR", { SP_CROSSOVER_PROB
, "solver_crpb", "double" } },
275 { "PSC1", { SP_COGNITIVE_CONST
, "solver_cog", "double" } },
276 { "PSC2", { SP_SOCIAL_CONST
, "solver_soc", "double" } },
277 { "PSWeight", { SP_CONSTRICTION_COEFF
, "solver_ccoeff", "double" } },
278 { "PSCL", { SP_MUTATION_PROB
, "solver_mtpb", "double" } },
280 { "LibrarySize", { SP_LIBRARY_SIZE
, "solver_lbsz", "int" } } };
282 // Stores the roots used for named ranges of constraint parts
283 // Items here must be in the same order as in ConstraintPart enum
284 std::vector
<OUString
> m_aConstraintParts
{ "solver_lhs", "solver_rel", "solver_rhs" };
287 /* A SolverSettings object is linked to the ScTable where solver parameters
288 * are located and saved to */
289 SolverSettings(ScTable
& pTable
);
291 SC_DLLPUBLIC OUString
GetParameter(SolverParameter eParam
);
292 SC_DLLPUBLIC
void SetParameter(SolverParameter eParam
, OUString sValue
);
293 SC_DLLPUBLIC ObjectiveType
GetObjectiveType() { return m_eObjType
; }
294 SC_DLLPUBLIC
void SetObjectiveType(ObjectiveType eType
);
295 SC_DLLPUBLIC
void GetEngineOptions(css::uno::Sequence
<css::beans::PropertyValue
>& aOptions
);
296 SC_DLLPUBLIC
void SetEngineOptions(css::uno::Sequence
<css::beans::PropertyValue
>& aOptions
);
297 SC_DLLPUBLIC
std::vector
<ModelConstraint
> GetConstraints() { return m_aConstraints
; }
298 SC_DLLPUBLIC
void SetConstraints(std::vector
<ModelConstraint
> aConstraints
);
300 SC_DLLPUBLIC
void SaveSolverSettings();
301 SC_DLLPUBLIC
void ResetToDefaults();
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */