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
+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;