| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fd52c72 commit 0ad4c07
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,19 +13,23 @@ | |||
| 13 | 13 | from collections import defaultdict | |
| 14 | 14 | from functools import reduce | |
| 15 | 15 | ||
| 16 | + | ||
| 16 | 17 | # ______________________________________________________________________________ | |
| 17 | 18 | ||
| 18 | 19 | ||
| 19 | 20 | def DTAgentProgram(belief_state): | |
| 20 | 21 | """A decision-theoretic agent. [Figure 13.1]""" | |
| 22 | + | ||
| 21 | 23 | def program(percept): | |
| 22 | 24 | belief_state.observe(program.action, percept) | |
| 23 | 25 | program.action = argmax(belief_state.actions(), | |
| 24 | 26 | key=belief_state.expected_outcome_utility) | |
| 25 | 27 | return program.action | |
| 28 | + | ||
| 26 | 29 | program.action = None | |
| 27 | 30 | return program | |
| 28 | 31 | ||
| 32 | + | ||
| 29 | 33 | # ______________________________________________________________________________ | |
| 30 | 34 | ||
| 31 | 35 | ||
@@ -132,6 +136,7 @@ def event_values(event, variables): | |||
| 132 | 136 | else: | |
| 133 | 137 | return tuple([event[var] for var in variables]) | |
| 134 | 138 | ||
| 139 | + | ||
| 135 | 140 | # ______________________________________________________________________________ | |
| 136 | 141 | ||
| 137 | 142 | ||
@@ -160,6 +165,7 @@ def enumerate_joint(variables, e, P): | |||
| 160 | 165 | return sum([enumerate_joint(rest, extend(e, Y, y), P) | |
| 161 | 166 | for y in P.values(Y)]) | |
| 162 | 167 | ||
| 168 | + | ||
| 163 | 169 | # ______________________________________________________________________________ | |
| 164 | 170 | ||
| 165 | 171 | ||
@@ -378,6 +384,7 @@ def __repr__(self): | |||
| 378 | 384 | ('MaryCalls', 'Alarm', {T: 0.70, F: 0.01}) | |
| 379 | 385 | ]) | |
| 380 | 386 | ||
| 387 | + | ||
| 381 | 388 | # ______________________________________________________________________________ | |
| 382 | 389 | ||
| 383 | 390 | ||
@@ -409,6 +416,7 @@ def enumerate_all(variables, e, bn): | |||
| 409 | 416 | return sum(Ynode.p(y, e) * enumerate_all(rest, extend(e, Y, y), bn) | |
| 410 | 417 | for y in bn.variable_values(Y)) | |
| 411 | 418 | ||
| 419 | + | ||
| 412 | 420 | # ______________________________________________________________________________ | |
| 413 | 421 | ||
| 414 | 422 | ||
@@ -498,6 +506,7 @@ def all_events(variables, bn, e): | |||
| 498 | 506 | for x in bn.variable_values(X): | |
| 499 | 507 | yield extend(e1, X, x) | |
| 500 | 508 | ||
| 509 | + | ||
| 501 | 510 | # ______________________________________________________________________________ | |
| 502 | 511 | ||
| 503 | 512 | # [Figure 14.12a]: sprinkler network | |
@@ -510,6 +519,7 @@ def all_events(variables, bn, e): | |||
| 510 | 519 | ('WetGrass', 'Sprinkler Rain', | |
| 511 | 520 | {(T, T): 0.99, (T, F): 0.90, (F, T): 0.90, (F, F): 0.00})]) | |
| 512 | 521 | ||
| 522 | + | ||
| 513 | 523 | # ______________________________________________________________________________ | |
| 514 | 524 | ||
| 515 | 525 | ||
@@ -521,6 +531,7 @@ def prior_sample(bn): | |||
| 521 | 531 | event[node.variable] = node.sample(event) | |
| 522 | 532 | return event | |
| 523 | 533 | ||
| 534 | + | ||
| 524 | 535 | # _________________________________________________________________________ | |
| 525 | 536 | ||
| 526 | 537 | ||
@@ -547,6 +558,7 @@ def consistent_with(event, evidence): | |||
| 547 | 558 | return all(evidence.get(k, v) == v | |
| 548 | 559 | for k, v in event.items()) | |
| 549 | 560 | ||
| 561 | + | ||
| 550 | 562 | # _________________________________________________________________________ | |
| 551 | 563 | ||
| 552 | 564 | ||
@@ -579,6 +591,7 @@ def weighted_sample(bn, e): | |||
| 579 | 591 | event[Xi] = node.sample(event) | |
| 580 | 592 | return event, w | |
| 581 | 593 | ||
| 594 | + | ||
| 582 | 595 | # _________________________________________________________________________ | |
| 583 | 596 | ||
| 584 | 597 | ||
@@ -612,6 +625,7 @@ def markov_blanket_sample(X, e, bn): | |||
| 612 | 625 | # (assuming a Boolean variable here) | |
| 613 | 626 | return probability(Q.normalize()[True]) | |
| 614 | 627 | ||
| 628 | + | ||
| 615 | 629 | # _________________________________________________________________________ | |
| 616 | 630 | ||
| 617 | 631 | ||
@@ -655,7 +669,7 @@ def forward_backward(HMM, ev, prior): | |||
| 655 | 669 | ||
| 656 | 670 | fv = [[0.0, 0.0] for _ in range(len(ev))] | |
| 657 | 671 | b = [1.0, 1.0] | |
| 658 | - bv = [b] # we don't need bv; but we will have a list of all backward messages here | ||
| 672 | + bv = [b] # we don't need bv; but we will have a list of all backward messages here | ||
| 659 | 673 | sv = [[0, 0] for _ in range(len(ev))] | |
| 660 | 674 | ||
| 661 | 675 | fv[0] = prior | |
@@ -671,6 +685,33 @@ def forward_backward(HMM, ev, prior): | |||
| 671 | 685 | ||
| 672 | 686 | return sv | |
| 673 | 687 | ||
| 688 | + | ||
| 689 | + def viterbi(HMM, ev, prior): | ||
| 690 | + """[Figure 15.5] | ||
| 691 | + Viterbi algorithm to find the most likely sequence. Computes the best path, | ||
| 692 | + given an HMM model and a sequence of observations.""" | ||
| 693 | + t = len(ev) | ||
| 694 | + ev.insert(0, None) | ||
| 695 | + | ||
| 696 | + m = [[0.0, 0.0] for _ in range(len(ev) - 1)] | ||
| 697 | + | ||
| 698 | + # the recursion is initialized with m1 = forward(P(X0), e1) | ||
| 699 | + m[0] = forward(HMM, prior, ev[1]) | ||
| 700 | + | ||
| 701 | + for i in range(1, t): | ||
| 702 | + m[i] = element_wise_product(HMM.sensor_dist(ev[i + 1]), | ||
| 703 | + [max(element_wise_product(HMM.transition_model[0], m[i - 1])), | ||
| 704 | + max(element_wise_product(HMM.transition_model[1], m[i - 1]))]) | ||
| 705 | + | ||
| 706 | + path = [0.0] * (len(ev) - 1) | ||
| 707 | + # the construction of the most likely sequence starts in the final state with the largest probability, | ||
| 708 | + # and runs backwards; the algorithm needs to store for each xt its best predecessor xt-1 | ||
| 709 | + for i in range(t, -1, -1): | ||
| 710 | + path[i - 1] = max(m[i - 1]) | ||
| 711 | + | ||
| 712 | + return path | ||
| 713 | + | ||
| 714 | + | ||
| 674 | 715 | # _________________________________________________________________________ | |
| 675 | 716 | ||
| 676 | 717 | ||
@@ -702,6 +743,7 @@ def fixed_lag_smoothing(e_t, HMM, d, ev, t): | |||
| 702 | 743 | else: | |
| 703 | 744 | return None | |
| 704 | 745 | ||
| 746 | + | ||
| 705 | 747 | # _________________________________________________________________________ | |
| 706 | 748 | ||
| 707 | 749 | ||
@@ -742,13 +784,15 @@ def particle_filtering(e, N, HMM): | |||
| 742 | 784 | ||
| 743 | 785 | return s | |
| 744 | 786 | ||
| 787 | + | ||
| 745 | 788 | # _________________________________________________________________________ | |
| 746 | - ## TODO: Implement continuous map for MonteCarlo similar to Fig25.10 from the book | ||
| 789 | + # TODO: Implement continuous map for MonteCarlo similar to Fig25.10 from the book | ||
| 747 | 790 | ||
| 748 | 791 | ||
| 749 | 792 | class MCLmap: | |
| 750 | 793 | """Map which provides probability distributions and sensor readings. | |
| 751 | 794 | Consists of discrete cells which are either an obstacle or empty""" | |
| 795 | + | ||
| 752 | 796 | def __init__(self, m): | |
| 753 | 797 | self.m = m | |
| 754 | 798 | self.nrows = len(m) | |
@@ -772,7 +816,7 @@ def ray_cast(self, sensor_num, kin_state): | |||
| 772 | 816 | # 0 | |
| 773 | 817 | # 3R1 | |
| 774 | 818 | # 2 | |
| 775 | - delta = ((sensor_num % 2 == 0)*(sensor_num - 1), (sensor_num % 2 == 1)*(2 - sensor_num)) | ||
| 819 | + delta = ((sensor_num % 2 == 0) * (sensor_num - 1), (sensor_num % 2 == 1) * (2 - sensor_num)) | ||
| 776 | 820 | # sensor direction changes based on orientation | |
| 777 | 821 | for _ in range(orient): | |
| 778 | 822 | delta = (delta[1], -delta[0]) | |
@@ -790,9 +834,9 @@ def ray_cast(sensor_num, kin_state, m): | |||
| 790 | 834 | return m.ray_cast(sensor_num, kin_state) | |
| 791 | 835 | ||
| 792 | 836 | M = len(z) | |
| 793 | - W = [0]*N | ||
| 794 | - S_ = [0]*N | ||
| 795 | - W_ = [0]*N | ||
| 837 | + W = [0] * N | ||
| 838 | + S_ = [0] * N | ||
| 839 | + W_ = [0] * N | ||
| 796 | 840 | v = a['v'] | |
| 797 | 841 | w = a['w'] | |
| 798 | 842 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments