Patch by Christof Lutteroth:

* copyright headers for the files of the libraries linprog and alm
* new class Summand for representing summands in a linear constraint
* merged class SoftConstraint into class Constraint; Constraint now
  supports both soft and hard constraint functionality
* new AddConstraint methods in class LinearSpec for directly setting
  constraints with 1 to 4 summands
* code cleanups by using aforementioned AddConstraint methods
* a new very simple test application for alm
* some style corrections


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24108 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2008-02-25 01:54:05 +00:00
parent da51719239
commit 0306945545
39 changed files with 918 additions and 1069 deletions
+45 -17
View File
@@ -1,11 +1,20 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef CONSTRAINT_H
#define CONSTRAINT_H
#include "OperatorType.h"
#include "Variable.h"
#include "ObjFunctionSummand.h"
#include <List.h>
#include <String.h>
#include <SupportDefs.h>
#include <math.h>
namespace LinearProgramming {
@@ -20,32 +29,51 @@ class Constraint {
public:
int32 Index();
BList* Coeffs();
BList* Vars();
virtual void ChangeLeftSide(BList* coeffs, BList* vars);
virtual OperatorType Op();
virtual void SetOp(OperatorType value);
BList* Summands();
void ChangeLeftSide(BList* summands);
void ChangeLeftSide(double coeff1, Variable* var1);
void ChangeLeftSide(double coeff1, Variable* var1,
double coeff2, Variable* var2);
void ChangeLeftSide(double coeff1, Variable* var1,
double coeff2, Variable* var2,
double coeff3, Variable* var3);
void ChangeLeftSide(double coeff1, Variable* var1,
double coeff2, Variable* var2,
double coeff3, Variable* var3,
double coeff4, Variable* var4);
OperatorType Op();
void SetOp(OperatorType value);
double RightSide();
void SetRightSide(double value);
BString ToString();
virtual ~Constraint();
void SetRightSide(double value);
double PenaltyNeg();
void SetPenaltyNeg(double value);
double PenaltyPos();
void SetPenaltyPos(double value);
Variable* DNeg() const;
Variable* DPos() const;
BString ToString();
~Constraint();
protected:
Constraint();
Constraint(LinearSpec* ls, BList* summands,
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
private:
Constraint(LinearSpec* ls, BList* coeffs, BList* vars,
OperatorType op, double rightSide);
protected:
LinearSpec* fLS;
BList* fCoeffs;
BList* fVars;
OperatorType fOp;
BList* fSummands;
OperatorType fOp;
double fRightSide;
Variable* fDNeg;
Variable* fDPos;
ObjFunctionSummand* fDNegSummand;
ObjFunctionSummand* fDPosSummand;
void _UpdateLeftSide();
public:
friend class LinearSpec;
friend class LinearSpec;
};
+34 -28
View File
@@ -1,3 +1,9 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef LINEAR_SPEC_H
#define LINEAR_SPEC_H
@@ -10,6 +16,7 @@
#include <List.h>
#include <OS.h>
#include <SupportDefs.h>
#include <math.h>
namespace LinearProgramming {
@@ -17,7 +24,6 @@ namespace LinearProgramming {
class Constraint;
class ObjFunctionSummand;
class PenaltyFunction;
class SoftConstraint;
class Variable;
/**
@@ -28,81 +34,81 @@ class LinearSpec {
public:
LinearSpec();
~LinearSpec();
void UpdateObjFunction();
void SetObjFunction(BList* coeffs, BList* vars);
void UpdateObjFunction();
void SetObjFunction(BList* summands);
ObjFunctionSummand* AddObjFunctionSummand(double coeff, Variable* var);
Variable* AddVariable();
Variable* AddVariable();
Constraint* AddConstraint(BList* coeffs, BList* vars,
Constraint* AddConstraint(BList* summands,
OperatorType op, double rightSide);
Constraint* AddConstraint(double coeff1, Variable* var1,
OperatorType op, double rightSide);
Constraint* AddConstraint(double coeff1, Variable* var1,
double coeff2, Variable* var2,
OperatorType op, double rightSide);
Constraint* AddConstraint(double coeff1, Variable* var1,
Constraint* AddConstraint(double coeff1, Variable* var1,
double coeff2, Variable* var2,
double coeff3, Variable* var3,
OperatorType op, double rightSide);
Constraint* AddConstraint(double coeff1, Variable* var1,
Constraint* AddConstraint(double coeff1, Variable* var1,
double coeff2, Variable* var2,
double coeff3, Variable* var3,
double coeff4, Variable* var4,
OperatorType op, double rightSide);
SoftConstraint* AddSoftConstraint(BList* coeffs, BList* vars,
Constraint* AddConstraint(BList* summands,
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
SoftConstraint* AddSoftConstraint(double coeff1, Variable* var1,
Constraint* AddConstraint(double coeff1, Variable* var1,
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
SoftConstraint* AddSoftConstraint(double coeff1, Variable* var1,
Constraint* AddConstraint(double coeff1, Variable* var1,
double coeff2, Variable* var2,
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
SoftConstraint* AddSoftConstraint(double coeff1, Variable* var1,
Constraint* AddConstraint(double coeff1, Variable* var1,
double coeff2, Variable* var2,
double coeff3, Variable* var3,
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
SoftConstraint* AddSoftConstraint(double coeff1, Variable* var1,
Constraint* AddConstraint(double coeff1, Variable* var1,
double coeff2, Variable* var2,
double coeff3, Variable* var3,
double coeff4, Variable* var4,
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
PenaltyFunction* AddPenaltyFunction(Variable* var, BList* xs, BList* gs);
void RemovePresolved();
PenaltyFunction* AddPenaltyFunction(Variable* var, BList* xs, BList* gs);
void RemovePresolved();
ResultType Presolve();
ResultType Solve();
void Save(char* fname);
void Save(char* fname);
int32 Columns() const;
void SetColumns(int32 value);
OptimizationType Optimization() const;
void SetOptimization(OptimizationType value);
void SetColumns(int32 value);
OptimizationType Optimization() const;
void SetOptimization(OptimizationType value);
lprec* LP() const;
void SetLP(lprec* value);
void SetLP(lprec* value);
BList* ObjFunctionSummands() const;
void SetObjFunctionSummands(BList* value);
void SetObjFunctionSummands(BList* value);
BList* Variables() const;
void SetVariables(BList* value);
void SetVariables(BList* value);
BList* Constraints() const;
void SetConstraints(BList* value);
void SetConstraints(BList* value);
ResultType Result() const;
void SetResult(ResultType value);
void SetResult(ResultType value);
double ObjectiveValue() const;
void SetObjectiveValue(double value);
void SetObjectiveValue(double value);
double SolvingTime() const;
void SetSolvingTime(double value);
void SetSolvingTime(double value);
protected:
int32 fColumns;
private:
lprec* fLpPresolved;
OptimizationType fOptimization;
OptimizationType fOptimization;
lprec* fLP;
BList* fObjFunctionSummands;
BList* fVariables;
@@ -112,8 +118,8 @@ private:
double fSolvingTime; // = Double.Nan
public:
friend class ObjFunctionSummand;
friend class SoftConstraint;
friend class ObjFunctionSummand;
friend class Constraint;
};
+13 -9
View File
@@ -1,6 +1,14 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef OBJ_FUNCTION_SUMMAND_H
#define OBJ_FUNCTION_SUMMAND_H
#include "Summand.h"
namespace LinearProgramming {
@@ -10,25 +18,21 @@ class Variable;
/**
* A summand of the objective function.
*/
class ObjFunctionSummand {
class ObjFunctionSummand : public Summand {
public:
double Coeff();
void SetCoeff(double coeff);
Variable* Var();
void SetVar(Variable* var);
~ObjFunctionSummand();
void SetCoeff(double coeff);
void SetVar(Variable* var);
~ObjFunctionSummand();
protected:
ObjFunctionSummand(LinearSpec* ls, double coeff, Variable* var);
private:
LinearSpec* fLS;
double fCoeff;
Variable* fVar;
public:
friend class LinearSpec;
friend class LinearSpec;
};
+6
View File
@@ -1,3 +1,9 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef OPERATOR_TYPE_H
#define OPERATOR_TYPE_H
+6
View File
@@ -1,3 +1,9 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef OPTIMIZATION_TYPE_H
#define OPTIMIZATION_TYPE_H
+6
View File
@@ -1,3 +1,9 @@
/*
* 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
+6
View File
@@ -1,3 +1,9 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef RESULT_TYPE_H
#define RESULT_TYPE_H
-54
View File
@@ -1,54 +0,0 @@
#ifndef SOFT_CONSTRAINT_H
#define SOFT_CONSTRAINT_H
#include "Constraint.h"
#include <List.h>
namespace LinearProgramming {
class LinearSpec;
class ObjFunctionSummand;
class Variable;
/**
* Soft constraint, i.e.&nbsp;one that does not necessarily have to be satisfied.
* Use this instead of hard constraints to avoid over-constrained specifications.
*/
class SoftConstraint : public Constraint {
public:
void ChangeLeftSide(BList* coeffs, BList* vars);
OperatorType Op();
void SetOp(OperatorType value);
double PenaltyNeg();
void SetPenaltyNeg(double value);
double PenaltyPos();
void SetPenaltyPos(double value);
//~ string ToString();
Variable* DNeg() const;
Variable* DPos() const;
~SoftConstraint();
protected:
SoftConstraint(LinearSpec* ls, BList* coeffs, BList* vars,
OperatorType op, double rightSide,
double penaltyNeg, double penaltyPos);
private:
Variable* fDNeg;
Variable* fDPos;
ObjFunctionSummand* fDNegSummand;
ObjFunctionSummand* fDPosSummand;
public:
friend class LinearSpec;
};
} // namespace LinearProgramming
using LinearProgramming::SoftConstraint;
#endif // SOFT_CONSTRAINT_H
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef SUMMAND_H
#define SUMMAND_H
namespace LinearProgramming {
class LinearSpec;
class Variable;
/**
* A summand of a linear term.
*/
class Summand {
public:
double Coeff();
void SetCoeff(double coeff);
Variable* Var();
void SetVar(Variable* var);
Summand(double coeff, Variable* var);
~Summand();
private:
double fCoeff;
Variable* fVar;
};
} // namespace LinearProgramming
using LinearProgramming::Summand;
#endif // OBJ_FUNCTION_SUMMAND_H
+13 -7
View File
@@ -1,3 +1,9 @@
/*
* Copyright 2007-2008, Christof Lutteroth, [email protected]
* Copyright 2007-2008, James Kim, [email protected]
* Distributed under the terms of the MIT License.
*/
#ifndef VARIABLE_H
#define VARIABLE_H
@@ -17,15 +23,15 @@ class Variable {
public:
int32 Index();
LinearSpec* LS() const;
void SetLS(LinearSpec* value);
void SetLS(LinearSpec* value);
double Value() const;
void SetValue(double value);
void SetValue(double value);
double Min() const;
void SetMin(double min);
void SetMin(double min);
double Max() const;
void SetMax(double max);
void SetRange(double min, double max);
//~ string ToString();
void SetMax(double max);
void SetRange(double min, double max);
//~ string ToString();
Constraint* IsEqual(Variable* var);
Constraint* IsSmallerOrEqual(Variable* var);
Constraint* IsGreaterorEqual(Variable* var);
@@ -42,7 +48,7 @@ private:
public:
friend class LinearSpec;
friend class SoftConstraint;
friend class Constraint;
};