package dml.test.integration.applications;

import org.junit.Test;

import java.util.HashMap;

import dml.runtime.matrix.io.MatrixValue.CellIndex;
import dml.test.integration.AutomatedTestBase;
import dml.test.integration.TestConfiguration;
import dml.test.utils.TestUtils;

public class L2SVMTest extends AutomatedTestBase {
	private final static String TEST_DIR = "applications/l2svm/";
	private final static String TEST_L2SVM = "L2SVM";

	@Override
	public void setUp() {
		setUpBase();
    	addTestConfiguration(TEST_L2SVM, new TestConfiguration(TEST_DIR, "L2SVM",
                new String[] { "w" }));
	}
	
	@Test
    public void testL2SVM(){
		int rows = 1000;
        int cols = 100;
        int lambda = 1;
       
        /* this is old way of running the test by passing the variable over to the dml script */
        TestConfiguration config = getTestConfiguration(TEST_L2SVM);
        config.addVariable("rows", rows);
        config.addVariable("cols", cols);
        config.addVariable("eps", Math.pow(10, -8));
        config.addVariable("lambda", lambda);
      
        /* This is for running the junit test by constructing the arguments directly */
		String L2SVM_HOME = SCRIPT_DIR + TEST_DIR;
		dmlArgs = new String[]{"-f", L2SVM_HOME + TEST_L2SVM + ".dml",
				               "-args", "\"" + L2SVM_HOME + INPUT_DIR + "X" + "\"", 
				                        "\"" + L2SVM_HOME + INPUT_DIR + "Y" + "\"", 
				                        Integer.toString(rows), Integer.toString(cols),
				                        Double.toString(Math.pow(10, -8)), Integer.toString(lambda),
				                        "\"" + L2SVM_HOME + OUTPUT_DIR + "w" + "\""};
		dmlArgsDebug = new String[]{"-f", L2SVM_HOME + TEST_L2SVM + ".dml", "-d",
							   "-args", "\"" + L2SVM_HOME + INPUT_DIR + "X" + "\"", 
                                        "\"" + L2SVM_HOME + INPUT_DIR + "Y" + "\"", 
                                        Integer.toString(rows), Integer.toString(cols),
                                        Double.toString(Math.pow(10, -8)), Integer.toString(lambda), 
                                        "\"" + L2SVM_HOME + OUTPUT_DIR + "w" + "\""};
		rCmd = "Rscript" + " " + L2SVM_HOME + TEST_L2SVM + ".R" + " " + 
		       L2SVM_HOME + INPUT_DIR + " " + Double.toString(Math.pow(10, -8)) + " " + Integer.toString(lambda) + " " + L2SVM_HOME + EXPECTED_DIR;
		
        loadTestConfiguration(config);

        double[][] X = getRandomMatrix(rows, cols, 0, 1, 0.01, -1);
        double[][] Y = getRandomMatrix(rows, 1, -1, 1, 1, -1);
        for(int i=0; i<rows; i++)
        	Y[i][0] = (Y[i][0] > 0) ? 1 : -1;
        
        writeInputMatrix("X", X, true);
        writeInputMatrix("Y", Y, true);
     
        boolean exceptionExpected = false;
		
        /* no calculation of expectedNumberOfJobs as for now, set to default temporarily */
		runTest(true, exceptionExpected, null, -1);
		
		runRScript(true);
		disableOutAndExpectedDeletion();
        
		HashMap<CellIndex, Double> wR = readRMatrixFromFS("w");
        HashMap<CellIndex, Double> wDML= readDMLMatrixFromHDFS("w");
        boolean success = TestUtils.compareMatrices(wR, wDML, Math.pow(10, -14), "wR", "wDML");
        System.out.println(success+"");
	}
}

@@ -698,24 +698,130 @@
     
-int lambda = 1;%0A
+double epsilon = 1.0e-8;%0A        double lambda = 1.0;%0A        int maxiterations = 3;%0A        int maxNumberOfMRJobs = 21;%0A 
     
@@ -1644,32 +1644,48 @@
 oString(
-Math.pow(10, -8)
+epsilon), Double.toString(lambda
 ), Integ
@@ -1696,22 +1696,29 @@
 oString(
-lambda
+maxiterations
 ),%0A%09%09%09%09 
@@ -2173,16 +2173,41 @@
 0, -8)),
+ Double.toString(lambda),
  Integer
@@ -2216,22 +2216,29 @@
 oString(
-lambda
+maxiterations
 ), %0A    
@@ -2442,32 +2442,55 @@
 oString(
-Math.pow(10, -8)
+epsilon) + %22 %22 + Double.toString(lambda
 ) + %22 %22 
@@ -2500,38 +2500,45 @@
 nteger.toString(
-lambda
+maxiterations
 ) + %22 %22 + L2SVM_
@@ -2962,94 +2962,8 @@
     
-/* no calculation of expectedNumberOfJobs as for now, set to default temporarily */%0A%09%09
 runT
@@ -2997,18 +2997,33 @@
 , null, 
--1
+maxNumberOfMRJobs
 );%0A%09%09%0A%09%09

Change DML scripts - L2SVM.dml - checks number of MR jobs now (21). invokes the R and dml scripts with appropriate command-line args (including max-iterations).