Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsimplex_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSIMPLEX_P_H
5#define QSIMPLEX_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtWidgets/private/qtwidgetsglobal_p.h>
19#include <QtCore/qhash.h>
20#include <QtCore/qpair.h>
21#include <QtCore/qstring.h>
22
23QT_REQUIRE_CONFIG(graphicsview);
24
25QT_BEGIN_NAMESPACE
26
27struct QSimplexVariable
28{
29 QSimplexVariable() : result(0), index(0) {}
30
31 qreal result;
32 int index;
33};
34
35
36/*!
37 \internal
38
39 Representation of a LP constraint like:
40
41 (c1 * X1) + (c2 * X2) + ... = K
42 or <= K
43 or >= K
44
45 Where (ci, Xi) are the pairs in "variables" and K the real "constant".
46*/
48{
50
56
60
62 QSimplexVariable * artificial;
63
64 void invert();
65
66 bool isSatisfied() {
67 qreal leftHandSide(0);
68
69 QHash<QSimplexVariable *, qreal>::const_iterator iter;
70 for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) {
71 leftHandSide += iter.value() * iter.key()->result;
72 }
73
74 Q_ASSERT(constant > 0 || qFuzzyCompare(1, 1 + constant));
75
76 if ((leftHandSide == constant) || qAbs(leftHandSide - constant) < 0.0000001)
77 return true;
78
79 switch (ratio) {
80 case LessOrEqual:
81 return leftHandSide < constant;
82 case MoreOrEqual:
83 return leftHandSide > constant;
84 default:
85 return false;
86 }
87 }
88
89#ifdef QT_DEBUG
92 result += QString::fromLatin1("-- QSimplexConstraint %1 --").arg(quintptr(this), 0, 16);
93
96 result += QString::fromLatin1(" %1 x %2").arg(iter.value()).arg(quintptr(iter.key()), 0, 16);
97 }
98
99 switch (ratio) {
100 case LessOrEqual:
101 result += QString::fromLatin1(" (less) <= %1").arg(constant);
102 break;
103 case MoreOrEqual:
104 result += QString::fromLatin1(" (more) >= %1").arg(constant);
105 break;
106 default:
107 result += QString::fromLatin1(" (eqal) == %1").arg(constant);
108 }
109
110 return result;
111 }
112#endif
113};
114
116{
118public:
119 QSimplex();
120 ~QSimplex();
121
122 qreal solveMin();
123 qreal solveMax();
124
125 bool setConstraints(const QList<QSimplexConstraint *> &constraints);
126 void setObjective(QSimplexConstraint *objective);
127
128 void dumpMatrix();
129
130private:
131 // Matrix handling
132 inline qreal valueAt(int row, int column);
133 inline void setValueAt(int row, int column, qreal value);
134 void clearRow(int rowIndex);
135 void clearColumns(int first, int last);
136 void combineRows(int toIndex, int fromIndex, qreal factor);
137
138 // Simplex
139 bool simplifyConstraints(QList<QSimplexConstraint *> *constraints);
140 int findPivotColumn();
141 int pivotRowForColumn(int column);
142 void reducedRowEchelon();
143 bool iterate();
144
145 // Helpers
146 void clearDataStructures();
147 void solveMaxHelper();
148 enum SolverFactor { Minimum = -1, Maximum = 1 };
149 qreal solver(SolverFactor factor);
150 void collectResults();
151
152 QList<QSimplexConstraint *> constraints;
153 QList<QSimplexVariable *> variables;
154 QSimplexConstraint *objective;
155
156 int rows;
157 int columns;
158 int firstArtificial;
159
160 qreal *matrix;
161};
162
163inline qreal QSimplex::valueAt(int rowIndex, int columnIndex)
164{
165 return matrix[rowIndex * columns + columnIndex];
166}
167
168inline void QSimplex::setValueAt(int rowIndex, int columnIndex, qreal value)
169{
170 matrix[rowIndex * columns + columnIndex] = value;
171}
172
173QT_END_NAMESPACE
174
175#endif // QSIMPLEX_P_H
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics V...
void setSizePolicy(QSizePolicy::Policy policy)
static QGraphicsAnchorPrivate * get(QGraphicsAnchor *q)
QGraphicsAnchorLayoutPrivate * layoutPrivate
The QGraphicsAnchor class represents an anchor between two items in a QGraphicsAnchorLayout.
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
void setDefaultSpacing(Qt::Orientation o, qreal spacing)
bool operator==(const QLayoutStyleInfo &other) const
bool operator!=(const QLayoutStyleInfo &other) const
qreal perItemSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation) const
qreal defaultSpacing(Qt::Orientation o) const
QWidget * widget() const
QStyle * style() const
QLayoutStyleInfo(QStyle *style, QWidget *widget)
bool setConstraints(const QList< QSimplexConstraint * > &constraints)
void dumpMatrix()
void setObjective(QSimplexConstraint *objective)
qreal solveMax()
qreal solveMin()
QSimplexConstraint * constraint(const GraphPath &path) const
Combined button and popup list for selecting options.
QT_REQUIRE_CONFIG(animation)
Q_DECLARE_TYPEINFO(GraphPath, Q_RELOCATABLE_TYPE)
QT_REQUIRE_CONFIG(graphicsview)
Q_DECLARE_INTERFACE(QNetworkAccessBackendFactory, QNetworkAccessBackendFactory_iid)
QHash< QSimplexVariable *, qreal > variables
Definition qsimplex_p.h:57
QPair< QSimplexVariable *, qreal > helper
Definition qsimplex_p.h:61
QSimplexVariable * artificial
Definition qsimplex_p.h:62
void refreshSizeHints(const QLayoutStyleInfo *styleInfo=nullptr)
AnchorVertexPair(AnchorVertex *v1, AnchorVertex *v2, AnchorData *data)
AnchorVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge)
ParallelAnchorData(AnchorData *first, AnchorData *second)
SequentialAnchorData(const QList< AnchorVertex * > &vertices, const QList< AnchorData * > &edges)