-
Notifications
You must be signed in to change notification settings - Fork 0
/
neighbourhood.py
20 lines (16 loc) · 981 Bytes
/
neighbourhood.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import copy
#generates one neighbour (plan) by swapping steps at index a and index b on indicated machine
#returns False if swap fails
def generate_neighbour(plan, machine, index_a, index_b):
if plan[machine][index_a].job == plan[machine][index_b].job: # Swapping generates an impermissible schedule
return False # when two steps of the same job are swapped
for index in range(index_a + 1, index_b): # Swapping also generates an impermissible schedule
if (plan[machine][index].job == plan[machine][index_a].job # when any step in-between belongs to either of
or plan[machine][index].job == plan[machine][index_b].job): # the swapped steps
return False
#copy original plan and swap elements
neighbour = copy.deepcopy(plan) #shallow copy would cause change in the original plan
swapElement = neighbour[machine][index_a]
neighbour[machine][index_a] = neighbour[machine][index_b]
neighbour[machine][index_b] = swapElement
return neighbour