def test(strategy, vials): # init() # ------------------------------- # Initialisation # ------------------------------- players = [o for o in game.layers['joueur']] nbPlayers = len(players) score = [0] * nbPlayers # on localise tous les états initiaux (loc du joueur) initStates = [o.get_rowcol() for o in game.layers['joueur']] # on localise tous les murs wallStates = [w.get_rowcol() for w in game.layers['obstacle']] # Placement aleatoire des fioles goalPos = [] for o in game.layers['ramassable']: # les rouges puis jaunes puis bleues # et on met la fiole qqpart au hasard x = random.randint(0, 19) y = random.randint(0, 19) while (x, y) in wallStates + goalPos: x = random.randint(0, 19) y = random.randint(0, 19) o.set_rowcol(x, y) goalPos.append((x, y)) game.layers['ramassable'].add(o) game.mainiteration() # on localise tous les objets ramassables goalStates = [o.get_rowcol() for o in game.layers['ramassable']] # Boucle principale de déplacements goalPos = [goalStates[i:i + 1] for i in range(nbPlayers)] playersStruct = [] t_0 = time.process_time() Node.set_world_dimensions(game.spriteBuilder.rowsize, game.spriteBuilder.colsize) if strategy == 0: for i in range(nbPlayers): playersStruct.append(CoopPlayer( initStates[i], goalPos[i], wallStates)) CoopPlayer.set_cut_off_limit(5) elif strategy == 1: playersStruct = CoopPlanner(initStates, goalPos, wallStates) CoopPlayer.set_cut_off_limit(5) else: for i in range(nbPlayers): playersStruct.append(AdvancedPlayer( initStates[i], goalPos[i], wallStates)) AdvancedPlayer.set_pathfinding_frequence(6) AdvancedPlayer.set_search_epochs() cpu_time = time.process_time() - t_0 epoch = 0 average_epochs = 0 done = [False] * nbPlayers while not is_over(score, vials): epoch += 1 current = [] for j in range(nbPlayers): # on fait bouger chaque joueur séquentiellement t_0 = time.process_time() if strategy == 1: next_row, next_col = playersStruct.next() else: next_row, next_col = playersStruct[j].next() t_f = time.process_time() cpu_time += t_f - t_0 current.append((next_row, next_col)) # and ((next_row,next_col) not in posPlayers) if ((next_row, next_col) not in wallStates) and next_row >= 0 and next_row <= 19 and next_col >= 0 and next_col <= 19: players[j].set_rowcol(next_row, next_col) # si on a trouvé un objet on le ramasse if (next_row, next_col) in goalPos[j]: o = players[j].ramasse(game.layers) # on enlève ce goalState de la liste goalPos[j].remove((next_row, next_col)) score[j] += 1 if score[j] < vials: # et on remet un même objet à un autre endroit x = random.randint(0, 19) y = random.randint(0, 19) while (x, y) in wallStates + current + [el for sub in goalPos for el in sub]: x = random.randint(0, 19) y = random.randint(0, 19) o.set_rowcol(x, y) goalPos[j].append((x, y)) # on ajoute ce nouveau goalState game.layers['ramassable'].add(o) if strategy == 1: playersStruct.add_goal(j, (x, y)) else: playersStruct[j].add_goal((x, y)) elif not done[j]: done[j] = True average_epochs += epoch print(score) game.mainiteration() return cpu_time, (average_epochs / nbPlayers)
def main(): # for arg in sys.argv: iterations = 50 # default n_players = 4 if len(sys.argv) == 3: iterations = int(sys.argv[1]) n_players = int(sys.argv[2]) print("Iterations: ") print(iterations) init(n_players) # ------------------------------- # Initialisation # ------------------------------- players = [o for o in game.layers['joueur']] nbPlayers = len(players) score = [0] * nbPlayers # 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 murs wallStates = [w.get_rowcol() for w in game.layers['obstacle']] # print ("Wall states:", wallStates) # ------------------------------- # Placement aleatoire des fioles # ------------------------------- goalPos = [] for o in game.layers['ramassable']: # les rouges puis jaunes puis bleues # et on met la fiole qqpart au hasard x = random.randint(0, 19) y = random.randint(0, 19) while (x, y) in wallStates + goalPos: x = random.randint(0, 19) y = random.randint(0, 19) o.set_rowcol(x, y) goalPos.append((x, y)) game.layers['ramassable'].add(o) game.mainiteration() print(game.layers['ramassable']) # on localise tous les objets ramassables goalStates = [o.get_rowcol() for o in game.layers['ramassable']] print("Goal states:", goalStates) # on donne a chaque joueur une fiole a ramasser # en essayant de faire correspondre les couleurs pour que ce soit plus simple à suivre # ------------------------------- # Boucle principale de déplacements # ------------------------------- goalPos = [goalStates[i:i + 1] for i in range(nbPlayers)] t_0 = time.process_time() Node.set_world_dimensions(game.spriteBuilder.rowsize, game.spriteBuilder.colsize) CoopPlayer.set_cut_off_limit(5) coop_planner = CoopPlanner(initStates, goalPos, wallStates) cpu_time = time.process_time() - t_0 previous = [(-1, -1)] * nbPlayers epoch = 0 done = 0 for i in range(iterations): epoch += 1 current = [] for j in range( nbPlayers): # on fait bouger chaque joueur séquentiellement t_0 = time.process_time() next_row, next_col = coop_planner.next() t_f = time.process_time() cpu_time += t_f - t_0 current.append((next_row, next_col)) # and ((next_row,next_col) not in posPlayers) if ( (next_row, next_col) not in wallStates ) and next_row >= 0 and next_row <= 19 and next_col >= 0 and next_col <= 19: players[j].set_rowcol(next_row, next_col) print("player", j, "in (", next_row, next_col, ")") # game.mainiteration() # si on a trouvé un objet on le ramasse print("\tgoal", goalPos[j]) if (next_row, next_col) in goalPos[j]: o = players[j].ramasse(game.layers) # game.mainiteration() print("Objet trouvé par le joueur ", j) # on enlève ce goalState de la liste goalPos[j].remove((next_row, next_col)) score[j] += 1 done += 1 # et on remet un même objet à un autre endroit # x = random.randint(0, 19) # y = random.randint(0, 19) # while (x, y) in wallStates + current + [el for sub in goalPos for el in sub]: # x = random.randint(0, 19) # y = random.randint(0, 19) # o.set_rowcol(x, y) # print("Objet trouvé par le joueur ", j, ", new goal :", x, y) # goalPos[j].append((x, y)) # on ajoute ce nouveau goalState # game.layers['ramassable'].add(o) # coop_planner.add_goal(j, (x, y)) if done == nbPlayers: break if done == nbPlayers: game.mainiteration() break collision = False concurrent = [(p, q) for p in range(nbPlayers) for q in range(p + 1, nbPlayers)] for p, q in concurrent: if current[p] == current[q]: collision = True break if previous[p] == current[q] and current[p] == previous[q]: collision = True break if collision: print("===== collision =====") while True: pass previous = current game.mainiteration() print("Ended iteration", i + 1) print("===================================") print("===================", "STATS", "===================") print("Total CPU time:", cpu_time) print("Number of epochs needed to complete the tasks:", epoch) # print("Average number of A* iterations:", # TimeAStar.NB_ITERS / TimeAStar.NB_CALLS) print("scores:", score) pygame.quit()
def main(): # for arg in sys.argv: iterations = 100 # default if len(sys.argv) == 2: iterations = int(sys.argv[1]) print("Iterations: ") print(iterations) init() # ------------------------------- # Building the matrix # ------------------------------- # 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* # ------------------------------- # ------------------------------- # Moving along the path # ------------------------------- # bon ici on fait juste un random walker pour exemple... row, col = initStates[0] #row2,col2 = (5,5) steps = [] Node.set_world_dimensions(game.spriteBuilder.rowsize, game.spriteBuilder.colsize) for i in range(iterations): if steps == []: steps = AStar(initial_state=(row, col), goal_state=goalStates[0], walls=wallStates).run() x_inc, y_inc = steps.pop() #x_inc, y_inc = random.choice([(0, 1), (0, -1), (1, 0), (-1, 0)]) next_row = row + x_inc next_col = col + y_inc if ( (next_row, next_col) not in wallStates ) and next_row >= 0 and next_row <= 20 and next_col >= 0 and next_col <= 20: player.set_rowcol(next_row, next_col) print("pos 1:", next_row, next_col) game.mainiteration() col = next_col row = next_row # si on a trouvé l'objet on le ramasse if (row, col) == goalStates[0]: o = game.player.ramasse(game.layers) game.mainiteration() print("Objet trouvé!", o) goalStates.pop(0) if goalStates == []: break ''' #x,y = game.player.get_pos() ''' pygame.quit()