def place_stores(self, slmap: SiteLocationMap, store_locations: Dict[int, List[Store]], current_funds: float): global count count += 1 store_conf = self.config['store_config'] num_rand = 100 sample_pos = [] for i in range(num_rand): x = random.randint(0, slmap.size[0]) y = random.randint(0, slmap.size[1]) sample_pos.append((x, y)) # Choose largest store type possible: if current_funds >= store_conf['large']['capital_cost']: store_type = 'large' elif current_funds >= store_conf['medium']['capital_cost']: store_type = 'medium' else: store_type = 'small' best_score = 0 best_pos = [] for pos in sample_pos: sample_store = Store(pos, store_type) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) if count < 9: sample_alloc = attractiveness_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() else: sample_alloc = closest_store_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() if sample_score > best_score: best_score = sample_score best_pos = [pos] elif sample_score == best_score: best_pos.append(pos) # max_alloc_positons = np.argwhere(alloc[self.player_id] == np.amax(alloc[self.player_id])) # pos = random.choice(max_alloc_positons) pos1 = random.choice(best_pos) if count <= 8: self.stores_to_place = [Store(random.choice(best_pos), store_type)] else: temp_current_funds = current_funds - store_conf[store_type]['capital_cost'] print(' ') print(temp_current_funds) print(' ') if temp_current_funds >= store_conf['large']['capital_cost']: store_type_2 = 'large' elif temp_current_funds >= store_conf['medium']['capital_cost']: store_type_2 = 'medium' else: store_type_2 = 'small' pos2 = random.choice(best_pos) self.stores_to_place = [Store(pos1, store_type), Store(pos2, store_type_2)] return
def get_attractiveness_allocation(self, slmap, store_locations, store_conf, store): store_locations[self.player_id].append(store) alloc = attractiveness_allocation(slmap, store_locations, store_conf) score = (alloc[self.player_id] * slmap.population_distribution).sum() store_locations[self.player_id].pop() return score
def calculate_profit( self, slmap: SiteLocationMap, store_locations: Dict[int, List[Store]], current_funds: float, sample: Store, ): cost = self.config["store_config"][ sample.store_type]["capital_cost"] * 1.5 sample_alloc = attractiveness_allocation( slmap, temp_store_locations, store_conf) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) sample_alloc = attractiveness_allocation( slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution ).sum() * self.config["profit_per_customer"] if sample_score > cost: return True return False
def place_stores(self, slmap: SiteLocationMap, store_locations: Dict[int, List[Store]], current_funds: float): store_conf = self.config['store_config'] sample_pos = [] # Choose largest store type possible: if current_funds >= store_conf['large']['capital_cost']: store_type = 'large' elif current_funds >= store_conf['medium']['capital_cost']: store_type = 'medium' else: store_type = 'small' #try to delete all taken points and radius? # use store_locations # store_conf[store_type]['attractiveness'] coords = [(x, y) for x in range(20) for y in range(20)] all_stores_pos = [] for player, player_stores in store_locations.items(): for player_store in player_stores: all_stores_pos.append(player_store.pos) # need to add radius r = store_conf[store_type]['attractiveness'] radcircle = player_store.pos[0]**2 + player_store.pos[1]**2 a = np.array(coord) b = np.where(radcircle < r) sample_pos = set(coords).symmetric_difference(all_stores_pos) #print ("sample pos: ",sample_pos) best_score = 0 best_pos = [] for pos in sample_pos: sample_store = Store(pos, store_type) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) sample_alloc = attractiveness_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() if sample_score > best_score: best_score = sample_score best_pos = [pos] elif sample_score == best_score: best_pos.append(pos) print("best pos: " + str(best_pos)) # max_alloc_positons = np.argwhere(alloc[self.player_id] == np.amax(alloc[self.player_id])) # pos = random.choice(max_alloc_positons) self.stores_to_place = [Store(random.choice(best_pos), store_type)] return
def place_stores( self, slmap: SiteLocationMap, store_locations: Dict[int, List[Store]], current_funds: float, ): store_conf = self.config["store_config"] num_rand = 100 sample_pos = [] for i in range(num_rand): x = random.randint(0, slmap.size[0]) y = random.randint(0, slmap.size[1]) sample_pos.append((x, y)) # Choose largest store type possible: if current_funds >= store_conf["large"]["capital_cost"]: store_type = "large" elif current_funds >= store_conf["medium"]["capital_cost"]: store_type = "medium" else: store_type = "small" best_score = 0 best_pos = [] for pos in sample_pos: sample_store = Store(pos, store_type) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) sample_alloc = attractiveness_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() if sample_score > best_score: best_score = sample_score best_pos = [pos] elif sample_score == best_score: best_pos.append(pos) # max_alloc_positons = np.argwhere(alloc[self.player_id] == np.amax(alloc[self.player_id])) # pos = random.choice(max_alloc_positons) self.stores_to_place = [Store(random.choice(best_pos), store_type)] return
def place_stores(self, slmap: SiteLocationMap, store_locations: Dict[int, List[Store]], current_funds: float): store_conf = self.config['store_config'] num_rand = 100 #Array of all stores all_stores_pos = [] for player, player_stores in store_locations.items(): for player_store in player_stores: all_stores_pos.append(player_store.pos) #instead of randomly choosing, choose locations with largest population sample_pos = [] #count number of stores to track round and increase min_dist num_stores = len(store_locations[self.player_id]) min_dist = 50 our_stores = store_locations[self.player_id] #sorted population density from highest to lowest sorted_indices = tuple( map( tuple, np.dstack( np.unravel_index( np.argsort(slmap.population_distribution.ravel()), slmap.size))[0][::-1])) counter = 0 for max_pos in sorted_indices: if counter >= num_rand: break too_close = False for store in our_stores: dist = np.sqrt( np.square(max_pos[0] - store.pos[0]) + np.square(max_pos[1] - store.pos[1])) if store.store_type == 'small': min_dist = 25 elif store.store_type == 'medium': min_dist = 50 else: min_dist = 100 if dist < min_dist: too_close = True if not too_close: counter = counter + 1 sample_pos.append((max_pos[0], max_pos[1])) # Choose largest store type possible: if current_funds >= store_conf['large']['capital_cost']: remaining_funds = current_funds - 100000 store_type = 'large' min_dist = 100 elif current_funds >= store_conf['medium']['capital_cost']: remaining_funds = current_funds - 50000 store_type = 'medium' min_dist = 50 else: remaining_funds = current_funds - 10000 store_type = 'small' min_dist = 25 best_score = 0 best_pos = [] for pos in sample_pos: sample_store = Store(pos, store_type) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) sample_alloc = attractiveness_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() if sample_score > best_score: best_score = sample_score best_pos = [pos] elif sample_score == best_score: best_pos.append(pos) # max_alloc_positons = np.argwhere(alloc[self.player_id] == np.amax(alloc[self.player_id])) # pos = random.choice(max_alloc_positons) first_store = Store(best_pos[0], store_type) self.stores_to_place = [first_store] for next_best_pos in best_pos: dist = np.sqrt( np.square(next_best_pos[0] - first_store.pos[0]) + np.square(next_best_pos[1] - first_store.pos[1])) if dist > min_dist: if current_funds >= remaining_funds + 10000: second_store_type = 'small' second_store = Store(next_best_pos, second_store_type) self.stores_to_place.append(second_store) return for pos in sample_pos: dist = np.sqrt( np.square(pos[0] - first_store.pos[0]) + np.square(pos[1] - first_store.pos[1])) if dist > min_dist: if current_funds >= remaining_funds + 10000: second_store_type = 'small' second_store = Store(pos, second_store_type) self.stores_to_place.append(second_store) return return
def place_stores(self, slmap: SiteLocationMap, store_locations: Dict[int, List[Store]], current_funds: float): store_conf = self.config['store_config'] #1 ------------ build q table -------------------- #find num stores on map num_stores=0 for player, player_stores in store_locations.items(): num_stores= num_stores + len(player_stores) print ("num stores",num_stores) # read choices and top_10 file = open(os.path.join(os.path.dirname(__file__),"data/Q_player_current_data.txt"),"r") lines = file.readlines() if (lines!= []): indicies=lines[0].split(" ") build_q_table(indicies[0], indicies[1], num_stores, current_funds) #for previous round file.close() #2 -----Find list of best choices randomly--- #NEEED STORE TYPE # Choose largest store type possible: if current_funds >= store_conf['large']['capital_cost']: store_type = 'large' elif current_funds >= store_conf['medium']['capital_cost']: store_type = 'medium' else: store_type = 'small' #random 100 and find score sample_pos_and_scores = [] num_rand = 100 attractives=[] for i in range(num_rand): x = random.randint(0, slmap.size[0]) y = random.randint(0, slmap.size[1]) pos=(x,y) sample_store = Store(pos, store_type) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) sample_alloc = attractiveness_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() sample_pos_and_scores.append((pos,sample_score)) #sort and find max 10 sorted_list=sorted(sample_pos_and_scores,key=lambda x: x[1], reverse=True) end=9 top_10= sorted_list[:end] ''' #throw away points that are in store_type range from each other indicies=[] for i in range(0, len(top_10)): for j in range(0, len(top_10)): if j not in indicies: distance=euclidean_distance(top_10[i][0], top_10[j][0]) if (distance < store_conf[store_type]['attractiveness']/2): indicies.append(j) break #look at next pair #remove indicies end=end+1 for idx in indicies: top_10.pop(idx) top_10.append(sorted_list[end]) end=end+1 ''' #print (top_10) #tuple of a tuple #3 - randomly choose 2 stores- temporary choices = random.sample(top_10, 2) index1 = top_10.index(choices[0]) index2 = top_10.index(choices[1]) # write choices and top_10 f = open(os.path.join(os.path.dirname(__file__),"data/Q_player_current_data.txt"),"w") f.write (str(index1) +' '+ str(index2)) f.close() #4 - make selection # sample_score <- close to each other, not give right value, doesn't account for overlap if choices[0][1]+ choices[1][1] > current_funds: print("only 1") #make most attractive selection if (choices[0][1]>=choices[1][1]): self.stores_to_place = [Store(choices[0][0], store_type)] else: self.stores_to_place = [Store(choices[1][0], store_type)] else: #make both selections print("both") selection.append(Store(choices[0][0], store_type)) selection.append(Store(choices[1][0], store_type)) self.stores_to_place = selection return
def place_stores(self, slmap: SiteLocationMap, store_locations: Dict[int, List[Store]], current_funds: float): store_conf = self.config['store_config'] #1-----find store type--- #NEEED STORE TYPE # Choose largest store type possible: if current_funds >= store_conf['large']['capital_cost']: store_type = 'large' elif current_funds >= store_conf['medium']['capital_cost']: store_type = 'medium' else: store_type = 'small' #2-----Find the attractiveness values--- sample_pos = [] num_stores = 0 for player, player_stores in store_locations.items(): num_stores= num_stores + len(player_stores) if num_stores < 2: for i in range(400): x = random.randint(0, slmap.size[0]) y = random.randint(0, slmap.size[1]) sample_pos.append((x,y)) else: for i in range(100): x = random.randint(0, slmap.size[0]) y = random.randint(0, slmap.size[1]) sample_pos.append((x,y)) best_score = 0 _pos = [] best_pos = [] score = [] for pos in sample_pos: sample_store = Store(pos, store_type) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) sample_alloc = attractiveness_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() if num_stores < 2: if sample_score > best_score: best_score = sample_score _pos = [pos] elif sample_score == best_score: _pos.append(pos) best_pos.append(pos) score.append(sample_score) sorted_score = sorted(score) # sorted rewards sorted_10 = sorted_score[-11:-1] # Get the state and indices ''' rewardfile = "data/q_rewards.txt" rewards = correctingrewards(rewardfile) statefile = "data/q_states.txt" states = state_values(statefile) actionfile = "data/q_action.txt" actions = action_values(actionfile) q_table = q_table(states, actions, rewards) ''' #file = open(os.path.join(os.path.dirname(__file__),"data/Q_player_current_data.txt"),"r") ansFile = open(os.path.join(os.path.dirname(__file__), "data/MLdata.txt"), 'r') lines = ansFile.readlines() # lines = data.split("\n") answer = {} for line in lines: num = line.split(" ") answer[int(num[0])]=[int(num[1]), int(num[2])] # Get indicies indices = answer[num_stores] # [2, 3] attract0 = sorted_10[indices[0]] #top 1 score attract1 = sorted_10[indices[1]] #top 2 score index0 = score.index(attract0) index1 = score.index(attract1) loc0 = best_pos[index0] loc1 = best_pos[index1] loc = best_pos[score.index(min(attract0, attract1))] ''' if current_funds > (store_conf[store_type]['capital_cost'])*2: self.stores_to_place = [Store(loc0, store_type), Store(loc1, store_type)] else: ''' if num_stores < 2: loc = _pos[0] self.stores_to_place = [Store(loc, store_type)] return
def place_stores(self, slmap: SiteLocationMap, store_locations: Dict[int, List[Store]], current_funds: float): global count count += 1 if count <= 3: store_conf = self.config['store_config'] # Configurable minimum distance away to place store min_dist = 50 # Check if it can buy any store at all if current_funds < store_conf['small']['capital_cost']: self.stores_to_place = [] return # Choose largest store type possible if current_funds >= store_conf['large']['capital_cost']: store_type = 'large' elif current_funds >= store_conf['medium']['capital_cost']: store_type = 'medium' else: store_type = 'small' # Find highest population location all_stores_pos = [] for player, player_stores in store_locations.items(): for player_store in player_stores: all_stores_pos.append(player_store.pos) sorted_indices = tuple(map(tuple, np.dstack( np.unravel_index(np.argsort(slmap.population_distribution.ravel()), slmap.size))[0][::-1])) for max_pos in sorted_indices: too_close = False for pos in all_stores_pos: dist = np.sqrt(np.square(max_pos[0] - pos[0]) + np.square(max_pos[1] - pos[1])) if dist < min_dist: too_close = True if not too_close: self.stores_to_place = [Store(max_pos, store_type)] return else: store_conf = self.config['store_config'] num_rand = 100 sample_pos = [] for i in range(num_rand): x = random.randint(0, slmap.size[0]) y = random.randint(0, slmap.size[1]) sample_pos.append((x, y)) # Choose largest store type possible: if current_funds >= store_conf['large']['capital_cost']: store_type = 'large' elif current_funds >= store_conf['medium']['capital_cost']: store_type = 'medium' else: store_type = 'small' best_score = 0 best_pos = [] for pos in sample_pos: sample_store = Store(pos, store_type) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) sample_alloc = attractiveness_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() if sample_score > best_score: best_score = sample_score best_pos = [pos] elif sample_score == best_score: best_pos.append(pos) # max_alloc_positons = np.argwhere(alloc[self.player_id] == np.amax(alloc[self.player_id])) # pos = random.choice(max_alloc_positons) self.stores_to_place = [Store(random.choice(best_pos), store_type)] return ''' store_conf = self.config['store_config'] num_rand = 100 sample_pos = [] for i in range(num_rand): x = random.randint(0, slmap.size[0]) y = random.randint(0, slmap.size[1]) sample_pos.append((x, y)) # Choose largest store type possible: if current_funds >= store_conf['large']['capital_cost']: store_type = 'large' elif current_funds >= store_conf['medium']['capital_cost']: store_type = 'medium' else: store_type = 'small' #all stores all_stores_pos = [] for player, player_stores in store_locations.items(): for player_store in player_stores: all_stores_pos.append(player_store.pos) #max densities sorted_indices = tuple(map(tuple, np.dstack(np.unravel_index(np.argsort(slmap.population_distribution.ravel()), slmap.size))[0][::-1])) best_score = 0 best_pos = [] #max density starts here if count < 3: random_value = random.randint(0,len(sorted_indices)-1) max_pos = sorted_indices[random_value] self.stores_to_place = [Store(max_pos, store_type)] return for pos in sample_pos: sample_store = Store(pos, store_type) temp_store_locations = copy.deepcopy(store_locations) temp_store_locations[self.player_id].append(sample_store) sample_alloc = attractiveness_allocation(slmap, temp_store_locations, store_conf) sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() # else: # print(count) # sample_alloc = closest_store_allocation(slmap, temp_store_locations) # sample_score = (sample_alloc[self.player_id] * slmap.population_distribution).sum() if sample_score > best_score: best_score = sample_score best_pos = [pos] elif sample_score == best_score: best_pos.append(pos) # max_alloc_positons = np.argwhere(alloc[self.player_id] == np.amax(alloc[self.player_id])) # pos = random.choice(max_alloc_positons) self.stores_to_place = [Store(random.choice(best_pos), store_type)] return ''''