Solved

# Optimising code.

Posted on 2008-11-14
218 Views
Hi all, I have a solving method called nelderMead that runs at the start of my app. Depending on parameters entered this method can go on for over a minute which is way too long.

What is happening is that nelderMead is repeatedly calling another method (oddsCompare)  and passing it different parameters. NelderMead then compares the returned double with a target value and if the difference is less than the tolerance (TOL) it breaks out of the loop.
This loop usually runs about 60ish times.

My question is: can anybody see a way of making this method perform faster?

Thanks.
private void nelderMead(double[] startParams, int paranum,double TOL){

double[] x1 = new double[paranum+1];

double[] xn = new double[paranum+1];

double[] xw = new double[paranum+1];

double[] xe = new double[paranum+1];

double[] xbar = new double[paranum+1];

double[] xr = new double[paranum+1];

double[] xc = new double[paranum+1];

double[] xcc = new double[paranum+1];

double[] funRes = new double[paranum+2];

double[] passParams = new double[paranum+1];

double[][] resmat = new double[paranum+2][paranum+2]; //TODO: sort out the references to the index of the arrays

//int MAXFUN = 1000;

double rho=1,Xi=2,gam=0.5,sigma=0.5;

for(int i = 1;i<paranum+1;i++)

resmat[1][i+1]=startParams[i];

resmat[1][1]=oddsCompare(startParams);

for(int j = 1; j<paranum+1;j++){

for(int i = 1;i< paranum+1;i++){

if(i == j){

if(startParams[i] == 0)

resmat[j + 1][i + 1] = 0.05;

else

resmat[j + 1][i + 1] = startParams[i] * 1.05;

}

else

resmat[j + 1][i + 1] = startParams[i];

passParams[i] = resmat[j + 1][i + 1];

}

resmat[j + 1][1]=oddsCompare(passParams);

}

int lnum=0;

for(lnum=1;lnum<1000;lnum++){

resmat=BubbleSort(resmat,paranum+1);

if(Math.abs(resmat[1][1] - resmat[paranum+1][1])<TOL)

break;

double f1 = resmat[1][1];

for(int i = 1;i<paranum+1;i++)

x1[i]=resmat[1][i+1];

double fn= resmat[paranum][1];

for(int i = 1;i<paranum+1;i++)

xn[i] =resmat[paranum+1][i+1];

double fw = resmat[paranum+1][1];

for(int i = 1;i<paranum+1;i++)

xw[i] = resmat[paranum+1][i+1];

for(int i = 1;i<paranum+1;i++){

xbar[i]=0;

for(int j = 1;j<paranum+1;j++)

xbar[i]=xbar[i]+resmat[j][i+1];

xbar[i] = xbar[i] / paranum;

}

for(int i = 1;i<paranum+1;i++)

xr[i] = xbar[i] + rho * (xbar[i] - xw[i]);

double fr=0;

fr=oddsCompare(xr);

double shrink = 0,newf=0;

double[] newpoint=xe;

if((fr >= f1) && (fr < fn)){

newpoint = xr;

newf = fr;

}

else if(fr < f1){

//calculate expansion point

for(int i = 1;i< paranum+1;i++)

xe[i] = xbar[i] + Xi * (xr[i] - xbar[i]);

double fe=0;

fe=oddsCompare(xe);

if (fe < fr){

newpoint = xe;

newf = fe;}

else{

newpoint = xr;

newf = fr;

}

}

else if(fr >= fn){

if((fr >= fn) && (fr < fw)){

for(int i = 1;i< paranum+1;i++)

xc[i] = xbar[i] + gam * (xr[i] - xbar[i]);

double fc=0;

fc=oddsCompare(xc);

if (fc <= fr){

newpoint = xc;

newf = fc;}

else

shrink = 1;

}

else{

for(int i = 1;i< paranum+1;i++)

xcc[i] = xbar[i] - gam * (xbar[i] - xw[i]);

double fcc=0;

fcc=oddsCompare(xcc);

if (fcc < fw){

newpoint = xcc;

newf = fcc;}

else

shrink = 1;

}

}

if(shrink == 1){

for(int scnt = 2;scnt< paranum+2 ;scnt++){

for(int i = 1;i< paranum+1;i++){

resmat[scnt][i + 1] = x1[i] + sigma * (resmat[scnt][i + 1] - x1[1]);

passParams[i] = resmat[scnt][i + 1];

}

resmat[scnt][1]=oddsCompare(passParams);

}

}

else{

for(int i = 1;i< paranum+1;i++)

resmat[paranum + 1][i + 1] = newpoint[i];

resmat[paranum + 1][1] = newf;

}

}

if(lnum == 1001){

//TODO: throw some error here

}

//resmat=BubbleSort(resmat,paranum+1);

//for(int i = 1;i< paranum+1;i++)

//funRes[i]=resmat[1][i];

//return funRes[0];

}

private double[][] BubbleSort(double[][] passVec,int arraySize){

double[]temp;

double[][] uVec=passVec;

int rownum=arraySize,colnum=arraySize;

temp=new double[colnum+1];

for(int i = rownum - 1;i> 0;i--){

for(int j = 1;j<i+1;j++){

if (uVec[j][1] > uVec[j + 1][1]) {

for(int k = 1;k < colnum+1;k++){

temp[k] = uVec[j + 1][k];

uVec[j + 1][k] = uVec[j][k];

uVec[j][k] = temp[k];

}

}

}

}

return uVec;

}
0
Question by:billyleo

LVL 86

Expert Comment

One easy thing you can do is to NOT use a bubble sort. Use Arrays.sort
0

Author Comment

is that efficient for 2 d arrays?
0

LVL 24

Expert Comment

Yes.
0

Author Comment

can i have an example?
0

LVL 24

Accepted Solution

A 2D array is an array of arrays thus ............ sort every sub-array.

;JOOP!
0

LVL 24

Expert Comment

:))
0

## Featured Post

### Suggested Solutions

count8 challlenge 13 67
countHi2 challenge 7 30
Modeling a class in java 5 22
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correctâ€¦
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be uâ€¦
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.