CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ConstraintSolver.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2023 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Constraint.hpp"
11#include "ConstraintError.hpp"
14
18
19#include <algorithm>
20#include <limits>
21#include <memory>
22#include <vector>
23
25
27 template<typename K, typename V, typename C = std::less<K>, typename A = DefaultAllocator> using MapType = SmallFlatMap<K, V, 0, C, A>;
28
30
31 class Symbol {
32 public:
33 using ID = SymbolID;
34
42
43 private:
46
47 public:
48 Symbol() : mID(0), mType(Invalid) {}
49 Symbol(const Type type, const ID id) : mID(id), mType(type) {}
50 ~Symbol() = default;
51
52 public:
53 ID id() const { return mID; }
54 Type type() const { return mType; }
55
56 friend bool operator<(const Symbol& lhs, const Symbol& rhs) { return lhs.mID < rhs.mID; }
57 friend bool operator==(const Symbol& lhs, const Symbol& rhs) { return lhs.mID == rhs.mID; }
58 };
59
60 class Row {
61 public:
63
64 private:
66 double mConstant;
67
68 public:
69 Row() : Row(0.0) {}
70 Row(const double constant) : mConstant(constant) {}
71 Row(const Row& other) = default;
72 ~Row() = default;
73
74 public:
76 [[nodiscard]] const CellMap& getCells() const { return mCells; }
77
79 [[nodiscard]] double getConstant() const { return mConstant; }
80
81 public:
85 double add(double value);
86
92 void insert(const Symbol& symbol, double coefficient = 1.0);
93
99 void insert(const Row& other, double coefficient = 1.0);
100
102 void remove(const Symbol& symbol);
103
106
116 void solveFor(const Symbol& symbol);
117
127 void solveFor(const Symbol& lhs, const Symbol& rhs);
128
132 [[nodiscard]] double coefficientFor(const Symbol& symbol) const;
133
141 void substitute(const Symbol& symbol, const Row& row);
142 };
143
149
151 struct EditInfo {
154
157
159 double constant;
160 };
161
166
172
177
182
183 bool mAutoSolve = false;
184
185 public:
187
192
194
195 public: // Constraints
202 void addConstraint(const Constraint& constraint);
203
205 for(const Constraint& constraint : constraints) {
206 addConstraint(constraint);
207 }
208 }
209
211 for(const Constraint& constraint : constraints) {
212 addConstraint(constraint);
213 }
214 }
215
219 void removeConstraint(const Constraint& constraint);
220
222 for(const Constraint& constraint : constraints) {
223 removeConstraint(constraint);
224 }
225 }
226
228 for(const Constraint& constraint : constraints) {
229 removeConstraint(constraint);
230 }
231 }
232
234 [[nodiscard]] bool hasConstraint(const Constraint& constraint) const { return mConstraints.find(constraint) != mConstraints.end(); }
235
236 public: // Edit Variables
245
250
253
261 void suggestValue(const ConstraintVariable& variable, double value);
262
263 public: // Solving & State
266
275 void reset();
276
278 void solve();
279
280 private:
281 struct RowDeleter {
282 template<typename T> void operator()(T& pair) { delete pair.second; }
283 };
284
285 void clearRows() {
286 std::for_each(mRows.begin(), mRows.end(), RowDeleter());
287 mRows.clear();
288 }
289
294
309 [[nodiscard]] UPtr<Row> createRow(const Constraint& constraint, Tag& tag);
310
322 [[nodiscard]] Symbol chooseSubject(const Row& row, const Tag& tag) const;
323
328
333 void substitute(const Symbol& symbol, const Row& row);
334
342 void optimize(const Row& objective);
343
353
361
371
375 [[nodiscard]] Symbol anyPivotableSymbol(const Row& row) const;
376
383 [[nodiscard]] RowMap::iterator getLeavingRow(const Symbol& entering);
384
400 [[nodiscard]] RowMap::iterator getMarkerLeavingRow(const Symbol& marker);
401
403 void removeConstraintEffects(const Constraint& cn, const Tag& tag);
404
406 void removeMarkerEffects(const Symbol& marker, const ConstraintStrength& strength);
407
409 [[nodiscard]] bool allDummies(const Row& row) const;
410 };
411
412} // namespace CeresEngine::Constraint
An equation or inequality involving one or more variables.
Definition Constraint.hpp:38
Definition ConstraintSolver.hpp:60
Row()
Definition ConstraintSolver.hpp:69
double coefficientFor(const Symbol &symbol) const
Get the coefficient for the given symbol.
CellMap mCells
Definition ConstraintSolver.hpp:65
void substitute(const Symbol &symbol, const Row &row)
Substitute a symbol with the data from another row.
void reverseSign()
Reverse the sign of the constant and all cells in the row.
double getConstant() const
Definition ConstraintSolver.hpp:79
void insert(const Row &other, double coefficient=1.0)
Insert a row into this row with a given coefficient.
double mConstant
Definition ConstraintSolver.hpp:66
double add(double value)
Add a constant value to the row constant.
void solveFor(const Symbol &symbol)
Solve the row for the given symbol.
void insert(const Symbol &symbol, double coefficient=1.0)
Insert a symbol into the row with a given coefficient.
MapType< Symbol, double > CellMap
Definition ConstraintSolver.hpp:62
void solveFor(const Symbol &lhs, const Symbol &rhs)
Solve the row for the given symbols.
void remove(const Symbol &symbol)
Remove the given symbol from the row.
const CellMap & getCells() const
Definition ConstraintSolver.hpp:76
Row(const double constant)
Definition ConstraintSolver.hpp:70
Definition ConstraintSolver.hpp:31
Symbol()
Definition ConstraintSolver.hpp:48
Type
Definition ConstraintSolver.hpp:35
@ Slack
Definition ConstraintSolver.hpp:38
@ Invalid
Definition ConstraintSolver.hpp:36
@ Error
Definition ConstraintSolver.hpp:39
@ External
Definition ConstraintSolver.hpp:37
@ Dummy
Definition ConstraintSolver.hpp:40
friend bool operator<(const Symbol &lhs, const Symbol &rhs)
Definition ConstraintSolver.hpp:56
Symbol(const Type type, const ID id)
Definition ConstraintSolver.hpp:49
SymbolID ID
Definition ConstraintSolver.hpp:33
friend bool operator==(const Symbol &lhs, const Symbol &rhs)
Definition ConstraintSolver.hpp:57
Type type() const
Definition ConstraintSolver.hpp:54
ID id() const
Definition ConstraintSolver.hpp:53
Type mType
Definition ConstraintSolver.hpp:45
ID mID
Definition ConstraintSolver.hpp:44
Definition ConstraintSolver.hpp:26
Symbol getVarSymbol(const ConstraintVariable &variable)
Get the symbol for the given variable.
bool allDummies(const Row &row) const
Test whether a row is composed of all dummy variables.
void solve()
Solves the constraint on the solver.
UPtr< Row > mArtificial
Definition ConstraintSolver.hpp:180
ConstraintSolver()
Definition ConstraintSolver.hpp:186
MapType< Constraint, Tag > ConstaintMap
Definition ConstraintSolver.hpp:164
void removeConstraints(const Span< const Constraint > &constraints)
Definition ConstraintSolver.hpp:221
void removeConstraints(const InitializerList< Constraint > constraints)
Definition ConstraintSolver.hpp:227
UPtr< Row > createRow(const Constraint &constraint, Tag &tag)
Create a new Row object for the given constraint.
Symbol chooseSubject(const Row &row, const Tag &tag) const
Choose the subject for solving for the row.
MapType< ConstraintVariable, Symbol > VarMap
Definition ConstraintSolver.hpp:162
void reset()
Reset the solver to the empty starting condition.
bool addWithArtificialVariable(const Row &row)
Add the row to the tableau using an artificial variable.
EditMap mEditVariables
Definition ConstraintSolver.hpp:176
SymbolID mNextID
Definition ConstraintSolver.hpp:181
void optimize(const Row &objective)
Optimize the system for the given objective function.
Symbol anyPivotableSymbol(const Row &row) const
Get the first Slack or Error symbol in the row.
void addConstraints(const Span< const Constraint > &constraints)
Definition ConstraintSolver.hpp:204
ConstraintSolver(const ConstraintSolver &)=delete
Symbol getDualEnteringSymbol(const Row &row) const
Compute the entering symbol for the dual optimize operation.
void removeConstraintEffects(const Constraint &cn, const Tag &tag)
Remove the effects of a constraint on the objective function.
void removeMarkerEffects(const Symbol &marker, const ConstraintStrength &strength)
Remove the effects of an error marker on the objective function.
bool hasEditVariable(const ConstraintVariable &variable) const
Test whether an edit variable has been added to the solver.
Definition ConstraintSolver.hpp:252
ConstraintSolver(ConstraintSolver &&)=delete
~ConstraintSolver()
Definition ConstraintSolver.hpp:193
void removeConstraint(const Constraint &constraint)
Remove a constraint from the solver.
void addConstraints(const InitializerList< Constraint > constraints)
Definition ConstraintSolver.hpp:210
void dualOptimize()
Optimize the system using the dual of the simplex method.
bool hasConstraint(const Constraint &constraint) const
Test whether a constraint has been added to the solver.
Definition ConstraintSolver.hpp:234
void addEditVariable(const ConstraintVariable &variable, const ConstraintStrength &strength)
Add an edit variable to the solver.
VarMap mVariables
Definition ConstraintSolver.hpp:175
ConstraintSolver & operator=(const ConstraintSolver &)=delete
MapType< Symbol, Row * > RowMap
Definition ConstraintSolver.hpp:163
MapType< ConstraintVariable, EditInfo > EditMap
Definition ConstraintSolver.hpp:165
UPtr< Row > mObjective
Definition ConstraintSolver.hpp:179
RowMap mRows
Definition ConstraintSolver.hpp:174
ConstaintMap mConstraints
Definition ConstraintSolver.hpp:173
RowMap::iterator getLeavingRow(const Symbol &entering)
Compute the row which holds the exit symbol for a pivot.
void removeEditVariable(const ConstraintVariable &variable)
Remove an edit variable from the solver.
void suggestValue(const ConstraintVariable &variable, double value)
Suggest a value for the given edit variable.
void updateVariables()
Update the values of the external solver variables.
Vector< Symbol > mInfeasibleRows
Definition ConstraintSolver.hpp:178
bool mAutoSolve
Definition ConstraintSolver.hpp:183
void substitute(const Symbol &symbol, const Row &row)
Substitute the parametric symbol with the given row.
Symbol getEnteringSymbol(const Row &objective) const
Compute the entering variable for a pivot operation.
void addConstraint(const Constraint &constraint)
Add a constraint to the solver.
ConstraintSolver & operator=(ConstraintSolver &&)=delete
void clearRows()
Definition ConstraintSolver.hpp:285
UInt64 SymbolID
Definition ConstraintSolver.hpp:29
RowMap::iterator getMarkerLeavingRow(const Symbol &marker)
Compute the leaving row for a marker variable.
SmallFlatMap< K, V, 0, C, A > MapType
Definition ConstraintSolver.hpp:27
Every constraint has a strength that determines where it sits in the hierarchy; strong constraints ar...
Definition ConstraintStrength.hpp:91
A variable as used in an expression.
Definition ConstraintVariable.hpp:50
Definition Constraint.hpp:22
std::unique_ptr< T, Deleter > UPtr
UPtr is a smart pointer that owns and manages another object through a pointer and disposes of that o...
Definition SmartPtr.hpp:28
std::uint64_t UInt64
Definition DataTypes.hpp:26
std::initializer_list< T > InitializerList
An object of type InitializerList<T> is a lightweight proxy object that provides access to an array o...
Definition InitializerList.hpp:40
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
sfl::small_flat_map< Key, T, N, Compare, StdAllocator< std::pair< Key, T >, RawAllocator > > SmallFlatMap
SmallFlatMap is a sorted associative container similar to Map.
Definition SmallFlatMap.hpp:30
tcb ::span< T, Extent > Span
Span describes an object that can refer to a contiguous sequence of objects with the first element of...
Definition Span.hpp:708
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
DualOptimizeGuard(ConstraintSolver &impl)
Definition ConstraintSolver.hpp:168
~DualOptimizeGuard()
Definition ConstraintSolver.hpp:169
ConstraintSolver & mSolver
Definition ConstraintSolver.hpp:170
A structure that holds information for an edit variable.
Definition ConstraintSolver.hpp:151
Constraint constraint
The constraint for the edit variable.
Definition ConstraintSolver.hpp:156
double constant
The constant value for the edit variable.
Definition ConstraintSolver.hpp:159
Tag tag
A tag for the edit variable.
Definition ConstraintSolver.hpp:153
void operator()(T &pair)
Definition ConstraintSolver.hpp:282
A tag that represents a symbol on the tableau.
Definition ConstraintSolver.hpp:145
Symbol marker
Definition ConstraintSolver.hpp:146
Symbol other
Definition ConstraintSolver.hpp:147