Commit a1d74db0 authored by Dr.李's avatar Dr.李

update to new interface

parent 6e29c702
...@@ -13,7 +13,7 @@ from libcpp.vector cimport vector ...@@ -13,7 +13,7 @@ from libcpp.vector cimport vector
cdef extern from "lpoptimizer.hpp" namespace "pfopt": cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef cppclass LpOptimizer: cdef cppclass LpOptimizer:
LpOptimizer(vector[double], vector[double], vector[double], vector[double]) except + LpOptimizer(int, int, double*, double*, double*, double*) except +
vector[double] xValue() vector[double] xValue()
double feval() double feval()
int status() int status()
...@@ -25,14 +25,19 @@ cdef class LPOptimizer: ...@@ -25,14 +25,19 @@ cdef class LPOptimizer:
def __init__(self, def __init__(self,
cnp.ndarray[double, ndim=2] cons_matrix, cnp.ndarray[double, ndim=2] cons_matrix,
cnp.ndarray[double] lbound, double[:] lbound,
cnp.ndarray[double] ubound, double[:] ubound,
cnp.ndarray[double] objective): double[:] objective):
cdef int n = lbound.shape[0]
self.cobj = new LpOptimizer(cons_matrix.flatten(order='C'), cdef int m = cons_matrix.shape[0]
lbound, cdef double[:] cons = cons_matrix.flatten(order='C');
ubound,
objective) self.cobj = new LpOptimizer(n,
m,
&cons[0],
&lbound[0],
&ubound[0],
&objective[0])
def __del__(self): def __del__(self):
del self.cobj del self.cobj
...@@ -49,13 +54,15 @@ cdef class LPOptimizer: ...@@ -49,13 +54,15 @@ cdef class LPOptimizer:
cdef extern from "mvoptimizer.hpp" namespace "pfopt": cdef extern from "mvoptimizer.hpp" namespace "pfopt":
cdef cppclass MVOptimizer: cdef cppclass MVOptimizer:
MVOptimizer(vector[double], MVOptimizer(int,
vector[double], double*,
vector[double], double*,
vector[double], double*,
vector[double], double*,
vector[double], int,
vector[double], double*,
double*,
double*,
double) except + double) except +
vector[double] xValue() vector[double] xValue()
double feval() double feval()
...@@ -67,22 +74,29 @@ cdef class QPOptimizer: ...@@ -67,22 +74,29 @@ cdef class QPOptimizer:
cdef MVOptimizer* cobj cdef MVOptimizer* cobj
def __init__(self, def __init__(self,
cnp.ndarray[double] expected_return, double[:] expected_return,
cnp.ndarray[double, ndim=2] cov_matrix, cnp.ndarray[double, ndim=2] cov_matrix,
cnp.ndarray[double] lbound, double[:] lbound,
cnp.ndarray[double] ubound, double[:] ubound,
cnp.ndarray[double, ndim=2] cons_matrix, cnp.ndarray[double, ndim=2] cons_matrix,
cnp.ndarray[double] clbound, double[:] clbound,
cnp.ndarray[double] cubound, double[:] cubound,
double risk_aversion=1.0): double risk_aversion=1.0):
self.cobj = new MVOptimizer(expected_return, cdef int n = lbound.shape[0]
cov_matrix.flatten(order='C'), cdef int m = cons_matrix.shape[0]
lbound, cdef double[:] cov = cov_matrix.flatten(order='C')
ubound, cdef double[:] cons = cons_matrix.flatten(order='C');
cons_matrix.flatten(order='C'),
clbound, self.cobj = new MVOptimizer(n,
cubound, &expected_return[0],
&cov[0],
&lbound[0],
&ubound[0],
m,
&cons[0],
&clbound[0],
&cubound[0],
risk_aversion) risk_aversion)
def __del__(self): def __del__(self):
......
...@@ -7,12 +7,14 @@ ...@@ -7,12 +7,14 @@
namespace pfopt { namespace pfopt {
class PFOPT_CLASS LpOptimizer { class LpOptimizer {
public: public:
LpOptimizer(const std::vector<double>& constraintsMatraix, LpOptimizer(int numVariables,
const std::vector<double>& lowerBound, int numCons,
const std::vector<double>& upperBound, double* constraintMatrix,
const std::vector<double>& objective); double* lowerBound,
double* upperBound,
double* objective);
std::vector<double> xValue() const; std::vector<double> xValue() const;
double feval() const; double feval() const;
...@@ -20,7 +22,7 @@ namespace pfopt { ...@@ -20,7 +22,7 @@ namespace pfopt {
private: private:
ClpSimplex model_; ClpSimplex model_;
int numberOfProb_; size_t numberOfProb_;
}; };
} }
......
...@@ -14,15 +14,16 @@ using Ipopt::IpoptCalculatedQuantities; ...@@ -14,15 +14,16 @@ using Ipopt::IpoptCalculatedQuantities;
namespace pfopt { namespace pfopt {
class PFOPT_CLASS MeanVariance : public TNLP { class MeanVariance : public TNLP {
public: public:
MeanVariance(const std::vector<double> &expectReturn, MeanVariance(int numAssets,
const std::vector<double> &varMatrix, double* expectReturn,
double* varMatrix,
double riskAversion=1.); double riskAversion=1.);
bool setBoundedConstraint(const std::vector<double>& lb, const std::vector<double>& ub); bool setBoundedConstraint(const double* lb, const double* ub);
bool setLinearConstrains(const std::vector<double>& consMatrix, const std::vector<double>& clb, const std::vector<double>& cub); bool setLinearConstrains(int numCons, const double* consMatrix, const double* clb, const double* cub);
virtual bool get_nlp_info(Index &n, Index &m, Index &nnz_jac_g, virtual bool get_nlp_info(Index &n, Index &m, Index &nnz_jac_g,
Index &nnz_h_lag, IndexStyleEnum &index_style); Index &nnz_h_lag, IndexStyleEnum &index_style);
...@@ -58,22 +59,20 @@ namespace pfopt { ...@@ -58,22 +59,20 @@ namespace pfopt {
private: private:
VectorXd expectReturn_; VectorXd expectReturn_;
MatrixXd varMatrix_; MatrixXd varMatrix_;
int numOfAssets_; const int numOfAssets_;
VectorXd xReal_; VectorXd xReal_;
double riskAversion_; const double riskAversion_;
std::vector<double> lb_; const double* lb_;
std::vector<double> ub_; const double* ub_;
VectorXd grad_f_; VectorXd grad_f_;
double feval_; double feval_;
std::vector<double> x_; std::vector<double> x_;
std::vector<Index> iRow_; std::vector<Index> iRow_;
std::vector<Index> jCol_; std::vector<Index> jCol_;
std::vector<double> g_grad_values_; std::vector<double> g_grad_values_;
const double* clb_;
std::vector<double> consMatrix_; const double* cub_;
std::vector<double> clb_;
std::vector<double> cub_;
Index m_; Index m_;
}; };
} }
......
...@@ -5,19 +5,23 @@ ...@@ -5,19 +5,23 @@
#include <coin/IpIpoptApplication.hpp> #include <coin/IpIpoptApplication.hpp>
namespace pfopt { namespace pfopt {
class PFOPT_CLASS MVOptimizer { class MVOptimizer {
public: public:
MVOptimizer(const std::vector<double> &expectReturn, MVOptimizer(int numAssets,
const std::vector<double> &varMatrix, double *expectReturn,
const std::vector<double> &lbound, double *varMatrix,
const std::vector<double> &ubound, double *lbound,
const std::vector<double> &consMatrix = std::vector<double>(), double *ubound,
const std::vector<double> &clb = std::vector<double>(), int numConstraints,
const std::vector<double> &cub = std::vector<double>(), double *consMatrix = nullptr,
double *clb = nullptr,
double *cub = nullptr,
double riskAversion = 1.); double riskAversion = 1.);
std::vector<double> xValue() const { return mvImpl_->xValue(); } std::vector<double> xValue() const { return mvImpl_->xValue(); }
double feval() const { return mvImpl_->feval(); } double feval() const { return mvImpl_->feval(); }
int status() const { return status_; } int status() const { return status_; }
private: private:
......
...@@ -8,7 +8,7 @@ using namespace alglib; ...@@ -8,7 +8,7 @@ using namespace alglib;
namespace pfopt { namespace pfopt {
class PFOPT_CLASS AlglibData { class AlglibData {
public: public:
AlglibData(const std::vector<double> &expectReturn, AlglibData(const std::vector<double> &expectReturn,
const std::vector<double> &varMatrix, const std::vector<double> &varMatrix,
......
...@@ -10,40 +10,4 @@ using Eigen::Map; ...@@ -10,40 +10,4 @@ using Eigen::Map;
using alglib::real_1d_array; using alglib::real_1d_array;
using alglib::real_2d_array; using alglib::real_2d_array;
#ifdef WIN32
#ifdef __cplusplus
#define DLL_EXPORT_C_DECL extern "C" __declspec(dllexport)
#define DLL_IMPORT_C_DECL extern "C" __declspec(dllimport)
#define DLL_EXPORT_DECL extern __declspec(dllexport)
#define DLL_IMPORT_DECL extern __declspec(dllimport)
#define DLL_EXPORT_CLASS_DECL __declspec(dllexport)
#define DLL_IMPORT_CLASS_DECL __declspec(dllimport)
#else
#define DLL_EXPORT_DECL __declspec(dllexport)
#define DLL_IMPORT_DECL __declspec(dllimport)
#endif
#else
#ifdef __cplusplus
#define DLL_EXPORT_C_DECL extern "C"
#define DLL_IMPORT_C_DECL extern "C"
#define DLL_EXPORT_DECL extern
#define DLL_IMPORT_DECL extern
#define DLL_EXPORT_CLASS_DECL
#define DLL_IMPORT_CLASS_DECL
#else
#define DLL_EXPORT_DECL extern
#define DLL_IMPORT_DECL extern
#endif
#endif
#ifdef PFOPF_EXPORTS
#define PFOPT_CLASS DLL_EXPORT_CLASS_DECL
#define PFOPT_API DLL_EXPORT_DECL
#else
#define PFOPT_CLASS DLL_IMPORT_CLASS_DECL
#define PFOPT_API DLL_IMPORT_DECL
#endif
#endif #endif
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
namespace pfopt { namespace pfopt {
namespace io { namespace io {
PFOPT_API std::vector<double> read_csv(const std::string &filePath); std::vector<double> read_csv(const std::string &filePath);
} }
PFOPT_API double min(const real_1d_array& array, int n = 0); double min(const real_1d_array& array, int n = 0);
PFOPT_API double max(const real_1d_array& array, int n = 0); double max(const real_1d_array& array, int n = 0);
PFOPT_API double sum(const real_1d_array& array, int n = 0); double sum(const real_1d_array& array, int n = 0);
bool is_close(double a, double b, double tol=1e-9); bool is_close(double a, double b, double tol=1e-9);
} }
......
...@@ -32,7 +32,8 @@ if platform.system() != "Windows": ...@@ -32,7 +32,8 @@ if platform.system() != "Windows":
"./libs/include/eigen", "./libs/include/eigen",
"./libs/include/alglib"], "./libs/include/alglib"],
libraries=['pfopt'], libraries=['pfopt'],
library_dirs=['./libs/lib/linux']), library_dirs=['./libs/lib/linux'],
extra_compile_args=['-std=c++11']),
] ]
else: else:
extensions = [ extensions = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment