2008-02-25 04:54:05 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
|
|
|
|
* Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
|
2010-09-20 08:23:27 +04:00
|
|
|
* Copyright 2010, Clemens Zeidler, haiku@clemens-zeidler.de
|
2008-02-25 04:54:05 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2008-02-06 13:51:44 +03:00
|
|
|
#ifndef LINEAR_SPEC_H
|
|
|
|
#define LINEAR_SPEC_H
|
|
|
|
|
2010-09-20 08:23:27 +04:00
|
|
|
#include <math.h>
|
2008-02-06 13:51:44 +03:00
|
|
|
|
|
|
|
#include <List.h>
|
|
|
|
#include <OS.h>
|
2012-01-18 08:06:19 +04:00
|
|
|
#include <Referenceable.h>
|
2011-01-25 07:59:40 +03:00
|
|
|
#include <Size.h>
|
2010-09-20 08:23:27 +04:00
|
|
|
#include <String.h>
|
2008-02-06 13:51:44 +03:00
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
2010-09-20 08:23:27 +04:00
|
|
|
#include "Constraint.h"
|
2010-12-13 22:23:33 +03:00
|
|
|
#include "LinearProgrammingTypes.h"
|
2010-09-20 08:23:27 +04:00
|
|
|
#include "Summand.h"
|
|
|
|
#include "Variable.h"
|
|
|
|
|
2008-02-06 13:51:44 +03:00
|
|
|
|
2010-09-20 10:26:32 +04:00
|
|
|
namespace LinearProgramming {
|
2010-09-20 08:23:27 +04:00
|
|
|
|
2011-01-25 07:59:40 +03:00
|
|
|
|
|
|
|
class LinearSpec;
|
|
|
|
|
|
|
|
|
|
|
|
const BSize kMinSize(0, 0);
|
|
|
|
const BSize kMaxSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
|
|
|
|
|
|
|
|
|
2010-12-13 21:41:58 +03:00
|
|
|
class SolverInterface {
|
|
|
|
public:
|
2011-01-25 07:59:40 +03:00
|
|
|
SolverInterface(LinearSpec* linSpec);
|
|
|
|
|
2010-12-13 21:41:58 +03:00
|
|
|
virtual ~SolverInterface() {}
|
|
|
|
|
2011-01-25 07:59:40 +03:00
|
|
|
virtual ResultType Solve() = 0;
|
|
|
|
|
|
|
|
virtual bool VariableAdded(Variable* variable) = 0;
|
|
|
|
virtual bool VariableRemoved(Variable* variable) = 0;
|
|
|
|
virtual bool VariableRangeChanged(Variable* variable) = 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;
|
2010-12-13 21:41:58 +03:00
|
|
|
|
|
|
|
virtual bool SaveModel(const char* fileName) = 0;
|
2011-01-25 07:59:40 +03:00
|
|
|
|
|
|
|
virtual BSize MinSize(Variable* width, Variable* height) = 0;
|
|
|
|
virtual BSize MaxSize(Variable* width, Variable* height) = 0;
|
|
|
|
|
2012-01-23 04:12:26 +04:00
|
|
|
virtual ResultType FindMins(const VariableList* variables) = 0;
|
|
|
|
virtual ResultType FindMaxs(const VariableList* variables) = 0;
|
|
|
|
|
2012-04-30 06:07:06 +04:00
|
|
|
virtual void GetRangeConstraints(const Variable* var,
|
|
|
|
const Constraint** _min,
|
|
|
|
const Constraint** _max) const = 0;
|
|
|
|
|
2011-01-25 07:59:40 +03:00
|
|
|
protected:
|
|
|
|
LinearSpec* fLinearSpec;
|
2010-12-13 21:41:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-09-20 08:23:27 +04:00
|
|
|
/*!
|
2008-02-06 13:51:44 +03:00
|
|
|
* Specification of a linear programming problem.
|
|
|
|
*/
|
2012-01-18 08:06:19 +04:00
|
|
|
class LinearSpec : public BReferenceable {
|
2008-02-06 13:51:44 +03:00
|
|
|
public:
|
2010-09-20 08:23:27 +04:00
|
|
|
LinearSpec();
|
|
|
|
virtual ~LinearSpec();
|
|
|
|
|
|
|
|
Variable* AddVariable();
|
2010-10-06 00:15:55 +04:00
|
|
|
bool AddVariable(Variable* variable);
|
|
|
|
bool RemoveVariable(Variable* variable,
|
|
|
|
bool deleteVariable = true);
|
|
|
|
int32 IndexOf(const Variable* variable) const;
|
2011-02-08 12:01:11 +03:00
|
|
|
int32 GlobalIndexOf(const Variable* variable) const;
|
2010-12-13 21:41:58 +03:00
|
|
|
bool UpdateRange(Variable* variable);
|
2010-09-20 08:23:27 +04:00
|
|
|
|
2010-11-30 05:08:10 +03:00
|
|
|
bool AddConstraint(Constraint* constraint);
|
|
|
|
bool RemoveConstraint(Constraint* constraint,
|
|
|
|
bool deleteConstraint = true);
|
|
|
|
|
2010-09-20 10:09:19 +04:00
|
|
|
Constraint* AddConstraint(SummandList* summands,
|
2010-09-20 08:23:27 +04:00
|
|
|
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,
|
|
|
|
double coeff2, Variable* var2,
|
|
|
|
double coeff3, Variable* var3,
|
|
|
|
OperatorType op, double rightSide);
|
|
|
|
Constraint* AddConstraint(double coeff1, Variable* var1,
|
|
|
|
double coeff2, Variable* var2,
|
|
|
|
double coeff3, Variable* var3,
|
|
|
|
double coeff4, Variable* var4,
|
|
|
|
OperatorType op, double rightSide);
|
|
|
|
|
2010-09-20 10:09:19 +04:00
|
|
|
Constraint* AddConstraint(SummandList* summands,
|
2010-09-20 08:23:27 +04:00
|
|
|
OperatorType op, double rightSide,
|
|
|
|
double penaltyNeg, double penaltyPos);
|
|
|
|
Constraint* AddConstraint(double coeff1, Variable* var1,
|
|
|
|
OperatorType op, double rightSide,
|
|
|
|
double penaltyNeg, double penaltyPos);
|
|
|
|
Constraint* AddConstraint(double coeff1, Variable* var1,
|
|
|
|
double coeff2, Variable* var2,
|
|
|
|
OperatorType op, double rightSide,
|
|
|
|
double penaltyNeg, double penaltyPos);
|
|
|
|
Constraint* AddConstraint(double coeff1, Variable* var1,
|
|
|
|
double coeff2, Variable* var2,
|
|
|
|
double coeff3, Variable* var3,
|
|
|
|
OperatorType op, double rightSide,
|
|
|
|
double penaltyNeg, double penaltyPos);
|
|
|
|
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);
|
|
|
|
|
2011-01-25 07:59:40 +03:00
|
|
|
BSize MinSize(Variable* width, Variable* height);
|
|
|
|
BSize MaxSize(Variable* width, Variable* height);
|
2010-09-20 08:23:27 +04:00
|
|
|
|
2012-01-23 04:12:26 +04:00
|
|
|
ResultType FindMins(const VariableList* variables);
|
|
|
|
ResultType FindMaxs(const VariableList* variables);
|
|
|
|
|
2010-09-20 08:23:27 +04:00
|
|
|
ResultType Solve();
|
2010-12-13 21:41:58 +03:00
|
|
|
bool Save(const char* fileName);
|
2010-09-20 08:23:27 +04:00
|
|
|
|
|
|
|
int32 CountColumns() const;
|
|
|
|
|
|
|
|
ResultType Result() const;
|
2011-01-25 07:59:40 +03:00
|
|
|
bigtime_t SolvingTime() const;
|
2010-09-20 08:23:27 +04:00
|
|
|
|
2011-03-14 03:24:12 +03:00
|
|
|
BString ToString() const;
|
2009-10-16 16:13:07 +04:00
|
|
|
|
2012-04-30 06:07:06 +04:00
|
|
|
void GetRangeConstraints(const Variable*,
|
|
|
|
const Constraint** _min,
|
|
|
|
const Constraint** _max) const;
|
2010-10-06 00:15:55 +04:00
|
|
|
const ConstraintList& Constraints() const;
|
2011-02-08 12:01:11 +03:00
|
|
|
const VariableList& UsedVariables() const;
|
|
|
|
const VariableList& AllVariables() const;
|
2008-02-06 13:51:44 +03:00
|
|
|
|
2010-11-30 05:08:10 +03:00
|
|
|
protected:
|
|
|
|
friend class Constraint;
|
|
|
|
bool UpdateLeftSide(Constraint* constraint);
|
|
|
|
bool UpdateRightSide(Constraint* constraint);
|
|
|
|
bool UpdateOperator(Constraint* constraint);
|
2008-02-06 13:51:44 +03:00
|
|
|
private:
|
2010-11-30 05:08:10 +03:00
|
|
|
/*! Check if all entries != NULL otherwise delete the list and its
|
|
|
|
entries. */
|
|
|
|
bool _CheckSummandList(SummandList* list);
|
|
|
|
Constraint* _AddConstraint(SummandList* leftSide,
|
|
|
|
OperatorType op, double rightSide,
|
|
|
|
double penaltyNeg, double penaltyPos);
|
|
|
|
|
2010-09-20 10:26:32 +04:00
|
|
|
VariableList fVariables;
|
2011-01-27 01:06:02 +03:00
|
|
|
VariableList fUsedVariables;
|
2010-09-20 10:26:32 +04:00
|
|
|
ConstraintList fConstraints;
|
2010-09-20 08:23:27 +04:00
|
|
|
ResultType fResult;
|
2011-01-25 07:59:40 +03:00
|
|
|
bigtime_t fSolvingTime;
|
2008-02-06 13:51:44 +03:00
|
|
|
|
2010-12-13 21:41:58 +03:00
|
|
|
SolverInterface* fSolver;
|
2008-02-06 13:51:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace LinearProgramming
|
|
|
|
|
|
|
|
using LinearProgramming::LinearSpec;
|
|
|
|
|
|
|
|
#endif // LINEAR_SPEC_H
|