线性规划求解器
This commit is contained in:
123
inc/solver.hpp
Normal file
123
inc/solver.hpp
Normal file
@ -0,0 +1,123 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
using std::vector;
|
||||
|
||||
class Expr;
|
||||
class Var;
|
||||
|
||||
Expr operator+(const Expr& x, const Expr& y);
|
||||
Expr operator-(const Expr& x, const Expr& y);
|
||||
Expr operator+(const Expr& x);
|
||||
Expr operator+(Var x, Var y);
|
||||
Expr operator+(Var x, double a);
|
||||
Expr operator+(double a, Var x);
|
||||
Expr operator-(const Expr& x);
|
||||
Expr operator-(Var x);
|
||||
Expr operator-(Var x, Var y);
|
||||
Expr operator-(Var x, double a);
|
||||
Expr operator-(double a, Var x);
|
||||
Expr operator*(double a, Var x);
|
||||
Expr operator*(Var x, double a);
|
||||
Expr operator*(const Expr& x, double a);
|
||||
Expr operator*(double a, const Expr& x);
|
||||
Expr operator/(Var x, double a);
|
||||
Expr operator/(const Expr& x, double a);
|
||||
|
||||
typedef vector<vector<double>> Matrix;
|
||||
|
||||
|
||||
enum class Rtn {
|
||||
LOADED,
|
||||
OPTIMAL,
|
||||
INFEASIBLE,
|
||||
INF_OR_UNBD,
|
||||
UNBOUNDED,
|
||||
CUTOFF,
|
||||
ITERATION_LIMIT,
|
||||
NODE_LIMIT,
|
||||
TIME_LIMIT,
|
||||
SOLUTION_LIMIT,
|
||||
INTERRUPTED,
|
||||
NUMERIC,
|
||||
SUBOPTIMAL,
|
||||
INPROGRESS,
|
||||
USER_OBJ_LIMIT,
|
||||
WORK_LIMIT,
|
||||
MEM_LIMIT,
|
||||
PIVOT,
|
||||
};
|
||||
|
||||
class Var {
|
||||
public:
|
||||
Var(double coef = 1) :col(0), coeffs(coef) {};
|
||||
double coeffs;
|
||||
int col;
|
||||
};
|
||||
|
||||
class Expr
|
||||
{
|
||||
private:
|
||||
double constant;
|
||||
std::vector<double> coeffs;
|
||||
std::vector<Var> vars;
|
||||
|
||||
public:
|
||||
Expr(const Expr& expr) = default;
|
||||
Expr(double constant = 0.0);
|
||||
Expr(Var var, double coeff = 1.0);
|
||||
|
||||
friend class Model;
|
||||
|
||||
friend Expr operator+(const Expr& x, const Expr& y);
|
||||
friend Expr operator+(const Expr& x);
|
||||
friend Expr operator+(Var x, Var y);
|
||||
friend Expr operator+(Var x, double a);
|
||||
friend Expr operator+(double a, Var x);
|
||||
friend Expr operator-(const Expr& x, const Expr& y);
|
||||
friend Expr operator-(const Expr& x);
|
||||
friend Expr operator-(Var x);
|
||||
friend Expr operator-(Var x, Var y);
|
||||
friend Expr operator-(Var x, double a);
|
||||
friend Expr operator-(double a, Var x);
|
||||
friend Expr operator*(double a, Var x);
|
||||
friend Expr operator*(Var x, double a);
|
||||
friend Expr operator*(const Expr& x, double a);
|
||||
friend Expr operator*(double a, const Expr& x);
|
||||
friend Expr operator/(Var x, double a);
|
||||
friend Expr operator/(const Expr& x, double a);
|
||||
|
||||
|
||||
Expr operator=(const Expr& rhs);
|
||||
void operator+=(const Expr& expr);
|
||||
void operator-=(const Expr& expr);
|
||||
void operator*=(double mult);
|
||||
void operator/=(double a);
|
||||
Expr operator+(const Expr& rhs);
|
||||
Expr operator-(const Expr& rhs);
|
||||
};
|
||||
|
||||
class Model
|
||||
{
|
||||
public:
|
||||
Model();
|
||||
Var* addVars(int col);
|
||||
void addConstr(const Expr& expr, char sense, double rhs);
|
||||
void setObjective(Expr obje, int sense = 0);
|
||||
void print();
|
||||
Rtn optimize(bool isMax = true);
|
||||
double get();
|
||||
|
||||
private:
|
||||
double _simplex();
|
||||
Rtn _pivot(std::pair<size_t, size_t>& p);
|
||||
void _gaussian(std::pair<size_t, size_t> p);
|
||||
|
||||
|
||||
Var* vars;
|
||||
|
||||
Matrix matrix;
|
||||
size_t cn, bn;
|
||||
vector<int> basic;
|
||||
Rtn rtn;
|
||||
double res;
|
||||
};
|
Reference in New Issue
Block a user