/* * 너무 복잡하면 힘들다. * 여기서 하던 filtering은 밖으로 뺀다. * 그리고 여기서 처리하던 limit, ioexception, 그외 exception도 밖으로 뺀다. * db insertion도 밖으로 뺀다. * 즉, 여기서는 무조건 값만 받고, 자연스럽게 또는 exception에 의해 return한다. * 2차 exception은 애초에 여기서 바로 다시 시도하는 것이 아니기 때문에 고려할 필요 없다. * * result, exception 정리를 위해 result exception throw로 return 단일화한다. */ public Result getListFromTag(String tag, Range<Long> range) { Result.Status status = Result.Status.Empty; // default는 not exception으로서 entire range travelled를 뜻한다. List<MediaFeedData> result = new ArrayList<MediaFeedData>(); // 값 유지를 위해 공간은 만들어두어야 한다. TODO: 다시 확인. // TODO: RANGE NULL CHECK 일단 뺐는데, 필요할지 확인. long from = range.getMinimum(); long to = range.getMaximum(); try { // library가 object 구조를 좀 애매하게 해놓아서, 바로 loop 하기보다, 1 cycle은 직접 작성해주는 구조가 되었다. TagMediaFeed list = instagram.getRecentMediaTags(tag, null, String.valueOf(to)); Pagination page = list.getPagination(); List<MediaFeedData> data = list.getData(); if (!addFilteredData(result, data, from, to)) { // filter가 안되어야만 다음으로 넘어가고, 아니면 그냥 그대로 끝이다. if (page.hasNextPage()) { MediaFeed nextList = instagram.getRecentMediaNextPage(page); Pagination nextPage = nextList.getPagination(); List<MediaFeedData> nextData = nextList.getData(); while (true) { if (!addFilteredData( result, nextData, from, to)) { // filter가 안되어야만 다음으로 넘어가고, 아니면 그냥 그대로 끝이다. if (result != null && result.size() > BATCH) { throw new Exception(); // TODO: 밑으로 내려가는지 확인. } if (nextPage.hasNextPage()) { nextList = instagram.getRecentMediaNextPage(nextPage); nextPage = nextList.getPagination(); nextData = nextList.getData(); } else { break; } } else { // if 자체가 while 안에서 실행되어야 되기 때문에 이렇게 else에서 break 걸어줘야 한다. break; } } } } } catch (Exception e) { // 아래의 단계를 거치게 해야 task loop에서의 처리가 깔끔해진다. status = Result.Status.Normal; // 기본적으로 normal. if (e instanceof InstagramException) { // 만약 insta exception이라면 exceed로 간주. status = Result.Status.Exceed; } if (result != null && !result.isEmpty()) { // 그런데 만약 full travel이라면 empty로 간주. long id = extractId(result.get(result.size() - 1).getId()); if (id == range.getMinimum()) { status = Result.Status.Empty; } } } return new Result(status, result); }
@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> 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])); }
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])); }
public boolean isCurrentTimeInSchedule() { DateTime now = DateTime.now(); if (dayOfWeekRange != null && !dayOfWeekRange.contains(now.getDayOfWeek())) { return false; } if (dayOfMonthRange != null && !dayOfMonthRange.contains(now.getDayOfMonth())) { return false; } if (timeRange != null) { Interval interval = new Interval( timeRange.getLeft().toDateTimeToday(), timeRange.getRight().toDateTimeToday()); if (!interval.contains(now)) { return false; } } return true; }
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(); } }