From 8286779101e70df778f5f086dd5bb5a997e4dc6f Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Tue, 8 Feb 2011 09:01:11 +0000 Subject: [PATCH] Make kp_solve interface work again. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40384 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/libs/alm/Area.h | 4 ++-- headers/libs/linprog/LinearSpec.h | 4 +++- headers/libs/linprog/Variable.h | 1 + src/libs/linprog/LPSolveInterface.cpp | 23 ++++++++++++----------- src/libs/linprog/LinearSpec.cpp | 20 +++++++++++++++++--- src/libs/linprog/Variable.cpp | 7 +++++++ 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/headers/libs/alm/Area.h b/headers/libs/alm/Area.h index 226269fab9..fdf6fc9c7e 100644 --- a/headers/libs/alm/Area.h +++ b/headers/libs/alm/Area.h @@ -20,7 +20,7 @@ #include "Tab.h" -#define USE_SCALE_VARIABLE 1 +#define USE_SCALE_VARIABLE 0 class Constraint; @@ -131,7 +131,7 @@ private: BSize& preferred); void _UpdatePreferredHeightConstraint( BSize& preferred); - + void _SetupPreferredConstraints(); private: BLayoutItem* fLayoutItem; diff --git a/headers/libs/linprog/LinearSpec.h b/headers/libs/linprog/LinearSpec.h index 3d7ce5ab56..2a7f4216d9 100644 --- a/headers/libs/linprog/LinearSpec.h +++ b/headers/libs/linprog/LinearSpec.h @@ -72,6 +72,7 @@ public: bool RemoveVariable(Variable* variable, bool deleteVariable = true); int32 IndexOf(const Variable* variable) const; + int32 GlobalIndexOf(const Variable* variable) const; bool UpdateRange(Variable* variable); bool AddConstraint(Constraint* constraint); @@ -132,7 +133,8 @@ public: void GetString(BString& string) const; const ConstraintList& Constraints() const; - const VariableList& Variables() const; + const VariableList& UsedVariables() const; + const VariableList& AllVariables() const; protected: friend class Constraint; diff --git a/headers/libs/linprog/Variable.h b/headers/libs/linprog/Variable.h index 6be58a3a88..00c4cb8dda 100644 --- a/headers/libs/linprog/Variable.h +++ b/headers/libs/linprog/Variable.h @@ -22,6 +22,7 @@ class Summand; class Variable { public: int32 Index() const; + int32 GlobalIndex() const; LinearSpec* LS() const; double Value() const; void SetValue(double value); diff --git a/src/libs/linprog/LPSolveInterface.cpp b/src/libs/linprog/LPSolveInterface.cpp index e9f46d14ac..e21f004354 100644 --- a/src/libs/linprog/LPSolveInterface.cpp +++ b/src/libs/linprog/LPSolveInterface.cpp @@ -49,7 +49,7 @@ LPSolveInterface::~LPSolveInterface() ResultType LPSolveInterface::Solve() { - const VariableList& variables = fLinearSpec->Variables(); + const VariableList& variables = fLinearSpec->AllVariables(); if (fLpPresolved != NULL) return _Presolve(variables); @@ -69,6 +69,7 @@ LPSolveInterface::Solve() for (int32 i = 0; i < size; i++) variables.ItemAt(i)->SetValue(x[i]); + break; } else if (result == kInfeasible) break; @@ -93,7 +94,7 @@ LPSolveInterface::VariableAdded(Variable* variable) bool LPSolveInterface::VariableRemoved(Variable* variable) { - if (!del_column(fLP, variable->Index() + 1)) + if (!del_column(fLP, variable->GlobalIndex() + 1)) return false; _RemovePresolved(); return true; @@ -105,7 +106,7 @@ LPSolveInterface::VariableRangeChanged(Variable* variable) { double min = variable->Min(); double max = variable->Max(); - if (!set_bounds(fLP, variable->Index() + 1, min, max)) + if (!set_bounds(fLP, variable->GlobalIndex() + 1, min, max)) return false; _RemovePresolved(); return true; @@ -124,7 +125,7 @@ LPSolveInterface::ConstraintAdded(Constraint* constraint) for (; nCoefficient < summands->CountItems(); nCoefficient++) { Summand* s = summands->ItemAt(nCoefficient); coeffs[nCoefficient] = s->Coeff(); - variableIndices[nCoefficient] = s->Var()->Index() + 1; + variableIndices[nCoefficient] = s->Var()->GlobalIndex() + 1; } double penaltyNeg = constraint->PenaltyNeg(); @@ -133,7 +134,7 @@ LPSolveInterface::ConstraintAdded(Constraint* constraint) constraint->PenaltyNeg(), fLinearSpec->AddVariable()); fObjFunction->AddItem(constraint->fDNegObjSummand); variableIndices[nCoefficient] - = constraint->fDNegObjSummand->Var()->Index() + 1; + = constraint->fDNegObjSummand->Var()->GlobalIndex() + 1; coeffs[nCoefficient] = 1.0; nCoefficient++; } @@ -144,7 +145,7 @@ LPSolveInterface::ConstraintAdded(Constraint* constraint) constraint->PenaltyPos(), fLinearSpec->AddVariable()); fObjFunction->AddItem(constraint->fDPosObjSummand); variableIndices[nCoefficient] - = constraint->fDPosObjSummand->Var()->Index() + 1; + = constraint->fDPosObjSummand->Var()->GlobalIndex() + 1; coeffs[nCoefficient] = -1.0; nCoefficient++; } @@ -204,7 +205,7 @@ LPSolveInterface::LeftSideChanged(Constraint* constraint) for (i = 0; i < leftSide->CountItems(); i++) { Summand* s = leftSide->ItemAt(i); coeffs[i] = s->Coeff(); - variableIndices[i] = s->Var()->Index() + 1; + variableIndices[i] = s->Var()->GlobalIndex() + 1; } double penaltyNeg = constraint->PenaltyNeg(); @@ -214,7 +215,7 @@ LPSolveInterface::LeftSideChanged(Constraint* constraint) constraint->PenaltyNeg(), fLinearSpec->AddVariable()); fObjFunction->AddItem(constraint->fDNegObjSummand); } - variableIndices[i] = constraint->fDNegObjSummand->Var()->Index() + 1; + variableIndices[i] = constraint->fDNegObjSummand->Var()->GlobalIndex() + 1; coeffs[i] = 1.0; i++; } else { @@ -230,7 +231,7 @@ LPSolveInterface::LeftSideChanged(Constraint* constraint) fLinearSpec->AddVariable()); fObjFunction->AddItem(constraint->fDPosObjSummand); } - variableIndices[i] = constraint->fDPosObjSummand->Var()->Index() + 1; + variableIndices[i] = constraint->fDPosObjSummand->Var()->GlobalIndex() + 1; coeffs[i] = -1.0; i++; } else { @@ -425,7 +426,7 @@ LPSolveInterface::_UpdateObjectiveFunction() for (int32 i = 0; i < size; i++) { current = (Summand*)fObjFunction->ItemAt(i); coeffs[i] = current->Coeff(); - varIndexes[i] = current->Var()->Index() + 1; + varIndexes[i] = current->Var()->GlobalIndex() + 1; } if (!SetObjectiveFunction(size, &coeffs[0], &varIndexes[0])) @@ -472,7 +473,7 @@ LPSolveInterface::_Presolve(const VariableList& variables) for (int32 i = 0; i < size; i++) { Variable* current = variables.ItemAt(i); current->SetValue(get_var_primalresult(fLpPresolved, - get_Norig_rows(fLpPresolved) + current->Index() + 1)); + get_Norig_rows(fLpPresolved) + current->GlobalIndex() + 1)); } } diff --git a/src/libs/linprog/LinearSpec.cpp b/src/libs/linprog/LinearSpec.cpp index 23de653112..d21e9db802 100644 --- a/src/libs/linprog/LinearSpec.cpp +++ b/src/libs/linprog/LinearSpec.cpp @@ -94,17 +94,17 @@ LinearSpec::AddVariable(Variable* variable) if (!fVariables.AddItem(variable)) return false; + if (!fSolver->VariableAdded(variable)) { fVariables.RemoveItem(variable); return false; } + variable->fIsValid = true; if (!UpdateRange(variable)) { RemoveVariable(variable, false); return false; } - - variable->fIsValid = true; return true; } @@ -153,6 +153,13 @@ LinearSpec::IndexOf(const Variable* variable) const } +int32 +LinearSpec::GlobalIndexOf(const Variable* variable) const +{ + return fVariables.IndexOf(variable); +} + + bool LinearSpec::UpdateRange(Variable* variable) { @@ -574,12 +581,19 @@ LinearSpec::Constraints() const const VariableList& -LinearSpec::Variables() const +LinearSpec::UsedVariables() const { return fUsedVariables; } +const VariableList& +LinearSpec::AllVariables() const +{ + return fVariables; +} + + /** * Gets the result type. * diff --git a/src/libs/linprog/Variable.cpp b/src/libs/linprog/Variable.cpp index f288112d45..9c01cc5f1d 100644 --- a/src/libs/linprog/Variable.cpp +++ b/src/libs/linprog/Variable.cpp @@ -37,6 +37,13 @@ Variable::Index() const } +int32 +Variable::GlobalIndex() const +{ + return fLS->GlobalIndexOf(this); +} + + /** * Gets the current linear specification. *