def repDynamique(initStates, goalStates, wallStates): # Initialisation nPlayers = len(initStates) nObjectives = len(goalStates) sObjectives = [0 for i in range(nObjectives)] aPath = [range(nPlayers) for i in range(nObjectives)] # Generation des chemins for i in range(nPlayers): for j in range(nObjectives): aPath[i][j] = doAStar(initStates[i], goalStates[j], wallStates) aPath[i].sort(key=lambda a: len(a)) # Encheres while sObjectives.count(0) > 0: for i in nPlayers: if sObjectives[i] >= nObjectives: print "boucle infinie" return repEnchere(initStates, goalStates, wallStates) #raise Exception('Echec de la resolution') if sObjectives[i] >= 0 and sObjectives[i] < nObjectives: o = aPath[sObjectives[i] % 2] if sObjectives.count(o) == 0: sObjectives[i] = o else: sObjectives[i] += 1 sObjectives.remove(o)
def repEnchere(initStates, goalStates, wallStates): nbPlayers = len(initStates) nbObj = len(goalStates) path = range(nbPlayers) playerPath = [range(nbPlayers) for i in range(nbObj)] pathSorter = [] for i in range(nbObj): for j in range(nbPlayers): playerPath[i][j] = doAStar(initStates[j], goalStates[i], wallStates) heapq.heappush(pathSorter, (len(playerPath[i][j]), (i, j))) cpt = 0 iList = [] jList = [] while cpt < nbPlayers: act = heapq.heappop(pathSorter) i, j = act[1] if (iList.count(i) == 0 and jList.count(j) == 0): path[j] = playerPath[i][j] iList.append(i) jList.append(j) cpt += 1 #print(path) return path
def repDynamique(initStates, goalStates, wallStates): # Initialisation nPlayers = len(initStates) nObjectives = len(goalStates) sObjectives = [0 for i in range(nObjectives)] aPath = [range(nPlayers) for i in range(nObjectives)] # Generation des chemins for i in range(nPlayers): for j in range(nObjectives): aPath[i][j] = doAStar(initStates[i], goalStates[j], wallStates) aPath[i].sort(key = lambda a: len(a)) # Encheres while sObjectives.count(0) > 0: for i in nPlayers: if sObjectives[i] >= nObjectives: print "boucle infinie" return repEnchere(initStates, goalStates, wallStates) #raise Exception('Echec de la resolution') if sObjectives[i] >= 0 and sObjectives[i] < nObjectives: o = aPath[sObjectives[i]%2] if sObjectives.count(o) == 0: sObjectives[i] = o else: sObjectives[i]+= 1 sObjectives.remove(o)
def repEnchere(initStates, goalStates, wallStates): nbPlayers = len(initStates) nbObj = len(goalStates) path = range(nbPlayers) playerPath = [range(nbPlayers) for i in range(nbObj)] pathSorter = [] for i in range(nbObj): for j in range(nbPlayers): playerPath[i][j] = doAStar(initStates[j], goalStates[i], wallStates) heapq.heappush(pathSorter, (len(playerPath[i][j]), (i, j))) cpt = 0 iList = [] jList = [] while cpt < nbPlayers: act = heapq.heappop(pathSorter) i,j = act[1] if(iList.count(i) == 0 and jList.count(j) == 0): path[j] = playerPath[i][j] iList.append(i) jList.append(j) cpt += 1 #print(path) return path
def main(): init("pathfindingWorld_MultiPlayer2") #------------------------------- # Building the matrix #------------------------------- #initStates= [] # on localise tous les états initiaux (loc du joueur) initStates = [o.get_rowcol() for o in game.layers['joueur']] print ("Init states:", initStates) # on localise tous les objets ramassables goalStates = [o.get_rowcol() for o in game.layers['ramassable']] print ("Goal states:", goalStates) # on localise tous les murs wallStates = [w.get_rowcol() for w in game.layers['obstacle']] #print ("Wall states:", wallStates) #------------------------------- # Building the best path with A* #------------------------------- path = doAStar(initStates[0], goalStates[0], wallStates) #------------------------------- # Moving along the path #------------------------------- for i in path: player.set_rowcol(i[0],i[1]) print ("pos:",i[0],i[1]) game.mainiteration() # si on a trouvé l'objet on le ramasse if (i[0],i[1])==goalStates[0]: o = game.player.ramasse(game.layers) game.mainiteration() print ("Objet trouvé!") break ''' #x,y = game.player.get_pos() ''' pygame.quit()
def repGaleShapley(initStates, goalStates, wallStates): # Initialisation nPlayers = len(initStates) nObjectives = len(goalStates) allPath = [range(nPlayers) for i in range(nObjectives)] pPath = [] pPathGS = [] oPath = [] oPathGS = [] # Generation des chemins for i in range(nPlayers): for j in range(nObjectives): allPath[i][j] = doAStar(initStates[i], goalStates[j], wallStates) pPath.append(range(nObjectives)) pPath[i].sort(key = lambda a : len(allPath[i][a])) pPathGS.append([i] + pPath[i]) # print("PATH INDEX OBJ : ", goalStates.index(pPath[i][j][-1])) # Choix pour les objectifs for i in range(nObjectives): oPath.append(range(nPlayers)) oPath[i].sort(key = lambda a : len(allPath[a][i])) oPathGS.append([i] + oPath[i]) # Gale-Shapley matcher = PartyMatcher() matcher.setApplicants(oPathGS) matcher.setProviders(pPathGS) matcher.match() matches = matcher.getMatch() # Application path = range(nPlayers) for i in range(len(matches)): #print("MATCH : ", matches[i][1]) #print("PATH : ", pPath[i][pPathGS[i].index(matches[i][1])]) x,y = matches[i] path[x] = allPath[x][y] #path[i] = pPath[i][pPathGS[i].index()] return path
def repGaleShapley(initStates, goalStates, wallStates): # Initialisation nPlayers = len(initStates) nObjectives = len(goalStates) allPath = [range(nPlayers) for i in range(nObjectives)] pPath = [] pPathGS = [] oPath = [] oPathGS = [] # Generation des chemins for i in range(nPlayers): for j in range(nObjectives): allPath[i][j] = doAStar(initStates[i], goalStates[j], wallStates) pPath.append(range(nObjectives)) pPath[i].sort(key=lambda a: len(allPath[i][a])) pPathGS.append([i] + pPath[i]) # print("PATH INDEX OBJ : ", goalStates.index(pPath[i][j][-1])) # Choix pour les objectifs for i in range(nObjectives): oPath.append(range(nPlayers)) oPath[i].sort(key=lambda a: len(allPath[a][i])) oPathGS.append([i] + oPath[i]) # Gale-Shapley matcher = PartyMatcher() matcher.setApplicants(oPathGS) matcher.setProviders(pPathGS) matcher.match() matches = matcher.getMatch() # Application path = range(nPlayers) for i in range(len(matches)): #print("MATCH : ", matches[i][1]) #print("PATH : ", pPath[i][pPathGS[i].index(matches[i][1])]) x, y = matches[i] path[x] = allPath[x][y] #path[i] = pPath[i][pPathGS[i].index()] return path
return state parser = argparse.ArgumentParser() parser.add_argument("problem_file") parser.add_argument("init_solution_file") parser.add_argument("num_trucks") parser.add_argument("truck_capacity") parser.add_argument("world_record_score") parser.add_argument("--plot", help="plot the map before the main loop engages", action = "store_true") parser.add_argument("--test", help="looks into the test directory rather than main directory", action = "store_true") args = parser.parse_args() problem_file = args.problem_file init_solution_file = args.init_solution_file num_trucks = int(args.num_trucks) do_plot = args.plot truck_capacity = int(args.truck_capacity) world_record_score = float( args.world_record_score ) test_environment = args.test init(problem_file) state = doAStar(initial_state(problem_file, init_solution_file), do_plot, world_record_score) states = [] while state.parent != None: # raw_input("\nEnter to see parent") # state.plot() states.append(state) state = state.parent filename = "solution_path_" + datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") write_solution_path(states, filename)