Example #1
0
  /**
   * Function to create a new classification with the given dataset.
   *
   * @param source The dataset.
   * @param model The model selected to make the classification.
   * @throws Exception If cannot build the classification.
   */
  public Classification(MyDataset source, Cut model) throws Exception {
    int index;
    Itemset itemset;
    double[] weights;
    perClassPerValue = new double[model.numSubsets()][0];
    perValue = new double[model.numSubsets()];
    total = 0;
    perClass = new double[source.numClasses()];

    for (int i = 0; i < model.numSubsets(); i++)
      perClassPerValue[i] = new double[source.numClasses()];

    Enumeration enum2 = source.enumerateItemsets();

    while (enum2.hasMoreElements()) {
      itemset = (Itemset) enum2.nextElement();
      index = model.whichSubset(itemset);

      if (index != -1) add(index, itemset);
      else {
        weights = model.weights(itemset);
        addWeights(itemset, weights);
      }
    }
  }
Example #2
0
 private static String toString(Cut<?> lowerBound, Cut<?> upperBound) {
   StringBuilder sb = new StringBuilder(16);
   lowerBound.describeAsLowerBound(sb);
   sb.append('\u2025');
   upperBound.describeAsUpperBound(sb);
   return sb.toString();
 }
Example #3
0
 /**
  * Returns {@code true} if {@code object} is a range having the same endpoints and bound types as
  * this range. Note that discrete ranges such as {@code (1..4)} and {@code [2..3]} are <b>not</b>
  * equal to one another, despite the fact that they each contain precisely the same set of values.
  * Similarly, empty ranges are not equal unless they have exactly the same representation, so
  * {@code [3..3)}, {@code (3..3]}, {@code (4..4]} are all unequal.
  */
 @Override
 public boolean equals(@Nullable Object object) {
   if (object instanceof Range) {
     Range<?> other = (Range<?>) object;
     return lowerBound.equals(other.lowerBound) && upperBound.equals(other.upperBound);
   }
   return false;
 }
Example #4
0
 @Test
 public void testCut() {
   Total total = new Total();
   Cut cut = new Cut();
   double before = total.reduce(sampleCompany);
   cut.walk(sampleCompany);
   double after = total.reduce(sampleCompany);
   assertEquals(before / 2.0d, after, 0);
 }
 static {
   List<Cut<Integer>> cutsToTest = Lists.newArrayList();
   for (int i = MIN_BOUND - 1; i <= MAX_BOUND + 1; i++) {
     cutsToTest.add(Cut.belowValue(i));
     cutsToTest.add(Cut.aboveValue(i));
   }
   cutsToTest.add(Cut.<Integer>aboveAll());
   cutsToTest.add(Cut.<Integer>belowAll());
   CUTS_TO_TEST = ImmutableList.copyOf(cutsToTest);
 }
Example #6
0
 public Rendered getolcut(int ol, Coord cc) {
   int nseq = MCache.this.olseq;
   if (this.olseq != nseq) {
     for (int i = 0; i < cutn.x * cutn.y; i++) cuts[i].ols = null;
     this.olseq = nseq;
   }
   Cut cut = geticut(cc);
   if (cut.ols == null) cut.ols = getcut(cc).makeols();
   return (cut.ols[ol]);
 }
Example #7
0
 public MapMesh getcut(Coord cc) {
   Cut cut = geticut(cc);
   if (cut.dmesh != null) {
     if (cut.dmesh.done() || (cut.mesh == null)) {
       cut.mesh = cut.dmesh.get();
       cut.dmesh = null;
     }
   }
   return (cut.mesh);
 }
Example #8
0
  /**
   * Returns a range that contains any value from {@code lower} to {@code upper}, where each
   * endpoint may be either inclusive (closed) or exclusive (open).
   *
   * @throws IllegalArgumentException if {@code lower} is greater than {@code upper}
   */
  public static <C extends Comparable<?>> Range<C> range(
      C lower, BoundType lowerType, C upper, BoundType upperType) {
    checkNotNull(lowerType);
    checkNotNull(upperType);

    Cut<C> lowerBound =
        (lowerType == BoundType.OPEN) ? Cut.aboveValue(lower) : Cut.belowValue(lower);
    Cut<C> upperBound =
        (upperType == BoundType.OPEN) ? Cut.belowValue(upper) : Cut.aboveValue(upper);
    return create(lowerBound, upperBound);
  }
Example #9
0
 private void buildcut(final Coord cc) {
   final Cut cut = geticut(cc);
   final int deftag = ++cut.deftag;
   cut.dmesh =
       Defer.later(
           new Defer.Callable<MapMesh>() {
             public MapMesh call() {
               Random rnd = new Random(id);
               rnd.setSeed(rnd.nextInt() ^ cc.x);
               rnd.setSeed(rnd.nextInt() ^ cc.y);
               return (MapMesh.build(MCache.this, rnd, ul.add(cc.mul(cutsz)), cutsz));
             }
           });
 }
