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

update libs for windows

parent 99c74c07
......@@ -8,14 +8,13 @@ Created on 2017-7-20
cimport numpy as cnp
import numpy as np
from libcpp.vector cimport vector
from libcpp cimport nullptr
cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef cppclass LpOptimizer:
LpOptimizer(int, int, double*, double*, double*, double*) except +
vector[double] xValue()
double* xValue()
double feval()
int status()
......@@ -23,18 +22,20 @@ cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef class LPOptimizer:
cdef LpOptimizer* cobj
cdef int n
cdef int m
def __cinit__(self,
cnp.ndarray[double, ndim=2] cons_matrix,
double[:] lbound,
double[:] ubound,
double[:] objective):
cdef int n = lbound.shape[0]
cdef int m = cons_matrix.shape[0]
self.n = lbound.shape[0]
self.m = cons_matrix.shape[0]
cdef double[:] cons = cons_matrix.flatten(order='C');
self.cobj = new LpOptimizer(n,
m,
self.cobj = new LpOptimizer(self.n,
self.m,
&cons[0],
&lbound[0],
&ubound[0],
......@@ -50,7 +51,13 @@ cdef class LPOptimizer:
return self.cobj.feval()
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":
......@@ -65,7 +72,7 @@ cdef extern from "mvoptimizer.hpp" namespace "pfopt":
double*,
double*,
double) except +
vector[double] xValue()
double* xValue()
double feval()
int status()
......@@ -73,6 +80,8 @@ cdef extern from "mvoptimizer.hpp" namespace "pfopt":
cdef class QPOptimizer:
cdef MVOptimizer* cobj
cdef int n
cdef int m
def __cinit__(self,
double[:] expected_return,
......@@ -84,27 +93,27 @@ cdef class QPOptimizer:
double[:] cubound=None,
double risk_aversion=1.0):
cdef int n = lbound.shape[0]
cdef int m
self.n = lbound.shape[0]
self.m = 0
cdef double[:] cov = cov_matrix.flatten(order='C')
cdef double[:] cons
if cons_matrix is not None:
m = cons_matrix.shape[0]
self.m = cons_matrix.shape[0]
cons = cons_matrix.flatten(order='C');
self.cobj = new MVOptimizer(n,
self.cobj = new MVOptimizer(self.n,
&expected_return[0],
&cov[0],
&lbound[0],
&ubound[0],
m,
self.m,
&cons[0],
&clbound[0],
&cubound[0],
risk_aversion)
else:
self.cobj = new MVOptimizer(n,
self.cobj = new MVOptimizer(self.n,
&expected_return[0],
&cov[0],
&lbound[0],
......@@ -122,7 +131,13 @@ cdef class QPOptimizer:
return self.cobj.feval()
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):
return self.cobj.status()
\ No newline at end of file
......@@ -2,7 +2,6 @@
#define pfopt_linear_programming_optimizer_hpp
#include "types.hpp"
#include <vector>
#include "ClpSimplex.hpp"
namespace pfopt {
......@@ -16,14 +15,16 @@ namespace pfopt {
double* upperBound,
double* objective);
std::vector<double> xValue() const { return sol_; }
~LpOptimizer() { delete [] sol_;}
double* xValue() const { return sol_; }
double feval() const;
int status() const { return model_.status(); }
private:
ClpSimplex model_;
size_t numberOfProb_;
std::vector<double> sol_;
double* sol_;
};
}
......
......@@ -22,6 +22,8 @@ namespace pfopt {
double* varMatrix,
double riskAversion=1.);
virtual ~MeanVariance() { delete [] x_;}
bool setBoundedConstraint(const double* lb, const double* ub);
bool setLinearConstrains(int numCons, const double* consMatrix, const double* clb, const double* cub);
......@@ -54,7 +56,7 @@ namespace pfopt {
IpoptCalculatedQuantities *ip_cq);
double feval() const { return feval_; }
std::vector<double> xValue() const { return x_; }
double* xValue() const { return x_; }
private:
VectorXd expectReturn_;
......@@ -68,7 +70,7 @@ namespace pfopt {
const double* ub_;
VectorXd grad_f_;
double feval_;
std::vector<double> x_;
double* x_;
std::vector<Index> iRow_;
std::vector<Index> jCol_;
std::vector<double> g_grad_values_;
......
......@@ -18,7 +18,7 @@ namespace pfopt {
double *cub = nullptr,
double riskAversion = 1.);
std::vector<double> xValue() const { return mvImpl_->xValue(); }
double* xValue() const { return mvImpl_->xValue(); }
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