Skip to content

Commit

Permalink
[COST] Normalize the return value of ClusterCost from `HasClusterCo…
Browse files Browse the repository at this point in the history
…st#of` (#1465)
  • Loading branch information
garyparrot authored Jan 31, 2023
1 parent e253725 commit 9faa0da
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public ClusterCost clusterCost(ClusterInfo clusterInfo, ClusterBean clusterBean)
Collectors.toUnmodifiableMap(
Map.Entry::getKey,
e -> e.getKey().clusterCost(clusterInfo, clusterBean).value()));
var totalWeight = costAndWeight.values().stream().mapToDouble(x -> x).sum();
var compositeScore =
costAndWeight.keySet().stream()
.mapToDouble(cost -> costAndWeight.get(cost) * scores.get(cost))
.mapToDouble(cost -> scores.get(cost) * costAndWeight.get(cost) / totalWeight)
.sum();

return new ClusterCost() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void testMerge() {
HasClusterCost cost2 = (c, b) -> () -> 0.8;
var merged = HasClusterCost.of(Map.of(cost0, 1D, cost1, 2D, cost2, 2D));
var result = merged.clusterCost(null, null).value();
Assertions.assertEquals(2.8, Math.round(result * 100.0) / 100.0);
Assertions.assertEquals(0.56, Math.round(result * 100.0) / 100.0);
}

@Test
Expand Down

0 comments on commit 9faa0da

Please sign in to comment.