/*
 * Benchmark of risk-based anonymization in ARX 3.0.0
 * Copyright 2015 - Fabian Prasser
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.deidentifier.arx.benchmark;

import java.io.File;
import java.io.IOException;

import org.deidentifier.arx.ARXAnonymizer;
import org.deidentifier.arx.ARXConfiguration;
import org.deidentifier.arx.ARXLattice;
import org.deidentifier.arx.ARXLattice.ARXNode;
import org.deidentifier.arx.ARXResult;
import org.deidentifier.arx.Data;
import org.deidentifier.arx.benchmark.BenchmarkSetup.BenchmarkDataset;
import org.deidentifier.arx.benchmark.BenchmarkSetup.BenchmarkPrivacyModel;
import org.deidentifier.arx.benchmark.BenchmarkSetup.BenchmarkUtilityMeasure;

import de.linearbits.subframe.Benchmark;
import de.linearbits.subframe.analyzer.ValueBuffer;
import de.linearbits.subframe.analyzer.buffered.BufferedArithmeticMeanAnalyzer;
import de.linearbits.subframe.analyzer.buffered.BufferedStandardDeviationAnalyzer;

/**
 * Main benchmark class.
 * 
 * @author Fabian Prasser
 */
public class BenchmarkExperiment2 {

    /** The benchmark instance */
    private static final Benchmark BENCHMARK   = new Benchmark(new String[] { "Dataset", "Polygamma" });

    /** TOTAL */
    public static final int        TOTAL       = BENCHMARK.addMeasure("Total");

    /** CHECK */
    public static final int        CHECK       = BENCHMARK.addMeasure("Check");

    /** UTILITY */
    public static final int        UTILITY     = BENCHMARK.addMeasure("Utility");

    /** Repetitions */
    private static final int       REPETITIONS = 5;

    /**
     * Main entry point
     * 
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        // Init
        BENCHMARK.addAnalyzer(UTILITY, new BufferedArithmeticMeanAnalyzer());
        BENCHMARK.addAnalyzer(UTILITY, new BufferedStandardDeviationAnalyzer());
        BENCHMARK.addAnalyzer(TOTAL, new ValueBuffer());
        BENCHMARK.addAnalyzer(CHECK, new ValueBuffer());
        
        // Repeat for each data set
        for (BenchmarkDataset data : BenchmarkSetup.getDatasets()) {

            // New run
            BENCHMARK.addRun(data.toString(), "true");
            anonymize(data, true);

            // New run
            BENCHMARK.addRun(data.toString(), "false");
            anonymize(data, false);

            // Write after each experiment
            BENCHMARK.getResults().write(new File("results/experiment2.csv"));
        }
    }
    
    /**
     * Performs the experiments
     * 
     * @param dataset
     * @throws IOException
     */
    private static void anonymize(BenchmarkDataset dataset, boolean usePolygamma) throws IOException {
        
        Data data = BenchmarkSetup.getData(dataset, BenchmarkPrivacyModel.UNIQUENESS_PITMAN);
        ARXConfiguration config = BenchmarkSetup.getConfiguration(dataset, BenchmarkUtilityMeasure.LOSS, BenchmarkPrivacyModel.UNIQUENESS_PITMAN, 0.01d);
        ARXAnonymizer anonymizer = new ARXAnonymizer();
        
        // Warmup
        ARXResult result = anonymizer.anonymize(data, config);
        data.getHandle().release();
        
        // Benchmark
        long time = System.currentTimeMillis();
        for (int i = 0; i < REPETITIONS; i++) {
            System.out.println(" - Run-1 " + (i + 1) + " of " + REPETITIONS);
            data.getHandle().release();
            result = anonymizer.anonymize(data, config);
        }
        time = System.currentTimeMillis() - time;
        BENCHMARK.addValue(UTILITY, BenchmarkMetadata.getRelativeLoss(data.getHandle(),
                                                                      result.getOutput(),
                                                                      result.getGlobalOptimum().getTransformation(),
                                                                      dataset,
                                                                      BenchmarkUtilityMeasure.LOSS));
        BENCHMARK.addValue(TOTAL, (int) time);
        BENCHMARK.addValue(CHECK, (int) ((double) time / (double) getNumChecks(result)));
    }
    
    /**
     * Returns the number of checks
     * @param result
     * @return
     */
    private static int getNumChecks(ARXResult result) {
        int checks = 0;
        ARXLattice lattice = result.getLattice();
        for (ARXNode[] level : lattice.getLevels()) {
            for (ARXNode node : level) {
                checks += node.isChecked() ? 1 : 0;
            }
        }
        return checks;
    }
}

@@ -1695,25 +1695,58 @@
 aset%22, %22
-Polygamma
+UtilityMeasure%22, %22Algorithm%22, %22Suppression
 %22 %7D);%0A%0A 
@@ -1757,32 +1757,34 @@
 ** TOTAL */%0A    
+//
 public static fi
@@ -1857,32 +1857,34 @@
 ** CHECK */%0A    
+//
 public static fi
@@ -2118,17 +2118,17 @@
 TIONS = 
-5
+2
 ;%0A%0A    /
@@ -2456,32 +2456,34 @@
 zer());%0A        
+//
 BENCHMARK.addAna
@@ -2515,32 +2515,34 @@
 fer());%0A        
+//
 BENCHMARK.addAna
@@ -2762,129 +2762,54 @@
 (), 
-%22true%22);%0A            anonymize(data, true);%0A%0A            // New run%0A            BENCHMARK.addRun(data.toString(), %22false%22
+BenchmarkPrivacyModel.K_ANONYMITY, %22RGR%22, 0.3d
 );%0A 
@@ -3278,33 +3278,27 @@
 cyModel.
-UNIQUENESS_PITMAN
+K_ANONYMITY
 );%0A     
@@ -3423,25 +3423,19 @@
 del.
-UNIQUENESS_PITMAN
+K_ANONYMITY
 , 0.
@@ -4459,32 +4459,34 @@
 LOSS));%0A        
+//
 BENCHMARK.addVal
@@ -4508,32 +4508,34 @@
  time);%0A        
+//
 BENCHMARK.addVal

Clean out unnecessary stuff