package paysim; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.Random; public class RepetitionHandler { ArrayList fileContents; private Double[] probabilityArr; long seed = 11343; Random r ; private double totNr = 0; public RepetitionHandler(long seed, PaySim p){ this.fileContents = new ArrayList(); init(p); this.r = new Random(seed); } public int getAction(){ int actionGenerated = 0; do { actionGenerated = this.generateAction(); } while (actionGenerated == -1); return actionGenerated; } //Main usage function public RepetitionContainer getRepetition(){ RepetitionContainer contToReturn = new RepetitionContainer(); //Firstly, get the action to be potentially repeated, CASH_IN/CASH_OUT ...etc. int action = this.getAction(); //Get the name of the action String actionType = this.fileContents.get(action).split(",")[0]; //Now, given that action, and based on the probabilities for each rep combination for that action, randomize how many //times to repeat that action ArrayList subList = new ArrayList(); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ point to optimize for(int i=6; i= total && randNr <= (total + currProb)){ //System.out.println("Returned: " + (i+1) + " Because RandNr (" + randNr + ") is >= " + total + " and <= " + (total+currProb) + "\n\n\n\n"); return i; }else{ total += currProb; } } return -1; } private int generateAction(){ double randNr = r.nextDouble(); double total = 0; for(int i=0; i= total && randNr <= (total + currProb)){ //System.out.println("Returned: " + (i+1) + " Because RandNr (" + randNr + ") is >= " + total + " and <= " + (total+currProb) + "\n\n\n\n"); return i; }else{ total += currProb; } } return -1; } private void init(PaySim paysim){ //Read the file contents readFileContents(paysim); //Init the totNr //initTotNr(); //Init the probability array initProbabilityArr(); } private void initProbabilityArr(){ //Init the probability array this.probabilityArr = new Double[6]; /* * 1: CASH_IN * 2: CASH_OUT * 3: DEBIT * 4: DEPOSIT * 5: PAYMENT * 6: TRANSFER */ ArrayList actions = new ArrayList(); actions.add("CASH_IN"); actions.add("CASH_OUT"); actions.add("DEBIT"); actions.add("DEPOSIT"); actions.add("PAYMENT"); actions.add("TRANSFER"); for(int i=0; i<6; i++){ this.probabilityArr[i] = Double.parseDouble(this.fileContents.get(i).split(",")[1]); } // for(Double d: this.probabilityArr){ // System.out.println(d + "\n"); // } } //Based on an action, go through the entire file that contains the probabilities, add the aggregate for the action and //return its freq in % private double getProbability(String action){ double probabilityToReturn = 0; double nrAction = 0; for(String s: this.fileContents){ String splitted[] = s.split(","); if(splitted[0].equals(action)){ double nr = Double.parseDouble(splitted[3]); nrAction += nr; } } //System.out.println("Action:\t" + action + "\t\t\tNr:\t" + nrAction + "\n"); probabilityToReturn = nrAction / this.totNr; return probabilityToReturn; } private void initTotNr(){ for(String s: this.fileContents){ String splitted[] = s.split(","); double nr = Double.parseDouble(splitted[3]); this.totNr += nr; } System.out.println("Tot\t" + this.totNr + "\n"); } private void readFileContents(PaySim paysim){ File f1 = new File(paysim.transferFreqModInit); try { BufferedReader reader = new BufferedReader(new FileReader(f1)); String line = ""; while((line = reader.readLine()) != null){ this.fileContents.add(line); } reader.close(); } catch (Exception e) { e.printStackTrace(); } File f = new File(paysim.transferFreqMod); try { BufferedReader reader = new BufferedReader(new FileReader(f)); String line = ""; reader.readLine(); while((line = reader.readLine()) != null){ this.fileContents.add(line); } reader.close(); } catch (Exception e) { e.printStackTrace(); } } public long getSeed() { return seed; } public void setSeed(long seed) { this.seed = seed; } }