Example #10
0
 Range(Cut<C> lowerBound, Cut<C> upperBound) {
   if (lowerBound.compareTo(upperBound) > 0) {
     throw new IllegalArgumentException("Invalid range: " + toString(lowerBound, upperBound));
   }
   this.lowerBound = lowerBound;
   this.upperBound = upperBound;
 }
Example #11
0
 @Override
 @Nullable
 public Range<C> rangeContaining(C value) {
   checkNotNull(value);
   Entry<Cut<C>, Range<C>> floorEntry = rangesByLowerCut.floorEntry(Cut.belowValue(value));
   if (floorEntry != null && floorEntry.getValue().contains(value)) {
     return floorEntry.getValue();
   } else {
     // TODO(kevinb): revisit this design choice
     return null;
   }
 }
Example #12
0
 /**
  * Returns {@code true} if there exists a (possibly empty) range which is {@linkplain #encloses
  * enclosed} by both this range and {@code other}.
  *
  * <p>For example,
  *
  * <ul>
  *   <li>{@code [2, 4)} and {@code [5, 7)} are not connected
  *   <li>{@code [2, 4)} and {@code [3, 5)} are connected, because both enclose {@code [3, 4)}
  *   <li>{@code [2, 4)} and {@code [4, 6)} are connected, because both enclose the empty range
  *       {@code [4, 4)}
  * </ul>
  *
  * <p>Note that this range and {@code other} have a well-defined {@linkplain #span union} and
  * {@linkplain #intersection intersection} (as a single, possibly-empty range) if and only if this
  * method returns {@code true}.
  *
  * <p>The connectedness relation is both reflexive and symmetric, but does not form an {@linkplain
  * Equivalence equivalence relation} as it is not transitive.
  */
 public boolean isConnected(Range<C> other) {
   return lowerBound.compareTo(other.upperBound) <= 0
       && other.lowerBound.compareTo(upperBound) <= 0;
 }
Example #13
0
 /**
  * Returns {@code true} if the bounds of {@code other} do not extend outside the bounds of this
  * range. Examples:
  *
  * <ul>
  *   <li>{@code [3..6]} encloses {@code [4..5]}
  *   <li>{@code (3..6)} encloses {@code (3..6)}
  *   <li>{@code [3..6]} encloses {@code [4..4)} (even though the latter is empty)
  *   <li>{@code (3..6]} does not enclose {@code [3..6]}
  *   <li>{@code [4..5]} does not enclose {@code (3..6)} (even though it contains every value
  *       contained by the latter range)
  *   <li>{@code [3..6]} does not enclose {@code (1..1]} (even though it contains every value
  *       contained by the latter range)
  * </ul>
  *
  * Note that if {@code a.encloses(b)}, then {@code b.contains(v)} implies {@code a.contains(v)},
  * but as the last two examples illustrate, the converse is not always true.
  *
  * <p>Being reflexive, antisymmetric and transitive, the {@code encloses} relation defines a
  * <i>partial order</i> over ranges. There exists a unique {@linkplain Ranges#all maximal} range
  * according to this relation, and also numerous {@linkplain #isEmpty minimal} ranges. Enclosure
  * also implies {@linkplain #isConnected connectedness}.
  */
 public boolean encloses(Range<C> other) {
   return lowerBound.compareTo(other.lowerBound) <= 0
       && upperBound.compareTo(other.upperBound) >= 0;
 }
Example #14
0
 /**
  * Returns {@code true} if {@code value} is within the bounds of this range. For example, on the
  * range {@code [0..2)}, {@code contains(1)} returns {@code true}, while {@code contains(2)}
  * returns {@code false}.
  */
 public boolean contains(C value) {
   checkNotNull(value);
   // let this throw CCE if there is some trickery going on
   return lowerBound.isLessThan(value) && !upperBound.isLessThan(value);
 }
Example #15
0
 /**
  * Returns {@code true} if this range is of the form {@code [v..v)} or {@code (v..v]}. (This does
  * not encompass ranges of the form {@code (v..v)}, because such ranges are <i>invalid</i> and
  * can't be constructed at all.)
  *
  * <p>Note that certain discrete ranges such as the integer range {@code (3..4)} are <b>not</b>
  * considered empty, even though they contain no actual values.
  */
 public boolean isEmpty() {
   return lowerBound.equals(upperBound);
 }
Example #16
0
 /**
  * Returns the type of this range's upper bound: {@link BoundType#CLOSED} if the range includes
  * its upper endpoint, {@link BoundType#OPEN} if it does not.
  *
  * @throws IllegalStateException if this range is unbounded above (that is, {@link
  *     #hasUpperBound()} returns {@code false})
  */
 public BoundType upperBoundType() {
   return upperBound.typeAsUpperBound();
 }
