0

I have a weighted set implementation that works off a map in the data structure:

class WeightedSet<T> {
    Map<T, Double> weights = new HashMap<>();
    double sum;
}

This implementation was built with the intention of many elements being added and possibly removed (think of rolling rewards from a loot table in a videogame)
Now consider the set:

var set = new WeightedSet<String>();
set.add("A", 12.0);
set.add("B", 3.0);

If I wanted to increase the chances of B being picked from this set by 15% I would not simply be able to change the weight to set.add("B", 3.0 * 1.15); as this only increases the weight by 1.15, and not the probability that it would be chosen by 1.15.

To get a weight increase that would accurately represent a probability pick chance increase of 15%, I would use the following equation which equates the desired increased percent pick chance with a value that represents the multiplied pick rate:

(dWeight + weight) / (sum + dweight) = weight / sum * multiplier
->
(dWeight + 3.0) / ((12.0 + 3.0) + dweight) = 3.0 / (12.0 + 3.0) * 1.15
->
dWeight = 0.58442
changedWeight = 3.58442

We can see that changedWeight does not equal the simple weight multiplier of 3.0 * 1.15 = 3.45 which is what we expect and want.

This works well for calculating the weight increase of a single value. I however, am stumped on how to calculate percent increases of multiple values at the same time as calculating one requires holding the others static but once one is calculated, subsequent weight changes will just affect its pick rate. So far I've concluded that they all need to be calculated at the same time via a system of equations. This is hard for programmers to do, and thus why I've come here to ask for advice for this issue if anyone's faced it in the past or has a solution.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.