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// Qt-Security score:significant reason:default
4
5#ifndef QSIMPLEX_P_H
6#define QSIMPLEX_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtWidgets/private/qtwidgetsglobal_p.h>
20#include <QtCore/qhash.h>
21#include <QtCore/qstring.h>
22
24
25QT_BEGIN_NAMESPACE
26
27struct QSimplexVariable
28{
29 QSimplexVariable() : result(0), index(0) {}
30
31 qreal result;
32 int index;
33protected:
34 QT_DECLARE_RO5_SMF_AS_DEFAULTED(QSimplexVariable)
35};
36
37// "pure" QSimplexVariable without the protected destructor
38struct QConcreteSimplexVariable final : QSimplexVariable
39{
40};
41
42/*!
43 \internal
44
45 Representation of a LP constraint like:
46
47 (c1 * X1) + (c2 * X2) + ... = K
48 or <= K
49 or >= K
50
51 Where (ci, Xi) are the pairs in "variables" and K the real "constant".
52*/
54{
56
58
64
68
70 QConcreteSimplexVariable *artificial;
71
72 void invert();
73
74 bool isSatisfied() {
75 qreal leftHandSide(0);
76
77 for (auto iter = variables.cbegin(); iter != variables.cend(); ++iter) {
78 leftHandSide += iter.value() * iter.key()->result;
79 }
80
81 Q_ASSERT(constant > 0 || qFuzzyCompare(1, 1 + constant));
82
83 if ((leftHandSide == constant) || qAbs(leftHandSide - constant) < 0.0000001)
84 return true;
85
86 switch (ratio) {
87 case LessOrEqual:
88 return leftHandSide < constant;
89 case MoreOrEqual:
90 return leftHandSide > constant;
91 default:
92 return false;
93 }
94 }
95
96#ifdef QT_DEBUG
99 result += QString::fromLatin1("-- QSimplexConstraint %1 --").arg(quintptr(this), 0, 16);
100
101 for (auto iter = variables.cbegin(); iter != variables.cend(); ++iter) {
102 result += QString::fromLatin1(" %1 x %2").arg(iter.value()).arg(quintptr(iter.key()), 0, 16);
103 }
104
105 switch (ratio) {
106 case LessOrEqual:
107 result += QString::fromLatin1(" (less) <= %1").arg(constant);
108 break;
109 case MoreOrEqual:
110 result += QString::fromLatin1(" (more) >= %1").arg(constant);
111 break;
112 default:
113 result += QString::fromLatin1(" (eqal) == %1").arg(constant);
114 }
115
116 return result;
117 }
118#endif
119};
120
121class QSimplex final
122{
124public:
125 QSimplex();
126 ~QSimplex();
127
128 qreal solveMin();
129 qreal solveMax();
130
131 bool setConstraints(const QList<QSimplexConstraint *> &constraints);
132 void setObjective(QSimplexConstraint *objective);
133
134 void dumpMatrix();
135
136private:
137 // Matrix handling
138 inline qreal valueAt(int row, int column);
139 inline void setValueAt(int row, int column, qreal value);
140 void clearRow(int rowIndex);
141 void clearColumns(int first, int last);
142 void combineRows(int toIndex, int fromIndex, qreal factor);
143
144 // Simplex
145 bool simplifyConstraints(QList<QSimplexConstraint *> *constraints);
146 int findPivotColumn();
147 int pivotRowForColumn(int column);
148 void reducedRowEchelon();
149 bool iterate();
150
151 // Helpers
152 void clearDataStructures();
153 void solveMaxHelper();
154 enum SolverFactor { Minimum = -1, Maximum = 1 };
155 qreal solver(SolverFactor factor);
156 void collectResults();
157
158 QList<QSimplexConstraint *> constraints;
159 QList<QSimplexVariable *> variables;
160 QSimplexConstraint *objective;
161
162 int rows;
163 int columns;
164 int firstArtificial;
165
166 qreal *matrix;
167};
168
169inline qreal QSimplex::valueAt(int rowIndex, int columnIndex)
170{
171 return matrix[rowIndex * columns + columnIndex];
172}
173
174inline void QSimplex::setValueAt(int rowIndex, int columnIndex, qreal value)
175{
176 matrix[rowIndex * columns + columnIndex] = value;
177}
178
179QT_END_NAMESPACE
180
181#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
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:65
QConcreteSimplexVariable * artificial
Definition qsimplex_p.h:70
std::pair< QConcreteSimplexVariable *, qreal > helper
Definition qsimplex_p.h:69
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)