- Remove lp_solve dependencies form Variable class and put everything into LinearSpec. As a side effect Variable management is a bit more consistence now. We want to replace lp_solve soon so it will be easier to replace it just in LinearSpec.

- Update copyrights.
- Lot of small things related to the Variable refactoring.



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38892 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Clemens Zeidler
2010-10-05 20:15:55 +00:00
parent 64141b1b74
commit fc691d7de2
12 changed files with 168 additions and 120 deletions
+2 -2
View File
@@ -8,12 +8,12 @@
#include "Constraint.h" #include "Constraint.h"
#include "LinearSpec.h" #include "LinearSpec.h"
#include "Tab.h"
namespace BALM { namespace BALM {
class BALMLayout; class BALMLayout;
class XTab;
/** /**
* Represents a column defined by two x-tabs. * Represents a column defined by two x-tabs.
@@ -36,7 +36,7 @@ public:
ConstraintList* Constraints() const; ConstraintList* Constraints() const;
protected: protected:
Column(LinearSpec* ls); Column(BALMLayout* layout);
protected: protected:
LinearSpec* fLS; LinearSpec* fLS;
+3 -3
View File
@@ -8,13 +8,13 @@
#include "Constraint.h" #include "Constraint.h"
#include "LinearSpec.h" #include "LinearSpec.h"
#include "Tab.h"
namespace BALM { namespace BALM {
class BALMLayout; class BALMLayout;
class YTab;
/** /**
* Represents a row defined by two y-tabs. * Represents a row defined by two y-tabs.
*/ */
@@ -34,7 +34,7 @@ public:
ConstraintList* Constraints() const; ConstraintList* Constraints() const;
protected: protected:
Row(LinearSpec* ls); Row(BALMLayout* layout);
protected: protected:
LinearSpec* fLS; LinearSpec* fLS;
-2
View File
@@ -27,7 +27,6 @@ protected:
public: public:
friend class BALMLayout; friend class BALMLayout;
friend class Column;
}; };
@@ -42,7 +41,6 @@ protected:
public: public:
friend class BALMLayout; friend class BALMLayout;
friend class Row;
}; };
+10 -8
View File
@@ -36,6 +36,12 @@ public:
virtual ~LinearSpec(); virtual ~LinearSpec();
Variable* AddVariable(); Variable* AddVariable();
bool AddVariable(Variable* variable);
bool RemoveVariable(Variable* variable,
bool deleteVariable = true);
int32 IndexOf(const Variable* variable) const;
bool SetRange(Variable* variable, double min,
double max);
Constraint* AddConstraint(SummandList* summands, Constraint* AddConstraint(SummandList* summands,
OperatorType op, double rightSide); OperatorType op, double rightSide);
@@ -86,8 +92,6 @@ public:
void SetObjectiveFunction(SummandList* objFunction); void SetObjectiveFunction(SummandList* objFunction);
void UpdateObjectiveFunction(); void UpdateObjectiveFunction();
ResultType Presolve();
void RemovePresolved();
ResultType Solve(); ResultType Solve();
void Save(const char* fileName); void Save(const char* fileName);
@@ -102,13 +106,12 @@ public:
operator BString() const; operator BString() const;
void GetString(BString& string) const; void GetString(BString& string) const;
protected: const ConstraintList& Constraints() const;
VariableList* Variables() const;
ConstraintList* Constraints() const;
int32 fCountColumns;
private: private:
ResultType Presolve();
void RemovePresolved();
lprec* fLpPresolved; lprec* fLpPresolved;
OptimizationType fOptimization; OptimizationType fOptimization;
lprec* fLP; lprec* fLP;
@@ -121,7 +124,6 @@ private:
public: public:
friend class Constraint; friend class Constraint;
friend class Variable;
}; };
+5 -6
View File
@@ -6,11 +6,9 @@
#ifndef VARIABLE_H #ifndef VARIABLE_H
#define VARIABLE_H #define VARIABLE_H
#include <File.h>
#include <ObjectList.h> #include <ObjectList.h>
#include <String.h> #include <String.h>
#include <SupportDefs.h>
namespace LinearProgramming { namespace LinearProgramming {
@@ -51,15 +49,18 @@ public:
double penaltyNeg, double penaltyPos); double penaltyNeg, double penaltyPos);
bool IsValid(); bool IsValid();
//! Dangerous the variable don't belong to the LinearSpec anymore,
//! delete it yourself!
void Invalidate(); void Invalidate();
virtual ~Variable(); ~Variable();
protected: protected:
Variable(LinearSpec* ls); Variable(LinearSpec* ls);
private: private:
LinearSpec* fLS; LinearSpec* fLS;
BObjectList<Summand> fUsingSummands; BObjectList<Summand> fUsingSummands;
// All Summands that link to this Variable // All Summands that link to this Variable
double fValue; double fValue;
@@ -71,9 +72,7 @@ private:
public: public:
friend class LinearSpec; friend class LinearSpec;
friend class Constraint;
friend class Summand; friend class Summand;
}; };
+28 -11
View File
@@ -10,6 +10,7 @@
#include <math.h> // for floor #include <math.h> // for floor
#include <new> #include <new>
#include <iostream>
#include "ViewLayoutItem.h" #include "ViewLayoutItem.h"
@@ -31,10 +32,10 @@ BALMLayout::BALMLayout(float spacing)
fSpacing(spacing), fSpacing(spacing),
fCurrentArea(NULL) fCurrentArea(NULL)
{ {
fLeft = new XTab(&fSolver); fLeft = AddXTab();
fRight = new XTab(&fSolver); fRight = AddXTab();
fTop = new YTab(&fSolver); fTop = AddYTab();
fBottom = new YTab(&fSolver); fBottom = AddYTab();
// the Left tab is always at x-position 0, and the Top tab is always at y-position 0 // the Left tab is always at x-position 0, and the Top tab is always at y-position 0
fLeft->SetRange(0, 0); fLeft->SetRange(0, 0);
@@ -64,7 +65,15 @@ BALMLayout::~BALMLayout()
XTab* XTab*
BALMLayout::AddXTab() BALMLayout::AddXTab()
{ {
return new XTab(&fSolver); XTab* tab = new XTab(&fSolver);
if (!tab)
return NULL;
if (!fSolver.AddVariable(tab)) {
delete tab;
return NULL;
}
return tab;
} }
@@ -76,7 +85,15 @@ BALMLayout::AddXTab()
YTab* YTab*
BALMLayout::AddYTab() BALMLayout::AddYTab()
{ {
return new YTab(&fSolver); YTab* tab = new YTab(&fSolver);
if (!tab)
return NULL;
if (!fSolver.AddVariable(tab)) {
delete tab;
return NULL;
}
return tab;
} }
@@ -88,7 +105,7 @@ BALMLayout::AddYTab()
Row* Row*
BALMLayout::AddRow() BALMLayout::AddRow()
{ {
return new Row(&fSolver); return new Row(this);
} }
@@ -102,7 +119,7 @@ BALMLayout::AddRow()
Row* Row*
BALMLayout::AddRow(YTab* top, YTab* bottom) BALMLayout::AddRow(YTab* top, YTab* bottom)
{ {
Row* row = new Row(&fSolver); Row* row = new Row(this);
if (top != NULL) if (top != NULL)
row->Constraints()->AddItem(row->Top()->IsEqual(top)); row->Constraints()->AddItem(row->Top()->IsEqual(top));
if (bottom != NULL) if (bottom != NULL)
@@ -119,7 +136,7 @@ BALMLayout::AddRow(YTab* top, YTab* bottom)
Column* Column*
BALMLayout::AddColumn() BALMLayout::AddColumn()
{ {
return new Column(&fSolver); return new Column(this);
} }
@@ -133,7 +150,7 @@ BALMLayout::AddColumn()
Column* Column*
BALMLayout::AddColumn(XTab* left, XTab* right) BALMLayout::AddColumn(XTab* left, XTab* right)
{ {
Column* column = new Column(&fSolver); Column* column = new Column(this);
if (left != NULL) if (left != NULL)
column->Constraints()->AddItem(column->Left()->IsEqual(left)); column->Constraints()->AddItem(column->Left()->IsEqual(left));
if (right != NULL) if (right != NULL)
@@ -677,7 +694,7 @@ BALMLayout::DerivedLayoutItems()
_SolveLayout(); _SolveLayout();
// if new layout is infasible, use previous layout // if new layout is infeasible, use previous layout
if (fSolver.Result() == INFEASIBLE) if (fSolver.Result() == INFEASIBLE)
return; return;
+1
View File
@@ -1,6 +1,7 @@
/* /*
* Copyright 2007-2008, Christof Lutteroth, [email protected] * Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected] * Copyright 2007-2008, James Kim, [email protected]
* Copyright 2010, Clemens Zeidler <[email protected]>
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
+8 -9
View File
@@ -1,6 +1,7 @@
/* /*
* Copyright 2007-2008, Christof Lutteroth, [email protected] * Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected] * Copyright 2007-2008, James Kim, [email protected]
* Copyright 2010, Clemens Zeidler <[email protected]>
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@@ -11,8 +12,6 @@
#include "OperatorType.h" #include "OperatorType.h"
#include "Tab.h" #include "Tab.h"
#include <SupportDefs.h>
/** /**
* The left boundary of the column. * The left boundary of the column.
@@ -72,7 +71,7 @@ Column::SetPrevious(Column* value)
fPrevious = value; fPrevious = value;
fPrevious->fNext = this; fPrevious->fNext = this;
value->fNextGlue = value->fRight->IsEqual(fLeft); value->fNextGlue = value->Right()->IsEqual(Left());
fPreviousGlue = value->fNextGlue; fPreviousGlue = value->fNextGlue;
} }
@@ -115,7 +114,7 @@ Column::SetNext(Column* value)
fNext = value; fNext = value;
fNext->fPrevious = this; fNext->fPrevious = this;
value->fPreviousGlue = fRight->IsEqual(value->fLeft); value->fPreviousGlue = Right()->IsEqual(value->Left());
fNextGlue = value->fPreviousGlue; fNextGlue = value->fPreviousGlue;
} }
@@ -156,7 +155,7 @@ Constraint*
Column::HasSameWidthAs(Column* column) Column::HasSameWidthAs(Column* column)
{ {
Constraint* constraint = fLS->AddConstraint( Constraint* constraint = fLS->AddConstraint(
-1.0, fLeft, 1.0, fRight, 1.0, column->fLeft, -1.0, column->fRight, -1.0, Left(), 1.0, Right(), 1.0, column->Left(), -1.0, column->Right(),
OperatorType(EQ), 0.0); OperatorType(EQ), 0.0);
fConstraints.AddItem(constraint); fConstraints.AddItem(constraint);
return constraint; return constraint;
@@ -188,10 +187,10 @@ Column::~Column()
/** /**
* Constructor. * Constructor.
*/ */
Column::Column(LinearSpec* ls) Column::Column(BALMLayout* layout)
{ {
fLS = ls; fLS = layout->Solver();
fLeft = new XTab(ls); fLeft = layout->AddXTab();
fRight = new XTab(ls); fRight = layout->AddXTab();
} }
+8 -7
View File
@@ -1,6 +1,7 @@
/* /*
* Copyright 2007-2008, Christof Lutteroth, [email protected] * Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected] * Copyright 2007-2008, James Kim, [email protected]
* Copyright 2010, Clemens Zeidler <[email protected]>
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@@ -73,7 +74,7 @@ Row::SetPrevious(Row* value)
fPrevious = value; fPrevious = value;
fPrevious->fNext = this; fPrevious->fNext = this;
value->fNextGlue = value->fBottom->IsEqual(fTop); value->fNextGlue = value->Bottom()->IsEqual(Top());
fPreviousGlue = value->fNextGlue; fPreviousGlue = value->fNextGlue;
} }
@@ -117,7 +118,7 @@ Row::SetNext(Row* value)
fNext = value; fNext = value;
fNext->fPrevious = this; fNext->fPrevious = this;
value->fPreviousGlue = fBottom->IsEqual(value->fTop); value->fPreviousGlue = Bottom()->IsEqual(value->Top());
fNextGlue = value->fPreviousGlue; fNextGlue = value->fPreviousGlue;
} }
@@ -158,7 +159,7 @@ Constraint*
Row::HasSameHeightAs(Row* row) Row::HasSameHeightAs(Row* row)
{ {
Constraint* constraint = fLS->AddConstraint( Constraint* constraint = fLS->AddConstraint(
-1.0, fTop, 1.0, fBottom, 1.0, row->fTop, -1.0, row->fBottom, -1.0, Top(), 1.0, Bottom(), 1.0, row->Top(), -1.0, row->Bottom(),
OperatorType(EQ), 0.0); OperatorType(EQ), 0.0);
fConstraints.AddItem(constraint); fConstraints.AddItem(constraint);
return constraint; return constraint;
@@ -193,10 +194,10 @@ Row::~Row()
/** /**
* Constructor. * Constructor.
*/ */
Row::Row(LinearSpec* ls) Row::Row(BALMLayout* layout)
{ {
fLS = ls; fLS = layout->Solver();
fTop = new YTab(ls); fTop = layout->AddYTab();
fBottom = new YTab(ls); fBottom = layout->AddYTab();
} }
+7 -9
View File
@@ -8,8 +8,6 @@
#include "LinearSpec.h" #include "LinearSpec.h"
#include "Variable.h" #include "Variable.h"
#include "lp_lib.h"
// Toggle debug output // Toggle debug output
//#define DEBUG_CONSTRAINT //#define DEBUG_CONSTRAINT
@@ -29,7 +27,7 @@
int32 int32
Constraint::Index() const Constraint::Index() const
{ {
int32 i = fLS->Constraints()->IndexOf(this); int32 i = fLS->Constraints().IndexOf(this);
if (i == -1) { if (i == -1) {
STRACE(("Constraint not part of fLS->Constraints().")); STRACE(("Constraint not part of fLS->Constraints()."));
return -1; return -1;
@@ -265,7 +263,7 @@ Constraint::SetPenaltyNeg(double value)
return; return;
if (fDNegObjSummand == NULL) { if (fDNegObjSummand == NULL) {
fDNegObjSummand = new Summand(value, new Variable(fLS)); fDNegObjSummand = new Summand(value, fLS->AddVariable());
fLS->ObjectiveFunction()->AddItem(fDNegObjSummand); fLS->ObjectiveFunction()->AddItem(fDNegObjSummand);
UpdateLeftSide(); UpdateLeftSide();
fLS->UpdateObjectiveFunction(); fLS->UpdateObjectiveFunction();
@@ -307,7 +305,7 @@ Constraint::SetPenaltyPos(double value)
return; return;
if (fDPosObjSummand == NULL) { if (fDPosObjSummand == NULL) {
fDPosObjSummand = new Summand(value, new Variable(fLS)); fDPosObjSummand = new Summand(value, fLS->AddVariable());
fLS->ObjectiveFunction()->AddItem(fDPosObjSummand); fLS->ObjectiveFunction()->AddItem(fDPosObjSummand);
UpdateLeftSide(); UpdateLeftSide();
fLS->UpdateObjectiveFunction(); fLS->UpdateObjectiveFunction();
@@ -441,7 +439,7 @@ Constraint::Invalidate()
} }
del_constraint(fLS->fLP, this->Index()); del_constraint(fLS->fLP, this->Index());
fLS->Constraints()->RemoveItem(this); const_cast<ConstraintList&>(fLS->Constraints()).RemoveItem(this);
} }
@@ -502,7 +500,7 @@ Constraint::Constraint(LinearSpec* ls, SummandList* summands, OperatorType op,
if (penaltyNeg != INFINITY if (penaltyNeg != INFINITY
&& fOp != OperatorType(LE)) { && fOp != OperatorType(LE)) {
fDNegObjSummand = new Summand(penaltyNeg, new Variable(fLS)); fDNegObjSummand = new Summand(penaltyNeg, ls->AddVariable());
fLS->fObjFunction->AddItem(fDNegObjSummand); fLS->fObjFunction->AddItem(fDNegObjSummand);
varIndexes[i] = fDNegObjSummand->Var()->Index(); varIndexes[i] = fDNegObjSummand->Var()->Index();
coeffs[i] = 1.0; coeffs[i] = 1.0;
@@ -513,7 +511,7 @@ Constraint::Constraint(LinearSpec* ls, SummandList* summands, OperatorType op,
if (penaltyPos != INFINITY if (penaltyPos != INFINITY
&& fOp != OperatorType(GE)) { && fOp != OperatorType(GE)) {
fDPosObjSummand = new Summand(penaltyPos, new Variable(fLS)); fDPosObjSummand = new Summand(penaltyPos, ls->AddVariable());
fLS->fObjFunction->AddItem(fDPosObjSummand); fLS->fObjFunction->AddItem(fDPosObjSummand);
varIndexes[i] = fDPosObjSummand->Var()->Index(); varIndexes[i] = fDPosObjSummand->Var()->Index();
coeffs[i] = -1.0; coeffs[i] = -1.0;
@@ -529,7 +527,7 @@ Constraint::Constraint(LinearSpec* ls, SummandList* summands, OperatorType op,
STRACE(("Error in add_constraintex.")); STRACE(("Error in add_constraintex."));
fLS->UpdateObjectiveFunction(); fLS->UpdateObjectiveFunction();
fLS->Constraints()->AddItem(this); const_cast<ConstraintList&>(fLS->Constraints()).AddItem(this);
} }
+80 -34
View File
@@ -1,9 +1,11 @@
/* /*
* Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
* Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
* Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de>
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include "LinearSpec.h" #include "LinearSpec.h"
@@ -13,7 +15,6 @@
*/ */
LinearSpec::LinearSpec() LinearSpec::LinearSpec()
: :
fCountColumns(0),
fLpPresolved(NULL), fLpPresolved(NULL),
fOptimization(MINIMIZE), fOptimization(MINIMIZE),
fObjFunction(new SummandList()), fObjFunction(new SummandList()),
@@ -40,8 +41,9 @@ LinearSpec::~LinearSpec()
delete (Constraint*)fConstraints.ItemAt(i); delete (Constraint*)fConstraints.ItemAt(i);
for (int32 i = 0; i < fObjFunction->CountItems(); i++) for (int32 i = 0; i < fObjFunction->CountItems(); i++)
delete (Summand*)fObjFunction->ItemAt(i); delete (Summand*)fObjFunction->ItemAt(i);
for (int32 i = 0; i < fVariables.CountItems(); i++) while (fVariables.CountItems() > 0)
delete (Variable*)fVariables.ItemAt(i); RemoveVariable(fVariables.ItemAt(0));
delete_lp(fLP); delete_lp(fLP);
delete fObjFunction; delete fObjFunction;
@@ -56,7 +58,75 @@ LinearSpec::~LinearSpec()
Variable* Variable*
LinearSpec::AddVariable() LinearSpec::AddVariable()
{ {
return new Variable(this); Variable* variable = new Variable(this);
if (!variable)
return NULL;
if (!AddVariable(variable)) {
delete variable;
return NULL;
}
return variable;
}
bool
LinearSpec::AddVariable(Variable* variable)
{
double d = 0;
int i = 0;
if (!fVariables.AddItem(variable))
return false;
if (add_columnex(fLP, 0, &d, &i) == 0) {
fVariables.RemoveItem(variable);
return false;
}
if (!SetRange(variable, -20000, 20000)) {
RemoveVariable(variable, false);
return false;
}
variable->fIsValid = true;
return true;
}
bool
LinearSpec::RemoveVariable(Variable* variable, bool deleteVariable)
{
int32 index = IndexOf(variable);
if (index < 0)
return false;
if (!del_column(fLP, index))
return false;
fVariables.RemoveItemAt(index - 1);
variable->Invalidate();
if (deleteVariable)
delete variable;
return true;
}
int32
LinearSpec::IndexOf(const Variable* variable) const
{
int32 i = fVariables.IndexOf(variable);
if (i == -1) {
printf("Variable 0x%p not part of fLS->Variables().\n", variable);
return -1;
}
return i + 1;
}
bool
LinearSpec::SetRange(Variable* variable, double min, double max)
{
return set_bounds(fLP, IndexOf(variable), min, max);
} }
@@ -396,7 +466,7 @@ LinearSpec::UpdateObjectiveFunction()
} }
if (!set_obj_fnex(fLP, size, &coeffs[0], &varIndexes[0])) if (!set_obj_fnex(fLP, size, &coeffs[0], &varIndexes[0]))
printf("Error in set_obj_fnex."); printf("Error in set_obj_fnex.\n");
RemovePresolved(); RemovePresolved();
} }
@@ -433,8 +503,8 @@ LinearSpec::Presolve()
if (fLpPresolved == NULL) { if (fLpPresolved == NULL) {
fLpPresolved = copy_lp(fLP); fLpPresolved = copy_lp(fLP);
set_presolve(fLpPresolved, PRESOLVE_ROWS | PRESOLVE_COLS | PRESOLVE_LINDEP, set_presolve(fLpPresolved, PRESOLVE_ROWS | PRESOLVE_COLS
get_presolveloops(fLpPresolved)); | PRESOLVE_LINDEP, get_presolveloops(fLpPresolved));
} }
fResult = (ResultType)solve(fLpPresolved); fResult = (ResultType)solve(fLpPresolved);
@@ -478,7 +548,7 @@ LinearSpec::Solve()
int32 size = fVariables.CountItems(); int32 size = fVariables.CountItems();
double x[size]; double x[size];
if (!get_variables(fLP, &x[0])) if (!get_variables(fLP, &x[0]))
printf("Error in get_variables."); printf("Error in get_variables.\n");
int32 i = 0; int32 i = 0;
while (i < size) { while (i < size) {
@@ -508,18 +578,6 @@ LinearSpec::Save(const char* fileName)
} }
/**
* Gets the number of columns.
*
* @return the number of columns
*/
int32
LinearSpec::CountColumns() const
{
return fCountColumns;
}
/** /**
* Gets the current optimization. * Gets the current optimization.
* The default is minimization. * The default is minimization.
@@ -550,27 +608,15 @@ LinearSpec::SetOptimization(OptimizationType value)
} }
/**
* Gets the the variables.
*
* @return the variables
*/
VariableList*
LinearSpec::Variables() const
{
return const_cast<VariableList*>(&fVariables);
}
/** /**
* Gets the constraints. * Gets the constraints.
* *
* @return the constraints * @return the constraints
*/ */
ConstraintList* const ConstraintList&
LinearSpec::Constraints() const LinearSpec::Constraints() const
{ {
return const_cast<ConstraintList*>(&fConstraints); return fConstraints;
} }
+16 -29
View File
@@ -1,18 +1,19 @@
/* /*
* Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
* Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
* Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de>
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include "Variable.h"
#include "Constraint.h"
#include "LinearSpec.h"
#include "OperatorType.h"
#include "lp_lib.h" #include "Variable.h"
#include <float.h> // for DBL_MAX #include <float.h> // for DBL_MAX
#include <File.h>
#include "Constraint.h"
#include "LinearSpec.h"
// Toggle debug output // Toggle debug output
//#define DEBUG_VARIABLE //#define DEBUG_VARIABLE
@@ -32,12 +33,7 @@
int32 int32
Variable::Index() const Variable::Index() const
{ {
int32 i = fLS->Variables()->IndexOf(this); return fLS->IndexOf(this);
if (i == -1) {
printf("Variable not part of fLS->Variables().");
return -1;
}
return i + 1;
} }
@@ -139,7 +135,7 @@ Variable::SetRange(double min, double max)
fMin = min; fMin = min;
fMax = max; fMax = max;
set_bounds(fLS->fLP, this->Index(), fMin, fMax); fLS->SetRange(this, fMin, fMax);
} }
@@ -279,20 +275,20 @@ Variable::IsValid()
void void
Variable::Invalidate() Variable::Invalidate()
{ {
STRACE(("Variable::Invalidate() on %s\n", ToString())); STRACE(("Variable::Invalidate() on %s\n", BString(*this).String()));
if (!fIsValid) if (!fIsValid)
return; return;
fIsValid = false; fIsValid = false;
del_column(fLS->fLP, Index());
fLS->Variables()->RemoveItem(this); fLS->RemoveVariable(this, false);
// invalidate all constraints that use this variable // invalidate all constraints that use this variable
ConstraintList markedForInvalidation; ConstraintList markedForInvalidation;
ConstraintList* constraints = fLS->Constraints(); const ConstraintList& constraints = fLS->Constraints();
for (int i = 0; i < constraints->CountItems(); i++) { for (int i = 0; i < constraints.CountItems(); i++) {
Constraint* constraint = constraints->ItemAt(i); Constraint* constraint = constraints.ItemAt(i);
if (!constraint->IsValid()) if (!constraint->IsValid())
continue; continue;
@@ -321,18 +317,9 @@ Variable::Variable(LinearSpec* ls)
fMin(0), fMin(0),
fMax(DBL_MAX), fMax(DBL_MAX),
fLabel(NULL), fLabel(NULL),
fIsValid(true) fIsValid(false)
{ {
fLS->Variables()->AddItem(this);
if (fLS->Variables()->CountItems() > fLS->CountColumns()) {
double d = 0;
int i = 0;
if (!add_columnex(fLS->fLP, 0, &d, &i))
printf("Error in add_columnex.");
}
SetRange(-20000, 20000);
} }
@@ -342,6 +329,6 @@ Variable::Variable(LinearSpec* ls)
*/ */
Variable::~Variable() Variable::~Variable()
{ {
Invalidate(); fLS->RemoveVariable(this, false);
} }