Skip to content

Commit

Permalink
removed normalization functionality from score fusion
Browse files Browse the repository at this point in the history
  • Loading branch information
faberf committed Oct 15, 2024
1 parent 16d97ee commit 888ebfc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import kotlin.math.pow
class WeightedScoreFusion(
override val inputs: List<Operator<out Retrievable>>,
weights: List<Float>,
val p: Float,
val normalize: Boolean
val p: Float
) : Aggregator {

private val weights: List<Float> = when {
weights.size > inputs.size -> weights.subList(0, inputs.size - 1)
weights.size < inputs.size -> weights + List(inputs.size - weights.size) {1f}
weights.size < inputs.size -> weights + List(inputs.size - weights.size) { 1f }
else -> weights
}

Expand All @@ -42,7 +41,7 @@ class WeightedScoreFusion(

val inputs = inputs.map { it.toFlow(scope).toList() }

//check if there is more than one populated input, return early if not
// Check if there is more than one populated input, return early if not
if (inputs.filter { it.isNotEmpty() }.size < 2) {
inputs.asSequence().flatten().forEach { emit(it) }
return@flow
Expand All @@ -51,53 +50,39 @@ class WeightedScoreFusion(
val scoreMap = mutableMapOf<RetrievableId, MutableList<Pair<Int, Retrievable>>>()

for ((index, retrieveds) in inputs.withIndex()) {

for (retrieved in retrieveds) {

if (!scoreMap.containsKey(retrieved.id)) {
scoreMap[retrieved.id] = mutableListOf()
}

scoreMap.computeIfAbsent(retrieved.id) { mutableListOf() }
scoreMap[retrieved.id]!!.add(index to retrieved)

}

}

for((_, retrieveds) in scoreMap) {
for ((_, retrieveds) in scoreMap) {

var score : Float
var first : Retrievable
var score: Float
val first: Retrievable

if (p == Float.POSITIVE_INFINITY){
score = retrieveds.map { ((it.second.filteredAttribute(ScoreAttribute::class.java))?.score ?: 0f) }.max()
if (p == Float.POSITIVE_INFINITY) {
// Max score selection when p = infinity
score = retrieveds.map { (it.second.filteredAttribute(ScoreAttribute::class.java)?.score ?: 0f) }
.maxOrNull() ?: 0f

first = retrieveds.first().second
}
else{
val normalization = (retrieveds.map { weights[it.first] }.sum()).pow(1/p)

if (normalization == 0f){
score = 0f
}
else {
score = retrieveds.map {
((it.second.filteredAttribute(ScoreAttribute::class.java))?.score ?: 0f).pow(p) * weights[it.first]
}.sum().pow(1 / p)
if (normalize) score /= normalization
}
} else {
// Compute weighted p-norm score without normalization
score = retrieveds.map {
(it.second.filteredAttribute(ScoreAttribute::class.java)?.score ?: 0f).pow(p) * weights[it.first]
}.sum().pow(1 / p)

first = retrieveds.first().second
}

//make a copy and override score
// Create a copy and override the score
val retrieved = first.copy()
retrieved.filteredAttribute(ScoreAttribute::class.java)
retrieved.addAttribute(ScoreAttribute.Unbound(score))

emit(retrieved)

}

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ class WeightedScoreFusionFactory : AggregatorFactory {
): Aggregator {
val weights = context[name, "weights"]?.split(",")?.mapNotNull { s -> s.trim().toFloatOrNull() } ?: emptyList()
val p = context[name, "p"]?.toFloatOrNull() ?: 1f
val normalize = context[name, "normalize"]?.toBoolean() ?: true
if (p == Float.POSITIVE_INFINITY && weights.isNotEmpty()) {
logger.warn { "Weights are ignored when p is set to infinity" }
}
return WeightedScoreFusion(inputs, weights, p, normalize)
return WeightedScoreFusion(inputs, weights, p)
}
}

0 comments on commit 888ebfc

Please sign in to comment.