/** * Initializes the matrix */ public void initialize() { m_matrix = new Object[m_size][m_size]; for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { setCell(i, j, i == j ? new Double(0.0) : new Double(1.0)); } } }
/** * Initializes the matrix */ public void initialize() { m_matrix = new Object[m_size][m_size]; for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { setCell(i, j, i == j ? new Double(0.0) : new Double(1.0)); } } }
/** * Sets a value at a specified position in the cost matrix. * * @param aValue the new value (should be of type Double). * @param rowIndex the row position * @param columnIndex the column position */ @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) { // double value = ((Double) aValue).doubleValue(); // m_matrix.setElement(rowIndex, columnIndex, value); // try to parse it as a double first Double val; try { val = new Double(((String) aValue)); } catch (Exception ex) { val = null; } if (val == null) { m_matrix.setCell(rowIndex, columnIndex, aValue); } else { m_matrix.setCell(rowIndex, columnIndex, val); } fireTableCellUpdated(rowIndex, columnIndex); }
/** * Sets a value at a specified position in the cost matrix. * * @param aValue the new value (should be of type Double). * @param rowIndex the row position * @param columnIndex the column position */ @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) { // double value = ((Double) aValue).doubleValue(); // m_matrix.setElement(rowIndex, columnIndex, value); // try to parse it as a double first Double val; try { val = new Double(((String) aValue)); } catch (Exception ex) { val = null; } if (val == null) { m_matrix.setCell(rowIndex, columnIndex, aValue); } else { m_matrix.setCell(rowIndex, columnIndex, val); } fireTableCellUpdated(rowIndex, columnIndex); }
/** * Normalizes the matrix so that the diagonal contains zeros. * */ public void normalize() { for (int y = 0; y < m_size; y++) { double diag = ((Double) getCell(y, y)).doubleValue(); for (int x = 0; x < m_size; x++) { setCell(x, y, new Double(((Double) getCell(x, y)).doubleValue() - diag)); } } }
/** * Normalizes the matrix so that the diagonal contains zeros. * */ public void normalize() { for (int y = 0; y < m_size; y++) { double diag = ((Double) getCell(y, y)).doubleValue(); for (int x = 0; x < m_size; x++) { setCell(x, y, new Double(((Double) getCell(x, y)).doubleValue() - diag)); } } }
/** * Creates a cost matrix that is a copy of another. * * @param toCopy the matrix to copy. */ public CostMatrix(CostMatrix toCopy) { this(toCopy.size()); for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { setCell(i, j, toCopy.getCell(i, j)); } } }
/** * Get a 2 class cost matrix with the supplied off diagonal costs set * * @param cost1 first cost value * @param cost2 second cost value * @return */ protected CostMatrix get2ClassCostMatrixNoExpressions(double cost1, double cost2) { CostMatrix matrix = new CostMatrix(2); matrix.setCell(0, 1, cost1); matrix.setCell(1, 0, cost2); return matrix; }
/** * Creates a cost matrix that is a copy of another. * * @param toCopy the matrix to copy. */ public CostMatrix(CostMatrix toCopy) { this(toCopy.size()); for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { setCell(i, j, toCopy.getCell(i, j)); } } }
/** * Get a 2 class cost matrix with the supplied off diagonal costs set * * @param cost1 first cost value * @param cost2 second cost value * @return */ protected CostMatrix get2ClassCostMatrixNoExpressions(double cost1, double cost2) { CostMatrix matrix = new CostMatrix(2); matrix.setCell(0, 1, cost1); matrix.setCell(1, 0, cost2); return matrix; }
for (int j = 0; j < matrix.size(); j++) { for (int k = 0; k < matrix.size(); k++) { matrix.setCell(j, k, m[j][k]);
for (int j = 0; j < matrix.size(); j++) { for (int k = 0; k < matrix.size(); k++) { matrix.setCell(j, k, m[j][k]);
private boolean replaceStrings(Instances dataset) throws Exception { boolean nonDouble = false; for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { if (getCell(i, j) instanceof String) { setCell(i, j, new InstanceExpression((String) getCell(i, j), dataset)); nonDouble = true; } else if (getCell(i, j) instanceof InstanceExpression) { nonDouble = true; } } } return nonDouble; }
private boolean replaceStrings(Instances dataset) throws Exception { boolean nonDouble = false; for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { if (getCell(i, j) instanceof String) { setCell(i, j, new InstanceExpression((String) getCell(i, j), dataset)); nonDouble = true; } else if (getCell(i, j) instanceof InstanceExpression) { nonDouble = true; } } } return nonDouble; }
/** * Tests a 2 class cost matrix with two expressions * * @throws Exception if a problem occurs */ public void test2ClassCostMatrixTwoExpressions() throws Exception { CostMatrix matrix = get2ClassCostMatrixNoExpressions(2, 6); matrix.setCell(0, 1, "log(a2^2)*a1-1"); matrix.setCell(1, 0, "exp(a1*cos(a2))/sqrt(a1/2)"); Instances data = getData(); Instances weighted = matrix.applyCostMatrix(data, null); assertEquals(-4.218876, weighted.instance(0).weight(), 1e-6); assertEquals(0.241157, weighted.instance(1).weight(), 1e-6); }
/** * Tests a 2 class cost matrix with two expressions * * @throws Exception if a problem occurs */ public void test2ClassCostMatrixTwoExpressions() throws Exception { CostMatrix matrix = get2ClassCostMatrixNoExpressions(2, 6); matrix.setCell(0, 1, "log(a2^2)*a1-1"); matrix.setCell(1, 0, "exp(a1*cos(a2))/sqrt(a1/2)"); Instances data = getData(); Instances weighted = matrix.applyCostMatrix(data, null); assertEquals(-4.218876, weighted.instance(0).weight(), 1e-6); assertEquals(0.241157, weighted.instance(1).weight(), 1e-6); }
/** * Tests a 2 class cost matrix with one simple expression * * @throws Exception if a problem occurs */ public void test2ClassCostMatrixOneSimpleExpression() throws Exception { CostMatrix matrix = get2ClassCostMatrixNoExpressions(2, 6); matrix.setCell(0, 1, "a2"); Instances data = getData(); Instances weighted = matrix.applyCostMatrix(data, null); assertEquals(5.0, weighted.instance(0).weight()); assertEquals(6.0, weighted.instance(1).weight()); }
/** * Tests a 2 class cost matrix with one more complex expression * * @throws Exception if a problem occurs */ public void test2ClassCostMatrixOneExpression() throws Exception { CostMatrix matrix = get2ClassCostMatrixNoExpressions(2, 6); matrix.setCell(0, 1, "log(a2^2)*a1-1"); Instances data = getData(); Instances weighted = matrix.applyCostMatrix(data, null); assertEquals(-4.218876, weighted.instance(0).weight(), 1e-6); assertEquals(6.0, weighted.instance(1).weight()); }
/** * Tests a 2 class cost matrix with one simple expression * * @throws Exception if a problem occurs */ public void test2ClassCostMatrixOneSimpleExpression() throws Exception { CostMatrix matrix = get2ClassCostMatrixNoExpressions(2, 6); matrix.setCell(0, 1, "a2"); Instances data = getData(); Instances weighted = matrix.applyCostMatrix(data, null); assertEquals(5.0, weighted.instance(0).weight()); assertEquals(6.0, weighted.instance(1).weight()); }
/** * Tests a 2 class cost matrix with one more complex expression * * @throws Exception if a problem occurs */ public void test2ClassCostMatrixOneExpression() throws Exception { CostMatrix matrix = get2ClassCostMatrixNoExpressions(2, 6); matrix.setCell(0, 1, "log(a2^2)*a1-1"); Instances data = getData(); Instances weighted = matrix.applyCostMatrix(data, null); assertEquals(-4.218876, weighted.instance(0).weight(), 1e-6); assertEquals(6.0, weighted.instance(1).weight()); }