/**
   * Entry point of the demo application.
   *
   * @param args command line arguments
   */
  public static void main(String[] args) {
    // Starts the sampler
    Sampler sampler = new Sampler();
    sampler.setDaemon(true);
    sampler.start();

    // Starts the measuring
    while (true) {
      try (Split ignored = SimonManager.getStopwatch("sampled-stopwatch").start()) {
        ExampleUtils.waitRandomlySquared(50);
      }
    }
  }
示例#2
0
 /**
  * Write a partition file for the given job, using the Sampler provided. Queries the sampler for a
  * sample keyset, sorts by the output key comparator, selects the keys for each rank, and writes
  * to the destination returned from {@link TotalOrderPartitioner#getPartitionFile}.
  */
 @SuppressWarnings("unchecked") // getInputFormat, getOutputKeyComparator
 public static <K, V> void writePartitionFile(Job job, Sampler<K, V> sampler)
     throws IOException, ClassNotFoundException, InterruptedException {
   Configuration conf = job.getConfiguration();
   final InputFormat inf = ReflectionUtils.newInstance(job.getInputFormatClass(), conf);
   int numPartitions = job.getNumReduceTasks();
   K[] samples = sampler.getSample(inf, job);
   RawComparator<K> comparator = (RawComparator<K>) job.getSortComparator();
   Arrays.sort(samples, comparator);
   Path dst = new Path(TotalOrderPartitioner.getPartitionFile(conf));
   FileSystem fs = dst.getFileSystem(conf);
   if (fs.exists(dst)) {
     fs.delete(dst, false);
   }
   SequenceFile.Writer writer =
       SequenceFile.createWriter(fs, conf, dst, job.getMapOutputKeyClass(), NullWritable.class);
   NullWritable nullValue = NullWritable.get();
   float stepSize = samples.length / (float) numPartitions;
   int last = -1;
   for (int i = 1; i < numPartitions; ++i) {
     int k = Math.round(stepSize * i);
     while (last >= k && comparator.compare(samples[last], samples[k]) == 0) {
       ++k;
     }
     writer.append(samples[k], nullValue);
     last = k;
   }
   writer.close();
 }
示例#3
0
  public void learn() {
    RankerFactory rf = new RankerFactory();
    PRINTLN("------------------------------------");
    PRINTLN("Training starts...");
    PRINTLN("------------------------------------");
    PRINTLN(
        new int[] {9, 9, 11}, new String[] {"bag", scorer.name() + "-B", scorer.name() + "-OOB"});
    PRINTLN("------------------------------------");
    // start the bagging process
    for (int i = 0; i < nBag; i++) {
      if (i % LambdaMART.gcCycle == 0) System.gc();
      Sampler sp = new Sampler();
      // create a "bag" of samples by random sampling from the training set
      List<RankList> bag = sp.doSampling(samples, subSamplingRate, true);
      // "out-of-bag" samples
      // List<RankList> outOfBag = sp.getRemains();
      LambdaMART r = (LambdaMART) rf.createRanker(rType, bag, features, scorer);
      // r.setValidationSet(outOfBag);

      boolean tmp = Ranker.verbose;
      Ranker.verbose = false; // turn of the progress messages from training this ranker
      r.init();
      r.learn();
      Ranker.verbose = tmp;
      // PRINTLN(new int[]{9, 9, 11}, new String[]{"b["+(i+1)+"]",
      // SimpleMath.round(r.getScoreOnTrainingData(), 4)+"",
      // SimpleMath.round(r.getScoreOnValidationData(), 4)+""});
      PRINTLN(
          new int[] {9, 9},
          new String[] {
            "b[" + (i + 1) + "]", SimpleMath.round(r.getScoreOnTrainingData(), 4) + ""
          });
      ensembles[i] = r.getEnsemble();
    }
    // Finishing up
    scoreOnTrainingData = scorer.score(rank(samples));
    PRINTLN("------------------------------------");
    PRINTLN("Finished sucessfully.");
    PRINTLN(scorer.name() + " on training data: " + SimpleMath.round(scoreOnTrainingData, 4));
    if (validationSamples != null) {
      bestScoreOnValidationData = scorer.score(rank(validationSamples));
      PRINTLN(
          scorer.name() + " on validation data: " + SimpleMath.round(bestScoreOnValidationData, 4));
    }
    PRINTLN("------------------------------------");
  }
 public void onHarvest() {
   Sample sampleMemory = Sampler.sampleMemory();
   if (sampleMemory != null) {
     Metric metric = new Metric("Memory/Used");
     metric.sample(sampleMemory.getValue().doubleValue());
     addMetric(metric);
   }
   super.onHarvest();
 }
