- Polish ToString version of Constraint a bit.

- Check the left side of a Constraint for Summands with same Variables and merge them. This fix some issues in the quadratic solver.



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42219 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Clemens Zeidler
2011-06-17 04:26:09 +00:00
parent b1227839e5
commit 94eb1edee7
+22 -7
View File
@@ -65,6 +65,20 @@ Constraint::SetLeftSide(SummandList* summands)
if (!fIsValid) if (!fIsValid)
return; return;
// check left side
for (int32 i = 0; i < summands->CountItems(); i++) {
Summand* summand = summands->ItemAt(i);
for (int32 a = i + 1; a < summands->CountItems(); a++) {
Summand* nextSummand = summands->ItemAt(a);
if (summand->Var() == nextSummand->Var()) {
summand->SetCoeff(summand->Coeff() + nextSummand->Coeff());
summands->RemoveItem(nextSummand);
delete nextSummand;
a--;
}
}
}
fLeftSide = summands; fLeftSide = summands;
fLS->UpdateLeftSide(this); fLS->UpdateLeftSide(this);
} }
@@ -80,7 +94,7 @@ Constraint::SetLeftSide(double coeff1, Variable* var1)
delete fLeftSide->ItemAt(i); delete fLeftSide->ItemAt(i);
fLeftSide->MakeEmpty(); fLeftSide->MakeEmpty();
fLeftSide->AddItem(new(std::nothrow) Summand(coeff1, var1)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff1, var1));
fLS->UpdateLeftSide(this); SetLeftSide(fLeftSide);
} }
@@ -96,7 +110,7 @@ Constraint::SetLeftSide(double coeff1, Variable* var1,
fLeftSide->MakeEmpty(); fLeftSide->MakeEmpty();
fLeftSide->AddItem(new(std::nothrow) Summand(coeff1, var1)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff1, var1));
fLeftSide->AddItem(new(std::nothrow) Summand(coeff2, var2)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff2, var2));
fLS->UpdateLeftSide(this); SetLeftSide(fLeftSide);
} }
@@ -114,7 +128,7 @@ Constraint::SetLeftSide(double coeff1, Variable* var1,
fLeftSide->AddItem(new(std::nothrow) Summand(coeff1, var1)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff1, var1));
fLeftSide->AddItem(new(std::nothrow) Summand(coeff2, var2)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff2, var2));
fLeftSide->AddItem(new(std::nothrow) Summand(coeff3, var3)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff3, var3));
fLS->UpdateLeftSide(this); SetLeftSide(fLeftSide);
} }
@@ -134,7 +148,7 @@ Constraint::SetLeftSide(double coeff1, Variable* var1,
fLeftSide->AddItem(new(std::nothrow) Summand(coeff2, var2)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff2, var2));
fLeftSide->AddItem(new(std::nothrow) Summand(coeff3, var3)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff3, var3));
fLeftSide->AddItem(new(std::nothrow) Summand(coeff4, var4)); fLeftSide->AddItem(new(std::nothrow) Summand(coeff4, var4));
fLS->UpdateLeftSide(this); SetLeftSide(fLeftSide);
} }
@@ -336,9 +350,11 @@ Constraint::ToString() const
if (fIsValid) { if (fIsValid) {
for (int i = 0; i < fLeftSide->CountItems(); i++) { for (int i = 0; i < fLeftSide->CountItems(); i++) {
Summand* s = static_cast<Summand*>(fLeftSide->ItemAt(i)); Summand* s = static_cast<Summand*>(fLeftSide->ItemAt(i));
if (i != 0 && s->Coeff() >= 0)
string << " + ";
string << (float)s->Coeff() << "*"; string << (float)s->Coeff() << "*";
string << "x"; string << "x";
string << s->Var()->Index() - 1; string << s->Var()->Index();
string << " "; string << " ";
} }
string << ((fOp == kEQ) ? "== " string << ((fOp == kEQ) ? "== "
@@ -369,7 +385,6 @@ Constraint::Constraint(LinearSpec* ls, SummandList* summands, OperatorType op,
double rightSide, double penaltyNeg, double penaltyPos) double rightSide, double penaltyNeg, double penaltyPos)
: :
fLS(ls), fLS(ls),
fLeftSide(summands),
fOp(op), fOp(op),
fRightSide(rightSide), fRightSide(rightSide),
fPenaltyNeg(penaltyNeg), fPenaltyNeg(penaltyNeg),
@@ -378,7 +393,7 @@ Constraint::Constraint(LinearSpec* ls, SummandList* summands, OperatorType op,
fDPosObjSummand(NULL), fDPosObjSummand(NULL),
fIsValid(true) fIsValid(true)
{ {
SetLeftSide(summands);
} }