Ejemplo n.º 1
0
  @Override
  public void populateDAG(DAG dag, Configuration conf) {
    String lPhoneRange = conf.get(PHONE_RANGE_PROP, null);
    if (lPhoneRange != null) {
      String[] tokens = lPhoneRange.split("-");
      if (tokens.length != 2) {
        throw new IllegalArgumentException("Invalid range: " + lPhoneRange);
      }
      this.phoneRange = Range.between(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]));
    }
    LOG.debug("Phone range {}", this.phoneRange);

    RandomEventGenerator phones = dag.addOperator("Receiver", RandomEventGenerator.class);
    phones.setMinvalue(this.phoneRange.getMinimum());
    phones.setMaxvalue(this.phoneRange.getMaximum());

    PhoneMovementGenerator movementGen =
        dag.addOperator("LocationFinder", PhoneMovementGenerator.class);
    dag.setAttribute(
        movementGen,
        OperatorContext.COUNTERS_AGGREGATOR,
        new BasicCounters.LongAggregator<MutableLong>());

    StatelessThroughputBasedPartitioner<PhoneMovementGenerator> partitioner =
        new StatelessThroughputBasedPartitioner<PhoneMovementGenerator>();
    partitioner.setCooldownMillis(conf.getLong(COOL_DOWN_MILLIS, 45000));
    partitioner.setMaximumEvents(conf.getLong(MAX_THROUGHPUT, 30000));
    partitioner.setMinimumEvents(conf.getLong(MIN_THROUGHPUT, 10000));
    dag.setAttribute(
        movementGen,
        OperatorContext.STATS_LISTENERS,
        Arrays.asList(new StatsListener[] {partitioner}));
    dag.setAttribute(movementGen, OperatorContext.PARTITIONER, partitioner);

    // generate seed numbers
    Random random = new Random();
    int maxPhone = phoneRange.getMaximum() - phoneRange.getMinimum();
    int phonesToDisplay = conf.getInt(TOTAL_SEED_NOS, 10);
    for (int i = phonesToDisplay; i-- > 0; ) {
      int phoneNo = phoneRange.getMinimum() + random.nextInt(maxPhone + 1);
      LOG.info("seed no: " + phoneNo);
      movementGen.phoneRegister.add(phoneNo);
    }
    // done generating data
    LOG.info("Finished generating seed data.");

    String gatewayAddress = dag.getValue(DAG.GATEWAY_CONNECT_ADDRESS);
    URI uri = URI.create("ws://" + gatewayAddress + "/pubsub");
    PubSubWebSocketOutputOperator<Object> wsOut =
        dag.addOperator("LocationResults", new PubSubWebSocketOutputOperator<Object>());
    wsOut.setUri(uri);
    PubSubWebSocketInputOperator<Map<String, String>> wsIn =
        dag.addOperator("QueryLocation", new PubSubWebSocketInputOperator<Map<String, String>>());
    wsIn.setUri(uri);
    // default partitioning: first connected stream to movementGen will be partitioned
    dag.addStream("Phone-Data", phones.integer_data, movementGen.data);
    dag.addStream("Results", movementGen.locationQueryResult, wsOut.input);
    dag.addStream("Query", wsIn.outputPort, movementGen.phoneQuery);
  }
 private Range<Integer> parseDayOfWeekRange(String dayOfWeek) {
   if (StringUtils.isBlank(dayOfWeek)) {
     return null;
   }
   String dow = normalize(dayOfWeek);
   String[] parts = StringUtils.split(dow, '-');
   return Range.between(getDOW(parts[0]), getDOW(parts[1]));
 }
 private Range<Integer> parseDayOfMonthRange(String dayOfMonth) {
   if (StringUtils.isBlank(dayOfMonth)) {
     return null;
   }
   String dom = normalize(dayOfMonth);
   String[] parts = StringUtils.split(dom, '-');
   return Range.between(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
 }
Ejemplo n.º 4
0
public class Rosenbrock {
  public static final String FUNCTION_NAME = "Rosenbrock";

  public static final Range<Double> range = Range.between(-2.048, 2.048);

  public static double getfitnessValue(double[] varArray) {
    double tempVar = 0.0;
    for (int i = 0; i < (varArray.length - 1); i++) {
      tempVar +=
          (Math.pow((1 - varArray[i]), 2)
              + 100 * Math.pow((varArray[i + 1] - Math.pow(varArray[i], 2)), 2));
    }
    return DoubleFormatter.getFormattedDouble(tempVar, FUNCTION_NAME);
  }

  public static Double getfitnessValueNP(Double[] individual) {
    Double tempVar = 0.0;
    for (int i = 0; i < (individual.length - 1); i++) {
      tempVar +=
          (Math.pow((1 - individual[i]), 2)
              + 100 * Math.pow((individual[i + 1] - Math.pow(individual[i], 2)), 2));
    }
    return DoubleFormatter.getFormattedDouble(tempVar, FUNCTION_NAME);
  }

  public static void main(String[] args) {
    double[] varArray = new double[30];
    for (int i = 0; i < varArray.length; i++) {
      varArray[i] = 1.0;
    }

    System.out.println(getfitnessValue(varArray));
  }

  public static Individual mutateIndividual(Individual individualtoMutate) {
    // small change delta
    double delta = 0.001;
    Double[] individualtoMutateArray = individualtoMutate.getValues();
    // initialize the neighbor solution
    Double[] mutatedIndividual = new Double[Constants.DIMENSIONS];
    Arrays.fill(mutatedIndividual, 0.0);
    Individual mutatedIndv = new Individual();
    // loop and change values of neighbor
    // solution based on problem set
    for (int i = 0; i < individualtoMutateArray.length; i++) {
      if (individualtoMutateArray[i] < 1.00 && checkLimitCondition(individualtoMutateArray[i])) {
        mutatedIndividual[i] =
            DoubleFormatter.getFormattedDouble(individualtoMutateArray[i] + delta, FUNCTION_NAME);
      } else if (individualtoMutateArray[i] > 1.00
          && checkLimitCondition(individualtoMutateArray[i])) {
        mutatedIndividual[i] =
            DoubleFormatter.getFormattedDouble(individualtoMutateArray[i] - delta, FUNCTION_NAME);
      } else {
        mutatedIndividual[i] =
            DoubleFormatter.getFormattedDouble(individualtoMutateArray[i], FUNCTION_NAME);
      }
    }
    mutatedIndv.setValues(mutatedIndividual);
    mutatedIndv.setFitnessValue(getfitnessValueNP(mutatedIndividual));
    // return changed neighbor array
    return mutatedIndv;
  }

  public static boolean checkLimitCondition(double val) {
    return val < Rosenbrock.range.getMaximum() && val > Rosenbrock.range.getMinimum();
  }
}