public int predictState(int from_state, int number_of_steps) { long lStartTime = System.currentTimeMillis(); int from_bin = getBinNumber((float) from_state); SimpleMatrix result = new SimpleMatrix(transition_matrix); for (int i = 1; i < number_of_steps; i++) result = result.mult(transition_matrix); SimpleMatrix row_result = result.extractVector(true, from_bin); // row_result.print(); double maxVal = row_result.get(0, 0); int maxValIndex = 0; for (int i = 1; i < row_result.numCols(); i++) { if (row_result.get(0, i) >= maxVal) { maxVal = row_result.get(0, i); maxValIndex = i; } } long lEndTime = System.currentTimeMillis(); logger.info( ("From: " + from_state + " In NumberOfSteps: " + number_of_steps + " MaxProbabilityBin: " + maxValIndex + " MostProbablyTo: " + getOrgValFromBinNumber(maxValIndex))); logger.info(("Time taken for prediction = " + (lEndTime - lStartTime))); return getOrgValFromBinNumber(maxValIndex); }
// ecuatia Rosenbrock // x(i) is coded with 8 bits public double fitness(SimpleMatrix cromosom) { int value = 0; int n = cromosom.numRows(); double x1, x2; for (int i = 0; i < n - 1; i++) { x1 = cromosom.get(i); x2 = cromosom.get(i + 1); value += (100 * Math.pow(x2 - Math.pow(x1, 2), 2) + Math.pow(x1 - 1, 2)); } return value; }
public static List<Cluster> ReassignCentrids( List<Cluster> kCentroids, SimpleMatrix distanceMatrix, SimpleMatrix dataSet, int[] featureSet) { List<Cluster> kCentroids_l = kCentroids; int[] clusterLoc = new int[dataSet.numRows()]; for (int iRows = 0; iRows < dataSet.numRows(); iRows++) { int clusterNo = 1; double minvalue = distanceMatrix.get(iRows, 1); for (int iCentroid = 0; iCentroid < kCentroids_l.size(); iCentroid++) { // System.out.println(iRows+" "+iCentroid); if (distanceMatrix.get(iRows, iCentroid) < minvalue) { clusterNo = iCentroid; minvalue = distanceMatrix.get(iRows, iCentroid); } } clusterLoc[iRows] = clusterNo; } // Backup Centroids anc clear current centroids for (int i = 0; i < kCentroids_l.size(); i++) { kCentroids_l.get(i).backup(); kCentroids_l.get(i).noPoints = 0; Arrays.fill(kCentroids_l.get(i).currPoints, -1); Arrays.fill(kCentroids_l.get(i).intIndex, -1); // System.out.println("Clusters Backed Up!"); } // printKCentroids(kCentroids_l); for (int i = 0; i < clusterLoc.length; i++) { int insLoc = kCentroids_l.get(clusterLoc[i]).noPoints; // System.out.println("Getting element"+dataSet.get(i, 0)); kCentroids_l.get(clusterLoc[i]).currPoints[insLoc] = (int) dataSet.get(i, 0); kCentroids_l.get(clusterLoc[i]).intIndex[insLoc] = i; kCentroids_l.get(clusterLoc[i]).noPoints++; } // System.out.println(Arrays.toString(clusterLoc)); // Now Calculate the best representative and give as new centroid values for (int i = 0; i < kCentroids.size(); i++) { for (int j = 0; j < featureSet.length; j++) { double tempAvg = 0; int[] travVector = kCentroids_l.get(i).intIndex; for (int k = 0; k < travVector.length; k++) { if (travVector[k] != -1) { tempAvg = tempAvg + dataSet.get(travVector[k], featureSet[j]); } } kCentroids.get(i).clusterCenter.set(0, featureSet[j], tempAvg / kCentroids.get(i).noPoints); } } return kCentroids_l; }
@Override public double[][] predict(List<PredictionPaper> testDocs) { String testData = "lda/test.dat"; createLdaInputTest(testData, testDocs); Utils.runCommand( "lib/lda-c-dist/lda inf " + " lib/lda-c-dist/settings.txt " + "lda/final " + testData + " lda/output", false); double[][] gammasMatrix = Utils.readMatrix("lda/output-gamma.dat", false); double alpha = Utils.readAlpha("lda/final.other"); for (int i = 0; i < gammasMatrix.length; i++) { for (int j = 0; j < gammasMatrix[i].length; j++) { gammasMatrix[i][j] -= alpha; } } SimpleMatrix gammas = new SimpleMatrix(gammasMatrix); SimpleMatrix beta = new SimpleMatrix(betaMatrix); SimpleMatrix probabilities = gammas.mult(beta); double[][] result = new double[probabilities.numRows()][probabilities.numCols()]; for (int row = 0; row < probabilities.numRows(); row++) { for (int col = 0; col < probabilities.numCols(); col++) { result[row][col] = probabilities.get(row, col); } } return result; }
// ecuatia sferei // sum from i=1 to m of x(i)^2 // x(i) is coded with 8 bits public double fitness(SimpleMatrix cromosom) { int value = 0; int n = cromosom.numCols(); for (int i = 0; i < n; i++) { value += Math.pow(cromosom.get(i), 2); } return value; }
public void trainMarkovChainModel(double[] input) { int input_size = input.length; int from_state, to_state; Integer[] count = new Integer[num_of_states]; for (int i = 0; i < num_of_states; i++) count[i] = 0; // Loop through the input and record the number of transitions for (int i = 0; i < input_size - 2; i++) { from_state = getBinNumber(input[i]); to_state = getBinNumber(input[i + 1]); // Increment entry by 1 // transition_matrix[from_state][to_state]++; transition_matrix.set(from_state, to_state, transition_matrix.get(from_state, to_state) + 1); count[from_state]++; // COMMENT THIS logger.info( "FromVal:" + input[i] + " FromBin: " + from_state + " ToVal:" + input[i + 1] + " ToBin: " + to_state + " TransitionCount: " + transition_matrix.get(from_state, to_state) + " TotalCount: " + count[from_state]); } // Calculate the transition probability matrix for (int i = 0; i < num_of_states; i++) { for (int j = 0; j < num_of_states; j++) { if (count[i] == 0) { // transition_matrix[i][j] transition_matrix.set(i, j, 0); } else { // transition_matrix[i][j]/=count[i]; transition_matrix.set(i, j, transition_matrix.get(i, j) / count[i]); } } } }
// ecuatia Rastrigin // x(i) is coded with 8 bits public double fitness(SimpleMatrix cromosom) { int value = 0; int n = cromosom.numCols(); double x1; for (int i = 0; i < n; i++) { x1 = cromosom.get(i); value += (x1 * x1 - 10 * Math.cos(Math.PI * x1) + 10); } return value; }
// ecuatia Griewank // x(i) is coded with 8 bits public double fitness(SimpleMatrix cromosom) { int sum = 0, product = 1; int n = cromosom.numCols(); double x1; for (int i = 0; i < n; i++) { x1 = cromosom.get(i); sum += x1 * x1; product *= Math.cos(x1 / Math.sqrt(i + 1)); } return 1 / 4000 * sum - product + 1; }
public static boolean detectConvergence( List<Cluster> kCentroids, int[] featureSet, double tolerance) { int iCentroids = kCentroids.size(); boolean toReturn = true; for (int i = 0; i < iCentroids; i++) { toReturn = true; SimpleMatrix current = kCentroids.get(i).clusterCenter.copy(); SimpleMatrix previous = kCentroids.get(i).clusterPrevious.copy(); for (int j = 0; j < featureSet.length; j++) { toReturn = true; // System.out.println(current.get(0, featureSet[j])+" "+previous.get(0, featureSet[j])+" // "+Math.abs(Math.abs(current.get(0, featureSet[j])) - Math.abs(previous.get(0, // featureSet[j])))+" "+tolerance*current.get(0, featureSet[j])); if (Math.abs( Math.abs(current.get(0, featureSet[j])) - Math.abs(previous.get(0, featureSet[j]))) > (tolerance * current.get(0, featureSet[j]))) { kCentroids.get(i).hasChanged = true; // System.out.println("Changes are there"); break; } else { kCentroids.get(i).hasChanged = false; } // System.out.println(kCentroids.get(i).hasChanged); } // System.out.println(kCentroids.get(i).hasChanged); // if(kCentroids.get(i).hasChanged=true) break; } // Check if I for true for (int j = 0; j < iCentroids; j++) { // System.out.println(kCentroids.get(j).hasChanged); if (kCentroids.get(j).hasChanged) { toReturn = false; break; } else { toReturn = true; } } // System.out.println("To reyurn"+toReturn); return toReturn; }
public Vector transform(Vector point) { double tempX = (matrix.get(0, 0) * point.X()) + (matrix.get(1, 0) * point.Y() + matrix.get(2, 0)); double tempY = (matrix.get(0, 1) * point.X()) + (matrix.get(1, 1) * point.Y() + matrix.get(2, 1)); return new Vector(tempX, tempY); }
/** Outputs the scores from the tree. Counts the tree nodes the same as setIndexLabels. */ static int outputTreeScores(PrintStream out, Tree tree, int index) { if (tree.isLeaf()) { return index; } out.print(" " + index + ":"); SimpleMatrix vector = RNNCoreAnnotations.getPredictions(tree); for (int i = 0; i < vector.getNumElements(); ++i) { out.print(" " + NF.format(vector.get(i))); } out.println(); index++; for (Tree child : tree.children()) { index = outputTreeScores(out, child, index); } return index; }
public static SimpleMatrix compDist( List<Cluster> kCentroids, SimpleMatrix dataSet, int[] featureSet, String distanceMetric) { int dRows = dataSet.numRows(); int dCols = kCentroids.size(); int[] features = featureSet; SimpleMatrix distMatrix = new SimpleMatrix(dRows, dCols); for (int iCentroid = 0; iCentroid < dCols; iCentroid++) { Cluster kcenter = kCentroids.get(iCentroid); for (int iRows = 0; iRows < dRows; iRows++) { double distTemp = 0; for (int iFeature = 0; iFeature < features.length; iFeature++) { double cX = kcenter.clusterCenter.get(0, features[iFeature]); double dX = dataSet.get(iRows, features[iFeature]); distTemp = distTemp + Math.pow(cX - dX, 2); } distMatrix.set(iRows, iCentroid, Math.sqrt(distTemp)); } } return distMatrix; }
public static void main(String[] args) { try { if (args.length < 5) { System.out.println( "Invalid Syntax usage. \n The Syntax is KMeans inputfile distance-metric #Centroids #Iterations Tolerance featureSet crossValidation testDataSet"); } String inputFile = args[0]; String distanceMetric = args[1]; int centroids = Integer.parseInt(args[2]); int iterations = Integer.parseInt(args[3]); double tolerance = Double.parseDouble(args[4]); String inpFeatures = args[5]; String crossValidation = args[6]; String testFile = args[7]; String[] temp = inpFeatures.split(","); int[] featureSet = new int[temp.length]; boolean hasConvered = false; for (int i = 0; i < temp.length; i++) { featureSet[i] = Integer.parseInt(temp[i]); } System.out.println("Features Considered are" + Arrays.toString(featureSet)); SimpleMatrix dataSet = new SimpleMatrix().loadCSV(inputFile); // Cluster Parameters int dsRows = dataSet.numRows(); int dsCol = dataSet.numCols(); // Cluster Initialization List<Cluster> kcentroids = new ArrayList<Cluster>(); for (int i = 0; i < centroids; i++) { int random = genRandom(0, dsRows - 1); int[] currPoints = new int[dsRows]; int[] intIndex = new int[dsRows]; // This will have remnance of the first chosen element Random r = new Random(); SimpleMatrix t = new SimpleMatrix().random(1, dsCol, 0, 0, r); Cluster centers = new Cluster(i, dataSet.extractVector(true, random), t, currPoints, intIndex); kcentroids.add(centers); } // printKCentroids(kcentroids); // dataSet.print(); SimpleMatrix distMatrix = compDist(kcentroids, dataSet, featureSet, distanceMetric); // distMatrix.print(); // System.out.println(distMatrix.get(0, 1)); // ReassignCentrids(kcentroids, distMatrix, dataSet,featureSet); // printKCentroids(kcentroids); for (int k = 0; k < iterations; k++) { System.out.println("------------------------Iteration " + k + " ------------------------"); distMatrix = compDist(kcentroids, dataSet, featureSet, distanceMetric); // distMatrix.print(); ReassignCentrids(kcentroids, distMatrix, dataSet, featureSet); printKCentroids(kcentroids); hasConvered = detectConvergence(kcentroids, featureSet, tolerance); // System.out.println(hasConvered); if (hasConvered) { System.out.println( "------------------------Has Converged : Tolerance------------------------"); break; } } if (!hasConvered) System.out.println( "------------------------Has Converged : Iterations------------------------"); if (crossValidation.equals("true")) { SimpleMatrix test = new SimpleMatrix().loadCSV(testFile); SimpleMatrix dist = compDist(kcentroids, test, featureSet, distanceMetric); System.out.println("----------Associating Set Data Sets to Calculated Centroid-------"); // dist.print(); List<Cluster> kCentroids_l = kcentroids; int[] clusterLoc = new int[test.numRows()]; for (int iRows = 0; iRows < test.numRows(); iRows++) { int clusterNo = 1; double minvalue = dist.get(iRows, 1); for (int iCentroid = 0; iCentroid < kCentroids_l.size(); iCentroid++) { // System.out.println(iRows+" "+iCentroid); if (dist.get(iRows, iCentroid) < minvalue) { clusterNo = iCentroid; minvalue = dist.get(iRows, iCentroid); } } clusterLoc[iRows] = clusterNo; } // System.out.println(Arrays.toString(clusterLoc)); for (int i = 0; i < kCentroids_l.size(); i++) { kCentroids_l.get(i).backup(); kCentroids_l.get(i).noPoints = 0; Arrays.fill(kCentroids_l.get(i).currPoints, -1); Arrays.fill(kCentroids_l.get(i).intIndex, -1); // System.out.println("Clusters Backed Up!"); } // printKCentroids(kCentroids_l); for (int i = 0; i < clusterLoc.length; i++) { int insLoc = kCentroids_l.get(clusterLoc[i]).noPoints; // System.out.println("Getting element"+dataSet.get(i, 0)); kCentroids_l.get(clusterLoc[i]).currPoints[insLoc] = (int) test.get(i, 0); kCentroids_l.get(clusterLoc[i]).intIndex[insLoc] = i; kCentroids_l.get(clusterLoc[i]).noPoints++; } printKCentroids(kCentroids_l); } } catch (Exception e) { System.out.println("Unfortunately There is an error."); e.printStackTrace(); } }
public double fitness(SimpleMatrix cromosom) { return Math.sin((Math.PI * cromosom.get(0)) / 256); }