src/gui/graphicsview/qsimplex_p.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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     uint 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 #ifdef QT_DEBUG
       
    99     bool isSatisfied() {
       
   100         qreal leftHandSide(0);
       
   101 
       
   102         QHash<QSimplexVariable *, qreal>::const_iterator iter;
       
   103         for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) {
       
   104             leftHandSide += iter.value() * iter.key()->result;
       
   105         }
       
   106 
       
   107         Q_ASSERT(constant > 0 || qFuzzyCompare(1, 1 + constant));
       
   108 
       
   109         if (qFuzzyCompare(1000 + leftHandSide, 1000 + constant))
       
   110             return true;
       
   111 
       
   112         switch (ratio) {
       
   113         case LessOrEqual:
       
   114             return leftHandSide < constant;
       
   115         case MoreOrEqual:
       
   116             return leftHandSide > constant;
       
   117         default:
       
   118             return false;
       
   119         }
       
   120     }
       
   121 #endif
       
   122 };
       
   123 
       
   124 class QSimplex
       
   125 {
       
   126 public:
       
   127     QSimplex();
       
   128     virtual ~QSimplex();
       
   129 
       
   130     qreal solveMin();
       
   131     qreal solveMax();
       
   132     QList<QSimplexVariable *> constraintsVariables();
       
   133 
       
   134     bool setConstraints(const QList<QSimplexConstraint *> constraints);
       
   135     void setObjective(QSimplexConstraint *objective);
       
   136 
       
   137     void dumpMatrix();
       
   138 
       
   139 private:
       
   140     // Matrix handling
       
   141     qreal valueAt(int row, int column);
       
   142     void setValueAt(int row, int column, qreal value);
       
   143     void clearRow(int rowIndex);
       
   144     void clearColumns(int first, int last);
       
   145     void combineRows(int toIndex, int fromIndex, qreal factor);
       
   146 
       
   147     // Simplex
       
   148     int findPivotColumn();
       
   149     int pivotRowForColumn(int column);
       
   150     void reducedRowEchelon();
       
   151     bool iterate();
       
   152 
       
   153     // Helpers
       
   154     void clearDataStructures();
       
   155     void solveMaxHelper();
       
   156     enum solverFactor { Minimum = -1, Maximum = 1 };
       
   157     qreal solver(solverFactor factor);
       
   158     void collectResults();
       
   159 
       
   160     QList<QSimplexConstraint *> constraints;
       
   161     QList<QSimplexVariable *> variables;
       
   162     QSimplexConstraint *objective;
       
   163 
       
   164     int rows;
       
   165     int columns;
       
   166     int firstArtificial;
       
   167 
       
   168     qreal *matrix;
       
   169 };
       
   170 
       
   171 inline QList<QSimplexVariable *> QSimplex::constraintsVariables()
       
   172 {
       
   173     return variables;
       
   174 }
       
   175 
       
   176 inline qreal QSimplex::valueAt(int rowIndex, int columnIndex)
       
   177 {
       
   178     return matrix[rowIndex * columns + columnIndex];
       
   179 }
       
   180 
       
   181 inline void QSimplex::setValueAt(int rowIndex, int columnIndex, qreal value)
       
   182 {
       
   183     matrix[rowIndex * columns + columnIndex] = value;
       
   184 }
       
   185 
       
   186 QT_END_NAMESPACE
       
   187 
       
   188 #endif // QSIMPLEX_P_H