44 #include <QtCore/qset.h> 45 #include <QtCore/qdebug.h> 147 for (
int i = 0; i < newConstraints.
size(); ++i) {
149 c->
constant = newConstraints[i]->constant;
150 c->
ratio = newConstraints[i]->ratio;
151 c->
variables = newConstraints[i]->variables;
157 qWarning() <<
"QSimplex: No feasible solution!";
209 slack->
index = ++variableIndex;
215 surplus->
index = ++variableIndex;
232 for (
int i = 0; i < artificialList.
size(); ++i)
233 artificialList[i]->
index = ++variableIndex;
234 artificialList.
clear();
247 qWarning() <<
"QSimplex: Unable to allocate memory!";
298 qWarning() <<
"QSimplex: No feasible solution!";
343 for (
int i = 0; i <
columns; ++i)
352 for (
int i = 0; i <
rows; ++i) {
354 for (
int j = first; j <= last; ++j)
364 qDebug(
"---- Simplex Matrix ----\n");
367 for (
int j = 0; j <
columns; ++j)
370 for (
int i = 0; i <
rows; ++i) {
374 for (
int j = 0; j <
columns; ++j)
378 qDebug(
"------------------------\n");
392 for (
int j = 1; j <
columns; ++j) {
393 qreal value = from[j];
399 to[j] += factor * value;
402 if (
qAbs(to[j]) < 0.0000000001)
415 for (
int j = 0; j <
columns-1; ++j) {
450 for (
int i = 1; i <
rows; ++i) {
456 if (quotient < min) {
459 }
else if ((quotient == min) && (
valueAt(i, 0) >
valueAt(minIndex, 0))) {
472 for (
int i = 1; i <
rows; ++i) {
473 int factorInObjectiveRow =
valueAt(i, 0);
491 if (pivotColumn == -1)
496 if (pivotRow == -1) {
497 qWarning() <<
"QSimplex: Unbounded problem!";
507 for (
int row=0; row <
rows; ++row) {
544 qreal resultOffset = 0;
620 for (
int i = 1; i <
rows; ++i) {
638 bool modified =
true;
645 while (iter != constraints->
end()) {
653 results.
insert(variable, result);
654 variable->
result = result;
655 variable->
index = -1;
682 iter = constraints->
erase(iter);
694 constant = -constant;
void solveMaxHelper()
Run simplex on the current matrix with the current objective.
bool setConstraints(const QList< QSimplexConstraint *> constraints)
Sets the new constraints in the simplex solver and returns whether the problem is feasible...
#define QT_END_NAMESPACE
This macro expands to.
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
void clearDataStructures()
QPair< QSimplexVariable *, qreal > helper
qreal solveMax()
Maximize the original objective.
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
QSimplexVariable * artificial
QList< QSimplexConstraint * > constraints
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
T take(const Key &key)
Removes the item with the key from the hash and returns the value associated with it...
The QHash class is a template class that provides a hash-table-based dictionary.
Q_DECL_CONSTEXPR T qAbs(const T &t)
QSimplexConstraint * objective
QHash< QSimplexVariable *, qreal > variables
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
const T value(const Key &key) const
Returns the value associated with the key.
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Q_CORE_EXPORT void qDebug(const char *,...)
void collectResults()
Reads results from the simplified matrix and saves them in the "result" member of each QSimplexVariab...
void setObjective(QSimplexConstraint *objective)
#define QT_BEGIN_NAMESPACE
This macro expands to.
QList< T > toList() const
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
qreal valueAt(int row, int column)
bool isEmpty() const
Returns true if the hash contains no items; otherwise returns false.
Q_CORE_EXPORT void qWarning(const char *,...)
bool iterate()
Does one iteration towards a better solution for the problem.
void clear()
Removes all items from the list.
qreal solver(solverFactor factor)
Both solveMin and solveMax are interfaces to this method.
int pivotRowForColumn(int column)
For a given pivot column, find the pivot row.
The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.
T & first()
Returns a reference to the first item in the list.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the hash.
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
bool simplifyConstraints(QList< QSimplexConstraint *> *constraints)
Looks for single-valued variables and remove them from the constraints list.
void clearRow(int rowIndex)
iterator erase(iterator pos)
Removes the item associated with the iterator pos from the list, and returns an iterator to the next ...
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
const Key key(const T &value) const
Returns the first key mapped to value.
int size() const
Returns the number of items in the list.
QList< QSimplexVariable * > variables
void setValueAt(int row, int column, qreal value)
void clearColumns(int first, int last)
int count(const Key &key) const
Returns the number of items associated with the key.
#define qPrintable(string)
qreal solveMin()
Minimize the original objective.
The QList class is a template class that provides lists.
void combineRows(int toIndex, int fromIndex, qreal factor)