def search(city): visited = [] best_score = None best_move = None for i in range(iterations): r = random() if r <= density_change_chance: idx = dens = -1 lmt = 0 #RZ limit the while loop, it will cause dead loop when density_change_chance is high while ((dens == -1 or idx == -1) \ or ("DENSITY", idx, dens) in visited) \ and lmt < 6 * 30 : #RZ possible moves idx = randint(id_range[0], id_range[1] - 1) #TOTO magic number here? dens = randint(density_range[0], density_range[1]) lmt = lmt + 1 #RZ mov = ("DENSITY", idx, dens) else: x = y = newid = -1 lmt = 0 #RZ limit the while loop while ((x == -1 or y == -1 or newid == -1) \ or ("CELL", x, y, newid) in visited) \ and lmt < 256 * 6 : #RZ possible moves x = randint(0, city.width - 1) y = randint(0, city.height - 1) newid = randint(id_range[0], id_range[1]) lmt = lmt + 1 #RZ mov = ("CELL", x, y, newid) visited.append(mov) scr = score(city, mov) if best_score == None or scr > best_score: best_score = scr best_move = mov suggested_city = move(city, best_move) # Update AI params based on this move - changes from Ryan log.info("AI search complete. Best score = {}. Best move = {}.".format( best_score, best_move)) suggested_city.updateAIMov(best_move) return (suggested_city, best_move, objective.get_metrics(suggested_city))
def metrics(self): return metrics_dictionary(objective.get_metrics(self))
def search(city, queue=None): """Random single moves AI search algorithm. Args: city (cityiograph.City): the city for which we would like to optimize our metrics visited (set, optional): describes previously visited states in our prediction Returns: 3-tuple: suggested_city (cityiograph.City): - best_move (3-tuple): move type { 'DENSITY', 'CELL' } (str) index (int) and new density (int) OR x (int), y (int) and new type id for that cell scores (list): AI metrics scores """ if queue is None: queue = set() visited = copy.copy(queue) best_score = None best_scores = None # RZ 170615 passing score array to json best_move = None # RZ 170615 update the weights before search objective.update_weights(city.AIWeights) for i in range(iterations): r = random.random() if r <= density_change_chance: idx = dens = -1 lmt = 0 # RZ limit the while loop, it will cause dead loop when density_change_chance is high while ((dens == -1 or idx == -1) or ("DENSITY", idx) in visited) \ and lmt < 6 * 30 * 4: # RZ possible moves * 4 # TOTO magic number here? idx = random.randint(id_range[0], id_range[1]) dens = random.randint(density_range[0], density_range[1]) lmt = lmt + 1 # RZ mov = ("DENSITY", idx, dens) else: x = y = newid = -1 lmt = 0 # RZ limit the while loop while ((x == -1 or y == -1 or newid == -1) or x < 4 or x > 12 or y < 4 or y > 12 or x == 8 or y == 8 or ("CELL", x, y, newid) in visited) \ and lmt < 256 * 6 * 4: # RZ possible moves * 4 # Focus center of city and no road cell x = random.randint(0, city.width - 1) y = random.randint(0, city.height - 1) newid = random.randint(id_range[0], id_range[1]) lmt = lmt + 1 # RZ mov = ("CELL", x, y, newid) # Add this move to the visited set if mov[0] == 'DENSITY': visited.add(mov[:-1]) else: visited.add(mov) # KL - minor optimization to avoid duplicate calls to score method [scr, best] = scores(city, mov) if best_score is None or scr > best_score: best_score = scr best_scores = best best_move = mov # Determine our suggested city based on this move suggested_city = move(city, best_move) # Run the ML prediction on this suggested city final_city = ML.predict(suggested_city) # Update AI params based on this move - changes from Ryan log.info("AI search complete. Best score = {}. Best move = {}.".format( best_score, best_move)) final_city.AIMov = best_move final_city.score = scr return final_city, best_move, objective.get_metrics(final_city)
unity_server.send_data(result) log.info( "Same city received, but new toggle value. New ml_city and ai_city data successfully sent to GH.\n") log.info("Waiting to receive new city...") else: # RZ 170626 same city received and not the first city, update # meta data for GH UI, do not write to local file # RZ 170614 update city.animBlink ml_city.animBlink = animBlink ai_city.animBlink = animBlink # Then, get metrics dicts for cities ml_metrics = metrics_dictionary(objective.get_metrics(ml_city)) ai_metrics = metrics_dictionary(ai_metrics_list) # RZ firstly, we need to update only the meta data of the 2 # cities, including slider position and AI Step ml_city.updateMeta(input_city) ai_city.updateMeta(input_city) # Send resulting 2-city dictionary (predict/ai) back to GH if input_city.AIStep < 20: # KL - accounting for AI step case result = {'predict': ml_city.to_dict(), 'ai': None} else: result = {'predict': ml_city.to_dict(), 'ai': ai_city.to_dict()} server.send_data(result) unity_server.send_data(result)