src/gui/graphicsview/qsimplex_p.h
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    61 struct QSimplexVariable
    61 struct QSimplexVariable
    62 {
    62 {
    63     QSimplexVariable() : result(0), index(0) {}
    63     QSimplexVariable() : result(0), index(0) {}
    64 
    64 
    65     qreal result;
    65     qreal result;
    66     uint index;
    66     int index;
    67 };
    67 };
    68 
    68 
    69 
    69 
    70 /*!
    70 /*!
    71   \internal
    71   \internal
    93     Ratio ratio;
    93     Ratio ratio;
    94 
    94 
    95     QPair<QSimplexVariable *, qreal> helper;
    95     QPair<QSimplexVariable *, qreal> helper;
    96     QSimplexVariable * artificial;
    96     QSimplexVariable * artificial;
    97 
    97 
    98 #ifdef QT_DEBUG
    98     void invert();
       
    99 
    99     bool isSatisfied() {
   100     bool isSatisfied() {
   100         qreal leftHandSide(0);
   101         qreal leftHandSide(0);
   101 
   102 
   102         QHash<QSimplexVariable *, qreal>::const_iterator iter;
   103         QHash<QSimplexVariable *, qreal>::const_iterator iter;
   103         for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) {
   104         for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) {
   104             leftHandSide += iter.value() * iter.key()->result;
   105             leftHandSide += iter.value() * iter.key()->result;
   105         }
   106         }
   106 
   107 
   107         Q_ASSERT(constant > 0 || qFuzzyCompare(1, 1 + constant));
   108         Q_ASSERT(constant > 0 || qFuzzyCompare(1, 1 + constant));
   108 
   109 
   109         if (qFuzzyCompare(1000 + leftHandSide, 1000 + constant))
   110         if ((leftHandSide == constant) || qAbs(leftHandSide - constant) < 0.0000001)
   110             return true;
   111             return true;
   111 
   112 
   112         switch (ratio) {
   113         switch (ratio) {
   113         case LessOrEqual:
   114         case LessOrEqual:
   114             return leftHandSide < constant;
   115             return leftHandSide < constant;
   116             return leftHandSide > constant;
   117             return leftHandSide > constant;
   117         default:
   118         default:
   118             return false;
   119             return false;
   119         }
   120         }
   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     }
   121 #endif
   146 #endif
   122 };
   147 };
   123 
   148 
   124 class QSimplex
   149 class QSimplex
   125 {
   150 {
   127     QSimplex();
   152     QSimplex();
   128     virtual ~QSimplex();
   153     virtual ~QSimplex();
   129 
   154 
   130     qreal solveMin();
   155     qreal solveMin();
   131     qreal solveMax();
   156     qreal solveMax();
   132     QList<QSimplexVariable *> constraintsVariables();
       
   133 
   157 
   134     bool setConstraints(const QList<QSimplexConstraint *> constraints);
   158     bool setConstraints(const QList<QSimplexConstraint *> constraints);
   135     void setObjective(QSimplexConstraint *objective);
   159     void setObjective(QSimplexConstraint *objective);
   136 
   160 
   137     void dumpMatrix();
   161     void dumpMatrix();
   143     void clearRow(int rowIndex);
   167     void clearRow(int rowIndex);
   144     void clearColumns(int first, int last);
   168     void clearColumns(int first, int last);
   145     void combineRows(int toIndex, int fromIndex, qreal factor);
   169     void combineRows(int toIndex, int fromIndex, qreal factor);
   146 
   170 
   147     // Simplex
   171     // Simplex
       
   172     bool simplifyConstraints(QList<QSimplexConstraint *> *constraints);
   148     int findPivotColumn();
   173     int findPivotColumn();
   149     int pivotRowForColumn(int column);
   174     int pivotRowForColumn(int column);
   150     void reducedRowEchelon();
   175     void reducedRowEchelon();
   151     bool iterate();
   176     bool iterate();
   152 
   177 
   166     int firstArtificial;
   191     int firstArtificial;
   167 
   192 
   168     qreal *matrix;
   193     qreal *matrix;
   169 };
   194 };
   170 
   195 
   171 inline QList<QSimplexVariable *> QSimplex::constraintsVariables()
       
   172 {
       
   173     return variables;
       
   174 }
       
   175 
       
   176 inline qreal QSimplex::valueAt(int rowIndex, int columnIndex)
   196 inline qreal QSimplex::valueAt(int rowIndex, int columnIndex)
   177 {
   197 {
   178     return matrix[rowIndex * columns + columnIndex];
   198     return matrix[rowIndex * columns + columnIndex];
   179 }
   199 }
   180 
   200