| 1 |
/*----------------------------------------------------------------------------- |
| 2 |
| Copyright (c) 2013-2026, Nucleic Development Team. |
| 3 |
| |
| 4 |
| Distributed under the terms of the Modified BSD License. |
| 5 |
| |
| 6 |
| The full license is in the file LICENSE, distributed with this software. |
| 7 |
|----------------------------------------------------------------------------*/ |
| 8 |
#pragma once |
| 9 |
#include "constraint.h" |
| 10 |
#include "debug.h" |
| 11 |
#include "solverimpl.h" |
| 12 |
#include "variable.h" |
| 13 |
|
| 14 |
|
| 15 |
namespace kiwi |
| 16 |
{ |
| 17 |
|
| 18 |
class Solver |
| 19 |
{ |
| 20 |
|
| 21 |
public: |
| 22 |
|
| 23 |
Solver() = default; |
| 24 |
|
| 25 |
~Solver() = default; |
| 26 |
|
| 27 |
/* Add a constraint to the solver. |
| 28 |
|
| 29 |
Throws |
| 30 |
------ |
| 31 |
DuplicateConstraint |
| 32 |
The given constraint has already been added to the solver. |
| 33 |
|
| 34 |
UnsatisfiableConstraint |
| 35 |
The given constraint is required and cannot be satisfied. |
| 36 |
|
| 37 |
*/ |
| 38 |
void addConstraint( const Constraint& constraint ) |
| 39 |
{ |
| 40 |
m_impl.addConstraint( constraint ); |
| 41 |
} |
| 42 |
|
| 43 |
/* Remove a constraint from the solver. |
| 44 |
|
| 45 |
Throws |
| 46 |
------ |
| 47 |
UnknownConstraint |
| 48 |
The given constraint has not been added to the solver. |
| 49 |
|
| 50 |
*/ |
| 51 |
void removeConstraint( const Constraint& constraint ) |
| 52 |
{ |
| 53 |
m_impl.removeConstraint( constraint ); |
| 54 |
} |
| 55 |
|
| 56 |
/* Test whether a constraint has been added to the solver. |
| 57 |
|
| 58 |
*/ |
| 59 |
bool hasConstraint( const Constraint& constraint ) const |
| 60 |
{ |
| 61 |
return m_impl.hasConstraint( constraint ); |
| 62 |
} |
| 63 |
|
| 64 |
/* Add an edit variable to the solver. |
| 65 |
|
| 66 |
This method should be called before the `suggestValue` method is |
| 67 |
used to supply a suggested value for the given edit variable. |
| 68 |
|
| 69 |
Throws |
| 70 |
------ |
| 71 |
DuplicateEditVariable |
| 72 |
The given edit variable has already been added to the solver. |
| 73 |
|
| 74 |
BadRequiredStrength |
| 75 |
The given strength is >= required. |
| 76 |
|
| 77 |
*/ |
| 78 |
void addEditVariable( const Variable& variable, double strength ) |
| 79 |
{ |
| 80 |
m_impl.addEditVariable( variable, strength ); |
| 81 |
} |
| 82 |
|
| 83 |
/* Remove an edit variable from the solver. |
| 84 |
|
| 85 |
Throws |
| 86 |
------ |
| 87 |
UnknownEditVariable |
| 88 |
The given edit variable has not been added to the solver. |
| 89 |
|
| 90 |
*/ |
| 91 |
void removeEditVariable( const Variable& variable ) |
| 92 |
{ |
| 93 |
m_impl.removeEditVariable( variable ); |
| 94 |
} |
| 95 |
|
| 96 |
/* Test whether an edit variable has been added to the solver. |
| 97 |
|
| 98 |
*/ |
| 99 |
bool hasEditVariable( const Variable& variable ) const |
| 100 |
{ |
| 101 |
return m_impl.hasEditVariable( variable ); |
| 102 |
} |
| 103 |
|
| 104 |
/* Suggest a value for the given edit variable. |
| 105 |
|
| 106 |
This method should be used after an edit variable as been added to |
| 107 |
the solver in order to suggest the value for that variable. After |
| 108 |
all suggestions have been made, the `solve` method can be used to |
| 109 |
update the values of all variables. |
| 110 |
|
| 111 |
Throws |
| 112 |
------ |
| 113 |
UnknownEditVariable |
| 114 |
The given edit variable has not been added to the solver. |
| 115 |
|
| 116 |
*/ |
| 117 |
void suggestValue( const Variable& variable, double value ) |
| 118 |
{ |
| 119 |
m_impl.suggestValue( variable, value ); |
| 120 |
} |
| 121 |
|
| 122 |
/* Update the values of the external solver variables. |
| 123 |
|
| 124 |
*/ |
| 125 |
void updateVariables() |
| 126 |
{ |
| 127 |
m_impl.updateVariables(); |
| 128 |
} |
| 129 |
|
| 130 |
/* Reset the solver to the empty starting condition. |
| 131 |
|
| 132 |
This method resets the internal solver state to the empty starting |
| 133 |
condition, as if no constraints or edit variables have been added. |
| 134 |
This can be faster than deleting the solver and creating a new one |
| 135 |
when the entire system must change, since it can avoid unecessary |
| 136 |
heap (de)allocations. |
| 137 |
|
| 138 |
*/ |
| 139 |
void reset() |
| 140 |
{ |
| 141 |
m_impl.reset(); |
| 142 |
} |
| 143 |
|
| 144 |
/* Dump a representation of the solver internals to stdout. |
| 145 |
|
| 146 |
*/ |
| 147 |
void dump() |
| 148 |
{ |
| 149 |
debug::dump( m_impl ); |
| 150 |
} |
| 151 |
|
| 152 |
/* Dump a representation of the solver internals to a stream. |
| 153 |
|
| 154 |
*/ |
| 155 |
void dump( std::ostream& out ) |
| 156 |
{ |
| 157 |
debug::dump( m_impl, out ); |
| 158 |
} |
| 159 |
|
| 160 |
/* Dump a representation of the solver internals to a string. |
| 161 |
|
| 162 |
*/ |
| 163 |
std::string dumps() |
| 164 |
{ |
| 165 |
return debug::dumps( m_impl ); |
| 166 |
} |
| 167 |
|
| 168 |
private: |
| 169 |
|
| 170 |
Solver( const Solver& ); |
| 171 |
|
| 172 |
Solver& operator=( const Solver& ); |
| 173 |
|
| 174 |
impl::SolverImpl m_impl; |
| 175 |
}; |
| 176 |
|
| 177 |
} // namespace kiwi |