Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.26 KB | None | 0 0
  1. /*
  2.  *
  3.  * @author Diego Lopes
  4.  * @version 1.0
  5.  */
  6. package moa.streams.generators;
  7.  
  8. import com.github.javacliparser.FloatOption;
  9. import com.github.javacliparser.IntOption;
  10. import com.yahoo.labs.samoa.instances.Attribute;
  11. import com.yahoo.labs.samoa.instances.DenseInstance;
  12. import com.yahoo.labs.samoa.instances.Instance;
  13. import com.yahoo.labs.samoa.instances.Instances;
  14. import com.yahoo.labs.samoa.instances.InstancesHeader;
  15. import java.util.Arrays;
  16. import java.util.Random;
  17. import moa.core.Example;
  18. import moa.core.FastVector;
  19. import moa.core.InstanceExample;
  20. import moa.core.ObjectRepository;
  21. import moa.options.AbstractOptionHandler;
  22. import moa.streams.InstanceStream;
  23. import moa.tasks.TaskMonitor;
  24.  
  25. /**
  26.  *
  27.  * @author Diego Lopes
  28.  * @version 1.0
  29.  */
  30. public class TelematicCarGenerators extends AbstractOptionHandler implements InstanceStream {
  31.  
  32.     /*
  33.     * OPTIONS
  34.      */
  35.     public IntOption functionOption = new IntOption("function", 'f', "Classification function used, as defined in the original paper.", 1, 1, 5);
  36.     public FloatOption noisePercentage = new FloatOption("noise", 'n', "% of class noise.", 0.05, 0.0, 1.0f);
  37.     public IntOption instanceRandomSeedOption = new IntOption("instanceRandomSeed", 'i', "Seed for random generation of instances.", 1);
  38.     /*
  39.     * INTERNALS
  40.      */
  41.     protected InstancesHeader streamHeader;
  42.     protected Random instanceRandom;
  43.     protected boolean nextClassShouldBeZero;
  44.     protected ClassFunction classFunction;
  45.     /*
  46.     * FEATURE DEFINITIONS
  47.     12 campos no total - tem 8 e faltam esses 4 date, email, longitude, latitude
  48.      */
  49.     protected static String caridValues[] = {"1",
  50.         "2",
  51.         "3",
  52.         "4",
  53.         "5",
  54.         "6",
  55.         "7",
  56.         "8",
  57.         "9"};
  58.  
  59.     protected static String ageValues[] = {"18",
  60.         "21",
  61.         "25",
  62.         "27",
  63.         "29",
  64.         "35",
  65.         "39",
  66.         "49"};
  67.  
  68.     protected static String avgVelocityValues[] = {"18",
  69.         "19",
  70.         "25",
  71.         "47",
  72.         "65",
  73.         "80",
  74.         "160",
  75.         "200"};
  76.  
  77.     protected static String genderValues[] = {"F", "M"};
  78.     protected static String crashValues[] = {"Y", "N"};
  79.  
  80.     protected static String kmValues[] = {"10",
  81.         "39",
  82.         "50",
  83.         "50",
  84.         "60",
  85.         "100",
  86.         "140",
  87.         "200"};
  88.  
  89.     protected static String marchaValues[] = {"Y", "N"};
  90.     protected static String roadValues[] = {"Y", "N"};
  91.     protected static String classValues[] = {"Perfect", "Regular", "Poor"};
  92.  
  93.     @Override
  94.     public void getDescription(StringBuilder sb, int i) {
  95.  
  96.     }
  97.  
  98.  
  99.     /*
  100.     * Labeling functions
  101.      */
  102.     protected interface ClassFunction {
  103.  
  104.         public int determineClass(String carid,
  105.                 String age,
  106.                 String avgVelocity,
  107.                 String gender,
  108.                 String crash,
  109.                 String km,
  110.                 String marcha,
  111.                 String road);
  112.         }
  113.  
  114.     protected static ClassFunction concepts[] = {
  115.         new ClassFunction() {
  116.             @Override
  117.             public int determineClass(String carid,
  118.                     String age,
  119.                     String avgVelocity,
  120.                     String gender,
  121.                     String crash,
  122.                     String km,
  123.                     String marcha,
  124.                     String road) {
  125.  
  126.                 if (Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) > 20
  127.                     || Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) < 90
  128.                         && crashValues.equals("N") && roadValues.equals("N")) {
  129.                     return indexOfValue("Perfect", classValues);;
  130.                 }
  131.       /*          else if (Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) > 20
  132.                  || Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) < 90
  133.                         && crashValues.equals("N") && roadValues.equals("Y")) {
  134.                     return indexOfValue("Perfect", classValues);
  135.                 } */
  136.                 else if (Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) > 20
  137.                     || Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) < 90
  138.                         && crashValues.equals("Y") && roadValues.equals("N")) {
  139.                     return indexOfValue("Regular", classValues);
  140.                 }
  141.                 else if (Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) > 20
  142.                     || Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) < 90
  143.                         && crashValues.equals("Y") && roadValues.equals("Y")) {
  144.                     return indexOfValue("Poor", classValues);
  145.                 }
  146.                 else if (Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) < 20
  147.                     || Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) > 90
  148.                         && crashValues.equals("N") && roadValues.equals("N")) {
  149.                     return indexOfValue("Perfect", classValues);
  150.                 }
  151.                 else if(Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) < 20
  152.                     || Integer.parseInt(avgVelocityValues[new Random().nextInt(avgVelocityValues.length)]) > 90
  153.                         && crashValues.equals("N") && roadValues.equals("Y")) {
  154.                     return indexOfValue("Regular", classValues);
  155.                 }
  156.                 else
  157.                     return indexOfValue("Poor", classValues);
  158.             };
  159.         }
  160.     };
  161.         /*
  162.         * Generator core
  163.         */
  164.  
  165.         @Override
  166.     public InstancesHeader getHeader() {
  167.         return streamHeader;
  168.     }
  169.  
  170.     @Override
  171.     public long estimatedRemainingInstances() {
  172.         return Integer.MAX_VALUE;
  173.     }
  174.  
  175.     @Override
  176.     public boolean hasMoreInstances() {
  177.         return true;
  178.     }
  179.  
  180.     @Override
  181.     public boolean isRestartable() {
  182.         return true;
  183.     }
  184.  
  185.     @Override
  186.     public void restart() {
  187.         this.instanceRandom = new Random(this.instanceRandomSeedOption.getValue());
  188.         this.nextClassShouldBeZero = false;
  189.     }
  190.  
  191.     int addNoise(int classObtained) {
  192.         if (this.instanceRandom.nextFloat() <= this.noisePercentage.getValue()) {
  193.             classObtained = classObtained == 0 ? 1 : 0;
  194.         }
  195.         return classObtained;
  196.     }
  197.  
  198.     private static int indexOfValue(String value, Object[] arr) {
  199.         int index = Arrays.asList(arr).indexOf(value);
  200.         return index;
  201.     }
  202.  
  203.     @Override
  204.     public void prepareForUseImpl(TaskMonitor tm, ObjectRepository or) {
  205.            
  206.     classFunction = concepts[this.functionOption.getValue() -1];
  207.  
  208.     FastVector attributes = new FastVector();
  209.     attributes.addElement(new Attribute("carid",
  210.         Arrays.asList(caridValues)));
  211.     attributes.addElement(new Attribute("age",
  212.         Arrays.asList(ageValues)));
  213.     attributes.addElement(new Attribute("avgVelocity",
  214.         Arrays.asList(avgVelocityValues)));
  215.     attributes.addElement(new Attribute("gender",
  216.         Arrays.asList(genderValues)));
  217.     attributes.addElement(new Attribute("crash",
  218.         Arrays.asList(crashValues)));
  219.     attributes.addElement(new Attribute("km",
  220.         Arrays.asList(kmValues)));
  221.     attributes.addElement(new Attribute("marcha",
  222.         Arrays.asList(marchaValues)));
  223.     attributes.addElement(new Attribute("road",
  224.         Arrays.asList(roadValues)));
  225.  
  226.     this.instanceRandom = new Random(System.currentTimeMillis());
  227.  
  228.     FastVector classLabels = new FastVector();
  229.     for (int i = 0; i < classValues.length; i++) {
  230.         classLabels.addElement(classValues[i]);
  231.     }
  232.  
  233.     attributes.addElement(new Attribute("class", classLabels));
  234.     this.streamHeader = new InstancesHeader(new Instances(
  235.         getCLICreationString(InstanceStream.class), attributes, 0));
  236.     this.streamHeader.setClassIndex(this.streamHeader.numAttributes() - 1);
  237.  
  238.     restart();
  239.     }
  240.  
  241.     @Override
  242.     public Example<Instance> nextInstance() {
  243.         Instance instnc = null;
  244.  
  245.         //randomize indexes for new instance
  246.         int indexCarid = this.instanceRandom.nextInt(caridValues.length);
  247.         int indexAge = this.instanceRandom.nextInt(ageValues.length);
  248.         int indexAvgVelocity = this.instanceRandom.nextInt(avgVelocityValues.length);
  249.         int indexGender = this.instanceRandom.nextInt(genderValues.length);
  250.         int indexCrash = this.instanceRandom.nextInt(crashValues.length);
  251.         int indexKm = this.instanceRandom.nextInt(kmValues.length);
  252.         int indexMarcha = this.instanceRandom.nextInt(marchaValues.length);
  253.         int indexRoad = this.instanceRandom.nextInt(roadValues.length);
  254.  
  255.         //retrieve values
  256.         String carid = caridValues[indexCarid];
  257.         String age = ageValues[indexAge];
  258.         String avgvelocity = avgVelocityValues[indexAvgVelocity];
  259.         String gender = genderValues[indexGender];
  260.         String crash = crashValues[indexCrash];
  261.         String km = kmValues[indexKm];
  262.         String marcha = marchaValues[indexMarcha];
  263.         String road = roadValues[indexRoad];
  264.         int classValue = classFunction.determineClass(carid, age, avgvelocity, gender, crash, km, marcha, road);
  265.  
  266.         instnc = new DenseInstance(streamHeader.numAttributes());
  267.         //set values
  268.         instnc.setDataset(this.getHeader());
  269.         instnc.setValue(0, Arrays.asList(caridValues).indexOf(carid));
  270.         instnc.setValue(1, Arrays.asList(ageValues).indexOf(age));
  271.         instnc.setValue(2, Arrays.asList(avgVelocityValues).indexOf(avgvelocity));
  272.         instnc.setValue(3, Arrays.asList(genderValues).indexOf(gender));
  273.         instnc.setValue(4, Arrays.asList(crashValues).indexOf(crash));
  274.         instnc.setValue(5, Arrays.asList(kmValues).indexOf(km));
  275.         instnc.setValue(6, Arrays.asList(marchaValues).indexOf(marcha));
  276.         instnc.setValue(7, Arrays.asList(roadValues).indexOf(road));
  277.         instnc.setClassValue((int) classValue);
  278.  
  279.         return new InstanceExample(instnc);
  280.     }
  281.  
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement