Skip to content

Commit

Permalink
smarmellata
Browse files Browse the repository at this point in the history
  • Loading branch information
DanySK committed Jan 28, 2025
1 parent 02b73fc commit 48c84a1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions alchemist-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
api(libs.jool)
api(libs.listset)
implementation(libs.kotlin.reflect)
implementation(libs.symmetric.matrix)

testImplementation(alchemist("euclidean-geometry"))
testImplementation(alchemist("physics"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package it.unibo.alchemist.model

import it.unibo.alchemist.core.Simulation
import org.danilopianini.symmetricmatrix.MutableDoubleSymmetricMatrix
import org.danilopianini.symmetricmatrix.SymmetricMatrix
import org.danilopianini.util.ListSet
import java.io.Serializable
import kotlin.math.max
Expand Down Expand Up @@ -238,41 +240,48 @@ interface Environment<T, P : Position<out P>> :
// diameters
// }

fun shortestHopPaths() = shortestPaths { n1, n2 ->
when {
n1 == n2 -> 0.0
getNeighborhood(n1).contains(n2) -> 1.0
else -> Double.POSITIVE_INFINITY
}
}

/**
* Computes all the minimum distances using the Floyd–Warshall algorithm
*/
fun floydWarshall(computeDistance: (Node<T>, Node<T>) -> Double = { _, _ -> 1.0 }) {
val nodes = nodes.toList()
/*
* The distances matrix is a triangular matrix stored in a flat array.
*/
val distances = DoubleArray(nodeCount * nodeCount / 2) { Double.POSITIVE_INFINITY }
fun indexOf(a: Int, b: Int): Int = min(a, b).let { min ->
require(min >= 0) { "Invalid index $min" }
min * (2 * nodeCount - min - 1) / 2 + max(a, b).also { max ->
require(max < nodeCount) { "Invalid index $max" }
fun shortestPaths(
computeDistance: (Node<T>, Node<T>) -> Double = { n1, n2 ->
when {
n1 == n2 -> 0.0
getNeighborhood(n1).contains(n2) -> getDistanceBetweenNodes(n1, n2)
else -> Double.POSITIVE_INFINITY
}
}
operator fun DoubleArray.get(i: Int, j: Int) = this[indexOf(i, j)]
operator fun DoubleArray.set(i: Int, j: Int, value: Double) {
this[indexOf(i, j)] = value
}
): Map<Pair<Node<T>, Node<T>>, Double> {
val nodes = nodes.toList()
/*
* Distance with self is always zero
* The distances matrix is a triangular matrix stored in a flat array.
*/
val distances = MutableDoubleSymmetricMatrix(nodeCount)
for (i in 0 until nodeCount) {
distances[i, i] = 0.0
for (j in i + 1 until nodeCount) {
distances[i, j] = computeDistance(nodes[i], nodes[j])
}
}
val result = LinkedHashMap<Pair<Node<T>, Node<T>>, Double>(nodes.size * (nodes.size + 1) / 2, 1.0f)
for (cycle in 0 until nodeCount) {
for (i in 0 until nodeCount) {
for (j in i + 1 until nodeCount) {
if (distances[i, j] > distances[i, cycle] + distances[cycle, j]) {
distances[i, j] = distances[i, cycle] + distances[cycle, j]
result.put(nodes[i] to nodes[j], distances[i, j])
}
// distances[i, j] = computeDistance(nodes[i], nodes[j])
}
}
}
return result
}

/**
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ simplelatlng = "com.javadocmd:simplelatlng:1.4.0"
slf4j = "org.slf4j:slf4j-api:2.0.16"
snakeyaml = "org.yaml:snakeyaml:2.3"
spotbugs-annotations = "com.github.spotbugs:spotbugs-annotations:4.9.0"
symmetric-matrix = "org.danilopianini:kotlin-symmetric-matrix:1.0.0"
redux-kotlin-threadsafe = "org.reduxkotlin:redux-kotlin-threadsafe:0.6.1"
svgsalamander = "guru.nidi.com.kitfox:svgSalamander:1.1.3"
trove4j = "net.sf.trove4j:trove4j:3.0.3"
Expand Down

0 comments on commit 48c84a1

Please sign in to comment.