Commit 893ab9e2 authored by Dr.李's avatar Dr.李
parents e0302605 744ba1a8
...@@ -44,7 +44,9 @@ alpha - mind 的安装极其简单,只需要在下载源码之后,运行: ...@@ -44,7 +44,9 @@ alpha - mind 的安装极其简单,只需要在下载源码之后,运行:
python setup.py install python setup.py install
``` ```
* *注意事项*: 在Linux系统上,请确保gcc版本大于5 * *注意事项*:
1. 在Linux系统上,请确保gcc版本大于4.8;
2. 在libs下面提供了依赖的一些库的二进制文件。linux版本的是在一台具有两个intel cpu的docker虚机上面编译完成的。如果需要实现最佳的性能,建议用户在目标机器上编译相关依赖的库。依赖的库源码地址:[portfolio-optimizer](https://github.com/alpha-miner/portfolio-optimizer)
## 数据源 ## 数据源
......
...@@ -8,14 +8,12 @@ Created on 2017-7-20 ...@@ -8,14 +8,12 @@ Created on 2017-7-20
cimport numpy as cnp cimport numpy as cnp
import numpy as np import numpy as np
from libcpp.vector cimport vector
from libcpp cimport nullptr
cdef extern from "lpoptimizer.hpp" namespace "pfopt": cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef cppclass LpOptimizer: cdef cppclass LpOptimizer:
LpOptimizer(int, int, double*, double*, double*, double*) except + LpOptimizer(int, int, double*, double*, double*, double*) except +
vector[double] xValue() double* xValue()
double feval() double feval()
int status() int status()
...@@ -23,18 +21,20 @@ cdef extern from "lpoptimizer.hpp" namespace "pfopt": ...@@ -23,18 +21,20 @@ cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef class LPOptimizer: cdef class LPOptimizer:
cdef LpOptimizer* cobj cdef LpOptimizer* cobj
cdef int n
cdef int m
def __cinit__(self, def __cinit__(self,
cnp.ndarray[double, ndim=2] cons_matrix, cnp.ndarray[double, ndim=2] cons_matrix,
double[:] lbound, double[:] lbound,
double[:] ubound, double[:] ubound,
double[:] objective): double[:] objective):
cdef int n = lbound.shape[0] self.n = lbound.shape[0]
cdef int m = cons_matrix.shape[0] self.m = cons_matrix.shape[0]
cdef double[:] cons = cons_matrix.flatten(order='C'); cdef double[:] cons = cons_matrix.flatten(order='C');
self.cobj = new LpOptimizer(n, self.cobj = new LpOptimizer(self.n,
m, self.m,
&cons[0], &cons[0],
&lbound[0], &lbound[0],
&ubound[0], &ubound[0],
...@@ -50,7 +50,13 @@ cdef class LPOptimizer: ...@@ -50,7 +50,13 @@ cdef class LPOptimizer:
return self.cobj.feval() return self.cobj.feval()
def x_value(self): def x_value(self):
return np.array(self.cobj.xValue()) cdef cnp.ndarray[double, ndim=1] res = np.zeros(self.n)
cdef double* c_arr = self.cobj.xValue()
for i in range(self.n):
res[i] = c_arr[i]
return res
cdef extern from "mvoptimizer.hpp" namespace "pfopt": cdef extern from "mvoptimizer.hpp" namespace "pfopt":
...@@ -65,7 +71,7 @@ cdef extern from "mvoptimizer.hpp" namespace "pfopt": ...@@ -65,7 +71,7 @@ cdef extern from "mvoptimizer.hpp" namespace "pfopt":
double*, double*,
double*, double*,
double) except + double) except +
vector[double] xValue() double* xValue()
double feval() double feval()
int status() int status()
...@@ -73,6 +79,8 @@ cdef extern from "mvoptimizer.hpp" namespace "pfopt": ...@@ -73,6 +79,8 @@ cdef extern from "mvoptimizer.hpp" namespace "pfopt":
cdef class QPOptimizer: cdef class QPOptimizer:
cdef MVOptimizer* cobj cdef MVOptimizer* cobj
cdef int n
cdef int m
def __cinit__(self, def __cinit__(self,
double[:] expected_return, double[:] expected_return,
...@@ -84,27 +92,27 @@ cdef class QPOptimizer: ...@@ -84,27 +92,27 @@ cdef class QPOptimizer:
double[:] cubound=None, double[:] cubound=None,
double risk_aversion=1.0): double risk_aversion=1.0):
cdef int n = lbound.shape[0] self.n = lbound.shape[0]
cdef int m self.m = 0
cdef double[:] cov = cov_matrix.flatten(order='C') cdef double[:] cov = cov_matrix.flatten(order='C')
cdef double[:] cons cdef double[:] cons
if cons_matrix is not None: if cons_matrix is not None:
m = cons_matrix.shape[0] self.m = cons_matrix.shape[0]
cons = cons_matrix.flatten(order='C'); cons = cons_matrix.flatten(order='C');
self.cobj = new MVOptimizer(n, self.cobj = new MVOptimizer(self.n,
&expected_return[0], &expected_return[0],
&cov[0], &cov[0],
&lbound[0], &lbound[0],
&ubound[0], &ubound[0],
m, self.m,
&cons[0], &cons[0],
&clbound[0], &clbound[0],
&cubound[0], &cubound[0],
risk_aversion) risk_aversion)
else: else:
self.cobj = new MVOptimizer(n, self.cobj = new MVOptimizer(self.n,
&expected_return[0], &expected_return[0],
&cov[0], &cov[0],
&lbound[0], &lbound[0],
...@@ -122,7 +130,13 @@ cdef class QPOptimizer: ...@@ -122,7 +130,13 @@ cdef class QPOptimizer:
return self.cobj.feval() return self.cobj.feval()
def x_value(self): def x_value(self):
return np.array(self.cobj.xValue()) cdef cnp.ndarray[double, ndim=1] res = np.zeros(self.n)
cdef double* c_arr = self.cobj.xValue()
for i in range(self.n):
res[i] = c_arr[i]
return res
def status(self): def status(self):
return self.cobj.status() return self.cobj.status()
\ No newline at end of file
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
#define pfopt_linear_programming_optimizer_hpp #define pfopt_linear_programming_optimizer_hpp
#include "types.hpp" #include "types.hpp"
#include <vector>
#include "ClpSimplex.hpp" #include "ClpSimplex.hpp"
namespace pfopt { namespace pfopt {
...@@ -16,14 +15,16 @@ namespace pfopt { ...@@ -16,14 +15,16 @@ namespace pfopt {
double* upperBound, double* upperBound,
double* objective); double* objective);
std::vector<double> xValue() const { return sol_; } ~LpOptimizer() { delete [] sol_;}
double* xValue() const { return sol_; }
double feval() const; double feval() const;
int status() const { return model_.status(); } int status() const { return model_.status(); }
private: private:
ClpSimplex model_; ClpSimplex model_;
size_t numberOfProb_; size_t numberOfProb_;
std::vector<double> sol_; double* sol_;
}; };
} }
......
...@@ -22,6 +22,8 @@ namespace pfopt { ...@@ -22,6 +22,8 @@ namespace pfopt {
double* varMatrix, double* varMatrix,
double riskAversion=1.); double riskAversion=1.);
virtual ~MeanVariance() { delete [] x_;}
bool setBoundedConstraint(const double* lb, const double* ub); bool setBoundedConstraint(const double* lb, const double* ub);
bool setLinearConstrains(int numCons, const double* consMatrix, const double* clb, const double* cub); bool setLinearConstrains(int numCons, const double* consMatrix, const double* clb, const double* cub);
...@@ -54,7 +56,7 @@ namespace pfopt { ...@@ -54,7 +56,7 @@ namespace pfopt {
IpoptCalculatedQuantities *ip_cq); IpoptCalculatedQuantities *ip_cq);
double feval() const { return feval_; } double feval() const { return feval_; }
std::vector<double> xValue() const { return x_; } double* xValue() const { return x_; }
private: private:
VectorXd expectReturn_; VectorXd expectReturn_;
...@@ -68,7 +70,7 @@ namespace pfopt { ...@@ -68,7 +70,7 @@ namespace pfopt {
const double* ub_; const double* ub_;
VectorXd grad_f_; VectorXd grad_f_;
double feval_; double feval_;
std::vector<double> x_; 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_;
......
...@@ -18,7 +18,7 @@ namespace pfopt { ...@@ -18,7 +18,7 @@ namespace pfopt {
double *cub = nullptr, double *cub = nullptr,
double riskAversion = 1.); double riskAversion = 1.);
std::vector<double> xValue() const { return mvImpl_->xValue(); } double* xValue() const { return mvImpl_->xValue(); }
double feval() const { return mvImpl_->feval(); } double feval() const { return mvImpl_->feval(); }
......
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