def randsol3(inst): """A heuristic generator which takes advantage of problem data in the simplest possible way: it chooses from the remaining cities with probabilities inversely weighted by distance.""" n = len(inst) sol = [] remaining = list(range(n)) # the start city is a decision variable, because we'll get # different results if we start at different cities x = random.choice(list(range(n))) sol.append(x) remaining.remove(x) i = 1 while i < n: # choose one of the remaining cities randomly, weighted by # inverse distance. x = choose_node(inst, x, remaining) sol.append(x) remaining.remove(x) i += 1 return sol
def randomPerm2(S): while not schedule_full(S): #print("HIIIIIIIIIIIIIIIIIIII") #print(S) #matrix = [[None for x in range(weeks)] for x in range(teams)] spaceNone = [] for team in range(teams): for week in range(weeks): if S[team][week] == None: spaceNone.append((team, week)) #print(' AAA ', spaceNone) sp = spaceNone[:] x, y = random.choice(sp) #while S[x][y] != None: # x = random.choice(range(teams)) # y = random.choice(range(weeks)) if S[x][y] == None: temp = getGame2(x, y, S) S[x][y] = temp if temp != 0: if temp > 0: S[abs(temp) - 1][y] = -(x + 1) else: S[abs(temp) - 1][y] = x + 1 return S
def randsol3(inst): """Take advantage of problem data in the simplest possible way""" n = len(inst) sol = [] remaining = list(range(n)) # the start city is a decision variable, because we'll get different results if # we start at different cities x = random.choice(list( range(n))) #(remaining) THIS WAS THE ROOT OF THE ERROR. # Partial + Mutable in Local + argument of wrapped function = weird side effect... just avoid this combination! sol.append(x) remaining.remove(x) i = 1 while i < n: # choose one of the remaining cities randomly, weighted by # inverse distance. x = choose_node(inst, x, remaining) sol.append(x) remaining.remove(x) i += 1 return sol
def stepuniform(features, costs, solution, alpha): # This implements the RCL as used in GRASP, which is a # step-uniform distribution In this version, alpha gives a # threshold on cost. This is the only type used in the EvoCOP # paper. min_cost, max_cost = min(costs.values()), max(costs.values()) RCL = [ feat for feat in features if costs[feat] <= min_cost + alpha * (max_cost - min_cost) ] return random.choice(RCL)
def getGame3(x, y, S): available = [ i for i in range(-teams, teams + 1) if i != 0 and abs(i) != (x + 1) ] a = [] b = [] for i in range(teams): a.append(S[i][y]) #for i in range(weeks): # b.append(S[x][i]) b = S[x] available = [ k for k in available if -k not in a and k not in a and k not in b ] # remove any duplicates available = [k for k in available if abs(k) != (x + 1)] # cant play yourself if y > 2: if b[y - 1] != None and b[y - 2] != None and b[y - 3] != None: if b[y - 1] < 0 and b[y - 2] < 0 and b[y - 3] < 0: available = [k for k in available if k > 0] # no repreat home or away if b[y - 1] > 0 and b[y - 2] > 0 and b[y - 3] > 0: available = [k for k in available if k < 0] if abs(b[y - 1]) == abs(b[y - 2]): available = [k for k in available if k != b[y - 2]] # no consecutive games # print('t ', b) #print(b[y-1]) available = [k for k in available if abs(k) != (abs(b[y - 1])) ] #cannot play the team before again available = [k for k in available if S[(abs(k) - 1)][y] != None] #trying to take cost matrix into account #if len(available) > 3: # highest = 0 # marker = 0 # for k in available: # temp = abs(costMat[x][y] - costMat[abs(k-1)][y]) # if temp > highest: # highest = temp # marker = k # available.remove(k) if len(available) < 1: return 0 else: return random.choice(available)
def swap_homes(S): # Choose a team to swap on team = len(S) - 1 swap_loc = S[team].index(random.choice(S[team])) swap_loc_mirror = S[team].index(home_away(S[team][swap_loc])) # Swap the first game and its opponent S[team][swap_loc] = home_away(S[team][swap_loc]) S = set_opponent(S, team, swap_loc) # Swap the matching game and its opponent S[team][swap_loc_mirror] = home_away(S[team][swap_loc_mirror]) S = set_opponent(S, team, swap_loc_mirror) #print("Swap_homes:",S) return S
def getGame2(x, y, S): # build available without 0 or the current team available = [ i for i in range(-teams, teams + 1) if i != 0 and abs(i) != (x + 1) ] rand = 00 a = [] b = [] for i in range(teams): a.append(S[i][y]) #for i in range(weeks): # b.append(S[x][i]) b = S[x] #print('A: ', available) #print(a) #print(b) av = available[:] while len(av): rand = random.choice(av) # print('RAND ', rand) z = [] if S[(abs(rand) - 1)][y] == None or S[( abs(rand) - 1)][y] == 0: # opposite isn't full if rand > 0: opp = -(x + 1) # create opposite value else: opp = x + 1 #for i in range(weeks): # z.append(S[abs(rand)-1][i]) # create opposite row if valid(b, a, S[abs(rand) - 1], rand, opp): # check if rand and opp are valid return rand av = [k for k in av if k != rand] # remove rand from the check #print(S) return 00
def getGame(x, y, S): easyR = [[None for x in range(weeks)] for x in range(teams)] for i in range(teams): easyR[i][y] = S[i][ y] # building a T matrix of relevant possible violations for i in range(weeks): easyR[x][i] = S[x][i] # check both rows when putting in the opposite value # random jumps to check squares available = [x for x in range(-teams, teams + 1) if x != 0] #available = [x for x in available if x != 0] checkWeek = [] checkTeam = [] for week in easyR: checkWeek.append( week[y]) # reducing to 1D array of each week aka vertical for i in range(weeks): checkTeam.append(easyR[x][i]) # each team , horizontal checkWeek = list(filter(lambda a: a != None, checkWeek)) # removing instances of None checkTeam = list(filter(lambda a: a != None, checkTeam)) available = [ k for k in available if abs(k) not in checkWeek and k not in checkTeam ] # remove any duplicates available = [k for k in available if abs(k) != (x + 1)] # cant play yourself #print('t ', checkTeam, ' ', y) if checkTeam != []: #print(checkTeam[y-1]) available = [k for k in available if abs(k) != abs(checkTeam[y - 1]) ] #cannot play the team before again for i in range(len(checkWeek)): try: available.remove( i + 1) # needs to check for in in checkWeek, not the length range except: # tries to remove double occurences in week, only 1-n in n teams print('y') try: available.remove(-(i + 1)) except: print('z') #print('w ', checkWeek) #print('t ', checkTeam) #print('a ', available) #print(y) if len(checkTeam) > 2: if checkTeam[y - 1] < 0 and checkTeam[y - 2] < 0: available = [k for k in available if k > 0] # no repreat home or away if checkTeam[y - 1] > 0 and checkTeam[y - 2] > 0: available = [k for k in available if k < 0] if abs(checkTeam[y - 1]) == abs(checkTeam[y - 2]): available = [k for k in available if k != checkTeam[y - 2]] # no consecutive games # print('t ', checkTeam) #available = [k for k in available if S[abs(k)-1][y] == None or S[abs(k)-1][y] == 0] if (x + 1) in checkWeek: rand = -(checkWeek.index(x + 1) + 1 ) # fill in the opposite number for each selection elif -(x + 1) in checkWeek: rand = checkWeek.index(-(x + 1)) + 1 else: try: rand = random.choice(available) except: rand = 0 return rand
def rev_shuffle(perm): # this is like multiple applications of 2-opt for i in range(len(perm)): ri = random.choice(range(i, len(perm))) perm[i:ri + 1] = perm[i:ri + 1][::-1] # reverse a section return perm
def swap_shuffle(perm): for i in range(len(perm)): ri = random.choice(range(i, len(perm))) perm[i], perm[ri] = perm[ri], perm[i] return perm