package edu.neu.ccs.pyramid.experiment;

import edu.neu.ccs.pyramid.configuration.Config;
import edu.neu.ccs.pyramid.eval.Accuracy;
import edu.neu.ccs.pyramid.util.Sampling;
import mltk.core.Instances;
import mltk.core.io.InstancesReader;
import mltk.predictor.Learner;
import mltk.predictor.glm.ElasticNetLearner;
import mltk.predictor.glm.GLM;

import java.io.File;
import java.util.stream.IntStream;

/**
 * elastic net
 * Created by chengli on 11/12/14.
 */
public class Exp22 {
    public static void main(String[] args) throws Exception{
        if (args.length !=1){
            throw new IllegalArgumentException("please specify the config file");
        }

        Config config = new Config(args[0]);
        System.out.println(config);
        run(config);


    }

    private static Config genHyperParams(){
        Config config = new Config();
        double lambda = Sampling.doubleLogUniform(0.001, 1000);
        double l1Ratio = Sampling.doubleUniform(0, 1);
        int iterations = Sampling.intUniform(50,200);
        config.setDouble("lambda",lambda);
        config.setDouble("l1Ratio",l1Ratio);
        config.setInt("iterations",iterations);
        return config;

    }

    private static void run(Config config) throws Exception{
        File trecFile = new File(config.getString("input.folder"),
                config.getString("input.trainData"));
        String matrixFile = new File(trecFile, "feature_matrix.txt").getAbsolutePath();
        Instances trainSet = InstancesReader.read(null, matrixFile);

        File testFile = new File(config.getString("input.folder"),
                config.getString("input.testData"));
        String testMatrixFile = new File(testFile, "feature_matrix.txt").getAbsolutePath();
        Instances testSet = InstancesReader.read(null,testMatrixFile);



        for (int run=0;run<config.getInt("numRuns");run++){
            Config hyperParams = genHyperParams();
            System.out.println("==============================");
            System.out.println("hyper parameters for the run:");
            System.out.println(hyperParams);
            double lambda = hyperParams.getDouble("lambda");
            double l1Ratio = hyperParams.getDouble("l1Ratio");
            int iterations = hyperParams.getInt("iterations");
            ElasticNetLearner learner = new ElasticNetLearner();
            learner.setTask(Learner.Task.CLASSIFICATION);
            learner.setMaxNumIters(iterations);
            learner.setVerbose(false);
            learner.setLambda(lambda);
            learner.setL1Ratio(l1Ratio);
            GLM glm = learner.build(trainSet);
            int[] predictions= IntStream.range(0, trainSet.size()).map(i-> glm.classify(trainSet.get(i)))
                    .toArray();
            int[] labels = IntStream.range(0,trainSet.size()).map(i-> (int)(trainSet.get(i).getTarget()))
                    .toArray();
            System.out.println("accuracy on the training set = "+ Accuracy.accuracy(labels, predictions));


            int[] testPredictions= IntStream.range(0,testSet.size()).map(i-> glm.classify(testSet.get(i)))
                    .toArray();
            int[] testLabels = IntStream.range(0,testSet.size()).map(i-> (int)(testSet.get(i).getTarget()))
                    .toArray();
            System.out.println("accuracy on the test set = "+ Accuracy.accuracy(testLabels,testPredictions));
        }
    }

}

@@ -114,21 +114,97 @@
 mid.
-eval.Accuracy
+dataset.*;%0Aimport edu.neu.ccs.pyramid.eval.Accuracy;%0Aimport edu.neu.ccs.pyramid.util.Pair
 ;%0Aim
@@ -414,16 +414,84 @@
 glm.GLM;
+%0Aimport org.apache.commons.math3.distribution.LogNormalDistribution;
 %0A%0Aimport
@@ -559,16 +559,39 @@
 stic net
+ hyper parameter tuning
 %0A * Crea
@@ -1080,13 +1080,9 @@
 001,
- 1000
+1
 );%0A 
@@ -1429,624 +1429,297 @@
     
-File trecFile = new File(config.getString(%22input.folder%22),%0A                config.getString(%22input.trainData%22));%0A        String matrixFile = new File(trecFile, %22feature_matrix.txt%22).getAbsolutePath();%0A        Instances trainSet = InstancesReader.read(null, matrixFile);%0A%0A        File testFile = new File(config.getString(%22input.folder%22),%0A                config.getString(%22input.testData%22));%0A        String testMatrixFile = new File(testFile, %22feature_matrix.txt%22).getAbsolutePath();%0A        Instances testSet = InstancesReader.read(null,testMatrixFile);%0A%0A%0A%0A        for (int run=0;run%3Cconfig.getInt(%22numRuns%22);run++)%7B
+for (int run=0;run%3Cconfig.getInt(%22numRuns%22);run++)%7B%0A            Pair%3CClfDataSet,ClfDataSet%3E dataSets = loadDataSets(config);%0A%0A            Instances trainSet = MLTKFormat.toInstances(dataSets.getFirst());%0A%0A            Instances validationSet = MLTKFormat.toInstances(dataSets.getSecond());%0A
 %0A   
@@ -2869,20 +2869,26 @@
   int%5B%5D 
-test
+validation
 Predicti
@@ -2902,36 +2902,42 @@
 tStream.range(0,
-test
+validation
 Set.size()).map(
@@ -2929,32 +2929,33 @@
 et.size()).map(i
+ 
 -%3E glm.classify(
@@ -2954,20 +2954,26 @@
 lassify(
-test
+validation
 Set.get(
@@ -3027,20 +3027,26 @@
   int%5B%5D 
-test
+validation
 Labels =
@@ -3064,20 +3064,26 @@
 range(0,
-test
+validation
 Set.size
@@ -3100,20 +3100,26 @@
 %3E (int)(
-test
+validation
 Set.get(
@@ -3215,20 +3215,26 @@
  on the 
-test
+validation
  set = %22
@@ -3257,34 +3257,480 @@
 acy(
-testLabels,testPredictions
+validationLabels,validationPredictions));%0A        %7D%0A    %7D%0A    %0A    private static Pair%3CClfDataSet, ClfDataSet%3E loadDataSets(Config config) throws Exception%7B%0A        File trecFile = new File(config.getString(%22input.folder%22),%0A                config.getString(%22input.trainData%22));%0A        ClfDataSet clfDataSet = TRECFormat.loadClfDataSet(trecFile, DataSetType.CLF_SPARSE,false);%0A        return DataSetUtil.splitToTrainValidation(clfDataSet,config.getDouble(%22trainPercentage%22
 ));%0A
@@ -3733,23 +3733,22 @@
 ));%0A        
-%7D
 %0A    %7D%0A%0A%7D%0A

use it for hyper parameter tuning