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

update to new interface

parent 6e29c702
......@@ -13,7 +13,7 @@ from libcpp.vector cimport vector
cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef cppclass LpOptimizer:
LpOptimizer(vector[double], vector[double], vector[double], vector[double]) except +
LpOptimizer(int, int, double*, double*, double*, double*) except +
vector[double] xValue()
double feval()
int status()
......@@ -25,14 +25,19 @@ cdef class LPOptimizer:
def __init__(self,
cnp.ndarray[double, ndim=2] cons_matrix,
cnp.ndarray[double] lbound,
cnp.ndarray[double] ubound,
cnp.ndarray[double] objective):
self.cobj = new LpOptimizer(cons_matrix.flatten(order='C'),
lbound,
ubound,
objective)
double[:] lbound,
double[:] ubound,
double[:] objective):
cdef int n = lbound.shape[0]
cdef int m = cons_matrix.shape[0]
cdef double[:] cons = cons_matrix.flatten(order='C');
self.cobj = new LpOptimizer(n,
m,
&cons[0],
&lbound[0],
&ubound[0],
&objective[0])
def __del__(self):
del self.cobj
......@@ -49,13 +54,15 @@ cdef class LPOptimizer:
cdef extern from "mvoptimizer.hpp" namespace "pfopt":
cdef cppclass MVOptimizer:
MVOptimizer(vector[double],
vector[double],
vector[double],
vector[double],
vector[double],
vector[double],
vector[double],
MVOptimizer(int,
double*,
double*,
double*,
double*,
int,
double*,
double*,
double*,
double) except +
vector[double] xValue()
double feval()
......@@ -67,22 +74,29 @@ cdef class QPOptimizer:
cdef MVOptimizer* cobj
def __init__(self,
cnp.ndarray[double] expected_return,
double[:] expected_return,
cnp.ndarray[double, ndim=2] cov_matrix,
cnp.ndarray[double] lbound,
cnp.ndarray[double] ubound,
double[:] lbound,
double[:] ubound,
cnp.ndarray[double, ndim=2] cons_matrix,
cnp.ndarray[double] clbound,
cnp.ndarray[double] cubound,
double[:] clbound,
double[:] cubound,
double risk_aversion=1.0):
self.cobj = new MVOptimizer(expected_return,
cov_matrix.flatten(order='C'),
lbound,
ubound,
cons_matrix.flatten(order='C'),
clbound,
cubound,
cdef int n = lbound.shape[0]
cdef int m = cons_matrix.shape[0]
cdef double[:] cov = cov_matrix.flatten(order='C')
cdef double[:] cons = cons_matrix.flatten(order='C');
self.cobj = new MVOptimizer(n,
&expected_return[0],
&cov[0],
&lbound[0],
&ubound[0],
m,
&cons[0],
&clbound[0],
&cubound[0],
risk_aversion)
def __del__(self):
......
......@@ -7,12 +7,14 @@
namespace pfopt {
class PFOPT_CLASS LpOptimizer {
class LpOptimizer {
public:
LpOptimizer(const std::vector<double>& constraintsMatraix,
const std::vector<double>& lowerBound,
const std::vector<double>& upperBound,
const std::vector<double>& objective);
LpOptimizer(int numVariables,
int numCons,
double* constraintMatrix,
double* lowerBound,
double* upperBound,
double* objective);
std::vector<double> xValue() const;
double feval() const;
......@@ -20,7 +22,7 @@ namespace pfopt {
private:
ClpSimplex model_;
int numberOfProb_;
size_t numberOfProb_;
};
}
......
......@@ -14,15 +14,16 @@ using Ipopt::IpoptCalculatedQuantities;
namespace pfopt {
class PFOPT_CLASS MeanVariance : public TNLP {
class MeanVariance : public TNLP {
public:
MeanVariance(const std::vector<double> &expectReturn,
const std::vector<double> &varMatrix,
MeanVariance(int numAssets,
double* expectReturn,
double* varMatrix,
double riskAversion=1.);
bool setBoundedConstraint(const std::vector<double>& lb, const std::vector<double>& ub);
bool setLinearConstrains(const std::vector<double>& consMatrix, const std::vector<double>& clb, const std::vector<double>& cub);
bool setBoundedConstraint(const double* lb, const double* ub);
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,
Index &nnz_h_lag, IndexStyleEnum &index_style);
......@@ -58,22 +59,20 @@ namespace pfopt {
private:
VectorXd expectReturn_;
MatrixXd varMatrix_;
int numOfAssets_;
const int numOfAssets_;
VectorXd xReal_;
double riskAversion_;
const double riskAversion_;
std::vector<double> lb_;
std::vector<double> ub_;
const double* lb_;
const double* ub_;
VectorXd grad_f_;
double feval_;
std::vector<double> x_;
std::vector<Index> iRow_;
std::vector<Index> jCol_;
std::vector<double> g_grad_values_;
std::vector<double> consMatrix_;
std::vector<double> clb_;
std::vector<double> cub_;
const double* clb_;
const double* cub_;
Index m_;
};
}
......
......@@ -5,19 +5,23 @@
#include <coin/IpIpoptApplication.hpp>
namespace pfopt {
class PFOPT_CLASS MVOptimizer {
class MVOptimizer {
public:
MVOptimizer(const std::vector<double> &expectReturn,
const std::vector<double> &varMatrix,
const std::vector<double> &lbound,
const std::vector<double> &ubound,
const std::vector<double> &consMatrix = std::vector<double>(),
const std::vector<double> &clb = std::vector<double>(),
const std::vector<double> &cub = std::vector<double>(),
MVOptimizer(int numAssets,
double *expectReturn,
double *varMatrix,
double *lbound,
double *ubound,
int numConstraints,
double *consMatrix = nullptr,
double *clb = nullptr,
double *cub = nullptr,
double riskAversion = 1.);
std::vector<double> xValue() const { return mvImpl_->xValue(); }
double feval() const { return mvImpl_->feval(); }
int status() const { return status_; }
private:
......
......@@ -8,7 +8,7 @@ using namespace alglib;
namespace pfopt {
class PFOPT_CLASS AlglibData {
class AlglibData {
public:
AlglibData(const std::vector<double> &expectReturn,
const std::vector<double> &varMatrix,
......
......@@ -10,40 +10,4 @@ using Eigen::Map;
using alglib::real_1d_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
......@@ -6,12 +6,12 @@
namespace pfopt {
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);
PFOPT_API double max(const real_1d_array& array, int n = 0);
PFOPT_API double sum(const real_1d_array& array, int n = 0);
double min(const real_1d_array& array, int n = 0);
double max(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);
}
......
......@@ -32,7 +32,8 @@ if platform.system() != "Windows":
"./libs/include/eigen",
"./libs/include/alglib"],
libraries=['pfopt'],
library_dirs=['./libs/lib/linux']),
library_dirs=['./libs/lib/linux'],
extra_compile_args=['-std=c++11']),
]
else:
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