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/.
15 #include <rtl/ustring.hxx>
16 #include <com/sun/star/beans/PropertyValue.hpp>
18 #include "document.hxx"
23 // These values are MS compatible
33 SP_OBJ_CELL
, // Objective cell
34 SP_OBJ_TYPE
, // Objective type (max, min, value)
35 SP_OBJ_VAL
, // Value (used when objective is of type "value")
36 SP_VAR_CELLS
, // Variable cells
37 SP_CONSTR_COUNT
, // Number of constraints (MSO only)
38 SP_LO_ENGINE
, // Engine name used in LO
39 SP_MS_ENGINE
, // Engine ID used in MSO
40 SP_INTEGER
, // Assume all variables are integer (0: no, 1: yes)
41 // LpSolve, CoinMP and SwarmSolver
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 // Engine options common for DEPS and SCO
48 SP_SWARM_SIZE
, // Size of Swarm
49 SP_LEARNING_CYCLES
, // Learning Cycles
50 SP_GUESS_VARIABLE_RANGE
, // Variable Bounds Guessing
51 SP_VARIABLE_RANGE_THRESHOLD
, // Variable Bounds Threshold (when guessing)
52 SP_ACR_COMPARATOR
, // Use ACR Comparator (instead of BCH)
53 SP_RND_STARTING_POINT
, // Use Random starting point
54 SP_STRONGER_PRNG
, // Use a stronger random generator (slower)
55 SP_STAGNATION_LIMIT
, // Stagnation Limit
56 SP_STAGNATION_TOLERANCE
, // Stagnation Tolerance
57 SP_ENHANCED_STATUS
, // Show enhanced solver status
59 SP_AGENT_SWITCH_RATE
, // Agent Switch Rate (DE Probability)
60 SP_SCALING_MIN
, // DE: Min Scaling Factor (0-1.2)
61 SP_SCALING_MAX
, // DE: Max Scaling Factor (0-1.2)
62 SP_CROSSOVER_PROB
, // DE: Crossover Probability (0-1)
63 SP_COGNITIVE_CONST
, // Cognitive Constant
64 SP_SOCIAL_CONST
, // Social Constant
65 SP_CONSTRICTION_COEFF
, // PS: Constriction Coefficient
66 SP_MUTATION_PROB
, // Mutation Probability (0-0.005)
68 SP_LIBRARY_SIZE
, // Size of library
71 // Starts at 1 to maintain MS compatibility
72 enum ConstraintOperator
81 // Parts of a constraint
89 // Stores the information of a single constraint (condition)
90 struct ModelConstraint
93 ConstraintOperator nOperator
;
97 : nOperator(CO_LESS_EQUAL
)
100 bool IsDefault() const
102 return aLeftStr
.isEmpty() && aRightStr
.isEmpty() && nOperator
== CO_LESS_EQUAL
;
106 /* Class SolverSettings
108 * This class is used to load/save and manipulate solver settings in a Calc tab.
110 * During initialization, (see Initialize() method) all settings stored in the tab are loaded onto
111 * the object. Settings that are not defined use default values.
113 * Read/Write methods are private and are used internally to load/write solver settings from
114 * named ranges associated with the sheet.
116 * Get/Set methods are public methods used to change object properties (they do not save data
119 * The method SaveSolverSettings() is used to create the named ranges containing the current
120 * property values into the file.
129 ScDocShell
* m_pDocShell
;
131 // Used to read/write the named ranges in the tab
132 ScRangeName
* m_pRangeName
;
135 ObjectiveType m_eObjType
;
137 OUString m_sVariableCells
;
138 OUString m_sLOEngineName
;
139 OUString m_sMSEngineId
;
141 // Solver engine options
143 OUString m_sNonNegative
;
144 OUString m_sEpsilonLevel
;
145 OUString m_sLimitBBDepth
;
147 OUString m_sAlgorithm
;
149 OUString m_sSwarmSize
;
150 OUString m_sLearningCycles
;
151 OUString m_sGuessVariableRange
;
152 OUString m_sVariableRangeThreshold
;
153 OUString m_sUseACRComparator
;
154 OUString m_sUseRandomStartingPoint
;
155 OUString m_sUseStrongerPRNG
;
156 OUString m_sStagnationLimit
;
157 OUString m_sTolerance
;
158 OUString m_sEnhancedSolverStatus
;
160 OUString m_sAgentSwitchRate
;
161 OUString m_sScalingFactorMin
;
162 OUString m_sScalingFactorMax
;
163 OUString m_sCrossoverProbability
;
164 OUString m_sCognitiveConstant
;
165 OUString m_sSocialConstant
;
166 OUString m_sConstrictionCoeff
;
167 OUString m_sMutationProbability
;
168 OUString m_sLibrarySize
;
170 css::uno::Sequence
<css::beans::PropertyValue
> m_aEngineOptions
;
172 std::vector
<ModelConstraint
> m_aConstraints
;
176 // Used to create or read a single solver parameter based on its named range
177 bool ReadParamValue(SolverParameter eParam
, OUString
& rValue
, bool bRemoveQuotes
= false);
178 bool ReadDoubleParamValue(SolverParameter eParam
, OUString
& rValue
);
179 void WriteParamValue(SolverParameter eParam
, OUString sValue
, bool bQuoted
= false);
180 void WriteDoubleParamValue(SolverParameter eParam
, std::u16string_view sValue
);
182 // Creates or reads all constraints stored in named ranges
183 void ReadConstraints();
184 void WriteConstraints();
186 // Used to create or get a single constraint part
187 bool ReadConstraintPart(ConstraintPart ePart
, tools::Long nIndex
, OUString
& rValue
);
188 void WriteConstraintPart(ConstraintPart ePart
, tools::Long nIndex
, OUString sValue
);
190 // Creates or reads all named ranges associated with solver engine options
194 void DeleteAllNamedRanges();
196 // Maps solver parameters to named ranges
197 std::map
<SolverParameter
, OUString
> m_mNamedRanges
198 = { { SP_OBJ_CELL
, "solver_opt" },
199 { SP_OBJ_TYPE
, "solver_typ" },
200 { SP_OBJ_VAL
, "solver_val" },
201 { SP_VAR_CELLS
, "solver_adj" },
202 { SP_CONSTR_COUNT
, "solver_num" },
203 { SP_LO_ENGINE
, "solver_lo_eng" },
204 { SP_MS_ENGINE
, "solver_eng" },
205 { SP_INTEGER
, "solver_int" },
206 { SP_NON_NEGATIVE
, "solver_neg" },
207 { SP_EPSILON_LEVEL
, "solver_eps" },
208 { SP_LIMIT_BBDEPTH
, "solver_bbd" },
209 { SP_TIMEOUT
, "solver_tim" },
210 { SP_ALGORITHM
, "solver_alg" },
211 { SP_SWARM_SIZE
, "solver_ssz" },
212 { SP_LEARNING_CYCLES
, "solver_lcy" },
213 { SP_GUESS_VARIABLE_RANGE
, "solver_gvr" },
214 { SP_VARIABLE_RANGE_THRESHOLD
, "solver_vrt" },
215 { SP_ACR_COMPARATOR
, "solver_acr" },
216 { SP_RND_STARTING_POINT
, "solver_rsp" },
217 { SP_STRONGER_PRNG
, "solver_prng" },
218 { SP_STAGNATION_LIMIT
, "solver_slim" },
219 { SP_STAGNATION_TOLERANCE
, "solver_stol" },
220 { SP_ENHANCED_STATUS
, "solver_enst" },
221 { SP_AGENT_SWITCH_RATE
, "solver_asr" },
222 { SP_SCALING_MIN
, "solver_smin" },
223 { SP_SCALING_MAX
, "solver_smax" },
224 { SP_CROSSOVER_PROB
, "solver_crpb" },
225 { SP_COGNITIVE_CONST
, "solver_cog" },
226 { SP_SOCIAL_CONST
, "solver_soc" },
227 { SP_CONSTRICTION_COEFF
, "solver_ccoeff" },
228 { SP_MUTATION_PROB
, "solver_mtpb" },
229 { SP_LIBRARY_SIZE
, "solver_lbsz" } };
231 // Maps LO solver implementation names to MS engine codes
232 std::map
<OUString
, OUString
> SolverNamesToExcelEngines
= {
233 { "com.sun.star.comp.Calc.CoinMPSolver", "2" }, // Simplex LP
234 { "com.sun.star.comp.Calc.LpsolveSolver", "2" }, // Simplex LP
235 { "com.sun.star.comp.Calc.SwarmSolver", "1" }, // GRG Nonlinear
236 { "com.sun.star.comp.Calc.NLPSolver.DEPSSolverImpl", "3" }, // DEPS
237 { "com.sun.star.comp.Calc.NLPSolver.SCOSolverImpl", "3" } // SCO
240 // Maps MS solver engine codes to LO solver implementation names
241 std::map
<OUString
, OUString
> SolverCodesToLOEngines
= {
242 { "1", "com.sun.star.comp.Calc.SwarmSolver" }, // GRG Nonlinear
243 { "2", "com.sun.star.comp.Calc.CoinMPSolver" }, // Simplex LP
244 { "3", "com.sun.star.comp.Calc.SwarmSolver" } // Evolutionary
247 // Maps LO solver parameters to named ranges to be used
248 // NonNegative: for MS compatibility, use 1 for selected and 2 for not selected
249 typedef std::vector
<std::variant
<OUString
, SolverParameter
>> TParamInfo
;
250 std::map
<OUString
, TParamInfo
> SolverParamNames
251 = { { u
"Integer"_ustr
, { SP_INTEGER
, "solver_int", "bool" } },
252 { u
"NonNegative"_ustr
, { SP_NON_NEGATIVE
, "solver_neg", "bool" } },
253 { u
"EpsilonLevel"_ustr
, { SP_EPSILON_LEVEL
, "solver_eps", "int" } },
254 { u
"LimitBBDepth"_ustr
, { SP_LIMIT_BBDEPTH
, "solver_bbd", "bool" } },
255 { u
"Timeout"_ustr
, { SP_TIMEOUT
, "solver_tim", "int" } },
256 { u
"Algorithm"_ustr
, { SP_ALGORITHM
, "solver_alg", "int" } },
258 { u
"AssumeNonNegative"_ustr
, { SP_NON_NEGATIVE
, "solver_neg", "bool" } },
259 { u
"SwarmSize"_ustr
, { SP_SWARM_SIZE
, "solver_ssz", "int" } },
260 { u
"LearningCycles"_ustr
, { SP_LEARNING_CYCLES
, "solver_lcy", "int" } },
261 { u
"GuessVariableRange"_ustr
, { SP_GUESS_VARIABLE_RANGE
, "solver_gvr", "bool" } },
262 { u
"VariableRangeThreshold"_ustr
,
263 { SP_VARIABLE_RANGE_THRESHOLD
, "solver_vrt", "double" } },
264 { u
"UseACRComparator"_ustr
, { SP_ACR_COMPARATOR
, "solver_acr", "bool" } },
265 { u
"UseRandomStartingPoint"_ustr
, { SP_RND_STARTING_POINT
, "solver_rsp", "bool" } },
266 { u
"UseStrongerPRNG"_ustr
, { SP_STRONGER_PRNG
, "solver_prng", "bool" } },
267 { u
"StagnationLimit"_ustr
, { SP_STAGNATION_LIMIT
, "solver_slim", "int" } },
268 { u
"Tolerance"_ustr
, { SP_STAGNATION_TOLERANCE
, "solver_stol", "double" } },
269 { u
"EnhancedSolverStatus"_ustr
, { SP_ENHANCED_STATUS
, "solver_enst", "bool" } },
271 { u
"AgentSwitchRate"_ustr
, { SP_AGENT_SWITCH_RATE
, "solver_asr", "double" } },
272 { u
"DEFactorMin"_ustr
, { SP_SCALING_MIN
, "solver_smin", "double" } },
273 { u
"DEFactorMax"_ustr
, { SP_SCALING_MAX
, "solver_smax", "double" } },
274 { u
"DECR"_ustr
, { SP_CROSSOVER_PROB
, "solver_crpb", "double" } },
275 { u
"PSC1"_ustr
, { SP_COGNITIVE_CONST
, "solver_cog", "double" } },
276 { u
"PSC2"_ustr
, { SP_SOCIAL_CONST
, "solver_soc", "double" } },
277 { u
"PSWeight"_ustr
, { SP_CONSTRICTION_COEFF
, "solver_ccoeff", "double" } },
278 { u
"PSCL"_ustr
, { SP_MUTATION_PROB
, "solver_mtpb", "double" } },
280 { u
"LibrarySize"_ustr
, { 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
{ u
"solver_lhs"_ustr
, u
"solver_rel"_ustr
,
285 u
"solver_rhs"_ustr
};
288 /* A SolverSettings object is linked to the ScTable where solver parameters
289 * are located and saved to */
290 SolverSettings(ScTable
& pTable
);
292 SC_DLLPUBLIC OUString
GetParameter(SolverParameter eParam
);
293 SC_DLLPUBLIC
void SetParameter(SolverParameter eParam
, OUString sValue
);
294 SC_DLLPUBLIC ObjectiveType
GetObjectiveType() { return m_eObjType
; }
295 SC_DLLPUBLIC
void SetObjectiveType(ObjectiveType eType
);
296 SC_DLLPUBLIC
void GetEngineOptions(css::uno::Sequence
<css::beans::PropertyValue
>& aOptions
);
297 SC_DLLPUBLIC
void SetEngineOptions(css::uno::Sequence
<css::beans::PropertyValue
>& aOptions
);
298 SC_DLLPUBLIC
std::vector
<ModelConstraint
> GetConstraints() { return m_aConstraints
; }
299 SC_DLLPUBLIC
void SetConstraints(std::vector
<ModelConstraint
> aConstraints
);
301 SC_DLLPUBLIC
void SaveSolverSettings();
302 SC_DLLPUBLIC
void ResetToDefaults();
303 SC_DLLPUBLIC
bool TabHasSolverModel();
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */