I have some events where each of them has a chance to occur, and weight, if they do. I want to create all possible combinations of event probabilities with corresponding weights. In the end, I need them in order of weight. This is similar to generating a probability tree, but I only care about the leaves received, and not about which nodes he took to get them. I donβt need to look for specific records while creating the final result, just to create all the values ββand sort them by weight.
There will be only about 5-15 events, but since there are 2 ^ n resulting opportunities with n events, and this needs to be done very often, I do not want this to take too much time. Speed ββis much more important than the amount of storage used.
The solution I came up with is slow but slow. Any idea for a quicker solution or some ideas for improvement?
class ProbWeight { double prob; double eventWeight; public ProbWeight(double aProb, double aeventWeight) { prob = aProb; eventWeight = aeventWeight; } public ProbWeight(ProbWeight aCellProb) { prob = aCellProb.getProb(); eventWeight = aCellProb.geteventWeight(); } public double getProb(){ return prob; } public double geteventWeight(){ return eventWeight; } public void doesHappen(ProbWeight aProb) { prob*=aProb.getProb(); eventWeight += aProb.geteventWeight(); } public void doesNotHappen(ProbWeight aProb) { prob*=(1-aProb.getProb()); } }
Siana source share