Table of Contents

Class DistanceMatrix<T>

Namespace
Bannerlord.ButterLib.DistanceMatrix
Assembly
Bannerlord.ButterLib.dll

A generic class that pairs given objects of type T and for each pair calculates the distance between the objects that formed it.

public abstract class DistanceMatrix<T> : DistanceMatrix where T : MBObjectBase

Type Parameters

T

The type of objects for which the distance matrix should be calculated.

Inheritance
DistanceMatrix<T>
Inherited Members

Remarks

DistanceMatrix<T> implements built-in calculation for the Hero, Settlement, Clan and Kingdom objects. For any other MBObjectBase subtypes custom EntityListGetter and DistanceCalculator methods should be provided using special constructor DistanceMatrix<T>.Create(Func<IEnumerable<T>>, Func<T, T, float>) .

Constructors

DistanceMatrix()

Initializes a new instance of the DistanceMatrix<T> class with default EntityListGetter and DistanceCalculator methods.

protected DistanceMatrix()

Exceptions

ArgumentException

DistanceMatrix(Func<IEnumerable<T>>, Func<T, T, float>)

Initializes a new instance of the DistanceMatrix<T> class with custom methods that will be used to get the list of analyzed objects and calculate the distances between them.

protected DistanceMatrix(Func<IEnumerable<T>> customListGetter, Func<T, T, float> customDistanceCalculator)

Parameters

customListGetter Func<IEnumerable<T>>

A delegate to the method that will be used to get a list of objects of type T for calculating the distances between them.

customDistanceCalculator Func<T, T, float>

A delegate to the method that will be used to calculate the distance between two given type T objects.

Exceptions

ArgumentException

Properties

AsDictionary

Raw distance matrix representation

public abstract Dictionary<ulong, float> AsDictionary { get; }

Property Value

Dictionary<ulong, float>

A dictionary of paired type T objects represented by unique 64-bit unsigned number as key and floating-point numbers, representing distances between those objects as value.

AsFlatenedDictionary

Objectified distance matrix representation for nearest neighbours processing

public abstract Dictionary<T, SortedSet<(T OtherObject, float Distance)>> AsFlatenedDictionary { get; }

Property Value

Dictionary<T, SortedSet<(T OtherObject, float Distance)>>

A dictionary of the T object type as a key and a values are represented by a SortedSet<T> containing tuples of all other objects and the corresponding distances in a form of floating point numbers.

AsTypedDictionary

Objectified distance matrix representation

public abstract Dictionary<(T Object1, T Object2), float> AsTypedDictionary { get; }

Property Value

Dictionary<(T Object1, T Object2), float>

A dictionary of paired type T objects as key and floating-point numbers, representing distances between those objects as value.

Methods

Create()

Initializes and returns a new instance of the DistanceMatrix<T> class as per actual implementation with the default EntityListGetter and DistanceCalculator methods.

public static DistanceMatrix<T>? Create()

Returns

DistanceMatrix<T>

Exceptions

ArgumentException

Create(Func<IEnumerable<T>>, Func<T, T, object[]?, float>, object[]?)

Initializes and returns a new instance of the DistanceMatrix<T> class as per actual implementation with custom methods that will be used to get the list of analyzed objects and calculate the distances between them.

public static DistanceMatrix<T>? Create(Func<IEnumerable<T>> customListGetter, Func<T, T, object[]?, float> customDistanceCalculator, object[]? distanceCalculatorArgs)

Parameters

customListGetter Func<IEnumerable<T>>

A delegate to the method that will be used to get a list of objects of type T for calculating the distances between them.

customDistanceCalculator Func<T, T, object[], float>

A delegate to the method that will be used to calculate the distance between two given type T objects.

distanceCalculatorArgs object[]

Optional array of arguments that will be passed to the calculation method.

Returns

DistanceMatrix<T>

Exceptions

ArgumentException

GetDistance(T, T)

Gets calculated distance between specified type T objects.

public abstract float GetDistance(T object1, T object2)

Parameters

object1 T

The first of the objects between which it is necessary to determine the distance.

object2 T

The second of the objects between which it is necessary to determine the distance.

Returns

float

A floating-point number representing the distance between two specified MBObjectBase objects; or NaN, if distance was not calculated or it is uncomputable.

GetNearestNeighbours(T, int)

Search for nearest neighbours of the specified type T object.

public abstract IEnumerable<(T OtherObject, float Distance)> GetNearestNeighbours(T inquiredObject, int count)

Parameters

inquiredObject T

Object to search nearest neighbours for.

count int

Number of neighbours to be returned.

Returns

IEnumerable<(T OtherObject, float Distance)>

GetNearestNeighbours(T, int, Func<(T OtherObject, float Distance), bool>)

Search for nearest neighbours of the specified type T object using provided search predicate.

public abstract IEnumerable<(T OtherObject, float Distance)> GetNearestNeighbours(T inquiredObject, int count, Func<(T OtherObject, float Distance), bool> searchPredicate)

Parameters

inquiredObject T

Object to search nearest neighbours for.

count int

Number of neighbours to be returned.

searchPredicate Func<(T OtherObject, float Distance), bool>

A search predicate to filter through neighbours before returning nearest ones that qualify.

Returns

IEnumerable<(T OtherObject, float Distance)>

Remarks

Does not automatically exclude NaN distances.

GetNearestNeighboursNormalized(T, int, Func<(T OtherObject, float Distance), bool>, float, float)

Search for nearest neighbours of the specified type T object using provided search predicate. Then normalize the result to a given range based on all the ditances between specified object and other objects in the Matrix that also qualify the predicate.

public abstract IEnumerable<(T OtherObject, float Distance)> GetNearestNeighboursNormalized(T inquiredObject, int count, Func<(T OtherObject, float Distance), bool> searchPredicate, float scaleMin = 0, float scaleMax = 100)

Parameters

inquiredObject T

Object to search nearest neighbours for.

count int

Number of neighbours to be returned.

searchPredicate Func<(T OtherObject, float Distance), bool>

A search predicate to filter through neighbours before returning nearest ones that qualify.

scaleMin float

Minimum normalized value.

scaleMax float

Maximum normalized value.

Returns

IEnumerable<(T OtherObject, float Distance)>

Remarks

Does not automatically exclude NaN distances.

GetNearestNeighboursNormalized(T, int, float, float)

Search for nearest neighbours of the specified type T object and then normalize the result to a given range based on all the ditances between specified object and other objects in the Matrix.

public abstract IEnumerable<(T OtherObject, float Distance)> GetNearestNeighboursNormalized(T inquiredObject, int count, float scaleMin = 0, float scaleMax = 100)

Parameters

inquiredObject T

Object to search nearest neighbours for.

count int

Number of neighbours to be returned.

scaleMin float

Minimum normalized value.

scaleMax float

Maximum normalized value.

Returns

IEnumerable<(T OtherObject, float Distance)>

Remarks

Automatically excludes NaN distances.

SetDistance(T, T, float)

Sets new distance value for the specified type T objects.

public abstract void SetDistance(T object1, T object2, float distance)

Parameters

object1 T

The first of the objects between which it is necessary to change the predetermined distance.

object2 T

The second of the objects between which it is necessary to change the predetermined distance.

distance float

New distance value.


This page was last modified at 05/14/2024 19:23:00 +03:00 (UTC).

Commit Message
Author:    Alexey Chernyshov
Commit:    84a6e3c9422d20723cd0a3eb1e1c685a2d2b48e4
Typo fix