示例#5
0
  public static void main(String[] args) {

    double[] sigma = {3E-4, 1E-6, 2.4E-2, 6.67E-5, 1E-6, 6E-3, 4.4E-4, 1E-2, 1E-6};
    double[] mu = {0.42E9, 2.27};
    double alpha = 0.0;
    Sampler sampler = new Sampler(mu, sigma, alpha);
    double[][] M = sampler.getCovMat();
    System.out.println("The Covirance Matrix is:");
    sampler.printMatrix(M);

    double[][] A = sampler.getCholDecompA();
    System.out.println("The decomposed Matrix A is");
    sampler.printMatrix(A);

    try {

      File file = new File("/Users/Weizheng/Documents/JavaWorkPlace/CLS_MCIntegrator/output.txt");
      // if file doesnt exists, then create it
      if (!file.exists()) {
        file.createNewFile();
      } else {
        file.delete();
        file.createNewFile();
      }

      PrintWriter fw = new PrintWriter(file);

      long startTime = System.currentTimeMillis();

      for (int i = 0; i < 1E3; i++) {
        double[] MultiNormalVector = sampler.nextMultiNormalVector();
        //				for(double a:MultiNormalVector ){
        //					fw.printf("%4.2e ", a);
        //				}
        //				fw.println("");
      }
      fw.close();
      long endTime = System.currentTimeMillis();
      System.out.println("That took " + (endTime - startTime) + " milliseconds");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
示例#6
0
  public void run() {
    ArrayList<Sensor> sensors = new ArrayList<Sensor>();
    ArrayList<Rule> rules = new ArrayList<Rule>();
    ArrayList<Actuator> actuators = new ArrayList<Actuator>();

    ArrayList<String> typeList =
        this.hasProperty("typeList")
            ? (ArrayList<String>) this.getBoxedProperty("typeList")
            : new ArrayList<String>();
    typeList.add("ddos");
    typeList.add("malicious");

    Location location = (Location) this.getBoxedProperty("location");

    Sampler sampler = (Sampler) this.getBoxedProperty("sampler");
    Double pheromone = (Double) sampler.execute().getBoxedProperty("result");

    Agent agent =
        (Agent)
            (new Agent())
                .setNewBoxedProperty("location", location)
                .setNewBoxedProperty("typeList", typeList)
                .setNewBoxedProperty("sensors", sensors)
                .setNewBoxedProperty("rules", rules)
                .setNewBoxedProperty("pheromone", pheromone)
                .setNewBoxedProperty("actuator", actuators);

    ArrayList<Agent> agentList = (ArrayList<Agent>) location.getBoxedProperty("agentList");

    Thing locationContainer = (new Thing()).setProperty("object", location);

    Thing agentListContainer = (new Thing()).setProperty("object", agentList);

    LocationSensor locationSensor =
        (LocationSensor)
            (new LocationSensor())
                .setNewBoxedProperty("agent", agent)
                .setProperty("resultContainer", locationContainer);
    sensors.add(locationSensor);

    AgentListSensor agentListSensor =
        (AgentListSensor)
            (new AgentListSensor())
                .setProperty("locationContainer", locationContainer)
                .setProperty("resultContainer", agentListContainer);
    sensors.add(agentListSensor);

    Thing DDoSCommAgentListContainer = new Thing();
    AgentSensor DDoSCommAgentSensor =
        (AgentSensor)
            (new AgentSensor())
                .setProperty("agentListContainer", agentListContainer)
                .setNewBoxedProperty("type", "ddoscomm")
                .setProperty("resultContainer", DDoSCommAgentListContainer);
    sensors.add(DDoSCommAgentSensor);

    ArrayList<Location> targetList =
        this.hasBoxedProperty("targetList")
            ? (ArrayList<Location>) this.getBoxedProperty("targetList")
            : new ArrayList<Location>();
    Thing targetListContainer = (new Thing()).setProperty("object", targetList);
    TargetSensor targetSensor =
        (TargetSensor)
            (new TargetSensor())
                .setProperty("DDoSCommAgentListContainer", DDoSCommAgentListContainer)
                .setProperty("targetListContainer", targetListContainer)
                .setProperty("resultContainer", targetListContainer);
    sensors.add(targetSensor);

    Thing graphContainer = (Thing) this.getProperty("graphContainer");
    Double lifeCounter =
        (Double)
            (this.hasProperty("lifeCounter")
                ? this.getBoxedProperty("lifeCounter")
                : new Double(100));

    MASONSimScheduler scheduler = (MASONSimScheduler) this.getBoxedProperty("scheduler");
    MASONSimDeScheduler descheduler = (MASONSimDeScheduler) this.getBoxedProperty("descheduler");

    ArrayList<String> ddosAttackTypeList = new ArrayList<String>();
    ddosAttackTypeList.add("ddosattack");
    ddosAttackTypeList.add("malicious");

    DDoSAgentRule ddosAgentRule =
        (DDoSAgentRule)
            (new DDoSAgentRule())
                .setNewBoxedProperty(
                    "SendCommAgentListActuator",
                    (new SendCommAgentListActuator())
                        .setNewBoxedProperty("scheduler", scheduler)
                        .setNewBoxedProperty("descheduler", descheduler)
                        .setProperty("locationContainer", locationContainer)
                        .setProperty("targetListContainer", targetListContainer)
                        .setProperty("graphContainer", graphContainer)
                        .setProperty("agentListContainer", agentListContainer)
                        .setNewBoxedProperty(
                            "CreateAgentActuator",
                            (new CreateAgentActuator())
                                .setNewBoxedProperty("scheduler", scheduler)
                                .setNewBoxedProperty("descheduler", descheduler)
                                .setNewBoxedProperty(
                                    "agentGenerator",
                                    (new CommAgentGenerator())
                                        .setNewBoxedProperty("typeList", ddosAttackTypeList)
                                        .setNewBoxedProperty("scheduler", scheduler)
                                        .setNewBoxedProperty("descheduler", descheduler)
                                        .setNewBoxedProperty("location", location)
                                        .setProperty("graphContainer", graphContainer)
                                        .setNewBoxedProperty("lifeCounter", lifeCounter))
                                .setProperty("agentListContainer", agentListContainer)));

    rules.add(ddosAgentRule);

    this.setNewBoxedProperty("result", agent);
  }