From ab37759687c9f5909c43aefa36f338d2c8ea73df Mon Sep 17 00:00:00 2001 From: Leo Strobel Date: Fri, 5 Apr 2024 12:13:59 +0200 Subject: [PATCH] [BUGFIX] Unique IDs for DC-Functions --- .../omod/core/LocationChoiceDCWeightFun.kt | 26 ++++++++++++------- .../uniwuerzburg/omod/core/LocationOption.kt | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/de/uniwuerzburg/omod/core/LocationChoiceDCWeightFun.kt b/src/main/kotlin/de/uniwuerzburg/omod/core/LocationChoiceDCWeightFun.kt index 514585f4..e45f29aa 100644 --- a/src/main/kotlin/de/uniwuerzburg/omod/core/LocationChoiceDCWeightFun.kt +++ b/src/main/kotlin/de/uniwuerzburg/omod/core/LocationChoiceDCWeightFun.kt @@ -7,6 +7,21 @@ import kotlinx.serialization.Transient import kotlin.math.exp import kotlin.math.ln +/** + * Return unique IDs used for the destination choice functions. + * These IDs are used to cache a location's attraction value, as calculated with the function, + * inside the location itself. + */ +object IDDispenser { + var nextID = 0 + + fun next() : Int { + val id = nextID + nextID += 1 + return id + } +} + /** * Destination choice model. Parent class. */ @@ -37,16 +52,7 @@ sealed class LocationChoiceDCWeightFun { abstract val coeffIndustrialUnits: Double @Transient - private var id: Int? = null - - fun getUniqueID() : Int { - id?.let { - return it - } ?: run { - id = hashCode() - return id!! - } - } + val id: Int = IDDispenser.next() /** * Calculates the natural logarithm of the deterrence function given the distance from the origin. diff --git a/src/main/kotlin/de/uniwuerzburg/omod/core/LocationOption.kt b/src/main/kotlin/de/uniwuerzburg/omod/core/LocationOption.kt index 1b77db95..7d54b021 100644 --- a/src/main/kotlin/de/uniwuerzburg/omod/core/LocationOption.kt +++ b/src/main/kotlin/de/uniwuerzburg/omod/core/LocationOption.kt @@ -71,7 +71,7 @@ class Building ( val properties = it.properties val point = transformer.toModelCRS( it.geometry.toJTS(geometryFactory) ).centroid - val attractions = dcFunctions.map { (_, v) -> v.getUniqueID() to v.calcAttraction(properties)}.toMap() + val attractions = dcFunctions.map { (_, v) -> v.id to v.calcAttraction(properties)}.toMap() Building( osmID = properties.osm_id,