| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -115,7 +115,7 @@ def support_pruning(self): | |||
| 115 | 115 | """Make sure we can prune values from domains. (We want to pay | |
| 116 | 116 | for this only if we use it.)""" | |
| 117 | 117 | if self.curr_domains is None: | |
| 118 | - self.curr_domains = dict((v, list(self.domains[v])) for v in self.variables) | ||
| 118 | + self.curr_domains = {v: list(self.domains[v]) for v in self.variables} | ||
| 119 | 119 | ||
| 120 | 120 | def suppose(self, var, value): | |
| 121 | 121 | "Start accumulating inferences from assuming var=value." | |
@@ -137,8 +137,8 @@ def choices(self, var): | |||
| 137 | 137 | def infer_assignment(self): | |
| 138 | 138 | "Return the partial assignment implied by the current inferences." | |
| 139 | 139 | self.support_pruning() | |
| 140 | - return dict((v, self.curr_domains[v][0]) | ||
| 141 | - for v in self.variables if 1 == len(self.curr_domains[v])) | ||
| 140 | + return {v: self.curr_domains[v][0] | ||
| 141 | + for v in self.variables if 1 == len(self.curr_domains[v])} | ||
| 142 | 142 | ||
| 143 | 143 | def restore(self, removals): | |
| 144 | 144 | "Undo a supposition and all inferences from it." | |
@@ -512,7 +512,7 @@ def flatten(seqs): return sum(seqs, []) | |||
| 512 | 512 | _ROWS = flatten([list(map(flatten, list(zip(*brow)))) for brow in _BGRID]) | |
| 513 | 513 | _COLS = list(zip(*_ROWS)) | |
| 514 | 514 | ||
| 515 | - _NEIGHBORS = dict([(v, set()) for v in flatten(_ROWS)]) | ||
| 515 | + _NEIGHBORS = {v: set() for v in flatten(_ROWS)} | ||
| 516 | 516 | for unit in map(set, _BOXES + _ROWS + _COLS): | |
| 517 | 517 | for v in unit: | |
| 518 | 518 | _NEIGHBORS[v].update(unit - set([v])) | |
@@ -567,8 +567,8 @@ def __init__(self, grid): | |||
| 567 | 567 | the digits 1-9 denote a filled cell, '.' or '0' an empty one; | |
| 568 | 568 | other characters are ignored.""" | |
| 569 | 569 | squares = iter(re.findall(r'\d|\.', grid)) | |
| 570 | - domains = dict((var, ([ch] if ch in '123456789' else '123456789')) | ||
| 571 | - for var, ch in zip(flatten(self.rows), squares)) | ||
| 570 | + domains = {var: [ch] if ch in '123456789' else '123456789' | ||
| 571 | + for var, ch in zip(flatten(self.rows), squares)} | ||
| 572 | 572 | for _ in squares: | |
| 573 | 573 | raise ValueError("Not a Sudoku grid", grid) # Too many squares | |
| 574 | 574 | CSP.__init__(self, None, domains, self.neighbors, different_values_constraint) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -241,9 +241,9 @@ def NaiveBayesLearner(dataset): | |||
| 241 | 241 | ||
| 242 | 242 | targetvals = dataset.values[dataset.target] | |
| 243 | 243 | target_dist = CountingProbDist(targetvals) | |
| 244 | - attr_dists = dict(((gv, attr), CountingProbDist(dataset.values[attr])) | ||
| 245 | - for gv in targetvals | ||
| 246 | - for attr in dataset.inputs) | ||
| 244 | + attr_dists = {(gv, attr): CountingProbDist(dataset.values[attr]) | ||
| 245 | + for gv in targetvals | ||
| 246 | + for attr in dataset.inputs} | ||
| 247 | 247 | for example in dataset.examples: | |
| 248 | 248 | targetval = example[dataset.target] | |
| 249 | 249 | target_dist.add(targetval) | |
@@ -882,9 +882,9 @@ def RestaurantDataSet(examples=None): | |||
| 882 | 882 | ||
| 883 | 883 | ||
| 884 | 884 | def T(attrname, branches): | |
| 885 | - branches = dict((value, (child if isinstance(child, DecisionFork) | ||
| 886 | - else DecisionLeaf(child))) | ||
| 887 | - for value, child in branches.items()) | ||
| 885 | + branches = {value: (child if isinstance(child, DecisionFork) | ||
| 886 | + else DecisionLeaf(child)) | ||
| 887 | + for value, child in branches.items()} | ||
| 888 | 888 | return DecisionFork(restaurant.attrnum(attrname), attrname, branches) | |
| 889 | 889 | ||
| 890 | 890 | """ [Figure 18.2] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -497,8 +497,9 @@ def pl_fc_entails(KB, q): | |||
| 497 | 497 | >>> pl_fc_entails(horn_clauses_KB, expr('Q')) | |
| 498 | 498 | True | |
| 499 | 499 | """ | |
| 500 | - count = dict([(c, len(conjuncts(c.args[0]))) for c in KB.clauses | ||
| 501 | - if c.op == '==>']) | ||
| 500 | + count = {c: len(conjuncts(c.args[0])) | ||
| 501 | + for c in KB.clauses | ||
| 502 | + if c.op == '==>'} | ||
| 502 | 503 | inferred = defaultdict(bool) | |
| 503 | 504 | agenda = [s for s in KB.clauses if is_prop_symbol(s.op)] | |
| 504 | 505 | while agenda: | |
@@ -642,7 +643,7 @@ def WalkSAT(clauses, p=0.5, max_flips=10000): | |||
| 642 | 643 | # set of all symbols in all clauses | |
| 643 | 644 | symbols = set(sym for clause in clauses for sym in prop_symbols(clause)) | |
| 644 | 645 | # model is a random assignment of true/false to the symbols in clauses | |
| 645 | - model = dict([(s, random.choice([True, False])) for s in symbols]) | ||
| 646 | + model = {s: random.choice([True, False]) for s in symbols} | ||
| 646 | 647 | for i in range(max_flips): | |
| 647 | 648 | satisfied, unsatisfied = [], [] | |
| 648 | 649 | for clause in clauses: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,13 +93,14 @@ def to_grid(self, mapping): | |||
| 93 | 93 | def to_arrows(self, policy): | |
| 94 | 94 | chars = { | |
| 95 | 95 | (1, 0): '>', (0, 1): '^', (-1, 0): '<', (0, -1): 'v', None: '.'} | |
| 96 | - return self.to_grid( | ||
| 97 | - dict([(s, chars[a]) for (s, a) in policy.items()])) | ||
| 96 | + return self.to_grid({s: chars[a] for (s, a) in policy.items()}) | ||
| 98 | 97 | ||
| 99 | 98 | # ______________________________________________________________________________ | |
| 99 | + | ||
| 100 | 100 | """ [Figure 17.1] | |
| 101 | 101 | A 4x3 grid environment that presents the agent with a sequential decision problem. | |
| 102 | 102 | """ | |
| 103 | + | ||
| 103 | 104 | sequential_decision_environment = GridMDP([[-0.04, -0.04, -0.04, +1], | |
| 104 | 105 | [-0.04, None, -0.04, -1], | |
| 105 | 106 | [-0.04, -0.04, -0.04, -0.04]], | |
@@ -110,7 +111,7 @@ def to_arrows(self, policy): | |||
| 110 | 111 | ||
| 111 | 112 | def value_iteration(mdp, epsilon=0.001): | |
| 112 | 113 | "Solving an MDP by value iteration. [Figure 17.4]" | |
| 113 | - U1 = dict([(s, 0) for s in mdp.states]) | ||
| 114 | + U1 = {s: 0 for s in mdp.states} | ||
| 114 | 115 | R, T, gamma = mdp.R, mdp.T, mdp.gamma | |
| 115 | 116 | while True: | |
| 116 | 117 | U = U1.copy() | |
@@ -141,8 +142,8 @@ def expected_utility(a, s, U, mdp): | |||
| 141 | 142 | ||
| 142 | 143 | def policy_iteration(mdp): | |
| 143 | 144 | "Solve an MDP by policy iteration [Figure 17.7]" | |
| 144 | - U = dict([(s, 0) for s in mdp.states]) | ||
| 145 | - pi = dict([(s, random.choice(mdp.actions(s))) for s in mdp.states]) | ||
| 145 | + U = {s: 0 for s in mdp.states} | ||
| 146 | + pi = {s: random.choice(mdp.actions(s)) for s in mdp.states} | ||
| 146 | 147 | while True: | |
| 147 | 148 | U = policy_evaluation(pi, U, mdp) | |
| 148 | 149 | unchanged = True | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -237,7 +237,7 @@ def __init__(self, X, parents, cpt): | |||
| 237 | 237 | elif isinstance(cpt, dict): | |
| 238 | 238 | # one parent, 1-tuple | |
| 239 | 239 | if cpt and isinstance(list(cpt.keys())[0], bool): | |
| 240 | - cpt = dict(((v,), p) for v, p in cpt.items()) | ||
| 240 | + cpt = {(v,): p for v, p in cpt.items()} | ||
| 241 | 241 | ||
| 242 | 242 | assert isinstance(cpt, dict) | |
| 243 | 243 | for vs, p in cpt.items(): | |
@@ -344,8 +344,8 @@ def make_factor(var, e, bn): | |||
| 344 | 344 | is the pointwise product of these factors for bn's variables.""" | |
| 345 | 345 | node = bn.variable_node(var) | |
| 346 | 346 | variables = [X for X in [var] + node.parents if X not in e] | |
| 347 | - cpt = dict((event_values(e1, variables), node.p(e1[var], e1)) | ||
| 348 | - for e1 in all_events(variables, bn, e)) | ||
| 347 | + cpt = {event_values(e1, variables): node.p(e1[var], e1) | ||
| 348 | + for e1 in all_events(variables, bn, e)} | ||
| 349 | 349 | return Factor(variables, cpt) | |
| 350 | 350 | ||
| 351 | 351 | ||
@@ -373,24 +373,23 @@ def __init__(self, variables, cpt): | |||
| 373 | 373 | def pointwise_product(self, other, bn): | |
| 374 | 374 | "Multiply two factors, combining their variables." | |
| 375 | 375 | variables = list(set(self.variables) | set(other.variables)) | |
| 376 | - cpt = dict((event_values(e, variables), self.p(e) * other.p(e)) | ||
| 377 | - for e in all_events(variables, bn, {})) | ||
| 376 | + cpt = {event_values(e, variables): self.p(e) * other.p(e) | ||
| 377 | + for e in all_events(variables, bn, {})} | ||
| 378 | 378 | return Factor(variables, cpt) | |
| 379 | 379 | ||
| 380 | 380 | def sum_out(self, var, bn): | |
| 381 | 381 | "Make a factor eliminating var by summing over its values." | |
| 382 | 382 | variables = [X for X in self.variables if X != var] | |
| 383 | - cpt = dict((event_values(e, variables), | ||
| 384 | - sum(self.p(extend(e, var, val)) | ||
| 385 | - for val in bn.variable_values(var))) | ||
| 386 | - for e in all_events(variables, bn, {})) | ||
| 383 | + cpt = {event_values(e, variables): sum(self.p(extend(e, var, val)) | ||
| 384 | + for val in bn.variable_values(var)) | ||
| 385 | + for e in all_events(variables, bn, {})} | ||
| 387 | 386 | return Factor(variables, cpt) | |
| 388 | 387 | ||
| 389 | 388 | def normalize(self): | |
| 390 | 389 | "Return my probabilities; must be down to one variable." | |
| 391 | 390 | assert len(self.variables) == 1 | |
| 392 | 391 | return ProbDist(self.variables[0], | |
| 393 | - dict((k, v) for ((k,), v) in self.cpt.items())) | ||
| 392 | + {k: v for ((k,), v) in self.cpt.items()}) | ||
| 394 | 393 | ||
| 395 | 394 | def p(self, e): | |
| 396 | 395 | "Look up my value tabulated for e." | |
@@ -442,8 +441,7 @@ def rejection_sampling(X, e, bn, N): | |||
| 442 | 441 | ... burglary, 10000).show_approx() | |
| 443 | 442 | 'False: 0.7, True: 0.3' | |
| 444 | 443 | """ | |
| 445 | - counts = dict((x, 0) | ||
| 446 | - for x in bn.variable_values(X)) # bold N in [Figure 14.14] | ||
| 444 | + counts = {x: 0 for x in bn.variable_values(X)} # bold N in [Figure 14.14] | ||
| 447 | 445 | for j in range(N): | |
| 448 | 446 | sample = prior_sample(bn) # boldface x in [Figure 14.14] | |
| 449 | 447 | if consistent_with(sample, e): | |
@@ -467,7 +465,7 @@ def likelihood_weighting(X, e, bn, N): | |||
| 467 | 465 | ... burglary, 10000).show_approx() | |
| 468 | 466 | 'False: 0.702, True: 0.298' | |
| 469 | 467 | """ | |
| 470 | - W = dict((x, 0) for x in bn.variable_values(X)) | ||
| 468 | + W = {x: 0 for x in bn.variable_values(X)} | ||
| 471 | 469 | for j in range(N): | |
| 472 | 470 | sample, weight = weighted_sample(bn, e) # boldface x, w in [Figure 14.15] | |
| 473 | 471 | W[sample[X]] += weight | |
| Back | FazBrowse Home | New Git URL |
0 commit comments