6#include <QtCore/qset.h>
7#include <QtCore/qdebug.h>
54 clearDataStructures();
60void QSimplex::clearDataStructures()
73 for (
int i = 0;
i < constraints.
size(); ++
i) {
74 delete constraints[
i]->helper.
first;
75 delete constraints[
i]->artificial;
76 delete constraints[
i];
98 clearDataStructures();
105 for (
int i = 0;
i < newConstraints.
size(); ++
i) {
108 c->ratio = newConstraints[
i]->ratio;
109 c->variables = newConstraints[
i]->variables;
114 if (!simplifyConstraints(&constraints)) {
115 qWarning(
"QSimplex: No feasible solution!");
116 clearDataStructures();
128 for (
int i = 0;
i < constraints.
size(); ++
i) {
133 variables = variablesSet.
values();
138 for (
int i = 0;
i < variables.
size(); ++
i) {
140 variables[
i]->index =
i + 1;
155 int variableIndex = variables.
size();
156 QList <QSimplexVariable *> artificialList;
158 for (
int i = 0;
i < constraints.
size(); ++
i) {
164 Q_ASSERT(constraints[
i]->artificial ==
nullptr);
166 switch(constraints[
i]->ratio) {
169 slack->
index = ++variableIndex;
170 constraints[
i]->helper.
first = slack;
171 constraints[
i]->helper.second = 1.0;
175 surplus->
index = ++variableIndex;
176 constraints[
i]->helper.
first = surplus;
177 constraints[
i]->helper.second = -1.0;
181 constraints[
i]->artificial = artificial;
182 artificialList += constraints[
i]->artificial;
191 firstArtificial = variableIndex + 1;
192 for (
int i = 0;
i < artificialList.
size(); ++
i)
193 artificialList[
i]->
index = ++variableIndex;
194 artificialList.
clear();
201 columns = variableIndex + 2;
203 rows = constraints.
size() + 1;
207 qWarning(
"QSimplex: Unable to allocate memory!");
210 for (
int i = columns * rows - 1;
i >= 0; --
i)
214 for (
int i = 1;
i <= constraints.
size(); ++
i) {
219 setValueAt(
i, 0,
c->artificial->index);
220 setValueAt(
i,
c->artificial->index, 1.0);
222 if (
c->helper.second != 0.0) {
224 setValueAt(
i,
c->helper.first->index,
c->helper.second);
229 setValueAt(
i, 0,
c->helper.first->index);
230 setValueAt(
i,
c->helper.first->index, 1.0);
234 for (
iter =
c->variables.constBegin();
235 iter !=
c->variables.constEnd();
237 setValueAt(
i,
iter.key()->index,
iter.value());
240 setValueAt(
i, columns - 1,
c->constant);
245 for (
int j = firstArtificial;
j < columns - 1; ++
j)
246 setValueAt(0,
j, 1.0);
257 if ((valueAt(0, columns - 1) != 0.0) && (
qAbs(valueAt(0, columns - 1)) > 0.00001)) {
258 qWarning(
"QSimplex: No feasible solution!");
259 clearDataStructures();
266 clearColumns(firstArtificial, columns - 2);
280void QSimplex::solveMaxHelper()
291 objective = newObjective;
297void QSimplex::clearRow(
int rowIndex)
300 for (
int i = 0;
i < columns; ++
i)
307void QSimplex::clearColumns(
int first,
int last)
309 for (
int i = 0;
i < rows; ++
i) {
321 qDebug(
"---- Simplex Matrix ----\n");
324 for (
int j = 0;
j < columns; ++
j)
327 for (
int i = 0;
i < rows; ++
i) {
331 for (
int j = 0;
j < columns; ++
j)
335 qDebug(
"------------------------\n");
341void QSimplex::combineRows(
int toIndex,
int fromIndex,
qreal factor)
349 for (
int j = 1;
j < columns; ++
j) {
359 if (
qAbs(to[
j]) < 0.0000000001)
367int QSimplex::findPivotColumn()
372 for (
int j = 0;
j < columns-1; ++
j) {
373 if (valueAt(0,
j) < min) {
399int QSimplex::pivotRowForColumn(
int column)
404 for (
int i = 1;
i < rows; ++
i) {
410 if (quotient < min) {
413 }
else if ((quotient == min) && (valueAt(
i, 0) > valueAt(minIndex, 0))) {
424void QSimplex::reducedRowEchelon()
426 for (
int i = 1;
i < rows; ++
i) {
427 int factorInObjectiveRow = valueAt(
i, 0);
428 combineRows(0,
i, -1 * valueAt(0, factorInObjectiveRow));
438bool QSimplex::iterate()
441 int pivotColumn = findPivotColumn();
442 if (pivotColumn == -1)
446 int pivotRow = pivotRowForColumn(pivotColumn);
447 if (pivotRow == -1) {
448 qWarning(
"QSimplex: Unbounded problem!");
453 qreal pivot = valueAt(pivotRow, pivotColumn);
455 combineRows(pivotRow, pivotRow, (1.0 - pivot) / pivot);
462 combineRows(
row, pivotRow, -1 * valueAt(
row, pivotColumn));
466 setValueAt(pivotRow, 0, pivotColumn);
486qreal QSimplex::solver(SolverFactor factor)
492 qreal resultOffset = 0;
501 if (
iter.key()->index == -1) {
502 resultOffset +=
iter.value() *
iter.key()->result;
506 setValueAt(0,
iter.key()->index, -1 * factor *
iter.value());
513 for (
int i = 0;
i < constraints.
size(); ++
i) {
520 return (
qToUnderlying(factor) * valueAt(0, columns - 1)) + resultOffset;
529 return solver(Minimum);
538 return solver(Maximum);
547void QSimplex::collectResults()
553 for (
int i = 0;
i < variables.
size(); ++
i)
559 for (
int i = 1;
i < rows; ++
i) {
560 int index = valueAt(
i, 0) - 1;
562 variables[
index]->result = valueAt(
i, columns - 1);
574 bool modified =
true;
581 while (
iter != constraints->
end()) {
599 if (
c->variables.contains(
r.key())) {
600 c->constant -=
r.value() *
c->variables.take(
r.key());
609 if (
c->variables.isEmpty()) {
611 if (
c->isSatisfied() ==
false)
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
qsizetype size() const noexcept
bool isEmpty() const noexcept
iterator erase(const_iterator begin, const_iterator end)
iterator insert(qsizetype i, parameter_type t)
const_reference at(qsizetype i) const noexcept
const_iterator constBegin() const noexcept
const_iterator constEnd() const noexcept
QList< T > values() const
const_iterator cbegin() const noexcept
iterator insert(const T &value)
bool setConstraints(const QList< QSimplexConstraint * > &constraints)
void setObjective(QSimplexConstraint *objective)
\macro QT_RESTRICTED_CAST_FROM_ASCII
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
QSet< QString >::iterator it
Combined button and popup list for selecting options.
constexpr const T & min(const T &a, const T &b)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter * iter
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr T qAbs(const T &t)
GLsizei const GLfloat * v
[13]
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
#define qPrintable(string)
constexpr std::underlying_type_t< Enum > qToUnderlying(Enum e) noexcept
static uint toIndex(ExecutionEngine *e, const Value &v)
QHash< QSimplexVariable *, qreal > variables