forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheuristics.py
31 lines (24 loc) · 993 Bytes
/
heuristics.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
import random
import flow
################
## Heuristics ##
################
################################################################
## A heuristic returns a single candidate permutation from
## a set of candidates that is given. The heuristic is also
## given access to the problem data in order to evaluate
## which candidate might be preferred.
def heur_hillclimbing(data, candidates):
# Returns the best candidate in the list
scores = [(flow.makespan(data, perm), perm) for perm in candidates]
return sorted(scores)[0][1]
def heur_random(data, candidates):
# Returns a random candidate choice
return random.choice(candidates)
def heur_random_hillclimbing(data, candidates):
# Returns a candidate with probability proportional to its rank in sorted quality
scores = [(flow.makespan(data, perm), perm) for perm in candidates]
i = 0
while (random.random() < 0.5) and (i < len(scores) - 1):
i += 1
return sorted(scores)[i][1]