public void run() { for (int i = start; i <= end; i++) { for (int j = i + 1; j < cc.getNodeNumber(); j++) { // double distance = calculateEuclideanDistance(cc.getCCPositions(), dim, i,j); double distance = 0; double dummy = 0; for (int d = 0; d < dim; d++) { dummy = (cc.getCCPositions()[i][d] - cc.getCCPositions()[j][d]); distance += (dummy * dummy); } distance = Math.sqrt(distance); if (distance < FORCEnDLayoutConfig.MIN_DISTANCE) continue; /* * calculate attraction or repulsion force * * attraction: * log(d(i,j)+1) x cost(i,j) x attraction factor * --------------------------------------------- * number of nodes * * repulsion: * cost(i,j) x repulsion factor * ------------------------------- * log(d(i,j)+1) x number of nodes * */ if (cc.getCCEdges().getEdgeCost(i, j) > 0) { double force = (Math.log(distance + 1) * cc.getCCEdges().getEdgeCost(i, j) * attraction) / distance; for (int d = 0; d < dim; d++) { double displacement = (cc.getCCPositions()[j][d] - cc.getCCPositions()[i][d]) * force; synchronized (allDisplacements) { allDisplacements[i][d] += displacement; allDisplacements[j][d] -= displacement; } } } else { double force = ((cc.getCCEdges().getEdgeCost(i, j) * repulsion) / Math.log(distance + 1)) / distance; for (int d = 0; d < dim; d++) { double displacement = (cc.getCCPositions()[j][d] - cc.getCCPositions()[i][d]) * force; synchronized (allDisplacements) { allDisplacements[i][d] += displacement; allDisplacements[j][d] -= displacement; } } } } } }
/** * Calculates the displacement vector for all nodes and saves it in a 2D double array. * * @param allDisplacements The displacement values for all nodes. * @param cc The current ConnectedComponent object. * @param dim The current dimensions the layouting is run in. * @param param The parameters object for FORCEnD. * @param temperature */ public static void calculateDisplacementVectors( double[][] allDisplacements, ConnectedComponent cc, int dim, FORCEnDParameters param, double temperature) { setDisplacementsToZero(allDisplacements, cc.getNodeNumber(), dim); double attraction = param.getAttractionFactor() / cc.getNodeNumber(); double repulsion = param.getRepulsionFactor() / cc.getNodeNumber(); int[] list = new int[cc.getNodeNumber()]; if (TaskConfig.useThreads) { // if(false){ ExecutorService es = java.util.concurrent.Executors.newFixedThreadPool(TaskConfig.maxNoThreads); for (int i = 0; i < TaskConfig.maxNoThreads; i++) { DisplacementCalculator s = new DisplacementCalculator( attraction, repulsion, allDisplacements, cc, dim, list, (int) Math.rint((double) cc.getNodeNumber() / TaskConfig.maxNoThreads) * i, Math.min( (int) Math.rint((double) cc.getNodeNumber() / TaskConfig.maxNoThreads) * (i + 1) - 1, cc.getNodeNumber() - 1)); es.execute(s); } // for (int i = 0; i < cc.getNodeNumber(); i++) { // int start = i; // int end = i; // int estimatedOperation = cc.getNodeNumber()-i; // // while(estimatedOperation<((cc.getNodeNumber()*(cc.getNodeNumber()-1))/2)/TaskConfig.maxNoThreads){ // if(i==cc.getNodeNumber()) break; // end++; // i++; // estimatedOperation+=cc.getNodeNumber()-i; // } // DisplacementCalculator s = new // DisplacementCalculator(attraction,repulsion,allDisplacements,cc,dim,list,start,end); // es.execute(s); // } es.shutdown(); try { es.awaitTermination(5, java.util.concurrent.TimeUnit.HOURS); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { double distance, force, displacement; for (int i = 0; i < cc.getNodeNumber(); i++) { /* * only need to calculate the forces for j<i, because force(i,j) = * force(j,i) * if it should at some stage not be the case, then change * this! */ for (int j = i + 1; j < cc.getNodeNumber(); j++) { distance = calculateEuclideanDistance(cc.getCCPositions(), dim, i, j); if (distance < FORCEnDLayoutConfig.MIN_DISTANCE) continue; /* * calculate attraction or repulsion force * * attraction: * log(d(i,j)+1) x cost(i,j) x attraction factor * --------------------------------------------- * number of nodes * * repulsion: * cost(i,j) x repulsion factor * ------------------------------- * log(d(i,j)+1) x number of nodes * */ if (cc.getCCEdges().getEdgeCost(i, j) > 0) { force = (Math.log(distance + 1) * cc.getCCEdges().getEdgeCost(i, j) * attraction) / distance; for (int d = 0; d < dim; d++) { displacement = (cc.getCCPositions()[j][d] - cc.getCCPositions()[i][d]) * force; allDisplacements[i][d] += displacement; allDisplacements[j][d] -= displacement; } } else { force = ((cc.getCCEdges().getEdgeCost(i, j) * repulsion) / Math.log(distance + 1)) / distance; for (int d = 0; d < dim; d++) { displacement = (cc.getCCPositions()[j][d] - cc.getCCPositions()[i][d]) * force; allDisplacements[i][d] += displacement; allDisplacements[j][d] -= displacement; } } } } } // }