From 5bced18eab0b1318f3293c1c5c5b1a9d52fa3770 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Mon, 10 Mar 2008 21:43:32 +0000 Subject: [PATCH] ALM/linprog patch by Christof Lutteroth: * Got rid of class ObjFunctionSummand. Both the constraint summands and the objective function summands are now stored using class Summand. * Some method names are more BeOS compliant now: SetX instead of ChangeX. * linprog test code now uses new AddConstraint methods. * CalculateMinSize and CalculateMaxSize did not free the memory they allocated. * Removed inappropriate setter and getter methods. * Memory allocated in class Constraint is freed now. * Other small changes. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24351 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/libs/alm/Area.h | 25 +- headers/libs/alm/BALMLayout.h | 40 ++- headers/libs/linprog/Constraint.h | 31 +-- headers/libs/linprog/LinearSpec.h | 35 ++- headers/libs/linprog/ObjFunctionSummand.h | 43 --- headers/libs/linprog/PenaltyFunction.h | 7 +- headers/libs/linprog/Summand.h | 1 + src/libs/alm/Area.cpp | 66 ++--- src/libs/alm/BALMLayout.cpp | 83 ++---- src/libs/linprog/Constraint.cpp | 220 ++++++++------- src/libs/linprog/Jamfile | 1 - src/libs/linprog/LinearSpec.cpp | 309 +++++++--------------- src/libs/linprog/ObjFunctionSummand.cpp | 57 ---- src/libs/linprog/PenaltyFunction.cpp | 63 +---- src/libs/linprog/Variable.cpp | 13 +- src/tests/libs/linprog/Program.cpp | 32 +-- 16 files changed, 361 insertions(+), 665 deletions(-) delete mode 100644 headers/libs/linprog/ObjFunctionSummand.h delete mode 100644 src/libs/linprog/ObjFunctionSummand.cpp diff --git a/headers/libs/alm/Area.h b/headers/libs/alm/Area.h index 0fb81966c6..10bda8df46 100644 --- a/headers/libs/alm/Area.h +++ b/headers/libs/alm/Area.h @@ -32,6 +32,7 @@ class Area { public: bool AutoPrefContentSize() const; void SetAutoPrefContentSize(bool value); + XTab* Left() const; void SetLeft(XTab* left); XTab* Right() const; @@ -40,10 +41,12 @@ public: void SetTop(YTab* top); YTab* Bottom() const; void SetBottom(YTab* bottom); + Row* GetRow() const; void SetRow(Row* row); Column* GetColumn() const; void SetColumn(Column* column); + BView* Content() const; void SetContent(BView* content); XTab* ContentLeft() const; @@ -76,6 +79,7 @@ public: void SetBottomInset(int32 bottom); void SetDefaultPrefContentSize(); //~ string ToString(); + Constraint* HasSameWidthAs(Area* area); Constraint* HasSameHeightAs(Area* area); BList* HasSameSizetAs(Area* area); @@ -92,18 +96,18 @@ protected: void DoLayout(); private: - void InitChildArea(); - void UpdateHorizontal(); - void UpdateVertical(); - void Init(BALMLayout* ls, XTab* left, YTab* top, - XTab* right, YTab* bottom, - BView* content, - BSize minContentSize); + void InitChildArea(); + void UpdateHorizontal(); + void UpdateVertical(); + void Init(BALMLayout* ls, XTab* left, YTab* top, + XTab* right, YTab* bottom, + BView* content, + BSize minContentSize); public: - static BSize kMaxSize; - static BSize kMinSize; - static BSize kUndefinedSize; + static BSize kMaxSize; + static BSize kMinSize; + static BSize kUndefinedSize; protected: BView* fContent; @@ -152,3 +156,4 @@ public: using BALM::Area; #endif // AREA_H + diff --git a/headers/libs/alm/BALMLayout.h b/headers/libs/alm/BALMLayout.h index 87309ea8a8..40443ef6b4 100644 --- a/headers/libs/alm/BALMLayout.h +++ b/headers/libs/alm/BALMLayout.h @@ -33,7 +33,7 @@ class BALMLayout : public BLayout, public LinearSpec { public: BALMLayout(); - void SolveLayout(); + void SolveLayout(); XTab* AddXTab(); YTab* AddYTab(); @@ -41,6 +41,7 @@ public: Row* AddRow(YTab* top, YTab* bottom); Column* AddColumn(); Column* AddColumn(XTab* left, XTab* right); + Area* AddArea(XTab* left, YTab* top, XTab* right, YTab* bottom, BView* content, BSize minContentSize); Area* AddArea(Row* row, Column* column, BView* content, @@ -50,40 +51,37 @@ public: Area* AddArea(Row* row, Column* column, BView* content); Area* AreaOf(BView* control); BList* Areas() const; - void SetAreas(BList* areas); - XTab* Left() const; - void SetLeft(XTab* left); - XTab* Right() const; - void SetRight(XTab* right); - YTab* Top() const; - void SetTop(YTab* top); - YTab* Bottom() const; - void SetBottom(YTab* bottom); - - void RecoverLayout(BView* parent); - LayoutStyleType LayoutStyle() const; - void SetLayoutStyle(LayoutStyleType style); - BLayoutItem* AddView(BView* child); - BLayoutItem* AddView(int32 index, BView* child); + XTab* Left() const; + XTab* Right() const; + YTab* Top() const; + YTab* Bottom() const; + + void RecoverLayout(BView* parent); + + LayoutStyleType LayoutStyle() const; + void SetLayoutStyle(LayoutStyleType style); + + BLayoutItem* AddView(BView* child); + BLayoutItem* AddView(int32 index, BView* child); bool AddItem(BLayoutItem* item); bool AddItem(int32 index, BLayoutItem* item); bool RemoveView(BView* child); bool RemoveItem(BLayoutItem* item); - BLayoutItem* RemoveItem(int32 index); + BLayoutItem* RemoveItem(int32 index); BSize MinSize(); BSize MaxSize(); BSize PreferredSize(); BAlignment Alignment(); bool HasHeightForWidth(); - void GetHeightForWidth(float width, float* min, + void GetHeightForWidth(float width, float* min, float* max, float* preferred); - void InvalidateLayout(); - void LayoutView(); + void InvalidateLayout(); + void LayoutView(); char* PerformancePath() const; - void SetPerformancePath(char* path); + void SetPerformancePath(char* path); private: BSize CalculateMinSize(); diff --git a/headers/libs/linprog/Constraint.h b/headers/libs/linprog/Constraint.h index 8d980dd6e9..6db27f1ff4 100644 --- a/headers/libs/linprog/Constraint.h +++ b/headers/libs/linprog/Constraint.h @@ -9,7 +9,7 @@ #include "OperatorType.h" #include "Variable.h" -#include "ObjFunctionSummand.h" +#include "Summand.h" #include #include @@ -29,18 +29,21 @@ class Constraint { public: int32 Index(); - BList* Summands(); - void ChangeLeftSide(BList* summands); - void ChangeLeftSide(double coeff1, Variable* var1); - void ChangeLeftSide(double coeff1, Variable* var1, + + BList* LeftSide(); + void SetLeftSide(BList* summands); + void UpdateLeftSide(); + void SetLeftSide(double coeff1, Variable* var1); + void SetLeftSide(double coeff1, Variable* var1, double coeff2, Variable* var2); - void ChangeLeftSide(double coeff1, Variable* var1, + void SetLeftSide(double coeff1, Variable* var1, double coeff2, Variable* var2, double coeff3, Variable* var3); - void ChangeLeftSide(double coeff1, Variable* var1, + void SetLeftSide(double coeff1, Variable* var1, double coeff2, Variable* var2, double coeff3, Variable* var3, double coeff4, Variable* var4); + OperatorType Op(); void SetOp(OperatorType value); double RightSide(); @@ -49,6 +52,7 @@ public: void SetPenaltyNeg(double value); double PenaltyPos(); void SetPenaltyPos(double value); + Variable* DNeg() const; Variable* DPos() const; @@ -56,21 +60,17 @@ public: ~Constraint(); protected: - Constraint(LinearSpec* ls, BList* summands, + Constraint(LinearSpec* ls, BList* summands, OperatorType op, double rightSide, double penaltyNeg, double penaltyPos); private: LinearSpec* fLS; - BList* fSummands; + BList* fLeftSide; OperatorType fOp; double fRightSide; - Variable* fDNeg; - Variable* fDPos; - ObjFunctionSummand* fDNegSummand; - ObjFunctionSummand* fDPosSummand; - - void _UpdateLeftSide(); + Summand* fDNegObjSummand; + Summand* fDPosObjSummand; public: friend class LinearSpec; @@ -82,3 +82,4 @@ public: using LinearProgramming::Constraint; #endif // CONSTRAINT_H + diff --git a/headers/libs/linprog/LinearSpec.h b/headers/libs/linprog/LinearSpec.h index 7dbc7cd578..4a7d4eaf60 100644 --- a/headers/libs/linprog/LinearSpec.h +++ b/headers/libs/linprog/LinearSpec.h @@ -7,6 +7,10 @@ #ifndef LINEAR_SPEC_H #define LINEAR_SPEC_H +#include "Variable.h" +#include "Constraint.h" +#include "Summand.h" +#include "PenaltyFunction.h" #include "OperatorType.h" #include "ResultType.h" #include "OptimizationType.h" @@ -34,9 +38,7 @@ class LinearSpec { public: LinearSpec(); ~LinearSpec(); - void UpdateObjFunction(); - void SetObjFunction(BList* summands); - ObjFunctionSummand* AddObjFunctionSummand(double coeff, Variable* var); + Variable* AddVariable(); Constraint* AddConstraint(BList* summands, @@ -79,47 +81,42 @@ public: double penaltyNeg, double penaltyPos); PenaltyFunction* AddPenaltyFunction(Variable* var, BList* xs, BList* gs); - void RemovePresolved(); + + BList* ObjFunction(); + void SetObjFunction(BList* summands); + void UpdateObjFunction(); + ResultType Presolve(); + void RemovePresolved(); ResultType Solve(); void Save(char* fname); - int32 Columns() const; - void SetColumns(int32 value); + int32 CountColumns() const; OptimizationType Optimization() const; void SetOptimization(OptimizationType value); - lprec* LP() const; - void SetLP(lprec* value); - BList* ObjFunctionSummands() const; - void SetObjFunctionSummands(BList* value); BList* Variables() const; - void SetVariables(BList* value); BList* Constraints() const; - void SetConstraints(BList* value); ResultType Result() const; - void SetResult(ResultType value); double ObjectiveValue() const; - void SetObjectiveValue(double value); double SolvingTime() const; - void SetSolvingTime(double value); protected: - int32 fColumns; + int32 fCountColumns; private: lprec* fLpPresolved; OptimizationType fOptimization; lprec* fLP; - BList* fObjFunctionSummands; + BList* fObjFunction; BList* fVariables; BList* fConstraints; ResultType fResult; double fObjectiveValue; - double fSolvingTime; // = Double.Nan + double fSolvingTime; public: - friend class ObjFunctionSummand; friend class Constraint; + friend class Variable; }; diff --git a/headers/libs/linprog/ObjFunctionSummand.h b/headers/libs/linprog/ObjFunctionSummand.h deleted file mode 100644 index 492defacf6..0000000000 --- a/headers/libs/linprog/ObjFunctionSummand.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz - * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz - * Distributed under the terms of the MIT License. - */ - -#ifndef OBJ_FUNCTION_SUMMAND_H -#define OBJ_FUNCTION_SUMMAND_H - -#include "Summand.h" - - -namespace LinearProgramming { - -class LinearSpec; -class Variable; - -/** - * A summand of the objective function. - */ -class ObjFunctionSummand : public Summand { - -public: - void SetCoeff(double coeff); - void SetVar(Variable* var); - ~ObjFunctionSummand(); - -protected: - ObjFunctionSummand(LinearSpec* ls, double coeff, Variable* var); - -private: - LinearSpec* fLS; - -public: - friend class LinearSpec; - -}; - -} // namespace LinearProgramming - -using LinearProgramming::ObjFunctionSummand; - -#endif // OBJ_FUNCTION_SUMMAND_H diff --git a/headers/libs/linprog/PenaltyFunction.h b/headers/libs/linprog/PenaltyFunction.h index a296d4dd62..c18d6efa90 100644 --- a/headers/libs/linprog/PenaltyFunction.h +++ b/headers/libs/linprog/PenaltyFunction.h @@ -26,12 +26,12 @@ protected: public: ~PenaltyFunction(); const Variable* Var() const; - const BList* Xs() const; - const BList* Gs() const; + const BList* Xs() const; + const BList* Gs() const; private: LinearSpec* fLS; - Variable* fVar; + Variable* fVar; BList* fXs; // double BList* fGs; // double BList* fConstraints; @@ -47,3 +47,4 @@ public: using LinearProgramming::PenaltyFunction; #endif // PENALTY_FUNCTION_H + diff --git a/headers/libs/linprog/Summand.h b/headers/libs/linprog/Summand.h index f844683c0a..2f25a36a22 100644 --- a/headers/libs/linprog/Summand.h +++ b/headers/libs/linprog/Summand.h @@ -37,3 +37,4 @@ private: using LinearProgramming::Summand; #endif // OBJ_FUNCTION_SUMMAND_H + diff --git a/src/libs/alm/Area.cpp b/src/libs/alm/Area.cpp index 53e728641c..4426498e0f 100644 --- a/src/libs/alm/Area.cpp +++ b/src/libs/alm/Area.cpp @@ -77,10 +77,10 @@ Area::SetLeft(XTab* left) fColumn = NULL; if (fChildArea == NULL) { - fMinContentWidth->ChangeLeftSide(-1.0, fLeft, 1.0, fRight); + fMinContentWidth->SetLeftSide(-1.0, fLeft, 1.0, fRight); if (fMaxContentWidth != NULL) - fMaxContentWidth->ChangeLeftSide(-1.0, fLeft, 1.0, fRight); + fMaxContentWidth->SetLeftSide(-1.0, fLeft, 1.0, fRight); } else UpdateHorizontal(); fLS->InvalidateLayout(); @@ -112,10 +112,10 @@ Area::SetRight(XTab* right) fColumn = NULL; if (fChildArea == NULL) { - fMinContentWidth->ChangeLeftSide(-1.0, fLeft, 1.0, fRight); + fMinContentWidth->SetLeftSide(-1.0, fLeft, 1.0, fRight); if (fMaxContentWidth != NULL) - fMaxContentWidth->ChangeLeftSide(-1.0, fLeft, 1.0, fRight); + fMaxContentWidth->SetLeftSide(-1.0, fLeft, 1.0, fRight); } else UpdateHorizontal(); fLS->InvalidateLayout(); @@ -143,10 +143,10 @@ Area::SetTop(YTab* top) fRow = NULL; if (fChildArea == NULL) { - fMinContentHeight->ChangeLeftSide(-1.0, fTop, 1.0, fBottom); + fMinContentHeight->SetLeftSide(-1.0, fTop, 1.0, fBottom); if (fMaxContentHeight != NULL) - fMaxContentHeight->ChangeLeftSide(-1.0, fTop, 1.0, fBottom); + fMaxContentHeight->SetLeftSide(-1.0, fTop, 1.0, fBottom); } else UpdateVertical(); fLS->InvalidateLayout(); @@ -174,10 +174,10 @@ Area::SetBottom(YTab* bottom) fRow = NULL; if (fChildArea == NULL) { - fMinContentHeight->ChangeLeftSide(-1.0, fTop, 1.0, fBottom); + fMinContentHeight->SetLeftSide(-1.0, fTop, 1.0, fBottom); if (fMaxContentHeight != NULL) - fMaxContentHeight->ChangeLeftSide(-1.0, fTop, 1.0, fBottom); + fMaxContentHeight->SetLeftSide(-1.0, fTop, 1.0, fBottom); } else UpdateVertical(); fLS->InvalidateLayout(); @@ -471,7 +471,7 @@ Area::SetContentAspectRatio(double ratio) OperatorType(EQ), 0.0); fConstraints->AddItem(fContentAspectRatioC); } else { - fContentAspectRatioC->ChangeLeftSide( + fContentAspectRatioC->SetLeftSide( -1.0, fLeft, 1.0, fRight, ratio, fTop, -ratio, fBottom); } } else @@ -855,10 +855,10 @@ Area::InitChildArea() fChildArea->fMaxContentSize = fMaxContentSize; fChildArea->fMaxContentWidth = fMaxContentWidth; - fMaxContentWidth->ChangeLeftSide(-1.0, fChildArea->Left(), 1.0, fChildArea->Right()); + fMaxContentWidth->SetLeftSide(-1.0, fChildArea->Left(), 1.0, fChildArea->Right()); fChildArea->fMaxContentHeight = fMaxContentHeight; - fMaxContentHeight->ChangeLeftSide(-1.0, fChildArea->Top(), 1.0, fChildArea->Bottom()); + fMaxContentHeight->SetLeftSide(-1.0, fChildArea->Top(), 1.0, fChildArea->Bottom()); } // if there are preferred content size constraints on this area, @@ -870,10 +870,10 @@ Area::InitChildArea() fChildArea->fExpandRigidity = fExpandRigidity; fChildArea->fPrefContentWidth = fPrefContentWidth; - fPrefContentWidth->ChangeLeftSide(-1.0, fChildArea->Left(), 1.0, fChildArea->Right()); + fPrefContentWidth->SetLeftSide(-1.0, fChildArea->Left(), 1.0, fChildArea->Right()); fChildArea->fPrefContentHeight = fPrefContentHeight; - fPrefContentHeight->ChangeLeftSide(-1.0, fChildArea->Top(), 1.0, fChildArea->Bottom()); + fPrefContentHeight->SetLeftSide(-1.0, fChildArea->Top(), 1.0, fChildArea->Bottom()); } } @@ -891,43 +891,43 @@ Area::UpdateHorizontal() // change the constraints leftConstraint and rightConstraint so that the horizontal // alignment and insets of the childArea within this area are as specified by the user if (fAlignment.Horizontal() == B_ALIGN_LEFT) { - fLeftConstraint->ChangeLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); + fLeftConstraint->SetLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); fLeftConstraint->SetOp(OperatorType(EQ)); fLeftConstraint->SetRightSide(fLeftInset); - fRightConstraint->ChangeLeftSide(-1.0, fChildArea->Right(), 1.0, fRight); + fRightConstraint->SetLeftSide(-1.0, fChildArea->Right(), 1.0, fRight); fRightConstraint->SetOp(OperatorType(GE)); fRightConstraint->SetRightSide(fRightInset); } else if (fAlignment.Horizontal() == B_ALIGN_RIGHT) { - fLeftConstraint->ChangeLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); + fLeftConstraint->SetLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); fLeftConstraint->SetOp(OperatorType(GE)); fLeftConstraint->SetRightSide(fLeftInset); - fRightConstraint->ChangeLeftSide(-1.0, fChildArea->Right(), 1.0, fRight); + fRightConstraint->SetLeftSide(-1.0, fChildArea->Right(), 1.0, fRight); fRightConstraint->SetOp(OperatorType(EQ)); fRightConstraint->SetRightSide(fRightInset); } else if (fAlignment.Horizontal() == B_ALIGN_HORIZONTAL_CENTER) { - fLeftConstraint->ChangeLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); + fLeftConstraint->SetLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); fLeftConstraint->SetOp(OperatorType(GE)); fLeftConstraint->SetRightSide(max(fLeftInset, fRightInset)); - fRightConstraint->ChangeLeftSide(-1.0, fLeft, 1.0, fChildArea->Left(), 1.0, fChildArea->Right(), -1.0, fRight); + fRightConstraint->SetLeftSide(-1.0, fLeft, 1.0, fChildArea->Left(), 1.0, fChildArea->Right(), -1.0, fRight); fRightConstraint->SetOp(OperatorType(EQ)); fRightConstraint->SetRightSide(0); } else if (fAlignment.Horizontal() == B_ALIGN_USE_FULL_WIDTH) { - fLeftConstraint->ChangeLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); + fLeftConstraint->SetLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); fLeftConstraint->SetOp(OperatorType(EQ)); fLeftConstraint->SetRightSide(fLeftInset); - fRightConstraint->ChangeLeftSide(-1.0, fChildArea->Right(), 1.0, fRight); + fRightConstraint->SetLeftSide(-1.0, fChildArea->Right(), 1.0, fRight); fRightConstraint->SetOp(OperatorType(EQ)); fRightConstraint->SetRightSide(fRightInset); } else if (fAlignment.Horizontal() == B_ALIGN_HORIZONTAL_UNSET) { - fLeftConstraint->ChangeLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); + fLeftConstraint->SetLeftSide(-1.0, fLeft, 1.0, fChildArea->Left()); fLeftConstraint->SetOp(OperatorType(GE)); fLeftConstraint->SetRightSide(fLeftInset); - fRightConstraint->ChangeLeftSide(-1.0, fChildArea->Right(), 1.0, fRight); + fRightConstraint->SetLeftSide(-1.0, fChildArea->Right(), 1.0, fRight); fRightConstraint->SetOp(OperatorType(GE)); fRightConstraint->SetRightSide(fRightInset); } @@ -945,43 +945,43 @@ void Area::UpdateVertical() { // change the constraints topConstraint and bottomConstraint so that the vertical // alignment and insets of the childArea within this area are as specified by the user if (fAlignment.Vertical() == B_ALIGN_TOP) { - fTopConstraint->ChangeLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); + fTopConstraint->SetLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); fTopConstraint->SetOp(OperatorType(EQ)); fTopConstraint->SetRightSide(fTopInset); - fBottomConstraint->ChangeLeftSide(-1.0, fChildArea->Bottom(), 1.0, fBottom); + fBottomConstraint->SetLeftSide(-1.0, fChildArea->Bottom(), 1.0, fBottom); fBottomConstraint->SetOp(OperatorType(GE)); fBottomConstraint->SetRightSide(fBottomInset); } else if (fAlignment.Vertical() == B_ALIGN_BOTTOM) { - fTopConstraint->ChangeLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); + fTopConstraint->SetLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); fTopConstraint->SetOp(OperatorType(GE)); fTopConstraint->SetRightSide(fTopInset); - fBottomConstraint->ChangeLeftSide(-1.0, fChildArea->Bottom(), 1.0, fBottom); + fBottomConstraint->SetLeftSide(-1.0, fChildArea->Bottom(), 1.0, fBottom); fBottomConstraint->SetOp(OperatorType(EQ)); fBottomConstraint->SetRightSide(fBottomInset); } else if (fAlignment.Vertical() == B_ALIGN_VERTICAL_CENTER) { - fTopConstraint->ChangeLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); + fTopConstraint->SetLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); fTopConstraint->SetOp(OperatorType(GE)); fTopConstraint->SetRightSide(max(fTopInset, fBottomInset)); - fBottomConstraint->ChangeLeftSide(-1.0, fTop, 1.0, fChildArea->Top(), 1.0, fChildArea->Bottom(), -1.0, fBottom); + fBottomConstraint->SetLeftSide(-1.0, fTop, 1.0, fChildArea->Top(), 1.0, fChildArea->Bottom(), -1.0, fBottom); fBottomConstraint->SetOp(OperatorType(EQ)); fBottomConstraint->SetRightSide(0); } else if (fAlignment.Vertical() == B_ALIGN_USE_FULL_HEIGHT) { - fTopConstraint->ChangeLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); + fTopConstraint->SetLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); fTopConstraint->SetOp(OperatorType(EQ)); fTopConstraint->SetRightSide(fTopInset); - fBottomConstraint->ChangeLeftSide(-1.0, fChildArea->Bottom(), 1.0, fBottom); + fBottomConstraint->SetLeftSide(-1.0, fChildArea->Bottom(), 1.0, fBottom); fBottomConstraint->SetOp(OperatorType(EQ)); fBottomConstraint->SetRightSide(fBottomInset); } else if (fAlignment.Vertical() == B_ALIGN_VERTICAL_UNSET) { - fTopConstraint->ChangeLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); + fTopConstraint->SetLeftSide(-1.0, fTop, 1.0, fChildArea->Top()); fTopConstraint->SetOp(OperatorType(GE)); fTopConstraint->SetRightSide(fTopInset); - fBottomConstraint->ChangeLeftSide(-1.0, fChildArea->Bottom(), 1.0, fBottom); + fBottomConstraint->SetLeftSide(-1.0, fChildArea->Bottom(), 1.0, fBottom); fBottomConstraint->SetOp(OperatorType(GE)); fBottomConstraint->SetRightSide(fBottomInset); } diff --git a/src/libs/alm/BALMLayout.cpp b/src/libs/alm/BALMLayout.cpp index 05cda1216d..dd78ea337e 100644 --- a/src/libs/alm/BALMLayout.cpp +++ b/src/libs/alm/BALMLayout.cpp @@ -289,18 +289,6 @@ BALMLayout::Areas() const } -/** - * Sets the ares. - * - * @param areas the areas - */ -void -BALMLayout::SetAreas(BList* areas) -{ - fAreas = areas; -} - - /** * Gets the left variable. */ @@ -311,16 +299,6 @@ BALMLayout::Left() const } -/** - * Sets the left variable. - */ -void -BALMLayout::SetLeft(XTab* left) -{ - fLeft = left; -} - - /** * Gets the right variable. */ @@ -331,16 +309,6 @@ BALMLayout::Right() const } -/** - * Sets the right variable. - */ -void -BALMLayout::SetRight(XTab* right) -{ - fRight = right; -} - - /** * Gets the top variable. */ @@ -351,16 +319,6 @@ BALMLayout::Top() const } -/** - * Sets the top variable. - */ -void -BALMLayout::SetTop(YTab* top) -{ - fTop = top; -} - - /** * Gets the bottom variable. */ @@ -371,16 +329,6 @@ BALMLayout::Bottom() const } -/** - * Sets the bottom variable. - */ -void -BALMLayout::SetBottom(YTab* bottom) -{ - fBottom = bottom; -} - - /** * Reverse engineers a GUI and recovers an ALM specification. * @param parent the parent container of the GUI @@ -641,14 +589,17 @@ BALMLayout::SetPerformancePath(char* path) BSize BALMLayout::CalculateMinSize() { - printf("CalculateMinSize"); - BList* buf = ObjFunctionSummands(); - SetObjFunctionSummands(new BList(2)); - AddObjFunctionSummand(1.0, fRight); - AddObjFunctionSummand(1.0, fBottom); + BList* oldObjFunction = ObjFunction(); + BList* newObjFunction = new BList(2); + newObjFunction->AddItem(new Summand(1.0, fRight)); + newObjFunction->AddItem(new Summand(1.0, fBottom)); + SetObjFunction(newObjFunction); SolveLayout(); - SetObjFunctionSummands(buf); + SetObjFunction(oldObjFunction); UpdateObjFunction(); + delete (Summand*)newObjFunction->ItemAt(0); + delete (Summand*)newObjFunction->ItemAt(1); + delete newObjFunction; if (Result() == UNBOUNDED) return Area::kMinSize; @@ -667,14 +618,17 @@ BALMLayout::CalculateMinSize() BSize BALMLayout::CalculateMaxSize() { - printf("CalculateMaxSize"); - BList* buf = ObjFunctionSummands(); - SetObjFunctionSummands(new BList(2)); - AddObjFunctionSummand(-1.0, fRight); - AddObjFunctionSummand(-1.0, fBottom); + BList* oldObjFunction = ObjFunction(); + BList* newObjFunction = new BList(2); + newObjFunction->AddItem(new Summand(-1.0, fRight)); + newObjFunction->AddItem(new Summand(-1.0, fBottom)); + SetObjFunction(newObjFunction); SolveLayout(); - SetObjFunctionSummands(buf); + SetObjFunction(oldObjFunction); UpdateObjFunction(); + delete (Summand*)newObjFunction->ItemAt(0); + delete (Summand*)newObjFunction->ItemAt(1); + delete newObjFunction; if (Result() == UNBOUNDED) return Area::kMaxSize; @@ -693,7 +647,6 @@ BALMLayout::CalculateMaxSize() BSize BALMLayout::CalculatePreferredSize() { - printf("CalculatePreferredSize"); SolveLayout(); if (Result() != OPTIMAL) { Save("failed-layout.txt"); diff --git a/src/libs/linprog/Constraint.cpp b/src/libs/linprog/Constraint.cpp index 85625e4111..c4190216e4 100644 --- a/src/libs/linprog/Constraint.cpp +++ b/src/libs/linprog/Constraint.cpp @@ -27,107 +27,116 @@ Constraint::Index() /** - * Gets the summands of the constraint. + * Gets the left side of the constraint. * - * @return pointer to a BList containing the summands of the constraint + * @return pointer to a BList containing the summands on the left side of the constraint */ BList* -Constraint::Summands() +Constraint::LeftSide() { - return fSummands; -} - - -void -Constraint::_UpdateLeftSide() -{ - double coeffs[fSummands->CountItems() + 2]; - int varIndexes[fSummands->CountItems() + 2]; - int32 i; - for (i = 0; i < fSummands->CountItems(); i++) { - Summand* s = (Summand*)fSummands->ItemAt(i); - coeffs[i] = s->Coeff(); - varIndexes[i] = s->Var()->Index(); - } - - if (fDNegSummand != NULL && fOp != OperatorType(LE)) { - varIndexes[i] = fDNegSummand->Var()->Index(); - coeffs[i] = 1.0; - i++; - } - - if (fDPosSummand != NULL && fOp != OperatorType(GE)) { - varIndexes[i] = fDPosSummand->Var()->Index(); - coeffs[i] = -1.0; - i++; - } - - if (!set_rowex(fLS->LP(), this->Index(), i, &coeffs[0], &varIndexes[0])) - printf("Error in set_rowex."); - - fLS->RemovePresolved(); + return fLeftSide; } /** - * Changes the left side of the constraint, i.e. its coefficients and variables. - * There must be exactly one coefficient for each variable. + * Sets the summands on the left side of the constraint. + * The old summands are NOT deleted. * * @param summands a BList containing the Summand objects that make up the new left side */ void -Constraint::ChangeLeftSide(BList* summands) +Constraint::SetLeftSide(BList* summands) { - fSummands = summands; - _UpdateLeftSide(); + fLeftSide = summands; + UpdateLeftSide(); } void -Constraint::ChangeLeftSide(double coeff1, Variable* var1) +Constraint::UpdateLeftSide() { - fSummands->MakeEmpty(); - fSummands->AddItem(new Summand(coeff1, var1)); - _UpdateLeftSide(); + double coeffs[fLeftSide->CountItems() + 2]; + int varIndexes[fLeftSide->CountItems() + 2]; + int32 i; + for (i = 0; i < fLeftSide->CountItems(); i++) { + Summand* s = (Summand*)fLeftSide->ItemAt(i); + coeffs[i] = s->Coeff(); + varIndexes[i] = s->Var()->Index(); + } + + if (fDNegObjSummand != NULL && fOp != OperatorType(LE)) { + varIndexes[i] = fDNegObjSummand->Var()->Index(); + coeffs[i] = 1.0; + i++; + } + + if (fDPosObjSummand != NULL && fOp != OperatorType(GE)) { + varIndexes[i] = fDPosObjSummand->Var()->Index(); + coeffs[i] = -1.0; + i++; + } + + if (!set_rowex(fLS->fLP, this->Index(), i, &coeffs[0], &varIndexes[0])) + printf("Error in set_rowex."); + + fLS->UpdateObjFunction(); + fLS->RemovePresolved(); } void -Constraint::ChangeLeftSide(double coeff1, Variable* var1, +Constraint::SetLeftSide(double coeff1, Variable* var1) +{ + for (int i=0; iCountItems(); i++) + delete (Summand*)fLeftSide->ItemAt(i); + fLeftSide->MakeEmpty(); + fLeftSide->AddItem(new Summand(coeff1, var1)); + UpdateLeftSide(); +} + + +void +Constraint::SetLeftSide(double coeff1, Variable* var1, double coeff2, Variable* var2) { - fSummands->MakeEmpty(); - fSummands->AddItem(new Summand(coeff1, var1)); - fSummands->AddItem(new Summand(coeff2, var2)); - _UpdateLeftSide(); + for (int i=0; iCountItems(); i++) + delete (Summand*)fLeftSide->ItemAt(i); + fLeftSide->MakeEmpty(); + fLeftSide->AddItem(new Summand(coeff1, var1)); + fLeftSide->AddItem(new Summand(coeff2, var2)); + UpdateLeftSide(); } void -Constraint::ChangeLeftSide(double coeff1, Variable* var1, +Constraint::SetLeftSide(double coeff1, Variable* var1, double coeff2, Variable* var2, double coeff3, Variable* var3) { - fSummands->MakeEmpty(); - fSummands->AddItem(new Summand(coeff1, var1)); - fSummands->AddItem(new Summand(coeff2, var2)); - fSummands->AddItem(new Summand(coeff3, var3)); - _UpdateLeftSide(); + for (int i=0; iCountItems(); i++) + delete (Summand*)fLeftSide->ItemAt(i); + fLeftSide->MakeEmpty(); + fLeftSide->AddItem(new Summand(coeff1, var1)); + fLeftSide->AddItem(new Summand(coeff2, var2)); + fLeftSide->AddItem(new Summand(coeff3, var3)); + UpdateLeftSide(); } void -Constraint::ChangeLeftSide(double coeff1, Variable* var1, +Constraint::SetLeftSide(double coeff1, Variable* var1, double coeff2, Variable* var2, double coeff3, Variable* var3, double coeff4, Variable* var4) { - fSummands->MakeEmpty(); - fSummands->AddItem(new Summand(coeff1, var1)); - fSummands->AddItem(new Summand(coeff2, var2)); - fSummands->AddItem(new Summand(coeff3, var3)); - fSummands->AddItem(new Summand(coeff4, var4)); - _UpdateLeftSide(); + for (int i=0; iCountItems(); i++) + delete (Summand*)fLeftSide->ItemAt(i); + fLeftSide->MakeEmpty(); + fLeftSide->AddItem(new Summand(coeff1, var1)); + fLeftSide->AddItem(new Summand(coeff2, var2)); + fLeftSide->AddItem(new Summand(coeff3, var3)); + fLeftSide->AddItem(new Summand(coeff4, var4)); + UpdateLeftSide(); } @@ -152,7 +161,7 @@ void Constraint::SetOp(OperatorType value) { fOp = value; - if (!set_constr_type(fLS->LP(), this->Index(), + if (!set_constr_type(fLS->fLP, this->Index(), ((fOp == OperatorType(EQ)) ? EQ : (fOp == OperatorType(GE)) ? GE : LE))) @@ -183,7 +192,7 @@ void Constraint::SetRightSide(double value) { fRightSide = value; - if (!set_rh(fLS->LP(), Index(), fRightSide)) + if (!set_rh(fLS->fLP, Index(), fRightSide)) printf("Error in set_rh."); fLS->RemovePresolved(); @@ -198,9 +207,9 @@ Constraint::SetRightSide(double value) double Constraint::PenaltyNeg() { - if (fDNegSummand == NULL) + if (fDNegObjSummand == NULL) return INFINITY; - return fDNegSummand->Coeff(); + return fDNegObjSummand->Coeff(); } @@ -213,16 +222,18 @@ Constraint::PenaltyNeg() void Constraint::SetPenaltyNeg(double value) { - if (fDNegSummand == NULL) { - fDNegSummand = fLS->AddObjFunctionSummand(value, new Variable(fLS)); - ChangeLeftSide(fSummands); + if (fDNegObjSummand == NULL) { + fDNegObjSummand = new Summand(value, new Variable(fLS)); + fLS->ObjFunction()->AddItem(fDNegObjSummand); + UpdateLeftSide(); fLS->UpdateObjFunction(); return; } - - if (value == fDNegSummand->Coeff()) + + if (value == fDNegObjSummand->Coeff()) return; - fDNegSummand->SetCoeff(value); + + fDNegObjSummand->SetCoeff(value); fLS->UpdateObjFunction(); } @@ -235,9 +246,9 @@ Constraint::SetPenaltyNeg(double value) double Constraint::PenaltyPos() { - if (fDPosSummand == NULL) + if (fDPosObjSummand == NULL) return INFINITY; - return fDPosSummand->Coeff(); + return fDPosObjSummand->Coeff(); } @@ -250,16 +261,18 @@ Constraint::PenaltyPos() void Constraint::SetPenaltyPos(double value) { - if (fDPosSummand == NULL) { - fDPosSummand = fLS->AddObjFunctionSummand(value, new Variable(fLS)); - ChangeLeftSide(fSummands); + if (fDPosObjSummand == NULL) { + fDPosObjSummand = new Summand(value, new Variable(fLS)); + fLS->ObjFunction()->AddItem(fDPosObjSummand); + UpdateLeftSide(); fLS->UpdateObjFunction(); return; } - if (value == fDPosSummand->Coeff()) + if (value == fDPosObjSummand->Coeff()) return; - fDPosSummand->SetCoeff(value); + + fDPosObjSummand->SetCoeff(value); fLS->UpdateObjFunction(); } @@ -272,9 +285,9 @@ Constraint::SetPenaltyPos(double value) Variable* Constraint::DNeg() const { - if (fDNegSummand == NULL) + if (fDNegObjSummand == NULL) return NULL; - return fDNegSummand->Var(); + return fDNegObjSummand->Var(); } @@ -286,9 +299,9 @@ Constraint::DNeg() const Variable* Constraint::DPos() const { - if (fDPosSummand == NULL) + if (fDPosObjSummand == NULL) return NULL; - return fDPosSummand->Var(); + return fDPosObjSummand->Var(); } @@ -299,7 +312,7 @@ Constraint::Constraint(LinearSpec* ls, BList* summands, OperatorType op, double rightSide, double penaltyNeg, double penaltyPos) { fLS = ls; - fSummands = summands; + fLeftSide = summands; fOp = op; fRightSide = rightSide; @@ -314,50 +327,57 @@ Constraint::Constraint(LinearSpec* ls, BList* summands, OperatorType op, if (penaltyNeg != INFINITY && fOp != OperatorType(LE)) { - fDNegSummand = ls->AddObjFunctionSummand(penaltyNeg, new Variable(ls)); - varIndexes[i] = fDNegSummand->Var()->Index(); + fDNegObjSummand = new Summand(penaltyNeg, new Variable(ls)); + ls->fObjFunction->AddItem(fDNegObjSummand); + varIndexes[i] = fDNegObjSummand->Var()->Index(); coeffs[i] = 1.0; i++; } else - fDNegSummand = NULL; + fDNegObjSummand = NULL; if (penaltyPos != INFINITY && fOp != OperatorType(GE)) { - fDPosSummand = ls->AddObjFunctionSummand(penaltyPos, new Variable(ls)); - varIndexes[i] = fDPosSummand->Var()->Index(); + fDPosObjSummand = new Summand(penaltyPos, new Variable(ls)); + ls->fObjFunction->AddItem(fDPosObjSummand); + varIndexes[i] = fDPosObjSummand->Var()->Index(); coeffs[i] = -1.0; i++; } else - fDPosSummand = NULL; + fDPosObjSummand = NULL; - if (!add_constraintex(ls->LP(), i, &coeffs[0], &varIndexes[0], + if (!add_constraintex(ls->fLP, i, &coeffs[0], &varIndexes[0], ((fOp == OperatorType(EQ)) ? EQ : (fOp == OperatorType(GE)) ? GE : LE), rightSide)) printf("Error in add_constraintex."); + fLS->UpdateObjFunction(); ls->Constraints()->AddItem(this); } /** * Destructor. - * Removes the constraint from its specification. + * Removes the constraint from its specification and deletes all the summands. */ Constraint::~Constraint() { - del_constraint(fLS->LP(), Index()); - delete fSummands; - if(fDNegSummand != NULL) { - delete fDNegSummand->Var(); - delete fDNegSummand; + for (int i=0; iCountItems(); i++) + delete (Summand*)fLeftSide->ItemAt(i); + delete fLeftSide; + + if (fDNegObjSummand != NULL) { + delete fDNegObjSummand->Var(); + delete fDNegObjSummand; } - if(fDPosSummand != NULL) { - delete fDPosSummand->Var(); - delete fDPosSummand; + if (fDPosObjSummand != NULL) { + delete fDPosObjSummand->Var(); + delete fDPosObjSummand; } + + del_constraint(fLS->fLP, Index()); fLS->Constraints()->RemoveItem(this); } diff --git a/src/libs/linprog/Jamfile b/src/libs/linprog/Jamfile index c236f5cf7f..99b7566dc5 100644 --- a/src/libs/linprog/Jamfile +++ b/src/libs/linprog/Jamfile @@ -8,7 +8,6 @@ SharedLibrary liblinprog.so : Constraint.cpp LinearSpec.cpp Summand.cpp - ObjFunctionSummand.cpp PenaltyFunction.cpp Variable.cpp : diff --git a/src/libs/linprog/LinearSpec.cpp b/src/libs/linprog/LinearSpec.cpp index 52c8072057..8da74f09b3 100644 --- a/src/libs/linprog/LinearSpec.cpp +++ b/src/libs/linprog/LinearSpec.cpp @@ -5,13 +5,6 @@ */ #include "LinearSpec.h" -#include "Constraint.h" -#include "ObjFunctionSummand.h" -#include "PenaltyFunction.h" -#include "Constraint.h" -#include "Variable.h" - -#include "lp_lib.h" /** @@ -25,78 +18,34 @@ LinearSpec::LinearSpec() printf("Couldn't construct a new model."); set_verbose(fLP, 1); - fObjFunctionSummands = new BList(1); - fVariables = new BList(1); - fConstraints = new BList(1); - fColumns = 0; + fObjFunction = new BList(); + fVariables = new BList(); + fConstraints = new BList(); + fCountColumns = 0; fLpPresolved = NULL; fOptimization = MINIMIZE; fResult = ERROR; - fObjectiveValue = NULL; + fObjectiveValue = NAN; + fSolvingTime = NAN; } /** * Destructor. - * Removes the specification. + * Removes the specification and deletes all constraints, + * objective function summands and variables. */ LinearSpec::~LinearSpec() { - delete_lp(fLP); -} - - -/** - * Updates the objective function. - */ -void -LinearSpec::UpdateObjFunction() -{ - int32 size = fObjFunctionSummands->CountItems(); - double coeffs[size]; - int varIndexes[size]; - ObjFunctionSummand* current; - for (int32 i = 0; i < size; i++) { - current = (ObjFunctionSummand*)fObjFunctionSummands->ItemAt(i); - coeffs[i] = current->Coeff(); - varIndexes[i] = current->Var()->Index(); - } - - if (!set_obj_fnex(fLP, size, &coeffs[0], &varIndexes[0])) - printf("Error in set_obj_fnex."); - RemovePresolved(); -} - - -/** - * Sets the objective function. - * - * @param coeffs the objective function's coefficients - * @param vars the objective function's variables - */ -void -LinearSpec::SetObjFunction(BList* summands) -{ - fObjFunctionSummands = summands; - UpdateObjFunction(); -} - - -/** - * Adds a new summand to the objective function. - * - * @param coeff the summand's coefficient - * @param var the summand's variable - * @return the new summand - */ -ObjFunctionSummand* -LinearSpec::AddObjFunctionSummand(double coeff, Variable* var) -{ - ObjFunctionSummand* s = new ObjFunctionSummand(this, coeff, var); - fObjFunctionSummands->AddItem(s); - UpdateObjFunction(); - return s; + int i; + for (i=0; iCountItems(); i++) + delete (Constraint*)fConstraints->ItemAt(i); + for (i=0; iCountItems(); i++) + delete (Summand*)fObjFunction->ItemAt(i); + for (i=0; iCountItems(); i++) + delete (Variable*)fVariables->ItemAt(i); + delete_lp(fLP); } @@ -116,7 +65,7 @@ LinearSpec::AddVariable() * Adds a new hard linear constraint to the specification. * * @param coeffs the constraint's coefficients - * @param vars the constraint's variables + * @param vars the constraint's variables * @param op the constraint's operand * @param rightSide the constant value on the constraint's right side * @return the new constraint @@ -135,7 +84,7 @@ LinearSpec::AddConstraint(BList* summands, OperatorType op, double rightSide) * Adds a new hard linear constraint to the specification with a single summand. * * @param coeff1 the constraint's first coefficient - * @param var1 the constraint's first variable + * @param var1 the constraint's first variable * @param op the constraint's operand * @param rightSide the constant value on the constraint's right side * @return the new constraint @@ -157,9 +106,9 @@ LinearSpec::AddConstraint(double coeff1, Variable* var1, * Adds a new hard linear constraint to the specification with two summands. * * @param coeff1 the constraint's first coefficient - * @param var1 the constraint's first variable + * @param var1 the constraint's first variable * @param coeff2 the constraint's second coefficient - * @param var2 the constraint's second variable + * @param var2 the constraint's second variable * @param op the constraint's operand * @param rightSide the constant value on the constraint's right side * @return the new constraint @@ -182,11 +131,11 @@ LinearSpec::AddConstraint(double coeff1, Variable* var1, * Adds a new hard linear constraint to the specification with three summands. * * @param coeff1 the constraint's first coefficient - * @param var1 the constraint's first variable + * @param var1 the constraint's first variable * @param coeff2 the constraint's second coefficient - * @param var2 the constraint's second variable + * @param var2 the constraint's second variable * @param coeff3 the constraint's third coefficient - * @param var3 the constraint's third variable + * @param var3 the constraint's third variable * @param op the constraint's operand * @param rightSide the constant value on the constraint's right side * @return the new constraint @@ -211,13 +160,13 @@ LinearSpec::AddConstraint(double coeff1, Variable* var1, * Adds a new hard linear constraint to the specification with four summands. * * @param coeff1 the constraint's first coefficient - * @param var1 the constraint's first variable + * @param var1 the constraint's first variable * @param coeff2 the constraint's second coefficient - * @param var2 the constraint's second variable + * @param var2 the constraint's second variable * @param coeff3 the constraint's third coefficient - * @param var3 the constraint's third variable + * @param var3 the constraint's third variable * @param coeff4 the constraint's fourth coefficient - * @param var4 the constraint's fourth variable + * @param var4 the constraint's fourth variable * @param op the constraint's operand * @param rightSide the constant value on the constraint's right side * @return the new constraint @@ -244,9 +193,9 @@ LinearSpec::AddConstraint(double coeff1, Variable* var1, * i.e. a constraint that does not always have to be satisfied. * * @param coeffs the constraint's coefficients - * @param vars the constraint's variables + * @param vars the constraint's variables * @param op the constraint's operand - * @param rightSide the constant value on the constraint's right side + * @param rightSide the constant value on the constraint's right side * @param penaltyNeg the coefficient penalizing negative deviations from the exact solution * @param penaltyPos the coefficient penalizing positive deviations from the exact solution */ @@ -265,9 +214,9 @@ LinearSpec::AddConstraint(BList* summands, OperatorType op, * Adds a new soft linear constraint to the specification with a single summand. * * @param coeff1 the constraint's first coefficient - * @param var1 the constraint's first variable + * @param var1 the constraint's first variable * @param op the constraint's operand - * @param rightSide the constant value on the constraint's right side + * @param rightSide the constant value on the constraint's right side * @param penaltyNeg the coefficient penalizing negative deviations from the exact solution * @param penaltyPos the coefficient penalizing positive deviations from the exact solution */ @@ -288,11 +237,11 @@ LinearSpec::AddConstraint(double coeff1, Variable* var1, * Adds a new soft linear constraint to the specification with two summands. * * @param coeff1 the constraint's first coefficient - * @param var1 the constraint's first variable + * @param var1 the constraint's first variable * @param coeff2 the constraint's second coefficient - * @param var2 the constraint's second variable + * @param var2 the constraint's second variable * @param op the constraint's operand - * @param rightSide the constant value on the constraint's right side + * @param rightSide the constant value on the constraint's right side * @param penaltyNeg the coefficient penalizing negative deviations from the exact solution * @param penaltyPos the coefficient penalizing positive deviations from the exact solution */ @@ -315,13 +264,13 @@ LinearSpec::AddConstraint(double coeff1, Variable* var1, * Adds a new soft linear constraint to the specification with three summands. * * @param coeff1 the constraint's first coefficient - * @param var1 the constraint's first variable + * @param var1 the constraint's first variable * @param coeff2 the constraint's second coefficient - * @param var2 the constraint's second variable + * @param var2 the constraint's second variable * @param coeff3 the constraint's third coefficient - * @param var3 the constraint's third variable + * @param var3 the constraint's third variable * @param op the constraint's operand - * @param rightSide the constant value on the constraint's right side + * @param rightSide the constant value on the constraint's right side * @param penaltyNeg the coefficient penalizing negative deviations from the exact solution * @param penaltyPos the coefficient penalizing positive deviations from the exact solution */ @@ -345,15 +294,15 @@ LinearSpec::AddConstraint(double coeff1, Variable* var1, * Adds a new soft linear constraint to the specification with four summands. * * @param coeff1 the constraint's first coefficient - * @param var1 the constraint's first variable + * @param var1 the constraint's first variable * @param coeff2 the constraint's second coefficient - * @param var2 the constraint's second variable + * @param var2 the constraint's second variable * @param coeff3 the constraint's third coefficient - * @param var3 the constraint's third variable + * @param var3 the constraint's third variable * @param coeff4 the constraint's fourth coefficient - * @param var4 the constraint's fourth variable + * @param var4 the constraint's fourth variable * @param op the constraint's operand - * @param rightSide the constant value on the constraint's right side + * @param rightSide the constant value on the constraint's right side * @param penaltyNeg the coefficient penalizing negative deviations from the exact solution * @param penaltyPos the coefficient penalizing positive deviations from the exact solution */ @@ -390,6 +339,56 @@ LinearSpec::AddPenaltyFunction(Variable* var, BList* xs, BList* gs) } +/** + * Gets the objective function. + * + * @return BList containing the objective function's summands + */ +BList* +LinearSpec::ObjFunction() +{ + return fObjFunction; +} + + +/** + * Sets a new objective function. + * The old objective function summands are NOT deleted. + * + * @param summands BList containing the objective function's summands + */ +void +LinearSpec::SetObjFunction(BList* summands) +{ + fObjFunction = summands; + UpdateObjFunction(); +} + + +/** + * Updates the internal representation of the objective function. + * Must be called whenever the summands of the objective function are changed. + */ +void +LinearSpec::UpdateObjFunction() +{ + int32 size = fObjFunction->CountItems(); + double coeffs[size]; + int varIndexes[size]; + Summand* current; + for (int32 i = 0; i < size; i++) { + current = (Summand*)fObjFunction->ItemAt(i); + coeffs[i] = current->Coeff(); + varIndexes[i] = current->Var()->Index(); + } + + if (!set_obj_fnex(fLP, size, &coeffs[0], &varIndexes[0])) + printf("Error in set_obj_fnex."); + + RemovePresolved(); +} + + /** * Remove a cached presolved model, if existent. * This is automatically done each time after the model has been changed, @@ -501,21 +500,9 @@ LinearSpec::Save(char* fname) * @return the number of columns */ int32 -LinearSpec::Columns() const +LinearSpec::CountColumns() const { - return fColumns; -} - - -/** - * Sets the number of columns. - * - * @param value the number of columns - */ -void -LinearSpec::SetColumns(int32 value) -{ - fColumns = value; + return fCountColumns; } @@ -549,54 +536,6 @@ LinearSpec::SetOptimization(OptimizationType value) } -/** - * Gets the lpsolve variable. - * - * @return the lpsolve variable - */ -lprec* -LinearSpec::LP() const -{ - return fLP; -} - - -/** - * Sets the lpsolve variable. - * - * @param value the lpsolve variable - */ -void -LinearSpec::SetLP(lprec* value) -{ - fLP = value; -} - - -/** - * Gets the objective function summand. - * - * @return the objective function summand - */ -BList* -LinearSpec::ObjFunctionSummands() const -{ - return fObjFunctionSummands; -} - - -/** - * Sets the objective function summand. - * - * @param value the objective function summand - */ -void -LinearSpec::SetObjFunctionSummands(BList* value) -{ - fObjFunctionSummands = value; -} - - /** * Gets the the variables. * @@ -609,18 +548,6 @@ LinearSpec::Variables() const } -/** - * Sets the the variables. - * - * @param value the variables - */ -void -LinearSpec::SetVariables(BList* value) -{ - fVariables = value; -} - - /** * Gets the constraints. * @@ -633,16 +560,6 @@ LinearSpec::Constraints() const } -/** - * Sets the constraints. - * - * @param value the constraints - */ -void LinearSpec::SetConstraints(BList* value) { - fConstraints = value; -} - - /** * Gets the result type. * @@ -655,18 +572,6 @@ LinearSpec::Result() const } -/** - * Sets the result type. - * - * @param value the result type - */ -void -LinearSpec::SetResult(ResultType value) -{ - fResult = value; -} - - /** * Gets the objective value. * @@ -679,18 +584,6 @@ LinearSpec::ObjectiveValue() const } -/** - * Sets the objective value. - * - * @param value the objective value - */ -void -LinearSpec::SetObjectiveValue(double value) -{ - fObjectiveValue = value; -} - - /** * Gets the solving time. * @@ -702,15 +595,3 @@ LinearSpec::SolvingTime() const return fSolvingTime; } - -/** - * Sets the solving time. - * - * @param value the solving time - */ -void -LinearSpec::SetSolvingTime(double value) -{ - fSolvingTime = value; -} - diff --git a/src/libs/linprog/ObjFunctionSummand.cpp b/src/libs/linprog/ObjFunctionSummand.cpp deleted file mode 100644 index b0e215c1a7..0000000000 --- a/src/libs/linprog/ObjFunctionSummand.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz - * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz - * Distributed under the terms of the MIT License. - */ - -#include "ObjFunctionSummand.h" -#include "Variable.h" -#include "LinearSpec.h" - - -/** - * Sets the summmand's coefficient. - * - * @param coeff coefficient - */ -void -ObjFunctionSummand::SetCoeff(double coeff) -{ - Summand::SetCoeff(coeff); - fLS->UpdateObjFunction(); -} - - -/** - * Sets the summand's variable. - * - * @param var variable - */ -void -ObjFunctionSummand::SetVar(Variable* var) -{ - Summand::SetVar(var); - fLS->UpdateObjFunction(); -} - - -/** - * Destructor. - * Removes the summand from the objective function. - */ -ObjFunctionSummand::~ObjFunctionSummand() -{ - fLS->ObjFunctionSummands()->RemoveItem(this); - fLS->UpdateObjFunction(); -} - - -/** - * Constructor. - */ -ObjFunctionSummand::ObjFunctionSummand(LinearSpec* ls, double coeff, Variable* var) - : Summand(coeff, var) -{ - fLS = ls; -} - diff --git a/src/libs/linprog/PenaltyFunction.cpp b/src/libs/linprog/PenaltyFunction.cpp index 79faf956a3..29c5c32a6d 100644 --- a/src/libs/linprog/PenaltyFunction.cpp +++ b/src/libs/linprog/PenaltyFunction.cpp @@ -6,7 +6,7 @@ #include "PenaltyFunction.h" #include "Constraint.h" -#include "ObjFunctionSummand.h" +#include "Summand.h" #include "OperatorType.h" #include "Variable.h" #include "LinearSpec.h" @@ -31,8 +31,8 @@ PenaltyFunction::PenaltyFunction(LinearSpec* ls, Variable* var, BList* xs, BList fVar = var; fXs = xs; fGs = gs; - fConstraints = new BList(1); - fObjFunctionSummands = new BList(1); + fConstraints = new BList(sizeGs + 1); + fObjFunctionSummands = new BList(sizeGs); fConstraints->AddItem(ls->AddConstraint(1.0, var, OperatorType(EQ), *(double*)(xs->ItemAt(0)), -*(double*)(gs->ItemAt(0)), @@ -44,9 +44,11 @@ PenaltyFunction::PenaltyFunction(LinearSpec* ls, Variable* var, BList* xs, BList fConstraints->AddItem(ls->AddConstraint(1.0, var, -1.0, dPos, OperatorType(LE), *(double*)(xs->ItemAt(i)))); - fObjFunctionSummands->AddItem(ls->AddObjFunctionSummand( - *(double*)(gs->ItemAt(i + 1)) - *(double*)(gs->ItemAt(i)), dPos)); + Summand* objSummand = new Summand(*(double*)(gs->ItemAt(i + 1)) - *(double*)(gs->ItemAt(i)), dPos); + ls->ObjFunction()->AddItem(objSummand); + fObjFunctionSummands->AddItem(objSummand); } + ls->UpdateObjFunction(); } @@ -56,53 +58,10 @@ PenaltyFunction::PenaltyFunction(LinearSpec* ls, Variable* var, BList* xs, BList */ PenaltyFunction::~PenaltyFunction() { - int32 sizeConstraints = fConstraints->CountItems(); - for (int32 i = 0; i < sizeConstraints; i++) { - Constraint* currentConstraint = (Constraint*)fConstraints->ItemAt(i); - delete currentConstraint; - } + for (int32 i = 0; i < fConstraints->CountItems(); i++) + delete (Constraint*)fConstraints->ItemAt(i); - int32 sizeObjFunctionSummands = fObjFunctionSummands->CountItems(); - for (int32 i = 0; i < sizeObjFunctionSummands; i++) { - ObjFunctionSummand* currentObjFunctionSummand = - (ObjFunctionSummand*)fObjFunctionSummands->ItemAt(i); - delete currentObjFunctionSummand; - } -} - - -/** - * Gets the variable. - * - * @return the variable - */ -const Variable* -PenaltyFunction::Var() const -{ - return fVar; -} - - -/** - * Gets the sampling points. - * - * @return the sampling points - */ -const BList* -PenaltyFunction::Xs() const -{ - return fXs; -} - - -/** - * Gets the sampling gradients. - * - * @return the sampling gradients - */ -const -BList* PenaltyFunction::Gs() const -{ - return fGs; + for (int32 i = 0; i < fObjFunctionSummands->CountItems(); i++) + delete (Summand*)fObjFunctionSummands->ItemAt(i); } diff --git a/src/libs/linprog/Variable.cpp b/src/libs/linprog/Variable.cpp index c4a773b35a..6dee50f9fe 100644 --- a/src/libs/linprog/Variable.cpp +++ b/src/libs/linprog/Variable.cpp @@ -98,7 +98,7 @@ void Variable::SetMin(double min) { fMin = min; - set_bounds(fLS->LP(), this->Index(), fMin, fMax); + set_bounds(fLS->fLP, this->Index(), fMin, fMax); } @@ -123,7 +123,7 @@ void Variable::SetMax(double max) { fMax = max; - set_bounds(fLS->LP(), this->Index(), fMin, fMax); + set_bounds(fLS->fLP, this->Index(), fMin, fMax); } @@ -138,7 +138,7 @@ Variable::SetRange(double min, double max) { fMin = min; fMax = max; - set_bounds(fLS->LP(), this->Index(), fMin, fMax); + set_bounds(fLS->fLP, this->Index(), fMin, fMax); } @@ -204,11 +204,10 @@ Variable::Variable(LinearSpec* ls) ls->Variables()->AddItem(this); - int32 size = ls->Variables()->CountItems(); - if (size > ls->Columns()) { + if (ls->Variables()->CountItems() > ls->CountColumns()) { double d = 0; int i = 0; - if (!add_columnex(ls->LP(), 0, &d, &i)) + if (!add_columnex(ls->fLP, 0, &d, &i)) printf("Error in add_columnex."); } } @@ -220,7 +219,7 @@ Variable::Variable(LinearSpec* ls) */ Variable::~Variable() { - del_column(fLS->LP(), this->Index()); + del_column(fLS->fLP, this->Index()); fLS->Variables()->RemoveItem(this); } diff --git a/src/tests/libs/linprog/Program.cpp b/src/tests/libs/linprog/Program.cpp index 23df336d17..a8e6bc5f61 100644 --- a/src/tests/libs/linprog/Program.cpp +++ b/src/tests/libs/linprog/Program.cpp @@ -7,10 +7,7 @@ #include #include -#include "Constraint.h" #include "LinearSpec.h" -#include "OperatorType.h" -#include "Variable.h" #include @@ -35,25 +32,14 @@ Test1() Variable* x1 = ls->AddVariable(); Variable* x2 = ls->AddVariable(); - BList* coeffs1 = new BList(1); - coeffs1->AddItem(new double(1.0)); - BList* vars1 = new BList(1); - vars1->AddItem(x1); - Constraint* c1 = ls->AddConstraint(coeffs1, vars1, OperatorType(LE), 108); + Constraint* c1 = ls->AddConstraint(1.0, x1, OperatorType(LE), 108); + Constraint* c2 = ls->AddConstraint(1.0, x2, OperatorType(GE), 113); - BList* coeffs2 = new BList(1); - coeffs2->AddItem(new double(1.0)); - BList* vars2 = new BList(1); - vars2->AddItem(x2); - Constraint* c2 = ls->AddConstraint(coeffs2, vars2, OperatorType(GE), 113); + BList* summands = new BList(2); + summands->AddItem(new Summand(1.0, x1)); + summands->AddItem(new Summand(1.0, x2)); + ls->SetObjFunction(summands); - BList* coeffs3 = new BList(2); - coeffs3->AddItem(new double(1.0)); - coeffs3->AddItem(new double(1.0)); - BList* vars3 = new BList(2); - vars3->AddItem(x1); - vars3->AddItem(x2); - ls->SetObjFunction(coeffs3, vars3); ls->Solve(); PrintVars(ls); @@ -61,11 +47,7 @@ Test1() ls->Solve(); PrintVars(ls); - BList* coeffs4 = new BList(1); - coeffs4->AddItem(new double(1.0)); - BList* vars4 = new BList(1); - vars4->AddItem(x2); - c2 = ls->AddConstraint(coeffs4, vars4, OperatorType(GE), 113); + c2 = ls->AddConstraint(1.0, x2, OperatorType(GE), 113); ls->Solve(); PrintVars(ls); }