dyce.evaluation
package reference
HResult
Bases: NamedTuple
Source code in dyce/evaluation.py
51 52 53 |
|
h: H
instance-attribute
outcome: RealLike
instance-attribute
PResult
Bases: NamedTuple
Source code in dyce/evaluation.py
56 57 58 |
|
p: P
instance-attribute
roll: RollT
instance-attribute
PWithSelection
Bases: NamedTuple
Source code in dyce/evaluation.py
61 62 63 64 65 66 67 |
|
p: P
instance-attribute
total: int
property
which: Iterable[_GetItemT] = ()
class-attribute
instance-attribute
aggregate_weighted(weighted_sources: Iterable[Tuple[HOrOutcomeT, int]], h_type: Type[H] = H) -> H
Aggregates weighted_sources into an H
object. Each of
weighted_sources is a two-tuple of either an outcome-count pair or a
histogram-count pair. This function is used in the implementation of the
expandable
decorator and derivatives (like the
foreach
function) as well as the (deprecated)
H.substitute
and P.foreach
methods. Unlike those, the histogram returned from this function is not reduced to
its lowest terms.
In nearly all cases, when a source contains a histogram, its total takes on the corresponding count’s weight. In other words, the sum of the counts of the histogram retains the same proportion to other outcomes as its corresponding count. This becomes clearer when there is no overlap between the histogram and the other outcomes.
1 2 3 4 |
|
An important exception
If a source is the empty histogram (H({})
), it and its count is omitted from
the result without scaling.
1 2 3 |
|
Source code in dyce/evaluation.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 |
|
expandable(f: Optional[_DependentTermT] = None, *, sentinel: H = _DEFAULT_SENTINEL) -> Union[Callable[[_DependentTermT], _ForEachEvaluatorT], _ForEachEvaluatorT]
Experimental
This function should be considered experimental and may change or disappear in future versions.
Calls dependent_term
for each set of outcomes from the product of any
independent sources provided to the decorated function and accumulates the results.
Independent sources are H
objects, P
objects, or
PWithSelection
wrapper objects. Results are
passed to dependent_term
via
HResult
objects or
PResult
objects, corresponding to the respective
independent term. This is useful for resolving dependent probabilities. Returned
histograms are always reduced to their lowest terms.
For example, let’s say we’re rolling a d20 but want to re-roll a 1
if
it comes up, then keep the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
When the decorated function returns an H
object, that histogram’s
outcomes are accumulated, but the counts retain their “scale” within the context of
the evaluation. This becomes clearer when there is no overlap between the evaluated
histogram and the other outcomes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Note that the sum of the outcomes’ counts from the d00 make up the same proportion as the one’s outcome and count they replaced from the d6.
1 2 3 4 5 6 7 |
|
We can leverage this to compute distributions for an “exploding” die (i.e., re-rolling and adding when rolling its highest face).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
@expandable
functions can call themselves recursively. They take a
limit keyword argument to control when such recursion should stop. The decorator
itself takes an optional argument sentinel, which defines what is returned once
limit is reached (or a RecursionError
is encountered, whichever comes
first). The default value for sentinel is H({0: 1})
and the value
ascribed to limit, if not provided, is 1
.
If limit is a whole number, it defines the maximum recursive evaluation “depth”.
The way to express no recursion (i.e., merely return sentinel) is to set limit
to an integral value of 0
. An integral value of -1
is
equivalent to setting it to sys.maxsize
.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
If limit is a fractional value between zero and one, exclusive, recursion will halt on any branch whose “contextual precision” is less than or equal to that value. Recursion is attempted for all of the outcomes of a(n evaluated) histogram or none of them. The contextual precision of a returned histogram is its proportion to the whole.
The contextual precision of the original (or top-level) execution is Fraction(1, 1)
or 1.0
. A limit of either of those values would
theoretically ensure no substitution. Similarly, a fractional value for limit of
Fraction(0, 1)
or 0.0
would theoretically ensure there is
no limit. However, These expressions would likely lead to confusion because they
have different meanings than equivalent integral values for limit. This is why
fractional types with values equivalent to zero and one are not allowed.
1 2 3 |
|
While whole number limit values will always cut off recursion at a constant depth, fractional limit values can skew results in favor of certain recursion branches. This is easily demonstrated when examining “unfair” dice (i.e., those disproportionately weighted toward particular faces).
1 2 3 4 5 6 7 8 9 |
|
Be aware that some recursions are guaranteed to result in maxing out the stack, even with fractional values for limit that are very close to one. We can often guard against this by short-circuiting recursion where we know the evaluated contextual probabilities do not asymptotically approach zero (e.g., where an entire branch reliably generates histograms with precisely one outcome).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
We can also evaluate multiple independent sources. For example, let’s say we want to understand when a d6 will beat each face on two d10s. We can use a nested function to also allow for a penalty or bonus modifier to the d6.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Now let’s say we want to introduce the concept of an “advantage” or “disadvantage” to the above, meaning we roll an extra d10 that can further penalize or benefit us. We could just roll 3d10 and look at the best or worst two of each roll.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
However, we could be more computationally more efficient by narrowing our selection
before we get to our evaluation function. We do this using
PWithSelection
objects whose PWithSelection.which
values are passed to the
P.rolls_with_counts
method when enumerating the
rolls.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
This function uses the aggregate_weighted
function in its implementation. As such, if the empty histogram (H({})
) is
returned at any point, the corresponding branch and its count is omitted from the
result without substitution or scaling. A silly example is modeling a d5 by
indefinitely re-rolling a d6 until something other than a 6 comes up.
1 2 3 4 5 6 |
|
This technique is more useful when modeling re-rolling certain derived outcomes, like ties in a contest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Expandables are quite flexible and well suited to modeling logical progressions with dependent variables. Consider the following mechanic:
-
Start with a total of zero.
-
Roll a six-sided die. If the face was a six, go to step 3. Otherwise, add the face to the total and stop.
-
Roll a four-sided die. Add the face to the total. If the face was a one, go to step 2. Otherwise, stop.
What is the likelihood of an even final tally? This can be approximated by:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
We can also use this decorator to help model expected damage from a single attack in d20-like role playing games.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
On the current implementation
This decorator relies on context variables for enforcing limits without requiring decorated functions to explicitly propagate additional state.
-
An integral limit in the low-to-mid single digits is often more than sufficient to exceed a useful precision. Consider starting small and edging up incrementally to avoid protracted execution times. Consider:
1 2 3 4 5 6 7 8 9 10 11
>>> @expandable ... def wicked_explode(h_result: HResult): ... if h_result.outcome == max(h_result.h): ... # Replace a high roll with two recursively exploding dice ... return wicked_explode(h_result.h) + wicked_explode(h_result.h) ... else: ... return h_result.outcome >>> h = wicked_explode(H(6), limit=6) >>> print(f"Likelihood of making {max(h)}: {h[max(h)] / h.total:.50%}") Likelihood of making 160: 0.00000000000000000000000000000000000000000000000947%
The above limit is tolerable for modern computing devices, but much more might render it intractable. ↩
Source code in dyce/evaluation.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
|
explode(source: _SourceT, predicate: _PredicateT = lambda result: result.outcome == max(result.h), limit: Optional[LimitT] = None, inf: Optional[LimitT] = float('inf')) -> H
Experimental
This function should be considered experimental and may change or disappear in future versions.
Approximates an “exploding” die (i.e., one where a running total is accumulated
and re-rolls are allowed so long as predicate returns True
).
predicate takes two arguments: outcome is the outcome being considered and h
is the histogram from which it originated. The default predicate returns
True
if its outcome is max(h)
, and False
otherwise. limit shares the same semantics as with the
expandable
decorator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Where h has a single outcome that satisfies predicate and limit is a
fractional value, this function returns special histograms, possibly leveraging the
inf parameter. The default for inf is float("inf")
.
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 |
|
Source code in dyce/evaluation.py
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
|
foreach(callback: _DependentTermT, *args: _POrPWithSelectionOrSourceT, limit: Optional[LimitT] = None, sentinel: H = _DEFAULT_SENTINEL, **kw: _POrPWithSelectionOrSourceT) -> H
Experimental
This function should be considered experimental and may change or disappear in future versions.
Shorthand for expandable(callback, sentinel=sentinel)(*args, limit=limit,**kw)
.
Many common cases do not need the full flexibility of the
expandable
. This wrapper that strives to be
simpler or more readable under those circumstances (e.g., where the callback is a
lambda
function).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Source code in dyce/evaluation.py
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 |
|