Multithread increases calculation time - Java - java

I was asked to check calculation time depending on number of threads working on the problem. Therefore I had written a program that calculates integral using Monte Carlo method. I am dividing the range for number of threads. After that I stats threads, which calculate their part, and finally sum partial results to get general one.
The problem is that time of calculation increases with number of threads instead of decreasing (i7 processor, Windows 7)
A few people working on it, and we do not know why is that. I hope someone will give me an advice.
I attach code:
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Runner {
private static final int MAXT = 10; // maksymalna ilość wątków
static PrintWriter outM;
static PrintWriter outMTime;
public static void main(String[] args){
double xp = 2;
double xk = 3;
filesOp();
// Wypisywanie kolumn tabeli
for(int threadNumber=1; threadNumber<=MAXT; threadNumber++){
outM.print("\t"+ threadNumber);
outMTime.print("\t"+ threadNumber);
}
double time1;
double time2;
//double startTime=System.currentTimeMillis(); // Przed wystartowaniem programu
for(int n=10000; n<=10000000; n=n*10){
System.out.println("Licze dla: " + n + " punktow.");
outM.print("\n"+n);
outMTime.print("\n"+n);
for(int threadNumber=1; threadNumber<=MAXT; threadNumber++){
outM.print("\t");
outMTime.print("\t");
time1=System.nanoTime();
multiThread(xp, xk, n, threadNumber);
time2=System.nanoTime();
outMTime.print((time2-time1)/1000000);
// czas pracy dla danej liczby wątków
}
}
outM.close();
outMTime.close();
}
public static void multiThread(double xp, double xk, int n, int threadNumber){
// Funkcja licząca całkę wielowątkowo.
// Całka do policzenia jest dzielona pomiędzy wątki
ArrayList<Thread> threadList = new ArrayList<Thread>();
ConcurrentLinkedQueue<Double> results = new ConcurrentLinkedQueue<Double>();
for(int i=0; i<threadNumber; i++){
MonteCarlo mc = new MonteCarlo( xp+(i*((xk-xp)/threadNumber)), xp+((i+1)*((xk-xp)/threadNumber)), (int)(n/threadNumber), results);
Thread t = new Thread(mc);
threadList.add(t);
t.start();
}
//for(int j=0; j<threadNumber; j++){ // pętla czeka na zakończenie wątków
for(Thread t : threadList){
try {
//while(t.isAlive()){}
//threadList.get(j).join();
t.join();
} catch (Exception e) {
e.printStackTrace();
}
}
double wynik = 0;
//for(int k=0; k<results.size(); k++){
for(double r: results){
//wynik = wynik + results.remove();
wynik= wynik + r;
}
outM.print(wynik);
}
public static void filesOp(){
File fileTemp;
fileTemp = new File("wyniki.txt");
if (fileTemp.exists()) fileTemp.delete();
fileTemp = new File("pomiary.txt");
if (fileTemp.exists()) fileTemp.delete();
try {
outM = new PrintWriter(new FileWriter("wyniki.txt", true));
outMTime = new PrintWriter(new FileWriter("pomiary.txt", true));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class MonteCarlo implements Runnable{
double xp;
double xk;
long n;
ConcurrentLinkedQueue<Double> results;
MonteCarlo(double xp, double xk, long n, ConcurrentLinkedQueue<Double> results){
this.xp=xp;
this.xk=xk;
this.n=n;
this.results=results;
}
//funkcja dla ktorej obliczamy calke
private static double func(double x) {
return x*x+3;
}
private static double funcIn(double x, double y) {
if (( y > 0) && (y <= func(x)))
return 1;
else if (( y > 0) && (y <= func(x)))
return -1;
return 0;
}
//random number from a to b
private static double randomPoint(double a, double b) {
return a + Math.random() * (b-a);
}
public void run(){
double yp, yk, calka;
int pointsIn;
yp = 0;
yk = Math.ceil(Math.max(func(xp), func(xk)));
pointsIn = 0;
for (long i=0; i<n; i++) {
pointsIn += funcIn(randomPoint(xp, xk), randomPoint(yp, yk));
}
calka = (pointsIn / (double)n) * ((xk-xp) * (yk-yp));
results.add(calka);
}
}
And the example of results:
1 2 3 4 5 6 7 8 9 10
10000 6.185818 2.821405 3.721287 3.470309 4.068365 3.604195 4.323075 4.192455 6.159694 4.239105
100000 10.994522 15.874134 34.992323 40.851124 36.199631 49.54579 45.122417 61.427132 55.845435 60.7661
1000000 108.653008 274.443662 340.274574 407.054352 437.455361 469.853467 496.849012 584.519687 571.09329 594.152023
10000000 1066.059033 2877.947652 3600.551966 4175.707089 4488.434247 5081.572093 5501.217804 6374.335759 6128.274553 6339.043475

The problem most likely lies in
private static double randomPoint(double a, double b) {
return a + Math.random() * (b-a);
}
Math.random() performs poorly under heavy contention. If you are using java 7 or later, try this instead:
private static double randomPoint(double a, double b) {
return ThreadLocalRandom.current().nextDouble(a, b);
}

Using static funtions frequently is one of the pitfalls in Multithreading.
A more general answer can be found in this post already.

Related

How to proccess blocking time of a simulated queue in java for an M/M/2 system?

hello guys for simulating the queue blocking time for an M/M/1 I came up with this very solution, but it is not Object-oriented unfortunately, also the problem is I want to simulate it with M/M/2 system,for instance I initialized lambda with 19 and mu with 20 just for ease up the calculation any solution, hint, code example will be greatly appreciated.
public class Main {
public static void main(String[] args) {
final int MAX_ENTITY = 100000;
final int SYSTEM_CAPACITY = 5;
final int BUSY = 1;
final int IDLE = 0;
double lambda = 19, mu = 20;
int blocked = 0;
int queue_length = 0;
int server_state = IDLE;
int entity = 0;
double next_av = getArivalRand(lambda);
double next_dp = next_av + getDeparturedRand(lambda);
while (entity <= MAX_ENTITY) {
//Arrival
if (next_av <= next_dp) {
entity++;
if (server_state == IDLE) {
server_state = BUSY;
} else if (queue_length < SYSTEM_CAPACITY - 1) {
queue_length++;
} else {
blocked++;
}
next_av += getArivalRand(lambda);
} // Departure
else if (queue_length > 0) {
queue_length--;
next_dp = next_dp + getDeparturedRand(mu);
} else {
server_state = IDLE;
next_dp = next_av + getDeparturedRand(mu);
}
}
System.out.println("Blocked Etity:" + blocked + "\n");
}
public static double getArivalRand(double lambda) {
return -1 / lambda * Math.log(1 - Math.random());
}
public static double getDeparturedRand(double mu) {
return -1 / mu * Math.log(1 - Math.random());
}
}
EDIT:
check here if u don't know about the queue theory
oh boy you're code needs serious refactoring in order to achieve M/M/2.
I created a gist file here which I think implements what you wanted,
In the gist file I created a Dispatcher class for balancing two queues in two servers and also I've simulated it with two seeds, it is much more Object-Oriented approach,
here is an example code from gist file which is for balancing load of
the tasks
if (server1.getQueueLength() < server2.getQueueLength())
currentServer = server1;
else if (server1.getQueueLength() > server2.getQueueLength())
currentServer = server2;
else if (currentServer == server1)
currentServer = server2;
else
currentServer = server1;

Formula Evaluator for XIRR function in Java

Facing issue with reading a cell in excel which is set with XIRR function.
I written my code in Java. Below is the code to set the formula. Please help on how can I read the value from the cell and not the formula.
cell.setCellFormula("XIRR(E2:E10, B2:B10");
CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));
cell.setCellStyle(style);
Below is the error while evaluating the cell using FormulaEvaluator
org.apache.poi.ss.formula.eval.NotImplementedFunctionException: XIRR
at org.apache.poi.ss.formula.atp.AnalysisToolPak$NotImplemented.evaluate(AnalysisToolPak.java:59)
at org.apache.poi.ss.formula.UserDefinedFunction.evaluate(UserDefinedFunction.java:61)
at org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:129)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:550)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:317)
... 18 more
Without patching apache poi with XIRR function directly calculating a result like Excel's XIRR function is possible using a User Defined Function in apache poi.
The following code provides exactly this.
It defines a class CalculateXIRR which then will be used as myXIRR function in apache poi. The CalculateXIRR uses either JXIRR - v1.0.0 (C) 2005 Gautam Satpathy or class Xirr derived from java program to calculate XIRR without using excel or any other library to calculate XIRR.
Also it provides code for test cases. At first the same test case as from the example in Excel's XIRR documentation. And then random test cases using random values and dates. Those test cases are written into an Excel workbook. Written are the result of the evaluation of the user defined myXIRR function as well as Excel's original XIRR function. So we can comparing the results.
My tests have shown that both XIRR calculation methods are pretty exact like Excel using reasonable values and dates. Only using values and dates which leads Excel's XIRR resulting in high negative percentages (lower than -60%) or very high percentages (greater than 1000%) both methods are different from Excel.
JXIRR - v1.0.0 from Gautam Satpathy is better suited to Excel as the class Xirr. The reason is pretty clear since the class Xirr will always fail if x in Math.pow((x + 1d), (dt0-dt) / 365d) is lower than -1d. If so, then the base of the Math.pow function is negative and since the exponent (dt0-dt) / 365d) is fractional, there is only a imaginary solution. This happens if Excel's XIRR is resulting in high negative percentages and the approximation tries to come from below -100%. JXIRR uses a goal seek method which seems to be more like the one of Excel itself.
Code:
import java.io.* ;
import org.apache.poi.ss.formula.functions.* ;
import org.apache.poi.ss.formula.udf.* ;
import org.apache.poi.ss.usermodel.* ;
import org.apache.poi.xssf.usermodel.* ;
import org.apache.poi.ss.formula.* ;
import org.apache.poi.ss.formula.eval.* ;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Random;
/*
https://github.com/ept/jxirr
(C) 2005 Gautam Satpathy
*/
import in.satpathy.financial.*;
public class XIRREvaluator {
private Workbook workbook;
private Sheet sheet;
private Row row;
private Cell cell;
private CellStyle percentStyle;
private CellStyle dateStyle;
private FormulaEvaluator evaluator;
private String[] labels;
private char c1;
private char c2;
private String[] formulas;
private Double[] values;
private SimpleDateFormat sdf;
private Date[] dates;
public XIRREvaluator() {
this.workbook = new XSSFWorkbook();
String[] functionNames = { "myXIRR" } ;
FreeRefFunction[] functionImpls = { new CalculateXIRR() } ;
UDFFinder udfs = new DefaultUDFFinder( functionNames, functionImpls ) ;
UDFFinder udfToolpack = new AggregatingUDFFinder( udfs ) ;
workbook.addToolPack(udfToolpack);
this.percentStyle = workbook.createCellStyle();
percentStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));
this.dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
this.evaluator = workbook.getCreationHelper().createFormulaEvaluator();
this.sheet = workbook.createSheet("Sheet1");
this.labels = new String[]{"XIRR", "myXIRR", "diff"};
this.sdf = new SimpleDateFormat("yyyy-MM-dd");
}
public void save() {
try {
workbook.write(new FileOutputStream("ExcelWorkbookXIRR.xlsx"));
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void testCaseFromExcelDocu(int startCol, int startRow) {
/*
This provides a test case as from the example in Excel's XIRR documentation:
https://support.office.com/en-us/article/XIRR-function-de1242ec-6477-445b-b11b-a303ad9adc9d
*/
if (startCol > 24) return;
try {
c1 = (char)(65+startCol);
c2 = (char)(65+startCol+1);
formulas = new String[]{"XIRR("+c1+(startRow+4)+":"+c1+(startRow+8)+","+c2+(startRow+4)+":"+c2+(startRow+8)+")",
"myXIRR("+c1+(startRow+4)+":"+c1+(startRow+8)+","+c2+(startRow+4)+":"+c2+(startRow+8)+")",
""+c2+(startRow+1)+"-"+c2+(startRow+2)};
values = new Double[]{-10000d, 2750d, 4250d, 3250d, 2750d};
dates = new Date[]{sdf.parse("2008-01-01"), sdf.parse("2008-03-01"), sdf.parse("2008-10-30"), sdf.parse("2009-02-15"), sdf.parse("2009-04-01")};
for (int r = startRow; r < startRow+3; r++) {
row = (sheet.getRow(r)==null)?sheet.createRow(r):sheet.getRow(r);
cell = row.createCell(startCol);
cell.setCellValue(labels[r-startRow]);
}
for (int r = startRow+3; r < startRow+8; r++) {
row = (sheet.getRow(r)==null)?sheet.createRow(r):sheet.getRow(r);
cell = row.createCell(startCol);
cell.setCellValue(values[r-startRow-3]);
cell = row.createCell(startCol+1);
cell.setCellValue(dates[r-startRow-3]);
cell.setCellStyle(dateStyle);
}
for (int r = startRow; r < startRow+2; r++) {
cell = sheet.getRow(r).createCell(startCol+1);
cell.setCellFormula(formulas[r-startRow]);
cell.setCellStyle(percentStyle);
if (r == startRow+1) {
cell = evaluator.evaluateInCell(cell);
System.out.println(new DataFormatter().formatCellValue(cell));
}
}
cell = sheet.getRow(startRow+2).createCell(startCol+1);
cell.setCellFormula(formulas[2]);
sheet.autoSizeColumn(startCol);
sheet.autoSizeColumn(startCol+1);
} catch (Exception e) {
e.printStackTrace();
}
}
private void randomTestCases(int startCol, int startRow, int count) {
/*
This provides randon test cases
*/
try {
long day = 24L*60L*60L*1000L;
long startDate = sdf.parse("2010-01-01").getTime();
for (int test = startCol; test < startCol+3*count; test+=3) {
if (test > 24) return;
c1 = (char)(65+test);
c2 = (char)(65+test+1);
Random rnd = new Random();
int rows = 5+rnd.nextInt(5);
formulas = new String[]{"XIRR("+c1+(startRow+4)+":"+c1+(startRow+3+rows)+","+c2+(startRow+4)+":"+c2+(startRow+3+rows)+")",
"myXIRR("+c1+(startRow+4)+":"+c1+(startRow+3+rows)+", "+c2+(startRow+4)+":"+c2+(startRow+3+rows)+")",
""+c2+(startRow+1)+"-"+c2+(startRow+2)};
values = new Double[rows];
values[0] = -1d*(rows-1d)*(1000+rnd.nextInt(5000));
for (int i = 1; i < rows; i++) {
values[i] = 1d*(1000+rnd.nextInt(5000));
}
dates = new Date[rows];
for (int i = 0; i < rows; i++) {
dates[i] = sdf.parse(sdf.format(new Date(startDate+=day*(1L+rnd.nextInt(150)))));
}
for (int r = startRow; r < startRow+3; r++) {
row = (sheet.getRow(r)==null)?sheet.createRow(r):sheet.getRow(r);
cell = row.createCell(test);
cell.setCellValue(labels[r-startRow]);
}
for (int r = startRow+3; r < startRow+3+rows; r++) {
row = (sheet.getRow(r)==null)?sheet.createRow(r):sheet.getRow(r);
cell = row.createCell(test);
cell.setCellValue(values[r-startRow-3]);
cell = row.createCell(test+1);
cell.setCellValue(dates[r-startRow-3]);
cell.setCellStyle(dateStyle);
}
for (int r = startRow; r < startRow+2; r++) {
cell = sheet.getRow(r).createCell(test+1);
cell.setCellFormula(formulas[r-startRow]);
cell.setCellStyle(percentStyle);
if (r == startRow+1) {
evaluator.clearAllCachedResultValues();
cell = evaluator.evaluateInCell(cell);
System.out.println(new DataFormatter().formatCellValue(cell));
}
}
cell = sheet.getRow(startRow+2).createCell(test+1);
cell.setCellFormula(formulas[2]);
sheet.autoSizeColumn(test);
sheet.autoSizeColumn(test+1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main( String[] args ) {
XIRREvaluator xirrEvaluator = new XIRREvaluator();
//test case as from the example in Excel's XIRR documentation
//starting on column 0, row 0
xirrEvaluator.testCaseFromExcelDocu(0,0);
//9 random test cases
//starting on column 0, row 10
xirrEvaluator.randomTestCases(0,10,9);
//9 random test cases
//starting on column 0, row 25
xirrEvaluator.randomTestCases(0,25,9);
xirrEvaluator.save();
}
}
/*
Class for user defined function myXIRR
*/
class CalculateXIRR implements FreeRefFunction {
#Override
public ValueEval evaluate( ValueEval[] args, OperationEvaluationContext ec ) {
if (args.length < 2 || args.length > 3) {
return ErrorEval.VALUE_INVALID;
}
double result;
try {
double[] values = ValueCollector.collectValues(args[0]);
double[] dates = ValueCollector.collectValues(args[1]);
double guess;
if(args.length == 3) {
ValueEval v = OperandResolver.getSingleValue(args[2], ec.getRowIndex(), ec.getColumnIndex()) ;
guess = OperandResolver.coerceValueToDouble(v);
} else {
guess = 0.1d;
}
result = calculateXIRR( values, dates, guess ) ;
checkValue(result);
} catch (EvaluationException e) {
//e.printStackTrace();
return e.getErrorEval();
}
return new NumberEval( result ) ;
}
public double calculateXIRR(double[] values, double[] dates, double guess ) {
double result;
/*
Either calculating XIRR using https://github.com/ept/jxirr (C) 2005 Gautam Satpathy
*/
XIRRData data = new XIRRData(values.length, guess, values, dates);
result = XIRR.xirr(data) - 1d;
/*
Or calculating XIRR Class Xirr
from https://stackoverflow.com/questions/36789967/java-program-to-calculate-xirr-without-using-excel-or-any-other-library
*/
//result = Xirr.Newtons_method(guess, values, dates);
return result;
}
static final void checkValue(double result) throws EvaluationException {
if (Double.isNaN(result) || Double.isInfinite(result)) {
throw new EvaluationException(ErrorEval.NUM_ERROR);
}
}
static final class ValueCollector extends MultiOperandNumericFunction {
private static final ValueCollector instance = new ValueCollector();
public ValueCollector() {
super(false, false);
}
public static double[] collectValues(ValueEval...operands) throws EvaluationException {
return instance.getNumberArray(operands);
}
protected double evaluate(double[] values) {
throw new IllegalStateException("should not be called");
}
}
}
/*
Class Xirr from https://stackoverflow.com/questions/36789967/java-program-to-calculate-xirr-without-using-excel-or-any-other-library
*/
final class Xirr {
private static final double tol = 0.00000001;
private static double f_xirr(double p, double dt, double dt0, double x) {
double resf = p * Math.pow((x + 1d), (dt0-dt) / 365d);
return resf;
}
private static double df_xirr(double p, double dt, double dt0, double x) {
double resf = (1d / 365d) * (dt0-dt) * p * Math.pow((x + 1d), ((dt0-dt) / 365d) - 1d);
return resf;
}
private static double total_f_xirr(double[] payments, double[] days, double x) {
double resf = 0d;
for (int i = 0; i < payments.length; i++) {
resf = resf + f_xirr(payments[i], days[i], days[0], x);
}
return resf;
}
private static double total_df_xirr(double[] payments, double[] days, double x) {
double resf = 0d;
for (int i = 0; i < payments.length; i++) {
resf = resf + df_xirr(payments[i], days[i], days[0], x);
}
return resf;
}
public static double Newtons_method(double guess, double[] payments, double[] days) {
double x0 = guess;
double x1 = 0d;
double err = 1e+100;
while (err > tol) {
x1 = x0 - total_f_xirr(payments, days, x0) / total_df_xirr(payments, days, x0);
err = Math.abs(x1 - x0);
x0 = x1;
}
return x0;
}
}

From String to Integer Functions

I am trying to write a program that will receive a function as a String and solve it. For ex. "5*5+2/2-8+5*5-2" should return 41
I wrote the code for multiplication and divisions and it works perfectly:
public class Solver
{
public static void operationS(String m)
{
ArrayList<String> z = new ArrayList<String>();
char e= ' ';
String x= " ";
for (int i =0; i<m.length();i++)
{
e= m.charAt(i);
x= Character.toString(e);
z.add(x);
}
for (int i =0; i<z.size();i++)
{
System.out.print(z.get(i));
}
other(z);
}
public static void other(ArrayList<String> j)
{
int n1=0;
int n2=0;
int f=0;
String n= " ";
for (int m=0; m<j.size();m++)
{
if ((j.get(m)).equals("*"))
{
n1 = Integer.parseInt(j.get(m-1));
n2 = Integer.parseInt(j.get(m+1));
f= n1*n2;
n = Integer.toString(f);
j.set(m,n);
j.remove(m+1);
j.remove(m-1);
m=0;
}
for (int e=0; e<j.size();e++)
{
if ((j.get(e)).equals("/"))
{
n1 = Integer.parseInt(j.get(e-1));
n2 = Integer.parseInt(j.get(e+1));
f= n1/n2;
n = Integer.toString(f);
j.set(e,n);
j.remove(e+1);
j.remove(e-1);
e=0;
}
}
}
System.out.println();
for (int i1 =0; i1<j.size();i1++)
{
System.out.print(j.get(i1)+",");
}
However, for adding and subtracting, since there isnt an order for adding and subtracting, just whichever comes first, I wrote the following:
int x1=0;
int x2=0;
int x3=0;
String z = " ";
for (int g=0; g<j.size();g++)
{
if ((j.get(g)).equals("+"))
{
x1= Integer.parseInt(j.get(g-1));
x2= Integer.parseInt(j.get(g+1));
x3= x1+x2;
z = Integer.toString(x3);
j.set(g,z);
j.remove(g+1);
j.remove(g-1);
g=0;
}
g=0;
if ((j.get(g)).equals("-"))
{
x1= Integer.parseInt(j.get(g-1));
x2= Integer.parseInt(j.get(g+1));
x3= x1-x2;
z = Integer.toString(x3);
j.set(g,z);
j.remove(g+1);
j.remove(g-1);
g=0;
}
g=0;
}
System.out.println();
for (int i1 =0; i1<j.size();i1++)
{
System.out.print(j.get(i1)+",");
}
After this, it prints:
25,+,1,-,8,+,25,–,2,
. What am I doing wrong? Multiplication and dividing seem to be working perfectly
You have 2 problems:
1) g=0; statements after if and else blocks will make you go into an infinite loop.
2) From the output you gave, the first minus (-) is Unicode character HYPHEN-MINUS (U+002D), while the second minus (–) is Unicode character EN DASH (U+2013), so (j.get(g)).equals("-") fails for the second minus as they are not equal.
Going for an answer that doesn't help with your exact specific problem, but that hopefully helps you much further than that.
On a first glance, there are various problems with your code:
Your are using super-short variable names all over the place. That saves you maybe 1 minute of typing overall; and costs you 5, 10, x minutes every time you read your code; or show it to other people. So: dont do that. Use names that say what the thing behind that name is about.
You are using a lot of low-level code. You use a "couting-for" loop to iterate a list (called j, that is really really horrible!) for example. Meaning: you make your code much more complicated to read than it ought to be.
In that way, it looks like nobody told you so far, but the idea of code is: it should be easy to read and understand. Probably you dont get grades for that, but believe me: in the long run, learning to write readable code is a super-important skill. If that got you curious, see if you can get a hand on "Clean code" by Robert Martin. And study that book. Then study it again. And again.
But the real problem is your approach to solve this problem. As I assume: this is some part of study assignment. And the next step will be that you don't have simple expressions such as "1+2*3"; but that you are asked to deal with something like "sqrt(2) + 3" and so on. Then you will be asked to add variables, etc. And then your whole approach breaks apart. Because your simple string operations won't do it any more.
In that sense: you should look into this question, and carefully study the 2nd answer by Boann to understand how to create a parser that dissects your input string into expressions that are then evaluated. Your code does both things "together"; thus making it super-hard to enhance the provided functionality.
You can use the built-in Javascript engine
public static void main(String[] args) throws Exception{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String code = "5*5+2/2-8+5*5-2";
System.out.println(engine.eval(code));
}
Primarily Don't Repeat Yourself (the DRY principle). And use abstractions (full names, extracting methods when sensible). Static methods are a bit cumbersome, when using several methods. Here it is handy to use separate methods.
Maybe you want something like:
Solver solver = new Solver();
List<String> expr = solver.expression("5*5+2/2-8+5*5-2");
String result = solver.solve(expr);
A more abstract Solver class would do:
class Solver {
List<String> expression(String expr) {
String[] args = expr.split("\\b");
List<String> result = new ArrayList<>();
Collections.addAll(result, args);
return result;
}
String solve(List<String> args) {
solveBinaryOps(args, "[*/]");
solveBinaryOps(args, "[-+]");
return args.stream().collect(Collectors.joining(""));
}
The above solveBinaryOps receives a regular expression pattern or alternatively simply in some form the operators you want to tackle.
It takes care of operator precedence.
private void solveBinaryOps(List<String> args, String opPattern) {
for (int i = 1; i + 1 < args.length; ++i) {
if (args.get(i).matches(opPattern)) {
String value = evalBinaryOp(args.get(i - 1), args.get(i), args.get(i + 1));
args.set(i, value);
args.remove(i + 1);
args.remove(i - 1);
--i; // Continue from here.
}
}
}
private String evalBinaryOp(String lhs, String op, String rhs) {
int x = Integer.parseInt(lhs);
int y = Integer.parseInt(rhs);
int z = 0;
switch (op) {
case "*":
z = x * y;
break;
case "/":
z = x / y;
break;
case "+":
z = x + y;
break;
case "-":
z = x - y;
break;
}
return Integer.toString(z);
}
}
The above can be improved at several points. But it is readable, and rewritable.
public class Solver {
public static void main(String args[]) {
operation("5+2*5-6/2+1+5*12/3");
}
public static void operation(String m) {
ArrayList<Object> expressions = new ArrayList<Object>();
String e;
String x = "";
for (int i = 0; i < m.length(); i++) {
e = m.substring(i, i + 1);
if (!(e.equals("*") || e.equals("/") || e.equals("+") || e
.equals("-"))) {
x += e;
continue;
} else {
if (!x.equals("") && x.matches("[0-9]+")) {
int oper = Integer.parseInt(x);
expressions.add(oper);
expressions.add(m.charAt(i));
x = "";
}
}
}
if (!x.equals("") && x.matches("[0-9]+")) {
int oper = Integer.parseInt(x);
expressions.add(oper);
x = "";
}
for (int i = 0; i < expressions.size(); i++) {
System.out.println(expressions.get(i));
}
evaluateExpression(expressions);
}
public static void evaluateExpression(ArrayList<Object> exp) {
//Considering priorities we calculate * and / first and put them in a list mulDivList
ArrayList<Object> mulDivList=new ArrayList<Object>();
for (int i = 0; i < exp.size(); i++) {
if (exp.get(i) instanceof Character) {
if ((exp.get(i)).equals('*')) {
int tempRes = (int) exp.get(i - 1) * (int) exp.get(i + 1);
exp.set(i - 1, null);
exp.set(i, null);
exp.set(i + 1, tempRes);
}
else if ((exp.get(i)).equals('/')) {
int tempRes = (int) exp.get(i - 1) / (int) exp.get(i + 1);
exp.set(i - 1, null);
exp.set(i, null);
exp.set(i + 1, tempRes);
}
}
}
//Create new list with only + and - operations
for(int i=0;i<exp.size();i++)
{
if(exp.get(i)!=null)
mulDivList.add(exp.get(i));
}
//Calculate + and - .
for(int i=0;i<mulDivList.size();i++)
{
if ((mulDivList.get(i)).equals('+')) {
int tempRes = (int) mulDivList.get(i - 1) + (int) mulDivList.get(i + 1);
mulDivList.set(i - 1, null);
mulDivList.set(i, null);
mulDivList.set(i + 1, tempRes);
}
else if ((mulDivList.get(i)).equals('-')) {
int tempRes = (int) mulDivList.get(i - 1) - (int) mulDivList.get(i + 1);
mulDivList.set(i - 1, null);
mulDivList.set(i, null);
mulDivList.set(i + 1, tempRes);
}
}
System.out.println("Result is : " + mulDivList.get(mulDivList.size() - 1));
}
}

Regarding time consuming calculations in java

I am trying to write a code for reading 120 files from a folder and performing some calculations on it. When i debug the code, it works fine, however, execution time is more than 20 mins, I am aware that this might be due to bug in the code. However, can someone look into it and suggest possible methods to reduce the execution time. Kindly let me know if I should provide further information. Thank You.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class myclass {
static int total = 1;
static int r = 0;
public static void main(String[] args) {
ArrayList<Double> mysignal = new ArrayList<Double>();
ArrayList<Double> mylist = new ArrayList<Double>();
double x;
double a;
myclass obj = new myclass();
String target_dir = "path for folder";
File dir = new File(target_dir);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String line;
while ((line = inputStream.readLine()) != null) {
System.out.println(line);
mysignal.add(Double.valueOf(line));
total++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a = obj.funtioneg(mysignal, total);
mylist.add(r, a);
System.out.println(mylist.get(r));
r++;
}
}
}
public double functioneg(ArrayList<Double> s, int N) {
ArrayList<Double> y = new ArrayList<Double>();
double sum = 0, a1 = 0;
double[] o1 = new double[N - 1];// processed signal
for (int n = 0; n < counter_main - 1; n++) {
for (int k = 0; k < 40; k++) {
if (n - k >= 0) {
a1 = s.get(n - k);
sum = sum + (a1 * a1);// energy
} else
sum = sum + 0;
}
o1[n] = sum;
sum = 0;
}
double sum1 = 0;
double avg;
for (int t = 0; t < counter_main - 1; t++) {
sum1 = sum1 + o1[t];
}
avg = sum1 / N - 1;
return (avg);
}
}
You need to close your InputStream
After reading each file in the directory (after your try - catch block) write the statement:
inputStream.close();
As andrewdleach pointed out, you should close your input stream.
Additionally you might want to try out the Java 8 function Files#walk (see this question) for more efficiently walking through the files.
First try to comment out the line:
System.out.println(line);
The output to console is slow (and I mean really slow), this line is basically duplicating the contents of each processed file to the console.
Other than that, you can as well try to accumulate time spent in the functioneq() method and/or parts of it (for example using System.nanoTime()) to find the most time consuming parts (or run under debugger and use profiling by sampling, which is the easiest profiling method and surprisingly effective - just pause the program repeatedly and see where it paused most frequently).

Octave dfdp.m for JAVA

I want to implement Levenberg Marquardt fitting in JAVA and found apache commons math suitable. Since I want to fit a function, where I dont have the derivative to calculate the gradient or Jacobian, I need somthing like dfdp.m from GNU octave to calculate numerical derivatives. Has someone done this already?
I did it myself, in case someone else needs it here is the approach
dfdp.m code
m=size(x,1); if (m==1), m=size(x,2); end %# PAK: in case #cols > #rows
n=length(p); %dimensions
ps=p; prt=zeros(m,n);del=zeros(n,1); % initialise Jacobian to Zero
for j=1:n
del(j)=dp(j) .*p(j); %cal delx=fract(dp)*param value(p)
if p(j)==0
del(j)=dp(j); %if param=0 delx=fraction
end
p(j)=ps(j) + del(j);
if del(j)~=0, f1=feval(func,x,p); %FJ ~= not equal (!=) ...> p is now (p + dp*p)
if dp(j) < 0, prt(:,j)=(f1-f)./del(j);
else
p(j)=ps(j) - del(j); %FJ ...> p is now (p - dp*p)
prt(:,j)=(f1-feval(func,x,p))./(2 .*del(j)); %FJ 2 steps from (ps + del) to (ps - del)
end
end
p(j)=ps(j); %restore p(j)
end
JAVA code
private static class GhoosProblem {
private double[][] data;
private double[] dp;
public GhoosProblem(double[][] datapoints, double[] delta_p) {
data = datapoints;
//dp= fractional increment of p for numerical derivatives
//dp(j)>0 central differences calculated
//dp(j)<0 one sided differences calculated
//dp(j)=0 sets corresponding partials to zero; i.e. holds p(j) fixed
dp = delta_p;
}
public MultivariateVectorFunction getModelFunction() {
return new MultivariateVectorFunction() {
public double[] value(double[] params) {
double[] values = new double[data.length];
for (int i = 0; i < values.length; ++i) {
final double t = data[i][0]; // get the double value
values[i] = params[0] *
Math.pow(t, params[2]) *
Math.exp(-params[1] * t); // Ghoos function
}
return values; // function values
}
};
}
public MultivariateMatrixFunction getModelFunctionJacobian2() {
return new MultivariateMatrixFunction() {
public double[][] value(double[] params) {
double[][] jacobian = new double[data.length][params.length];
final double a = params[0];
final double b = params[2];
final double c = params[1];
for (int i = 0; i < jacobian.length; ++i) {
final double t = data[i][0]; // get the double value
jacobian[i][0] = Math.pow(t, b) * Math.exp(-c*t);
jacobian[i][2] = a * Math.exp(-c*t) * Math.pow(t, b) * Math.log(t);
jacobian[i][1] = a * Math.pow(t, b) * (-t*Math.exp(-c*t));
}
//System.out.println("Jacobian= "+ Arrays.deepToString(jacobian));
return jacobian;
}
};
}
// compared to Ge2.m octave
public MultivariateMatrixFunction getModelFunctionJacobian() {
return new MultivariateMatrixFunction() {
public double[][] value(double[] params) {
int m = data.length; // cols
int n = params.length; // rows
double[] p = params;
double[] ps = params;
double[] del = new double[n];
double[] f = new double[n];
double[] f1 = new double[n];
BlockRealMatrix prt = new BlockRealMatrix(m, n); // initializes to zeros
f=feval(p);
for (int j=0; j<n; ++j) {
del[j]=dp[j] * p[j]; //delta_x=fractional(dp) * param value(p)
if (p[j]==0)
del[j]=dp[j]; //if param=0 delta_x=fractional(dp)
p[j]=ps[j] + del[j];
if (del[j]!=0) {
f1=feval(p); //p is now (p + dp*p)
if (dp[j]<0)
prt.setColumn(j,(new ArrayRealVector(f1)).subtract(new ArrayRealVector(f)).mapDivideToSelf(del[j]).toArray()); // one sided diff
else {
p[j]=ps[j] - del[j]; // p is now (p - dp*p)
prt.setColumn(j,(new ArrayRealVector(f1)).subtract(new ArrayRealVector(feval(p))).mapDivideToSelf(2*del[j]).toArray()); // central diff
}
}
p[j]=ps[j]; //restore p(j)
}//for
//System.out.println("Jacobian= "+ Arrays.deepToString(prt.getData()));
return prt.getData(); //jacobian, dimension is (m x n)
}
};
}
public double[] feval(double[] params) {
double[] values = new double[data.length];
for (int i = 0; i < values.length; ++i) {
final double t = data[i][0]; // get the double value
values[i] = params[0] *
Math.pow(t, params[2]) *
Math.exp(-params[1] * t); // Ghoos function
}
return values;
}
}//GhoosProblem
sorry if idention of code did not come out nice!
the relevant part is the getModelFunctionJacobian() -Function
I have renamed the analytical derivatives part as getModelFunctionJacobian2(), and posted here for comparison
to complete with here is the levenberg marquardt setup to use the GhoosFunction
public void fit() {
final double[][] dataPoints = { // x, y
//{0.0/60, 0.0}, // never use {0, 0} => org.apache.commons.math3.exception.ConvergenceException: illegal state: unable to perform Q.R decomposition on the 17x3 jacobian matrix
{15.0/60, 8.891104},
{30.0/60, 13.21852},
{45.0/60, 28.09051},
{60.0/60, 43.0011},
{75.0/60, 57.43561},
{90.0/60, 67.06862},
{105.0/60, 82.60239},
{120.0/60, 72.4649},
{135.0/60, 61.4},
{150.0/60, 43.97924},
{165.0/60, 30.6},
{180.0/60, 20.77112},
{195.0/60, 15.5},
{210.0/60, 10.85442},
{225.0/60, 9.33},
{240.0/60, 7.260234},
};
final double[] initialGuess = { 1.0, 1.0, 1.0 }; // p
final double[] fract_change = { 1E-4, 1E-4, 1E-4 }; // dp should be below 0.0001
final GhoosProblem problem = new GhoosProblem(dataPoints, fract_change);
final int len = dataPoints.length;
final double[] weights = new double[len];
final double[] target = new double[len];
for (int i = 0; i < len; i++){
weights[i] = 1.0;// / dataPoints[i][1];
target[i] = dataPoints[i][1];
}
final LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer()
.withCostRelativeTolerance(1E-4) // stol in octave
.withParameterRelativeTolerance(1E-4); // dp should be below 0.0001
final Optimum optimum = optimizer.optimize(
builder(problem)
.weight(new DiagonalMatrix(weights))
.target(target)
.start(initialGuess)
.maxIterations(100)
.build()
);
final RealVector solution = optimum.getPoint();
solution.setEntry(0, solution.getEntry(0) / 60.0); // go back to minutes
System.out.println("solution= " + solution);
System.out.println("CostRelativeTolerance= " + optimizer.getCostRelativeTolerance());
System.out.println("ParameterRelativeTolerance= " + optimizer.getParameterRelativeTolerance());
System.out.println("evaluations= " + optimum.getEvaluations());
System.out.println("iterations= " + optimum.getIterations());
//System.out.println("residuals= " + optimum.getResiduals());
System.out.println("RMS= " + optimum.getRMS());
System.out.println("sigma= " + optimum.getSigma(1E-10));
}//fit
public LeastSquaresBuilder builder(GhoosProblem problem){
return new LeastSquaresBuilder()
.checkerPair(new SimpleVectorValueChecker(1e-6, 1e-6)) // The SimpleVectorValueChecker Class (Simple implementation of the ConvergenceChecker) contains a method that uses the value of the function between two successive iterations of the optimisation algorithm to check if convergence has occured
.maxEvaluations(Integer.MAX_VALUE)
.maxIterations(Integer.MAX_VALUE)
//.lazyEvaluation(true)
.model(problem.getModelFunction(), problem.getModelFunctionJacobian());
}

Categories

Resources