[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Optimising code.

Posted on 2008-11-14
6
Medium Priority
?
244 Views
Last Modified: 2012-05-05
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;	
		
	}

Open in new window

0
Comment
Question by:billyleo
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 22958026
One easy thing you can do is to NOT use a bubble sort. Use Arrays.sort
0
 

Author Comment

by:billyleo
ID: 22958031
is that efficient for 2 d arrays?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22958106
Yes.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:billyleo
ID: 22958221
can i have an example?
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 2000 total points
ID: 22958368
A 2D array is an array of arrays thus ............ sort every sub-array.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 23029757
:))
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question