| 1 | package Games::Dice::Probability; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 29 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | # Be a Good Module | ||||||||||||||||||||
| 6 | 29 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 29 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | #use diagnostics; | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | # Our version number. | ||||||||||||||||||||
| 11 | 29 | our $VERSION = '0.02'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | # Required Modules | ||||||||||||||||||||
| 14 | # Math::Sumbolic::AuxFunctions is for the calcs of binomial coefficients. | ||||||||||||||||||||
| 15 | # | ||||||||||||||||||||
| 16 | # Parse::RecDescent parses the dice expressions. | ||||||||||||||||||||
| 17 | 29 | ▶ | use Math::Symbolic::AuxFunctions; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 29 | ▶ | use Parse::RecDescent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Optional Modules | ||||||||||||||||||||
| 21 | # Debug::ShowStuff is used by $self->debug() to descend and display hashes | ||||||||||||||||||||
| 22 | # in the object. | ||||||||||||||||||||
| 23 | # | ||||||||||||||||||||
| 24 | # Memoize is used by $self->new() to speed up calculations on individual | ||||||||||||||||||||
| 25 | # nodes in the expression tree. If there are duplicate nodes, the second | ||||||||||||||||||||
| 26 | # and all subsequent calc_distribution calls with the same parameters will | ||||||||||||||||||||
| 27 | # just return the cached values. | ||||||||||||||||||||
| 28 | # | ||||||||||||||||||||
| 29 | # Thanks to Mark Mills for the optional-module code snippet, and general | ||||||||||||||||||||
| 30 | # ideas, tips, and tricks for the whole module. He's my local Perl | ||||||||||||||||||||
| 31 | # Monk...you can email him thanks and offers of cookies at: | ||||||||||||||||||||
| 32 | # extremely{plus}pm{at}hostile{dot}org | ||||||||||||||||||||
| 33 | 29 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=14.2
| |||||||||||||||||||||
| 34 | # Is Debug::ShowStuff available? | ||||||||||||||||||||
| 35 | 29 | ▶ | if ( eval q/ require Debug::ShowStuff / ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | # If so, import the routines. | ||||||||||||||||||||
| 37 | 0 | Debug::ShowStuff->import("showref"); | |||||||||||||||||||
| 38 | } else { | ||||||||||||||||||||
| 39 | # Not available. Place stub in its place. | ||||||||||||||||||||
| 40 | 0 | ▶ | eval q/ sub showref { print @_; print "\n"; } /; | ||||||||||||||||||
|
Subroutine
showref: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # Is Memoize available? | ||||||||||||||||||||
| 44 | 29 | ▶ | if ( eval q/ require Memoize / ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | # If so, import the routines. | ||||||||||||||||||||
| 46 | 29 | Memoize->import(); | |||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||
| 48 | # Not available. Place stub in its place. | ||||||||||||||||||||
| 49 | 0 | eval q/ sub memoize { return; } /; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | # Binomial Coefficient Shortcut | ||||||||||||||||||||
| 54 | # Non-polluted namespaces are cool and all, but sometimes it's too much to | ||||||||||||||||||||
| 55 | # type a 44 character long subroutine name multiple times in a simple | ||||||||||||||||||||
| 56 | # equation. Sheesh. | ||||||||||||||||||||
| 57 | 29 | my $binco = \&Math::Symbolic::AuxFunctions::binomial_coeff; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # Recursive Parsing Grammar | ||||||||||||||||||||
| 60 | # Parsing grammar and tree code outright stolen from Sam Holden's | ||||||||||||||||||||
| 61 | # DiceDistribution.pm at http://sam.holden.id.au/junk/DICEDISTRIBUTION/. | ||||||||||||||||||||
| 62 | # Added a divide expression for division calculations. Changed the way | ||||||||||||||||||||
| 63 | # Fudge Dice are expressed to be #d[fF] which is more inline with how | ||||||||||||||||||||
| 64 | # they are represented elsewhere. Added mid# dice expression as part | ||||||||||||||||||||
| 65 | # of dicenode. | ||||||||||||||||||||
| 66 | 29 | my $DiceGrammar = <<'END_GRAMMAR'; | |||||||||||||||||||
| 67 | expression: add_sub end { $item[1] } | ||||||||||||||||||||
| 68 | add_sub: mult_div '+' add_sub { { left => $item[1], op => '+', right => $item[3] } } | ||||||||||||||||||||
| 69 | add_sub: mult_div '-' add_sub { { left => $item[1], op => '-', right => $item[3] } } | ||||||||||||||||||||
| 70 | add_sub: mult_div | ||||||||||||||||||||
| 71 | mult_div: bracket '/' mult_div { { left => $item[1], op => '/', right => $item[3] } } | ||||||||||||||||||||
| 72 | mult_div: bracket '*' mult_div { { left => $item[1], op => '*', right => $item[3] } } | ||||||||||||||||||||
| 73 | mult_div: bracket | ||||||||||||||||||||
| 74 | bracket: '(' add_sub ')' { $item[2] } | ||||||||||||||||||||
| 75 | bracket: dicenode | ||||||||||||||||||||
| 76 | dicenode: /(\d+|mi)d(\d+|f)/i | ||||||||||||||||||||
| 77 | dicenode: /\d+/ | ||||||||||||||||||||
| 78 | end: /\s*$/ | ||||||||||||||||||||
| 79 | END_GRAMMAR | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # Dice Parsing Object | ||||||||||||||||||||
| 82 | 29 | ▶ | my $DiceParser = Parse::RecDescent->new($DiceGrammar) || die("bad grammar"); | ||||||||||||||||||
|
Condition50%1/2
'Parse::RecDescent'->new($DiceGrammar) || die('bad grammar')
MC/DC0%0/1
'Parse::RecDescent'->new($DiceGrammar) || die('bad grammar')
'Parse::RecDescent'->new($DiceGrammar)
Truth table50%1/2
A
A= 'Parse::RecDescent'->new($DiceGrammar)
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # import() | ||||||||||||||||||||
| 85 | # Faux import function that either memoizes the calc portions of the | ||||||||||||||||||||
| 86 | # module (default) or doesn't. | ||||||||||||||||||||
| 87 | 29 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 88 | # Default is to memoize. Saving cycles is a Good Thing. However, if | ||||||||||||||||||||
| 89 | # someone passes an unmemoize argument, then we will respect their wishes. | ||||||||||||||||||||
| 90 | 29 | ▶ | if ( ! grep(/(un|no)memo(ize)*/i,@_) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | # Attempt to memoize the calculation subroutines. This will either | ||||||||||||||||||||
| 92 | # truly memoize, or the stub memoize function will simply return having | ||||||||||||||||||||
| 93 | # done nothing. | ||||||||||||||||||||
| 94 | 29 | memoize('calc_distribution'); | |||||||||||||||||||
| 95 | # Not certain if calc_combination will benefit from Memoize. Need to | ||||||||||||||||||||
| 96 | # test further. | ||||||||||||||||||||
| 97 | 29 | memoize('calc_combination'); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | # All is well. Return. | ||||||||||||||||||||
| 101 | 29 | return; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | # debug() | ||||||||||||||||||||
| 105 | # Print debugging information about object. | ||||||||||||||||||||
| 106 | 0 | ▶ | sub debug { | ||||||||||||||||||
|
Subroutine
debug: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 107 | # The object of our attention. | ||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # For every piece of the object... | ||||||||||||||||||||
| 111 | 0 | foreach my $key ( sort(keys(%$self)) ) { | |||||||||||||||||||
| 112 | # Output the name... | ||||||||||||||||||||
| 113 | 0 | print "self->{$key}="; | |||||||||||||||||||
| 114 | # And if it is a reference... | ||||||||||||||||||||
| 115 | 0 | ▶ | if ( ref($self->{$key}) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | # Print the contents of the reference... | ||||||||||||||||||||
| 117 | 0 | print "\n"; | |||||||||||||||||||
| 118 | 0 | showref($self->{$key}); | |||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||
| 120 | # Or, print the value. | ||||||||||||||||||||
| 121 | 0 | print $self->{$key} . "\n"; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | # All is well. | ||||||||||||||||||||
| 126 | 0 | return(0); | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | # new(expression) | ||||||||||||||||||||
| 130 | # Creates a new object based on the provided dice expression. | ||||||||||||||||||||
| 131 | 29 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||
| 132 | 29 | my $invocant = shift; | |||||||||||||||||||
| 133 | 29 | ▶ | my $class = ref($invocant) || $invocant; | ||||||||||||||||||
|
Condition33%1/3
ref $invocant || $invocant
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant
$invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
| |||||||||||||||||||||
| 134 | 29 | my $self = {}; | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 29 | ▶ | die("must provide dice expression") unless (@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 29 | ▶ | if (@_ != 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 0 | die("new() called with too many arguments"); | |||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||
| 141 | 29 | ▶ | $self->{EXPRESSION} = $DiceParser->expression(@_) || die "could not parse expression"; | ||||||||||||||||||
|
Condition50%1/2
$DiceParser->expression(@_) || die('could not parse expression')
MC/DC0%0/1
$DiceParser->expression(@_) || die('could not parse expression')
$DiceParser->expression(@_)
Truth table50%1/2
A
A= $DiceParser->expression(@_)
| |||||||||||||||||||||
| 142 | 29 | ▶ | $self->{DISTRIBUTION} = travel($self->{EXPRESSION}) || die "could not travel parsed expression"; | ||||||||||||||||||
|
Condition50%1/2
travel($self->{'EXPRESSION'}) || die('could not travel parsed expression')
MC/DC0%0/1
travel($self->{'EXPRESSION'}) || die('could not travel parsed expression')
travel($self->{'EXPRESSION'})
Truth table50%1/2
A
A= travel($self->{'EXPRESSION'})
| |||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 29 | return(bless($self, $class)); | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | # travel(expression) | ||||||||||||||||||||
| 149 | # Travel the parsed expression, returning hash of value => permutations. | ||||||||||||||||||||
| 150 | 61 | ▶ | sub travel { | ||||||||||||||||||
|
Subroutine
travel: called CC=12 SCAR=25.1
Pod
undocumented
| |||||||||||||||||||||
| 151 | 61 | ▶ | my $node = shift || die "invalid or missing expression node"; | ||||||||||||||||||
|
Condition50%1/2
shift() || die('invalid or missing expression node')
MC/DC0%0/1
shift() || die('invalid or missing expression node')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 61 | ▶ | if ( ref($node) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 16 | for ($node->{op}) { | |||||||||||||||||||
| 155 | 16 | ▶ | /(\+)/ && return( calc_combination($1, travel($node->{left}), travel($node->{right})) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | 9 | ▶ | /(\-)/ && return( calc_combination($1, travel($node->{left}), travel($node->{right})) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 6 | ▶ | /(\*)/ && return( calc_combination($1, travel($node->{left}), travel($node->{right})) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 2 | ▶ | /(\/)/ && return( calc_combination($1, travel($node->{left}), travel($node->{right})) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 45 | for ($node) { | |||||||||||||||||||
| 163 | 45 | ▶ | /^(\d+)(d)(\d+|f)/i && return( calc_distribution($2,$1,$3) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 164 | 8 | ▶ | /^(mi)(d)(\d+|f)/i && return( calc_distribution("m",3,$3) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 2 | ▶ | /^\d+$/ && return( {$node => 1} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 0 | die("invalid token in expression."); | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | # calc_distribution(method,numdice,numsides) | ||||||||||||||||||||
| 172 | # Calculate distribution of values/permutations given: method, number of | ||||||||||||||||||||
| 173 | # dice, and number of sides per die. | ||||||||||||||||||||
| 174 | 41 | ▶ | sub calc_distribution { | ||||||||||||||||||
|
Subroutine
calc_distribution: called CC=11 SCAR=24.0
| |||||||||||||||||||||
| 175 | # The dice method requested. | ||||||||||||||||||||
| 176 | 41 | my $method = shift; | |||||||||||||||||||
| 177 | # The number of dice. | ||||||||||||||||||||
| 178 | 41 | my $n = shift; $n += 0; | |||||||||||||||||||
| 179 | # The number of sides/faces on each die. | ||||||||||||||||||||
| 180 | # f/F = Fudge dice = d3. | ||||||||||||||||||||
| 181 | 41 | ▶ | my $s = shift; $s += 0 unless $s =~ /f/i; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | # Loop value based on total/face. | ||||||||||||||||||||
| 183 | 41 | my $t1; | |||||||||||||||||||
| 184 | # Hash to return containing the distribution. | ||||||||||||||||||||
| 185 | my %dist; | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | # If dice node method is simple-sum nDs or nDf... | ||||||||||||||||||||
| 188 | 41 | ▶ | if ( $method =~ /d/i ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 189 | 35 | ▶ | if ( $s =~ /f/i ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 190 | # If dice node method is fudge nDf... | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | # First, get the distribution for nDs... | ||||||||||||||||||||
| 193 | 6 | my $tempdist = calc_distribution("d",$n,3); | |||||||||||||||||||
| 194 | # Then loop to build the new distribution from (-n..0)... | ||||||||||||||||||||
| 195 | 6 | my $origkey = $n; | |||||||||||||||||||
| 196 | 6 | ▶ | for ($t1=-1 * $n; $t1 <= 0; $t1++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | # Changing the original values in (n..ns) to (-n..n), | ||||||||||||||||||||
| 198 | # copy the original distribution to the new one with | ||||||||||||||||||||
| 199 | # the correct values... | ||||||||||||||||||||
| 200 | 27 | $dist{$t1} = $$tempdist{$origkey}; | |||||||||||||||||||
| 201 | 27 | $dist{$t1*-1} = $$tempdist{$origkey}; | |||||||||||||||||||
| 202 | 27 | $origkey++; | |||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | } elsif ( $n eq 1 ) { | ||||||||||||||||||||
| 205 | # Save compute cycles if only one die... | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | # Each face has a single chance... | ||||||||||||||||||||
| 208 | 4 | ▶ | for ($t1=1; $t1 <= $s; $t1++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 209 | 19 | $dist{$t1} = 1; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | } else { | ||||||||||||||||||||
| 212 | # Else compute number of combinations for a total on the face of n | ||||||||||||||||||||
| 213 | # dice. | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | # Minimum and maximum sums. | ||||||||||||||||||||
| 216 | 25 | my $tmin = $n; | |||||||||||||||||||
| 217 | 25 | my $tmax = $n*$s; | |||||||||||||||||||
| 218 | # Peak sum is the sum around which the bell-curve mirrors, saving | ||||||||||||||||||||
| 219 | # compute time. | ||||||||||||||||||||
| 220 | 25 | my $tpeak = ($tmin+$tmax)/2; | |||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | # For each total (t1) in $tmin..$tmax, calculate the number of | ||||||||||||||||||||
| 223 | # combinations giving that total. | ||||||||||||||||||||
| 224 | 25 | ▶ | for ($t1=$tmin; $t1 <= $tpeak; $t1++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | # Total (t2) that is the mirror point in the bell curve. | ||||||||||||||||||||
| 226 | 188 | my $t2 = $tmin + $tmax - $t1; | |||||||||||||||||||
| 227 | # Ceiling for the sum function. | ||||||||||||||||||||
| 228 | 188 | my $ceil = int( ($t1-$n) / $s ); | |||||||||||||||||||
| 229 | # Result. | ||||||||||||||||||||
| 230 | 188 | my $res = 0; | |||||||||||||||||||
| 231 | # Loop control for the sum funtion. | ||||||||||||||||||||
| 232 | 188 | my $k; | |||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | # Sum Function: For each k in 0 to $ceil... | ||||||||||||||||||||
| 235 | 188 | ▶ | for ($k=0; $k <= $ceil; $k++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 236 | # Calculate and add to previous results. | ||||||||||||||||||||
| 237 | 246 | $res += ((-1)**$k) * &$binco($n,$k) * &$binco(($t1-($s*$k)-1),($n-1)); | |||||||||||||||||||
| 238 | } | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | # Set the mirrored points of the distribution. | ||||||||||||||||||||
| 241 | # Note: $t1 and $t2 can be equal once at | ||||||||||||||||||||
| 242 | # $t1=$tpeak when s is even. | ||||||||||||||||||||
| 243 | 188 | $dist{$t1} = $res; | |||||||||||||||||||
| 244 | 188 | $dist{$t2} = $res; | |||||||||||||||||||
| 245 | } | ||||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | } elsif ( $method =~ /m/i ) { | ||||||||||||||||||||
| 248 | # If dice method is take-the-middle-value nMs... | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | # Minimum and maximum values. | ||||||||||||||||||||
| 251 | 6 | my $tmin = 1; | |||||||||||||||||||
| 252 | 6 | my $tmax = $s; | |||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | # For each value (t1) in $tmin..$tmax, calculate the number of | ||||||||||||||||||||
| 255 | # combinations giving t1 as the middle face value. | ||||||||||||||||||||
| 256 | 6 | ▶ | for ($t1=$tmin; $t1 <= $tmax; $t1++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 257 | 60 | $dist{$t1} = 1 + ( 3 * ($s - 1) ) + ( 6 * ($t1 - 1) * ($s - $t1) ); | |||||||||||||||||||
| 258 | } | ||||||||||||||||||||
| 259 | } | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | # Return the value=>combinations distribution hash for this node. | ||||||||||||||||||||
| 262 | 41 | return({%dist}); | |||||||||||||||||||
| 263 | } | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | # calc_combination(operand,distribution1,distribution2) | ||||||||||||||||||||
| 266 | # Combine two distributions with the method provided. | ||||||||||||||||||||
| 267 | 16 | ▶ | sub calc_combination { | ||||||||||||||||||
|
Subroutine
calc_combination: called CC=8 SCAR=20.8
| |||||||||||||||||||||
| 268 | # Calculation to perform on values. | ||||||||||||||||||||
| 269 | 16 | my $op = shift; | |||||||||||||||||||
| 270 | # Distributions to combine. | ||||||||||||||||||||
| 271 | 16 | my $dist1 = shift; | |||||||||||||||||||
| 272 | 16 | my $dist2 = shift; | |||||||||||||||||||
| 273 | # The combined distribution. | ||||||||||||||||||||
| 274 | 16 | my %cdist; | |||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | # For each value in the first distribution... | ||||||||||||||||||||
| 277 | 16 | foreach my $val1 ( sort {$a+0 <=> $b+0} keys(%$dist1) ) { | |||||||||||||||||||
| 278 | # Combine it with every value in the second distribution... | ||||||||||||||||||||
| 279 | 206 | foreach my $val2 ( sort {$a+0 <=> $b+0} keys(%$dist2) ) { | |||||||||||||||||||
| 280 | # The new value of which is calculated based on combine method... | ||||||||||||||||||||
| 281 | 28724 | my $newval; | |||||||||||||||||||
| 282 | 28724 | for ($op) { | |||||||||||||||||||
| 283 | 28724 | ▶ | /\+/ && do { $newval = $val1 + $val2 }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 284 | 28724 | ▶ | /\-/ && do { $newval = $val1 - $val2 }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 285 | 28724 | ▶ | /\*/ && do { $newval = $val1 * $val2 }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 286 | 28724 | ▶ | /\// && do { $newval = int($val1 / $val2) }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 287 | } | ||||||||||||||||||||
| 288 | # Calculate the new combined combinations and set it to the new | ||||||||||||||||||||
| 289 | # value in the new distribution. | ||||||||||||||||||||
| 290 | 28724 | $cdist{$newval} += $$dist1{$val1} * $$dist2{$val2}; | |||||||||||||||||||
| 291 | } | ||||||||||||||||||||
| 292 | } | ||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | # Return the combined distribution. | ||||||||||||||||||||
| 295 | 16 | return({%cdist}); | |||||||||||||||||||
| 296 | } | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | # combinations(targetvalue) | ||||||||||||||||||||
| 299 | # Calculate number of combinations for a target value. | ||||||||||||||||||||
| 300 | 303 | ▶ | sub combinations { | ||||||||||||||||||
|
Subroutine
combinations: called CC=7 SCAR=20.4
Pod
undocumented
| |||||||||||||||||||||
| 301 | 303 | my $self = shift; | |||||||||||||||||||
| 302 | 303 | my $targetvalue = shift; | |||||||||||||||||||
| 303 | |||||||||||||||||||||
| 304 | 303 | ▶ | $targetvalue = "ALL" unless defined($targetvalue); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | 303 | ▶ | if ( ref($self) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 307 | 303 | ▶ | if (@_ != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 308 | 0 | die("combinations() called incorrectly"); | |||||||||||||||||||
| 309 | } else { | ||||||||||||||||||||
| 310 | 303 | ▶ | if ( $targetvalue eq "ALL" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 311 | 28 | ▶ | if ( $self->{COMBINATIONS} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 312 | 0 | return ( $self->{COMBINATIONS} ); | |||||||||||||||||||
| 313 | } else { | ||||||||||||||||||||
| 314 | 28 | foreach my $value ( values(%{$self->{DISTRIBUTION}}) ) { | |||||||||||||||||||
| 315 | 12201 | $self->{COMBINATIONS} += $value; | |||||||||||||||||||
| 316 | } | ||||||||||||||||||||
| 317 | 28 | return($self->{COMBINATIONS}); | |||||||||||||||||||
| 318 | } | ||||||||||||||||||||
| 319 | } else { | ||||||||||||||||||||
| 320 | 275 | return( $self->{DISTRIBUTION}->{$targetvalue} ); | |||||||||||||||||||
| 321 | } | ||||||||||||||||||||
| 322 | } | ||||||||||||||||||||
| 323 | } else { | ||||||||||||||||||||
| 324 | 0 | die("combinations() called on non-object"); | |||||||||||||||||||
| 325 | } | ||||||||||||||||||||
| 326 | } | ||||||||||||||||||||
| 327 | |||||||||||||||||||||
| 328 | # distribution() | ||||||||||||||||||||
| 329 | # Returns a hash containing the distribution in value=>combinations format. | ||||||||||||||||||||
| 330 | 0 | ▶ | sub distribution { | ||||||||||||||||||
|
Subroutine
distribution: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 331 | 0 | my $self = shift; | |||||||||||||||||||
| 332 | |||||||||||||||||||||
| 333 | 0 | ▶ | if ( ref($self) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 334 | 0 | ▶ | if (@_ != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 335 | 0 | die("distribution() called with argument on object"); | |||||||||||||||||||
| 336 | } else { | ||||||||||||||||||||
| 337 | 0 | return( $self->{DISTRIBUTION} ); | |||||||||||||||||||
| 338 | } | ||||||||||||||||||||
| 339 | } else { | ||||||||||||||||||||
| 340 | 0 | ▶ | if (@_ != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 341 | 0 | ▶ | my $expression = $DiceParser->expression(@_) || die "could not parse expression"; | ||||||||||||||||||
|
Condition0%0/2
$DiceParser->expression(@_) || die('could not parse expression')
MC/DC0%0/1
$DiceParser->expression(@_) || die('could not parse expression')
$DiceParser->expression(@_)
Truth table0%0/2
A
A= $DiceParser->expression(@_)
| |||||||||||||||||||||
| 342 | 0 | return( travel($expression) ); | |||||||||||||||||||
| 343 | } else { | ||||||||||||||||||||
| 344 | 0 | die("no expression provided for non-object distribution() call"); | |||||||||||||||||||
| 345 | } | ||||||||||||||||||||
| 346 | } | ||||||||||||||||||||
| 347 | } | ||||||||||||||||||||
| 348 | |||||||||||||||||||||
| 349 | # probability(targetvalue) | ||||||||||||||||||||
| 350 | # Returns the probability for targetvalue, or a hash of probabilities in | ||||||||||||||||||||
| 351 | # value=>probability format. | ||||||||||||||||||||
| 352 | 12201 | ▶ | sub probability { | ||||||||||||||||||
|
Subroutine
probability: called CC=8 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||
| 353 | 12201 | my $self = shift; | |||||||||||||||||||
| 354 | 12201 | my $targetvalue = shift; | |||||||||||||||||||
| 355 | |||||||||||||||||||||
| 356 | 12201 | ▶ | $targetvalue = "ALL" unless defined($targetvalue); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 357 | |||||||||||||||||||||
| 358 | 12201 | ▶ | if ( ref($self) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 359 | 12201 | ▶ | if (@_ != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 360 | 0 | die("probability() called incorrectly"); | |||||||||||||||||||
| 361 | } else { | ||||||||||||||||||||
| 362 | 12201 | ▶ | if ( ! exists($self->{PROBABILITIES}) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 363 | 28 | my $combs = $self->combinations(); | |||||||||||||||||||
| 364 | 28 | my %probs; | |||||||||||||||||||
| 365 | 28 | foreach my $value ( keys(%{$self->{DISTRIBUTION}}) ) { | |||||||||||||||||||
| 366 | 12201 | $probs{$value} = $self->{DISTRIBUTION}->{$value} / $combs; | |||||||||||||||||||
| 367 | } | ||||||||||||||||||||
| 368 | 28 | $self->{PROBABILITIES} = {%probs}; | |||||||||||||||||||
| 369 | } | ||||||||||||||||||||
| 370 | 12201 | ▶ | if ( $targetvalue eq "ALL" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 371 | 0 | return( $self->{PROBABILITIES} ); | |||||||||||||||||||
| 372 | } else { | ||||||||||||||||||||
| 373 | 12201 | ▶ | return( $self->{PROBABILITIES}->{$targetvalue} || undef ); | ||||||||||||||||||
|
Condition50%1/2
$self->{'PROBABILITIES'}{$targetvalue} || undef
MC/DC0%0/1
$self->{'PROBABILITIES'}{$targetvalue} || undef
$self->{'PROBABILITIES'}{$targetvalue}
Truth table50%1/2
A
A= $self->{'PROBABILITIES'}{$targetvalue}
| |||||||||||||||||||||
| 374 | } | ||||||||||||||||||||
| 375 | } | ||||||||||||||||||||
| 376 | } else { | ||||||||||||||||||||
| 377 | 0 | die("probability() called on non-object"); | |||||||||||||||||||
| 378 | } | ||||||||||||||||||||
| 379 | } | ||||||||||||||||||||
| 380 | |||||||||||||||||||||
| 381 | # bounds() | ||||||||||||||||||||
| 382 | # Returns the min and max values of the valueset. | ||||||||||||||||||||
| 383 | 56 | ▶ | sub bounds { | ||||||||||||||||||
|
Subroutine
bounds: called CC=3 SCAR=12.7
Pod
undocumented
| |||||||||||||||||||||
| 384 | 56 | my $self = shift; | |||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | 56 | ▶ | if ( ref($self) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 387 | 56 | ▶ | if (@_ != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 388 | 0 | die("bounds() called with argument on object"); | |||||||||||||||||||
| 389 | } else { | ||||||||||||||||||||
| 390 | 56 | return( [ $self->min(), $self->max() ] ); | |||||||||||||||||||
| 391 | } | ||||||||||||||||||||
| 392 | } else { | ||||||||||||||||||||
| 393 | 0 | die("bounds() called on non-object"); | |||||||||||||||||||
| 394 | } | ||||||||||||||||||||
| 395 | } | ||||||||||||||||||||
| 396 | |||||||||||||||||||||
| 397 | # max() | ||||||||||||||||||||
| 398 | # Returns the max value of the valueset. | ||||||||||||||||||||
| 399 | 84 | ▶ | sub max { | ||||||||||||||||||
|
Subroutine
max: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||
| 400 | 84 | my $self = shift; | |||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | 84 | ▶ | if ( ref($self) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 403 | 84 | ▶ | if (@_ != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 404 | 0 | die("max() called with argument on object"); | |||||||||||||||||||
| 405 | } else { | ||||||||||||||||||||
| 406 | 84 | ▶ | if ( $self->{MAX} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 407 | 56 | return( $self->{MAX} ); | |||||||||||||||||||
| 408 | } else { | ||||||||||||||||||||
| 409 | 28 | my @values = sort {$b+0 <=> $a+0} keys(%{$self->{DISTRIBUTION}}); | |||||||||||||||||||
| 410 | 28 | $self->{MAX} = shift(@values); | |||||||||||||||||||
| 411 | 28 | return( $self->{MAX} ); | |||||||||||||||||||
| 412 | } | ||||||||||||||||||||
| 413 | } | ||||||||||||||||||||
| 414 | } else { | ||||||||||||||||||||
| 415 | 0 | die("max() called on non-object"); | |||||||||||||||||||
| 416 | } | ||||||||||||||||||||
| 417 | } | ||||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | # min() | ||||||||||||||||||||
| 420 | # Returns the min value of the valueset. | ||||||||||||||||||||
| 421 | 84 | ▶ | sub min { | ||||||||||||||||||
|
Subroutine
min: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||
| 422 | 84 | my $self = shift; | |||||||||||||||||||
| 423 | |||||||||||||||||||||
| 424 | 84 | ▶ | if ( ref($self) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 425 | 84 | ▶ | if (@_ != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 426 | 0 | die("min() called with argument on object"); | |||||||||||||||||||
| 427 | } else { | ||||||||||||||||||||
| 428 | 84 | ▶ | if ( $self->{MIN} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 429 | 52 | return( $self->{MIN} ); | |||||||||||||||||||
| 430 | } else { | ||||||||||||||||||||
| 431 | 32 | my @values = sort {$a+0 <=> $b+0} keys(%{$self->{DISTRIBUTION}}); | |||||||||||||||||||
| 432 | 32 | $self->{MIN} = shift(@values); | |||||||||||||||||||
| 433 | 32 | return( $self->{MIN} ); | |||||||||||||||||||
| 434 | } | ||||||||||||||||||||
| 435 | } | ||||||||||||||||||||
| 436 | } else { | ||||||||||||||||||||
| 437 | 0 | die("min() called on non-object"); | |||||||||||||||||||
| 438 | } | ||||||||||||||||||||
| 439 | } | ||||||||||||||||||||
| 440 | |||||||||||||||||||||
| 441 | 29 | 1; | |||||||||||||||||||
| 442 | |||||||||||||||||||||
| 443 | __END__ | ||||||||||||||||||||