-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhc.py
66 lines (49 loc) · 1.77 KB
/
hc.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
56
57
58
59
60
61
62
63
64
65
66
from state import State
import random
def schedule_to_dict(schedule):
DAY_LABELS = ['Luni', 'Marti', 'Miercuri', 'Joi', 'Vineri']
INTERVAL_LABELS = [(8,10), (10,12), (12,14), (14,16), (16,18), (18,20)]
dict = {}
for i in range(DAYS):
day_dict = {}
for j in range(INTERVALS):
day_dict[INTERVAL_LABELS[j]] = schedule[i][j]
dict[DAY_LABELS[i]] = day_dict
return dict
def apply_hill_climbing(days, intervals, schedule, classes, prof_prefs, rooms, prof_classes, prof_jobs):
global DAYS, INTERVALS
DAYS, INTERVALS = days, intervals
State.DAYS = DAYS
State.INTERVALS = INTERVALS
state = State(schedule, classes, prof_prefs, rooms, prof_classes, prof_jobs, 0)
restarts, states, result = hill_climbing(state)
return restarts, states, result, result.conflicts()
def hill_climbing(initial: State, max_iters: int = 2500, max_restarts: int = 10):
iters, states = 0, 0
restarts = 0
state = initial.clone()
random.seed()
is_best = False
best = None
while restarts < max_restarts:
restarts += 1
iters = 0
while iters < max_iters:
iters += 1
next_states = state.get_next_states()
states += len(next_states)
if len(next_states) == 0:
break
state = random.choice(next_states)
if state.is_final():
if not best or state.conflicts() < best.conflicts():
best = state
if state.conflicts() == 0:
is_best = True
break
if is_best:
break
else:
state = initial.clone()
random.seed(random.random())
return restarts, states, best