Example #17
0
 /** Returns a range that contains all values less than or equal to {@code endpoint}. */
 public static <C extends Comparable<?>> Range<C> atMost(C endpoint) {
   return create(Cut.<C>belowAll(), Cut.aboveValue(endpoint));
 }
Example #18
0
 /** Returns a hash code for this range. */
 @Override
 public int hashCode() {
   return lowerBound.hashCode() * 31 + upperBound.hashCode();
 }
Example #19
0
 /**
  * Returns the type of this range's lower bound: {@link BoundType#CLOSED} if the range includes
  * its lower endpoint, {@link BoundType#OPEN} if it does not.
  *
  * @throws IllegalStateException if this range is unbounded below (that is, {@link
  *     #hasLowerBound()} returns {@code false})
  */
 public BoundType lowerBoundType() {
   return lowerBound.typeAsLowerBound();
 }
Example #20
0
 /**
  * Returns the lower endpoint of this range.
  *
  * @throws IllegalStateException if this range is unbounded below (that is, {@link
  *     #hasLowerBound()} returns {@code false})
  */
 public C lowerEndpoint() {
   return lowerBound.endpoint();
 }
Example #21
0
 /** Returns {@code true} if this range has a lower endpoint. */
 public boolean hasLowerBound() {
   return lowerBound != Cut.belowAll();
 }
Example #22
0
 /**
  * Returns a range that contains all values strictly greater than {@code lower} and less than or
  * equal to {@code upper}.
  *
  * @throws IllegalArgumentException if {@code lower} is greater than {@code upper}
  */
 public static <C extends Comparable<?>> Range<C> openClosed(C lower, C upper) {
   return create(Cut.aboveValue(lower), Cut.aboveValue(upper));
 }
Example #23
0
 /** Returns a range that contains every value of type {@code C}. */
 public static <C extends Comparable<?>> Range<C> all() {
   return create(Cut.<C>belowAll(), Cut.<C>aboveAll());
 }
Example #24
0
 /** Returns a range that contains all values greater than or equal to {@code endpoint}. */
 public static <C extends Comparable<?>> Range<C> atLeast(C endpoint) {
   return create(Cut.belowValue(endpoint), Cut.<C>aboveAll());
 }
Example #25
0
 /** Returns a range that contains all values strictly greater than {@code endpoint}. */
 public static <C extends Comparable<?>> Range<C> greaterThan(C endpoint) {
   return create(Cut.aboveValue(endpoint), Cut.<C>aboveAll());
 }
Example #26
0
 /**
  * Returns the canonical form of this range in the given domain. The canonical form has the
  * following properties:
  *
  * <ul>
  *   <li>equivalence: {@code a.canonical().contains(v) == a.contains(v)} for all {@code v} (in
  *       other words, {@code a.canonical(domain).asSet(domain).equals(a.asSet(domain))}
  *   <li>uniqueness: unless {@code a.isEmpty()}, {@code a.asSet(domain).equals(b.asSet(domain))}
  *       implies {@code a.canonical(domain).equals(b.canonical(domain))}
  *   <li>idempotence: {@code a.canonical(domain).canonical(domain).equals(a.canonical(domain))}
  * </ul>
  *
  * Furthermore, this method guarantees that the range returned will be one of the following
  * canonical forms:
  *
  * <ul>
  *   <li>[start..end)
  *   <li>[start..+∞)
  *   <li>(-∞..end) (only if type {@code C} is unbounded below)
  *   <li>(-∞..+∞) (only if type {@code C} is unbounded below)
  * </ul>
  */
 public Range<C> canonical(DiscreteDomain<C> domain) {
   checkNotNull(domain);
   Cut<C> lower = lowerBound.canonical(domain);
   Cut<C> upper = upperBound.canonical(domain);
   return (lower == lowerBound && upper == upperBound) ? this : create(lower, upper);
 }
Example #27
0
 /**
  * Returns a range that contains all values greater than or equal to {@code lower} and strictly
  * less than {@code upper}.
  *
  * @throws IllegalArgumentException if {@code lower} is greater than {@code upper}
  */
 public static <C extends Comparable<?>> Range<C> closedOpen(C lower, C upper) {
   return create(Cut.belowValue(lower), Cut.belowValue(upper));
 }
Example #28
0
 /** Returns {@code true} if this range has an upper endpoint. */
 public boolean hasUpperBound() {
   return upperBound != Cut.aboveAll();
 }
Example #29
0
 /**
  * Returns the upper endpoint of this range.
  *
  * @throws IllegalStateException if this range is unbounded above (that is, {@link
  *     #hasUpperBound()} returns {@code false})
  */
 public C upperEndpoint() {
   return upperBound.endpoint();
 }
Example #30
0
 /** Returns a range that contains all values strictly less than {@code endpoint}. */
 public static <C extends Comparable<?>> Range<C> lessThan(C endpoint) {
   return create(Cut.<C>belowAll(), Cut.belowValue(endpoint));
 }