阅读更多
#include <jni.h>
#include <stdio.h>
#include <iostream.h>
#include "sovfuzz.h"
#include "stdafx.h"
#include "matlib.h"
int sol(int m,int n,double srh,double seh,double dup,double growth,double *r,double *ret){
int retcode=0;
initM(MATCOM_VERSION);
Mm R=zeros(m,n),SGM=zeros(n,n),a=zeros(n,1),rate=zeros(n,1),up=zeros(n,1),h=zeros(m,1);
int register i,j,k;
for(i=0;i<m;i++){
for(j=0;j<n;j++)
R.r(i+1,j+1)=*(r+i*n+j);
}
double dtmp=0,sgmh;
for(dtmp=0,i=1;i<=m;i++){
h.r(i,1)=srh+seh*(i-1)/(m-1);
dtmp+=srh+seh*(i-1)/(m-1);
}
sgmh=dtmp;
for(i=1;i<=n;i++){
for(dtmp=0,j=1;j<=m;j++)
dtmp+=h.r(j,1)*R.r(j,i);
a.r(i,1)=(double)dtmp/sgmh;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++){
for(dtmp=0,k=1;k<=m;k++)
dtmp+=(R.r(k,i)-a.r(i,1))*(R.r(k,j)-a.r(j,1))*h.r(k,1);
SGM.r(i,j)=(double)dtmp/sgmh;
}
for(dtmp=0,i=1;i<=m;i++){
h.r(i,1)=srh+seh*(i-1)/(m-1);
dtmp+=srh+seh*(i-1)/(m-1);
}
sgmh=dtmp;
for(i=1;i<=n;i++){
for(dtmp=0,j=1;j<=m;j++)
dtmp+=h.r(j,1)*R.r(j,i);
a.r(i,1)=(double)dtmp/sgmh;
}
for(i=1;i<=n;i++){
up.r(i)=dup;
rate.r(i,1)=1+a.r(i,1);
}
double dStatus, dCost,dn=double(n);
///////////////////////////
// get moki portfolio
typedef void (LINGOCALL)( void*[], int*, int*);
LINGOCALL* m_pLINGOCall;
HINSTANCE m_hInstLINGO;
// Load the LINGO DLL
VERIFY( m_hInstLINGO = ::LoadLibrary("lingodll.dll"));
// Get a pointer to the LINGO script processor
VERIFY( m_pLINGOCall = (LINGOCALL*) ::GetProcAddress( (HMODULE) m_hInstLINGO, "LGCSCRIPT"));
// Build our list of pointers for LINGO
void* pArgs[4];
int nArgs = 4;
int nErrorCode;
pArgs[0] = (void*) "SET TERSEO 1\nTAKE stock.LNG\nGO\nQUIT\n";
pArgs[1] = (void*) "stock.LOG";
pArgs[2] = NULL;
double* pTransfermoki[9] = {&dn,&growth,rate.addr(), up.addr(),SGM.addr(),ret,&dCost, &dStatus, NULL};
pArgs[3] = pTransfermoki;
(*m_pLINGOCall)( pArgs, &nArgs, &nErrorCode);
if ( nErrorCode || dStatus){
printf("%s","Get fuzz portfolio error!!");
retcode= - 1;
}else
retcode=round(dCost*100);
///////////////////////////
FreeLibrary( m_hInstLINGO);
exitM();
return retcode;
}
JNIEXPORT jint JNICALL Java_sovfuzz_getportfilio
(JNIEnv * env, jclass obj, jint m, jint n,jdouble jstarth, jdouble jsteph, jdouble jup, jdouble jgrowth,jdoubleArray retdata, jdoubleArray arr){
jdouble *carr,*cret;
jint sum = 0;
jint asize,rsize;
carr = env->GetDoubleArrayElements(arr, NULL);
asize=env->GetArrayLength(arr);
if (carr == NULL) {
return -1; /* exception occurred */
}
cret = env->GetDoubleArrayElements(retdata, NULL);
rsize=env->GetArrayLength(retdata);
if (cret == NULL) {
return -1; /* exception occurred */
}
// const char *cfile;
// cfile=env->GetStringUTFChars(filename,NULL);
sum=sol(m,n,jstarth,jsteph,jup,jgrowth,cret,carr);
env->ReleaseDoubleArrayElements(arr, carr, 0);
env->ReleaseDoubleArrayElements(retdata, cret, 0);
// env->ReleaseStringUTFChars(filename,cfile);
return sum;
}