Class CostMatrix<T>

Object
CostMatrix<T>
Type Parameters:
T - element-type in lists

public final class CostMatrix<T> extends Object
Stores costs between all possible pairings between elements from two lists.

Note that costs are often distances (symmetric) but not necessarily.

Internally, a matrix-like structure is used.

The two lists are referred to as first and second.

Author:
Owen Feehan
  • Method Details

    • create

      public static <T> CostMatrix<T> create(List<T> first, List<T> second, boolean symmetric, CheckedToDoubleBiFunction<T,T,CreateException> costCalculator) throws CreateException
      Creates by calculating the cost between elements.

      All possible pairings are calculated involving an element from the first list and an element from the second list.

      Type Parameters:
      T - element-type in lists
      Parameters:
      first - the collection of elements that forms the left-side of the cost calculation, indexed by index1 in the matrix. It should have efficient random-access.
      second - the collection of elements that forms the right-side of the cost calculation, indexed by index2 in the matrix. It should have efficient random-access.
      symmetric - if cost(a,b) = cost(b,a), then set this true, for quicker calculations.
      costCalculator - calculates cost(a,b)
      Returns:
      a newly created matrix
      Throws:
      CreateException - if either list is empty or a distance cannot be calculated between elements.
    • getCost

      public double getCost(int index1, int index2)
      Gets the cost from an element from the first-list to an element from the second-list.
      Parameters:
      index1 - index of element in first list
      index2 - index of element in second list
      Returns:
      the cost.
    • sizeFirst

      public int sizeFirst()
      The number of elements in the first list.
      Returns:
      number of elements
    • sizeSecond

      public int sizeSecond()
      The number of elements in the second list.
      Returns:
      number of elements
    • getFirst

      public List<T> getFirst()
      The first set of elements.
    • getSecond

      public List<T> getSecond()
      The second set of elements.
    • getMatrix

      public double[][] getMatrix()
      A two-dimensional array mapping the costs from first to second.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object