Add an alternative solver to lp_solve. The solver based on Ingo's active set solver but is able to handle arbitrary hard and soft constraints. The advantage to lp_solve is that the active set solver can optimize variable in respect to a quadratic objective function. This makes it possible to minimise the quadratic derivation to a desired value e.g. \Sum_i(x_i - x_{i,pref})^2 -> min.

The solver part has been refactored in this way that both solver can be used with the same layout specifications. The active set solver is default now; the performance is not as good as lp_solve, though.



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40285 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Clemens Zeidler
2011-01-25 04:59:40 +00:00
parent 6537cf9750
commit 7583db5a1e
19 changed files with 2207 additions and 640 deletions
+2 -2
View File
@@ -124,8 +124,6 @@ private:
/*! Add a view without initialize the Area. */
BLayoutItem* _CreateLayoutItem(BView* view);
void _SolveLayout();
void _UpdateAreaConstraints();
BSize _CalculateMinSize();
@@ -152,8 +150,10 @@ private:
Area* fCurrentArea;
#if USE_SCALE_VARIABLE
Variable* fScaleWidth;
Variable* fScaleHeight;
#endif
};
} // namespace BALM
+11 -1
View File
@@ -20,6 +20,9 @@
#include "Tab.h"
#define USE_SCALE_VARIABLE 1
class Constraint;
@@ -106,6 +109,7 @@ public:
private:
Area(BLayoutItem* item);
#if USE_SCALE_VARIABLE
void _Init(LinearSpec* ls, XTab* left, YTab* top,
XTab* right, YTab* bottom,
Variable* scaleWidth,
@@ -113,6 +117,11 @@ private:
void _Init(LinearSpec* ls, Row* row, Column* column,
Variable* scaleWidth,
Variable* scaleHeight);
#else
void _Init(LinearSpec* ls, XTab* left, YTab* top,
XTab* right, YTab* bottom);
void _Init(LinearSpec* ls, Row* row, Column* column);
#endif
void _DoLayout();
@@ -152,9 +161,10 @@ private:
double fContentAspectRatio;
Constraint* fContentAspectRatioC;
#if USE_SCALE_VARIABLE
Variable* fScaleWidth;
Variable* fScaleHeight;
#endif
public:
friend class BALMLayout;
+7 -5
View File
@@ -27,7 +27,6 @@ class LinearSpec;
* May render a specification infeasible.
*/
class Constraint {
public:
int32 Index() const;
@@ -57,24 +56,26 @@ public:
const char* Label();
void SetLabel(const char* label);
void WriteXML(BFile* file);
Variable* DNeg() const;
Variable* DPos() const;
bool IsSoft() const;
bool IsValid();
void Invalidate();
operator BString() const;
void GetString(BString& string) const;
void PrintToStream();
~Constraint();
protected:
Constraint(LinearSpec* ls,
SummandList* summands, OperatorType op,
double rightSide, double penaltyNeg,
double penaltyPos);
double rightSide,
double penaltyNeg = -1,
double penaltyPos = -1);
private:
LinearSpec* fLS;
@@ -92,6 +93,7 @@ private:
public:
friend class LinearSpec;
friend class LPSolveInterface;
};
+31 -40
View File
@@ -11,47 +11,51 @@
#include <List.h>
#include <OS.h>
#include <Size.h>
#include <String.h>
#include <SupportDefs.h>
#include "Constraint.h"
#include "LinearProgrammingTypes.h"
#include "PenaltyFunction.h"
#include "Summand.h"
#include "Variable.h"
namespace LinearProgramming {
class LinearSpec;
const BSize kMinSize(0, 0);
const BSize kMaxSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
class SolverInterface {
public:
SolverInterface(LinearSpec* linSpec);
virtual ~SolverInterface() {}
virtual ResultType Solve(VariableList& variables) = 0;
virtual double GetObjectiveValue() = 0;
virtual ResultType Solve() = 0;
virtual bool AddVariable() = 0;
virtual bool RemoveVariable(int variable) = 0;
virtual bool SetVariableRange(int variable, double min,
double max) = 0;
virtual bool VariableAdded(Variable* variable) = 0;
virtual bool VariableRemoved(Variable* variable) = 0;
virtual bool VariableRangeChanged(Variable* variable) = 0;
virtual bool AddConstraint(int nElements,
double* coefficients, int* variableIndices,
OperatorType op, double rightSide) = 0;
virtual bool RemoveConstraint(int constraint) = 0;
virtual bool SetLeftSide(int constraint, int nElements,
double* coefficients,
int* variableIndices) = 0;
virtual bool SetRightSide(int constraint, double value) = 0;
virtual bool SetOperator(int constraint,
OperatorType op) = 0;
virtual bool SetObjectiveFunction(int nElements,
double* coefficients,
int* variableIndices) = 0;
virtual bool SetOptimization(OptimizationType value) = 0;
virtual bool ConstraintAdded(Constraint* constraint) = 0;
virtual bool ConstraintRemoved(Constraint* constraint) = 0;
virtual bool LeftSideChanged(Constraint* constraint) = 0;
virtual bool RightSideChanged(Constraint* constraint) = 0;
virtual bool OperatorChanged(Constraint* constraint) = 0;
virtual bool SaveModel(const char* fileName) = 0;
virtual BSize MinSize(Variable* width, Variable* height) = 0;
virtual BSize MaxSize(Variable* width, Variable* height) = 0;
protected:
LinearSpec* fLinearSpec;
};
@@ -113,31 +117,22 @@ public:
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
PenaltyFunction* AddPenaltyFunction(Variable* var, BList* xs,
BList* gs);
SummandList* ObjectiveFunction();
//! Caller takes ownership of the Summand's and the SummandList.
SummandList* SwapObjectiveFunction(
SummandList* objFunction);
void SetObjectiveFunction(SummandList* objFunction);
void UpdateObjectiveFunction();
BSize MinSize(Variable* width, Variable* height);
BSize MaxSize(Variable* width, Variable* height);
ResultType Solve();
bool Save(const char* fileName);
int32 CountColumns() const;
OptimizationType Optimization() const;
void SetOptimization(OptimizationType value);
ResultType Result() const;
double ObjectiveValue() const;
double SolvingTime() const;
bigtime_t SolvingTime() const;
operator BString() const;
void GetString(BString& string) const;
const ConstraintList& Constraints() const;
const VariableList& Variables() const;
protected:
friend class Constraint;
@@ -152,14 +147,10 @@ private:
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
OptimizationType fOptimization;
SummandList* fObjFunction;
VariableList fVariables;
ConstraintList fConstraints;
ResultType fResult;
double fObjectiveValue;
double fSolvingTime;
bigtime_t fSolvingTime;
SolverInterface* fSolver;
};
-50
View File
@@ -1,50 +0,0 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef PENALTY_FUNCTION_H
#define PENALTY_FUNCTION_H
#include <List.h>
namespace LinearProgramming {
class LinearSpec;
class Variable;
/**
* Penalty function.
*/
class PenaltyFunction {
protected:
PenaltyFunction(LinearSpec* ls, Variable* var, BList* xs, BList* gs);
public:
~PenaltyFunction();
const Variable* Var() const;
const BList* Xs() const;
const BList* Gs() const;
private:
LinearSpec* fLS;
Variable* fVar;
BList* fXs; // double
BList* fGs; // double
BList* fConstraints;
BList* fObjFunctionSummands;
public:
friend class LinearSpec;
};
} // namespace LinearProgramming
using LinearProgramming::PenaltyFunction;
#endif // PENALTY_FUNCTION_H
+1
View File
@@ -27,6 +27,7 @@ public:
Variable* Var();
void SetVar(Variable* var);
int32 VariableIndex();
private:
double fCoeff;
Variable* fVar;