From ec9753ad71bb2f9684e4e13503a855d751d74d08 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Fri, 14 Jul 2023 21:07:48 +0200 Subject: [PATCH] Implement Cost --- .../org/pgpainless/wot/dijkstra/Cost.kt | 36 +++++++++++++++++++ .../org/pgpainless/wot/dijkstra/CostTest.kt | 29 +++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/Cost.kt create mode 100644 wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CostTest.kt diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/Cost.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/Cost.kt new file mode 100644 index 00000000..c06bcf81 --- /dev/null +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/Cost.kt @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2023 Heiko Schaefer +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.wot.dijkstra + +import kotlin.math.min + +/** + * A path's "cost". + * + * This Cost type is used for comparison in Dijkstra's algorithm. + */ +internal class Cost( + // The path's length (i.e., the number of hops to the target). + // *Less* length is "cheaper" (short paths require less "depth" in delegations). + val length: Int, + + // The trust amount along this path. + // Smaller trust amount is "more expensive" + // (paths with a low trust amount are more constraining and thus less desirable). + val amount: Int, +) : Comparable { + + // "Smaller than" means: the path is "cheaper", and thus preferable: + // - A small length (requiring fewer hops). + // - For equal length: A higher "trust amount" (which is less constraining) + override fun compareTo(other: Cost) = + compareValuesBy(this, other, { it.length }, { -it.amount }) + + override fun toString() = "length $length, amount $amount" + + /** Calculate the cost of a path, when adding a new segment with trust amount `amount` */ + fun extendBy(amount: Int) = Cost(this.length + 1, min(amount, this.amount)) + +} \ No newline at end of file diff --git a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CostTest.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CostTest.kt new file mode 100644 index 00000000..a1db56d8 --- /dev/null +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/dijkstra/CostTest.kt @@ -0,0 +1,29 @@ +// SPDX-FileCopyrightText: 2023 Heiko Schaefer +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.wot.dijkstra + +import kotlin.test.Test + +class CostTest { + + @Test + fun cost() { + val cost1 = Cost(1, 60) + val cost2 = Cost(1, 120) + + val cost3 = Cost(2, 60) + val cost4 = Cost(2, 120) + + assert(cost1 > cost2) // cost2 is "cheaper": it constrains the amount of the path less + assert(cost1 < cost3) // cost2 is "cheaper": it costs fewer hops + + assert(cost2 < cost3) + assert(cost3 > cost4) + + assert(cost1 < cost4) // cost1 is "cheaper": even though it constrains the amount more, it costs fewer hops + assert(cost2 < cost4) + } + +} \ No newline at end of file