[1] | 1 | package geniusweb.bagga.dicehaggler;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.io.BufferedWriter;
|
---|
| 5 | import java.io.File;
|
---|
| 6 | import java.io.FileReader;
|
---|
| 7 | import java.io.FileWriter;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.io.InputStream;
|
---|
| 10 | import java.io.InputStreamReader;
|
---|
| 11 | import java.io.LineNumberReader;
|
---|
| 12 | import java.util.stream.Collectors;
|
---|
| 13 |
|
---|
| 14 | import org.datavec.api.records.reader.RecordReader;
|
---|
| 15 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader;
|
---|
| 16 | import org.datavec.api.split.FileSplit;
|
---|
| 17 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
|
---|
| 18 | import org.nd4j.linalg.api.ndarray.INDArray;
|
---|
| 19 | import org.nd4j.linalg.dataset.DataSet;
|
---|
| 20 | import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization;
|
---|
| 21 | import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize;
|
---|
| 22 | import org.nd4j.linalg.factory.Nd4j;
|
---|
| 23 | import org.nd4j.linalg.io.ResourceUtils;
|
---|
| 24 |
|
---|
| 25 | public class Dl4jUtils {
|
---|
| 26 |
|
---|
| 27 | public DataNormalization getTrainedNormalization(String fileName) throws IOException, InterruptedException {
|
---|
| 28 | final int numLinesToSkip = 1;
|
---|
| 29 | final char delimiter = ',';
|
---|
| 30 | //final File excelFile = new File(fileName);
|
---|
| 31 | // ClassLoader classLoader = getClass().getClassLoader();
|
---|
| 32 | // //InputStream resourceFile = classLoader.getResourceAsStream(fileName);
|
---|
| 33 | // //Properties config = new Properties();
|
---|
| 34 | // //config.load(resourceFile);
|
---|
| 35 | // //String resourceFileName = config.getProperty("name");
|
---|
| 36 | // //String resourceFileName = classLoader.getResource(fileName).getFile();
|
---|
| 37 | // String resourceFileName;
|
---|
| 38 | // try (InputStream inputStream = getClass().getResourceAsStream("/"+fileName);
|
---|
| 39 | // BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
---|
| 40 | // resourceFileName = reader.lines()
|
---|
| 41 | // .collect(Collectors.joining(System.lineSeparator()));
|
---|
| 42 | // }
|
---|
| 43 | // final File excelFile;
|
---|
| 44 | //// if(resourceFileName == null) {
|
---|
| 45 | //// System.out.println("inside the if consition:");
|
---|
| 46 | //// excelFile = ResourceUtils.getFile(fileName);
|
---|
| 47 | //// }
|
---|
| 48 | //// else
|
---|
| 49 | //// {
|
---|
| 50 | //// System.out.println("inside else condition");
|
---|
| 51 | // excelFile = new File(resourceFileName);
|
---|
| 52 | //// }
|
---|
| 53 |
|
---|
| 54 | String completeFile;
|
---|
| 55 | InputStream aa = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
|
---|
| 56 | BufferedReader br = new BufferedReader(new InputStreamReader(aa));
|
---|
| 57 | completeFile = br.lines().collect(Collectors.joining(System.lineSeparator()));
|
---|
| 58 | // final File excelFile;
|
---|
| 59 | //
|
---|
| 60 | // excelFile = new File(completeFile);
|
---|
| 61 | File excelFile = File.createTempFile("tempfile", ".csv");
|
---|
| 62 | BufferedWriter bw = new BufferedWriter(new FileWriter(excelFile));
|
---|
| 63 | bw.write(completeFile);
|
---|
| 64 | bw.close();
|
---|
| 65 |
|
---|
| 66 | final RecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter);
|
---|
| 67 | recordReader.initialize(new FileSplit(excelFile));
|
---|
| 68 | System.out.println(recordReader.getLabels());
|
---|
| 69 | final int batchSize = getRowCountFromExcel(excelFile);
|
---|
| 70 | final RecordReaderDataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize - 1);
|
---|
| 71 | iterator.setCollectMetaData(true); // Instruct the iterator to collect metadata, and store
|
---|
| 72 | // it in the DataSet objects
|
---|
| 73 | final DataSet allData = iterator.next();
|
---|
| 74 |
|
---|
| 75 | final DataNormalization normalizer = new NormalizerStandardize();
|
---|
| 76 | normalizer.fit(allData); // Collect the statistics (mean/stdev) from the training data. This
|
---|
| 77 | // does not modify the input data
|
---|
| 78 | // Apply normalization to the test data. This is using statistics calculated from the
|
---|
| 79 | // *training* set
|
---|
| 80 | return normalizer;
|
---|
| 81 | // allData.shuffle(123)
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | private int getRowCountFromExcel(File file) throws IOException {
|
---|
| 85 | int linenumber = 0;
|
---|
| 86 | if (file.exists()) {
|
---|
| 87 | final FileReader fr = new FileReader(file);
|
---|
| 88 | final LineNumberReader lnr = new LineNumberReader(fr);
|
---|
| 89 |
|
---|
| 90 | while (lnr.readLine() != null) {
|
---|
| 91 | linenumber++;
|
---|
| 92 | }
|
---|
| 93 | // System.out.println("Total number of lines : " + linenumber);
|
---|
| 94 | lnr.close();
|
---|
| 95 |
|
---|
| 96 | } else {
|
---|
| 97 | System.out.println("File does not exist!");
|
---|
| 98 | }
|
---|
| 99 | return linenumber;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public static INDArray getInput(double time, int totalNumberOfPossibleBids, int numberOfIssues, int givenNumberOfbids,
|
---|
| 103 | double recentlyReceivedBidUtility, double opponentBestBidUtility, double averageOpponentUtility,
|
---|
| 104 | double standardDevUtility) {
|
---|
| 105 | final INDArray features = Nd4j.zeros(8);
|
---|
| 106 | features.putScalar(new int[] { 0 }, time);
|
---|
| 107 | features.putScalar(new int[] { 1 }, totalNumberOfPossibleBids);
|
---|
| 108 | features.putScalar(new int[] { 2 }, numberOfIssues);
|
---|
| 109 | features.putScalar(new int[] { 3 }, givenNumberOfbids);
|
---|
| 110 | features.putScalar(new int[] { 4 }, recentlyReceivedBidUtility);
|
---|
| 111 | features.putScalar(new int[] { 5 }, opponentBestBidUtility);
|
---|
| 112 | features.putScalar(new int[] { 6 }, averageOpponentUtility);
|
---|
| 113 | features.putScalar(new int[] { 7 }, standardDevUtility);
|
---|
| 114 | return features;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | }
|
---|