| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #ifndef QSIMPLEX_P_H
|
|---|
| 43 | #define QSIMPLEX_P_H
|
|---|
| 44 |
|
|---|
| 45 | //
|
|---|
| 46 | // W A R N I N G
|
|---|
| 47 | // -------------
|
|---|
| 48 | //
|
|---|
| 49 | // This file is not part of the Qt API. It exists purely as an
|
|---|
| 50 | // implementation detail. This header file may change from version to
|
|---|
| 51 | // version without notice, or even be removed.
|
|---|
| 52 | //
|
|---|
| 53 | // We mean it.
|
|---|
| 54 | //
|
|---|
| 55 |
|
|---|
| 56 | #include <QtCore/qhash.h>
|
|---|
| 57 | #include <QtCore/qpair.h>
|
|---|
| 58 |
|
|---|
| 59 | QT_BEGIN_NAMESPACE
|
|---|
| 60 |
|
|---|
| 61 | struct QSimplexVariable
|
|---|
| 62 | {
|
|---|
| 63 | QSimplexVariable() : result(0), index(0) {}
|
|---|
| 64 |
|
|---|
| 65 | qreal result;
|
|---|
| 66 | int index;
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | /*!
|
|---|
| 71 | \internal
|
|---|
| 72 |
|
|---|
| 73 | Representation of a LP constraint like:
|
|---|
| 74 |
|
|---|
| 75 | (c1 * X1) + (c2 * X2) + ... = K
|
|---|
| 76 | or <= K
|
|---|
| 77 | or >= K
|
|---|
| 78 |
|
|---|
| 79 | Where (ci, Xi) are the pairs in "variables" and K the real "constant".
|
|---|
| 80 | */
|
|---|
| 81 | struct QSimplexConstraint
|
|---|
| 82 | {
|
|---|
| 83 | QSimplexConstraint() : constant(0), ratio(Equal), artificial(0) {}
|
|---|
| 84 |
|
|---|
| 85 | enum Ratio {
|
|---|
| 86 | LessOrEqual = 0,
|
|---|
| 87 | Equal,
|
|---|
| 88 | MoreOrEqual
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | QHash<QSimplexVariable *, qreal> variables;
|
|---|
| 92 | qreal constant;
|
|---|
| 93 | Ratio ratio;
|
|---|
| 94 |
|
|---|
| 95 | QPair<QSimplexVariable *, qreal> helper;
|
|---|
| 96 | QSimplexVariable * artificial;
|
|---|
| 97 |
|
|---|
| 98 | void invert();
|
|---|
| 99 |
|
|---|
| 100 | bool isSatisfied() {
|
|---|
| 101 | qreal leftHandSide(0);
|
|---|
| 102 |
|
|---|
| 103 | QHash<QSimplexVariable *, qreal>::const_iterator iter;
|
|---|
| 104 | for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) {
|
|---|
| 105 | leftHandSide += iter.value() * iter.key()->result;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | Q_ASSERT(constant > 0 || qFuzzyCompare(1, 1 + constant));
|
|---|
| 109 |
|
|---|
| 110 | if ((leftHandSide == constant) || qAbs(leftHandSide - constant) < 0.0000001)
|
|---|
| 111 | return true;
|
|---|
| 112 |
|
|---|
| 113 | switch (ratio) {
|
|---|
| 114 | case LessOrEqual:
|
|---|
| 115 | return leftHandSide < constant;
|
|---|
| 116 | case MoreOrEqual:
|
|---|
| 117 | return leftHandSide > constant;
|
|---|
| 118 | default:
|
|---|
| 119 | return false;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | #ifdef QT_DEBUG
|
|---|
| 124 | QString toString() {
|
|---|
| 125 | QString result;
|
|---|
| 126 | result += QString::fromAscii("-- QSimplexConstraint %1 --").arg(quintptr(this), 0, 16);
|
|---|
| 127 |
|
|---|
| 128 | QHash<QSimplexVariable *, qreal>::const_iterator iter;
|
|---|
| 129 | for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) {
|
|---|
| 130 | result += QString::fromAscii(" %1 x %2").arg(iter.value()).arg(quintptr(iter.key()), 0, 16);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | switch (ratio) {
|
|---|
| 134 | case LessOrEqual:
|
|---|
| 135 | result += QString::fromAscii(" (less) <= %1").arg(constant);
|
|---|
| 136 | break;
|
|---|
| 137 | case MoreOrEqual:
|
|---|
| 138 | result += QString::fromAscii(" (more) >= %1").arg(constant);
|
|---|
| 139 | break;
|
|---|
| 140 | default:
|
|---|
| 141 | result += QString::fromAscii(" (eqal) == %1").arg(constant);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | return result;
|
|---|
| 145 | }
|
|---|
| 146 | #endif
|
|---|
| 147 | };
|
|---|
| 148 |
|
|---|
| 149 | class QSimplex
|
|---|
| 150 | {
|
|---|
| 151 | public:
|
|---|
| 152 | QSimplex();
|
|---|
| 153 | virtual ~QSimplex();
|
|---|
| 154 |
|
|---|
| 155 | qreal solveMin();
|
|---|
| 156 | qreal solveMax();
|
|---|
| 157 |
|
|---|
| 158 | bool setConstraints(const QList<QSimplexConstraint *> constraints);
|
|---|
| 159 | void setObjective(QSimplexConstraint *objective);
|
|---|
| 160 |
|
|---|
| 161 | void dumpMatrix();
|
|---|
| 162 |
|
|---|
| 163 | private:
|
|---|
| 164 | // Matrix handling
|
|---|
| 165 | qreal valueAt(int row, int column);
|
|---|
| 166 | void setValueAt(int row, int column, qreal value);
|
|---|
| 167 | void clearRow(int rowIndex);
|
|---|
| 168 | void clearColumns(int first, int last);
|
|---|
| 169 | void combineRows(int toIndex, int fromIndex, qreal factor);
|
|---|
| 170 |
|
|---|
| 171 | // Simplex
|
|---|
| 172 | bool simplifyConstraints(QList<QSimplexConstraint *> *constraints);
|
|---|
| 173 | int findPivotColumn();
|
|---|
| 174 | int pivotRowForColumn(int column);
|
|---|
| 175 | void reducedRowEchelon();
|
|---|
| 176 | bool iterate();
|
|---|
| 177 |
|
|---|
| 178 | // Helpers
|
|---|
| 179 | void clearDataStructures();
|
|---|
| 180 | void solveMaxHelper();
|
|---|
| 181 | enum solverFactor { Minimum = -1, Maximum = 1 };
|
|---|
| 182 | qreal solver(solverFactor factor);
|
|---|
| 183 | void collectResults();
|
|---|
| 184 |
|
|---|
| 185 | QList<QSimplexConstraint *> constraints;
|
|---|
| 186 | QList<QSimplexVariable *> variables;
|
|---|
| 187 | QSimplexConstraint *objective;
|
|---|
| 188 |
|
|---|
| 189 | int rows;
|
|---|
| 190 | int columns;
|
|---|
| 191 | int firstArtificial;
|
|---|
| 192 |
|
|---|
| 193 | qreal *matrix;
|
|---|
| 194 | };
|
|---|
| 195 |
|
|---|
| 196 | inline qreal QSimplex::valueAt(int rowIndex, int columnIndex)
|
|---|
| 197 | {
|
|---|
| 198 | return matrix[rowIndex * columns + columnIndex];
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | inline void QSimplex::setValueAt(int rowIndex, int columnIndex, qreal value)
|
|---|
| 202 | {
|
|---|
| 203 | matrix[rowIndex * columns + columnIndex] = value;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | QT_END_NAMESPACE
|
|---|
| 207 |
|
|---|
| 208 | #endif // QSIMPLEX_P_H
|
|---|