Metrics { public static double measureDynamism(Scenario s) { return measureDynamism(s, s.getTimeWindow().end()); } private  Metrics(); static double getVehicleSpeed(Scenario s); static ImmutableMultiset<Class<?>> getEventTypeCounts(Scenario s); static void checkTimeWindowStrictness(Scenario s); static ImmutableList<Long> getArrivalTimes(Scenario s); static ImmutableSortedMultiset<Double> computeHistogram(
      Iterable<Double> input, double binSize); static StatisticalSummary measureUrgency(Scenario s); static double measureDynamism(Scenario s); static double measureDynamism(Scenario s, long lengthOfDay); static double measureDynamism(Iterable<Double> arrivalTimes,
      double lengthOfDay); static ImmutableList<Point> getServicePoints(Scenario s);  }
@Test public void dynamismTest2() { final double[] ordersPerHour = {15d}; final StandardDeviation sd = new StandardDeviation(); final RandomGenerator rng = new MersenneTwister(123L); final List<Times> times = newArrayList(); times.add(asTimes(1000, 250L, 500L, 750L)); times.add(asTimes(1000, 100L, 500L, 750L)); times.add(asTimes(1000, 100L, 200L, 300L, 400L, 500L, 600L, 700L, 800L, 900L)); times.add(asTimes(1000, 100L, 200L, 300L, 399L, 500L, 600L, 700L, 800L, 900L)); times .add(asTimes(1000, 10L, 150L, 250L, 350L, 450L, 550L, 650L, 750L, 850L, 950L)); times .add(asTimes(1000, 50L, 150L, 250L, 350L, 450L, 551L, 650L, 750L, 850L, 950L)); times.add(asTimes(1000, 250L, 500L, 750L)); times.add(asTimes(1000, 0L, 50L, 55L, 57L, 59L, 60L, 100L, 150L, 750L)); times.add(asTimes(1000, 5L, 5L, 5L, 5L)); times.add(asTimes(1000, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L)); times.add(asTimes(1000, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L)); times.add(asTimes(1000, 0L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 999L)); times.add(asTimes(1000, 500L, 500L, 500L, 500L)); times.add(asTimes(1000, 5L, 5L, 5L, 5L, 400L, 410L, 430L, 440L, 800L, 810L, 820L, 830L)); times.add(asTimes(1000, 0L, 0L, 0L)); times.add(asTimes(1000, 1L, 1L, 1L)); times.add(asTimes(1000, 999L, 999L, 999L)); times.add(asTimes(1000, 0L, 0L, 500L, 500L, 999L, 999L)); times.add(asTimes(1000, 250L, 250L, 500L, 500L, 750L, 750L)); times.add(asTimes(1000, 250L, 250L, 250L, 500L, 500L, 500L, 750L, 750L, 750L)); for (int i = 0; i < 10; i++) { times.add(generateTimes(rng, 10d)); } for (int i = 0; i < 10; i++) { times.add(generateTimes(rng, 30d)); } for (int i = 0; i < 5; i++) { final List<Double> ts = generateTimes(rng, 50d).list; final List<Double> newTs = newArrayList(); for (final double l : ts) { newTs.add(l); newTs.add(Math.min(999, Math.max(0, l + DoubleMath.roundToLong(rng.nextDouble() * 6d - 3d, RoundingMode.HALF_EVEN)))); } times.add(asTimesDouble(1000, newTs)); } for (int i = 0; i < 5; i++) { final List<Double> ts = generateTimes(rng, 100d).list; final List<Double> newTs = newArrayList(); System.out.println("num events: " + ts.size()); for (final double l : ts) { newTs.add(l); newTs.add(Math.min(999, Math.max(0, l + DoubleMath.roundToLong(rng.nextDouble() * 2d - 1d, RoundingMode.HALF_EVEN)))); newTs.add(Math.min(999, Math.max(0, l + DoubleMath.roundToLong(rng.nextDouble() * 2d - 1d, RoundingMode.HALF_EVEN)))); } times.add(asTimesDouble(1000, newTs)); } final List<Long> t2 = asList(10L, 30L, 50L, 70L, 90L); for (int i = 0; i < 5; i++) { final List<Long> c = newArrayList(); for (int j = 0; j < i + 1; j++) { c.addAll(t2); } Collections.sort(c); times.add(asTimes(100, c)); } final List<Long> t = asList(100L, 300L, 500L, 700L, 900L); for (int i = 0; i < 15; i++) { final List<Long> c = newArrayList(); for (int j = 0; j < i + 1; j++) { c.addAll(t); } Collections.sort(c); times.add(asTimes(1000, c)); } final List<Long> variant = newArrayList(); variant.addAll(t); for (int i = 0; i < 70; i++) { variant.add(100L); } Collections.sort(variant); times.add(asTimes(1000, variant)); checkState(variant.size() == 75); checkState(times.get(times.size() - 2).list.size() == 75, "", times.get(times.size() - 2).list.size()); for (int i = 0; i < 10; i++) { times.add(generateTimes(rng, (i + 1) * 100d)); } final ImmutableList<Long> all = ContiguousSet.create( Range.closedOpen(0L, 1000L), DiscreteDomain.longs()).asList(); times.add(asTimes(1000, all)); final List<Long> more = newArrayList(all); for (final long l : all) { if (l % 2 == 0) { more.add(l); } } Collections.sort(more); times.add(asTimes(1000, more)); final List<Long> more2 = newArrayList(all); for (int i = 0; i < 200; i++) { more2.add(100L); } for (int i = 0; i < 100; i++) { more2.add(200L); } Collections.sort(more2); final List<Long> newMore = newArrayList(); for (int i = 0; i < more2.size(); i++) { newMore.add(more2.get(i) * 10L); } times.add(asTimes(1000, more2)); times.add(asTimes(10000, newMore)); for (int k = 0; k < 5; k++) { final List<Double> ts = newArrayList(generateTimes(rng, 20).list); final List<Double> additions = newArrayList(); for (int i = 0; i < ts.size(); i++) { if (i % 3 == 0) { for (int j = 0; j < k; j++) { additions.add(ts.get(i) + rng.nextDouble() * 10); } } } ts.addAll(additions); Collections.sort(ts); times.add(asTimesDouble(1000, ts)); } final Times regular = asTimes(10, 0L, 1L, 2L, 5L, 6L, 7L, 8L, 9L); for (int i = 1; i < 4; i++) { final List<Double> newList = newArrayList(); for (final double l : regular.list) { newList.add(l * Math.pow(10, i)); } times .add( asTimesDouble((int) (regular.length * Math.pow(10, i)), newList)); } times.add(asTimes(1000, 250L, 250L, 250L, 500L, 500L, 500L, 750L, 750L, 750L)); times.add(asTimes(1000, 250L, 500L, 500L, 500L, 500L, 500L, 500L, 500L, 750L)); times.add(asTimes(1000, 100L, 100L, 200L, 200L, 300L, 300L, 400L, 400L, 500L, 500L, 600L, 600L, 700L, 700L, 800L, 800L, 900L, 900L)); times.add(asTimes(1000, 100L, 200L, 200L, 200L, 200L, 200L, 200L, 200L, 200L, 200L, 200L, 300L, 400L, 500L, 600L, 700L, 800L, 900L)); times.add(asTimes(1000, 100L, 200L, 300L, 400L, 500L, 600L, 700L, 800L, 800L, 800L, 800L, 800L, 800L, 800L, 800L, 800L, 800L, 900L)); for (int j = 0; j < ordersPerHour.length; j++) { System.out.println("=========" + ordersPerHour[j] + "========="); for (int i = 0; i < times.size(); i++) { System.out.println("----- " + i + " -----"); final double dod8 = measureDynamism(times.get(i).list, times.get(i).length); System.out.printf("%1.3f%%\n", dod8 * 100d); final double name = Math.round(dod8 * 100000d) / 1000d; try { final File dest = new File( "files/generator/times/orders" + Strings.padStart(Integer.toString(i), 3, '0') + "-" + name + ".times"); Files.createParentDirs(dest); Files.write( times.get(i).length + "\n" + Joiner.on("\n").join(times.get(i).list) + "\n", dest, Charsets.UTF_8); } catch (final IOException e) { throw new IllegalArgumentException(); } } } }
@Test public void testDynamismScaleInvariant0() { final double expectedDynamism = 0d; final List<Integer> numEvents = asList(7, 30, 50, 70, 100, 157, 234, 748, 998, 10000, 100000); final int scenarioLength = 1000; for (final int num : numEvents) { final List<Double> scenario = newArrayList(); for (int i = 0; i < num; i++) { scenario.add(300d); } final double dyn = measureDynamism(scenario, scenarioLength); assertEquals(expectedDynamism, dyn, 0.0001); } }
@Test public void testDynamismScaleInvariant50() { final double expectedDynamism = 0.5d; final List<Integer> numEvents = asList(30, 50, 70, 100, 158, 234, 426, 748, 998, 10000, 100000); final int scenarioLength = 1000; for (final int num : numEvents) { final List<Double> scenario = newArrayList(); final int unique = num / 2; final double dist = scenarioLength / (double) unique; for (int i = 0; i < num / 2; i++) { scenario.add(dist / 2d + i * dist); scenario.add(dist / 2d + i * dist); } final double dyn = measureDynamism(scenario, scenarioLength); assertEquals(expectedDynamism, dyn, 0.02); } }
@Test public void testDynamismScaleInvariant100() { final double expectedDynamism = 1d; final List<Integer> numEvents = asList(7, 30, 50, 70, 100, 157, 234, 748, 998, 10000, 100000); final int scenarioLength = 1000; for (final int num : numEvents) { final double dist = scenarioLength / (double) num; final List<Double> scenario = newArrayList(); for (int i = 0; i < num; i++) { scenario.add(dist / 2d + i * dist); } final double dyn = measureDynamism(scenario, scenarioLength); assertEquals(expectedDynamism, dyn, 0.0001); } }
@Test public void testDynamismTimeScaleInvariant() { final RandomGenerator rng = new MersenneTwister(123); final double startLengthOfDay = 1000; final int repetitions = 3; final List<Integer> numEvents = asList(200, 300, 400, 500, 600, 700, 800); final List<Double> lengthsOfDay = asList(startLengthOfDay, 1300d, 2000d, 4500d, 7600d, 15000d, 60000d, 10000d, 100000d); for (final int events : numEvents) { for (int j = 0; j < repetitions; j++) { final List<Double> scenario = newArrayList(); for (int i = 0; i < events; i++) { scenario.add(rng.nextDouble() * startLengthOfDay); } Collections.sort(scenario); double dod = -1; for (final double dayLength : lengthsOfDay) { final List<Double> cur = newArrayList(); for (final double i : scenario) { cur.add(i * (dayLength / startLengthOfDay)); } final double curDod = measureDynamism(cur, dayLength); if (dod >= 0d) { assertEquals(dod, curDod, 0.001); } dod = curDod; } } } }
@Test public void testDynamismShiftInvariant() { final RandomGenerator rng = new MersenneTwister(123); final double lengthOfDay = 1000; final int repetitions = 10; final List<Integer> numEvents = asList(20, 50, 120, 200, 300, 500, 670, 800); for (final int num : numEvents) { for (int j = 0; j < repetitions; j++) { final List<Double> scenario = newArrayList(); for (int i = 0; i < num; i++) { scenario.add(rng.nextDouble() * lengthOfDay); } Collections.sort(scenario); final double curDod = measureDynamism(scenario, lengthOfDay); final List<Double> leftShiftedScenario = newArrayList(); final double leftMost = scenario.get(0); for (int i = 0; i < num; i++) { leftShiftedScenario.add(scenario.get(i) - leftMost); } final double leftDod = measureDynamism(leftShiftedScenario, lengthOfDay); final List<Double> rightShiftedScenario = newArrayList(); final double rightMost = lengthOfDay - scenario.get(scenario.size() - 1) - 0.00000001; for (int i = 0; i < num; i++) { rightShiftedScenario.add(scenario.get(i) + rightMost); } final double rightDod = measureDynamism(rightShiftedScenario, lengthOfDay); assertEquals(curDod, leftDod, 0.0001); assertEquals(curDod, rightDod, 0.0001); } } }
@Test public void testExample() { final List<Double> figA = asList(.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5); final List<Double> figB = asList(.5, 1.2, 2.9, 3.3, 4.2, 5.1, 6.9, 7.3, 8.5, 9.9); final List<Double> figC = asList(.5, .7, .9, 3.3, 3.6, 4.1, 7.9, 8.3, 8.5, 9.9); final List<Double> figD = asList(.5, .7, .9, 1.3, 1.6, 8.1, 8.3, 8.4, 8.5, 9.9); final List<Double> figE = asList(4.1, 4.15, 4.2, 4.3, 4.32, 4.39, 4.43, 4.48, 4.6); final List<Double> figF = asList(1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1); System.out.println("fig A = " + Metrics.measureDynamism(figA, 10) * 100); System.out.println("fig B = " + Metrics.measureDynamism(figB, 10) * 100); System.out.println("fig C = " + Metrics.measureDynamism(figC, 10) * 100); System.out.println("fig D = " + Metrics.measureDynamism(figD, 10) * 100); System.out.println("fig E = " + Metrics.measureDynamism(figE, 10) * 100); System.out.println("fig F = " + Metrics.measureDynamism(figF, 10) * 100); }