forked from asparagus/search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
55 lines (37 loc) · 1.22 KB
/
utils.py
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
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Helper functions taken from aima-python."""
import math
import random
# ______________________________________________________________________________
# argmin and argmax
argmin = min
argmax = max
def _identity(x):
return x
def argmin_random_tie(seq, key=_identity):
"""
Return an element with lowest fn(seq[i]) score.
Break ties randomly.
"""
return argmin(shuffled(seq), key=key)
def argmax_random_tie(seq, key=_identity):
"""
Return an element with highest fn(seq[i]) score.
Break ties randomly.
"""
return argmax(shuffled(seq), key=key)
def shuffled(iterable):
"""Randomly shuffle a copy of iterable."""
items = list(iterable)
random.shuffle(items)
return items
# ______________________________________________________________________________
# argmin and argmax
def exp_schedule(k=20, lam=0.005, limit=100):
"""One possible schedule function for simulated annealing."""
return lambda t: (k * math.exp(-lam * t) if t < limit else 0)
# ______________________________________________________________________________
def probability(p):
"""Return true with probability p."""
return p > random.uniform(0.0, 1.0)