1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/****************************************************************************
**
** Copyright (C) 1992-$THISYEAR$ $TROLLTECH$. All rights reserved.
**
** This file is part of the $MODULE$ of the Qt Toolkit.
**
** $TROLLTECH_DUAL_LICENSE$
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#ifndef QSIMPLEX_P_H
#define QSIMPLEX_P_H
#include <QtCore/qhash.h>
#include <QtCore/qpair.h>
struct QSimplexVariable
{
QSimplexVariable() : result(0), index(0) {};
qreal result;
uint index;
};
/*!
\internal
Representation of a LP constraint like:
(c1 * X1) + (c2 * X2) + ... = K
or <= K
or >= K
Where (ci, Xi) are the pairs in "variables" and K the real "constant".
*/
struct QSimplexConstraint
{
QSimplexConstraint() : constant(0), ratio(Equal), artificial(0) {};
enum Ratio {
LessOrEqual = 0,
Equal,
MoreOrEqual
};
QHash<QSimplexVariable *, qreal> variables;
qreal constant;
Ratio ratio;
QPair<QSimplexVariable *, qreal> helper;
QSimplexVariable * artificial;
};
class QSimplex
{
public:
QSimplex();
virtual ~QSimplex();
qreal solveMin();
qreal solveMax();
QList<QSimplexVariable *> constraintsVariables();
void setConstraints(const QList<QSimplexConstraint *> constraints);
void setObjective(QSimplexConstraint *objective);
void dumpMatrix();
private:
// Matrix handling
qreal valueAt(int row, int column);
void setValueAt(int row, int column, qreal value);
void clearRow(int rowIndex);
void clearColumns(int first, int last);
void combineRows(int toIndex, int fromIndex, qreal factor);
// Simplex
int findPivotColumn();
int pivotRowForColumn(int column);
void reducedRowEchelon();
bool iterate();
// Helpers
void clearDataStructures();
void solveMaxHelper();
enum solverFactor { Minimum = -1, Maximum = 1 };
qreal solver(solverFactor factor);
void collectResults();
QList<QSimplexConstraint *> constraints;
QList<QSimplexVariable *> variables;
QSimplexConstraint *objective;
int rows;
int columns;
int firstArtificial;
qreal *matrix;
};
inline QList<QSimplexVariable *> QSimplex::constraintsVariables()
{
return variables;
}
inline qreal QSimplex::valueAt(int rowIndex, int columnIndex)
{
return matrix[rowIndex * columns + columnIndex];
}
inline void QSimplex::setValueAt(int rowIndex, int columnIndex, qreal value)
{
matrix[rowIndex * columns + columnIndex] = value;
}
#endif
|