From d5d6497467d9c02f8889f457c1d82718f48d6d8b Mon Sep 17 00:00:00 2001 From: baium Date: Sat, 9 Oct 2021 01:48:57 -0400 Subject: [PATCH 1/2] "finished greedy" --- players/g6_greedy.py | 38 ++++++++++++++++++++++++++++++++++++++ players/g6_player.py | 29 ++++++++++++++++------------- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/players/g6_greedy.py b/players/g6_greedy.py index 8b13789..b1e0169 100644 --- a/players/g6_greedy.py +++ b/players/g6_greedy.py @@ -1 +1,39 @@ +import math +import numpy as np +import copy +import logging +from typing import Callable, Dict, List, Tuple, Union +def get_level(self, curr_level,i,j): + highest_level = 0 + for x in range(i,i+2): + for y in range(j,j+2): + if curr_level[x][y]>highest_level: + highest_level = curr_level[x][y] + return highest_level +def get_icecream(self,top_layer,curr_level,level,i,j): + value = 0 + amount = 0 + if level==0: + return 0,0 + for x in range(i,i+2): + for y in range(j,j+2): + if curr_level[x][y]==level: + amount+=1 + value+=(len(self.flavor_preference)+1-self.flavor_preference.index(top_layer[i][j])) + return value, amount +def greedy(self, top_layer, curr_level,maxAmount): + bestValue = -1.0 # best mean value of ice-cream + bestPos=(-1,-1) # the position of bestValue + bestAmount=0 # the amount of ice-cream of bestValue + for i in range(23): + for j in range(14): + level = get_level(self,curr_level,i,j) + value,amount = get_icecream(self,top_layer,curr_level,level,i,j) + if (amount>0 and amount<=maxAmount): + if (float(value/amount)>bestValue): + bestAmount = amount + bestValue = float(value/amount) + bestPos = (i,j) + print(bestValue*bestAmount) + return bestPos, bestAmount \ No newline at end of file diff --git a/players/g6_player.py b/players/g6_player.py index 5f30626..4c91d0e 100644 --- a/players/g6_player.py +++ b/players/g6_player.py @@ -3,6 +3,7 @@ import copy import logging from typing import Callable, Dict, List, Tuple, Union +from .g6_greedy import greedy,get_level,get_icecream class Player: @@ -17,11 +18,11 @@ def __init__(self, flavor_preference: List[int], rng: np.random.Generator, logge self.flavor_preference = flavor_preference self.rng = rng self.logger = logger - self.state = None + self.state = {"count":0} + self.greedy = greedy def serve(self, top_layer: np.ndarray, curr_level: np.ndarray, player_idx: int, get_flavors: Callable[[], List[int]], get_player_count: Callable[[], int], get_served: Callable[[], List[Dict[int, int]]], get_turns_received: Callable[[], List[int]]) -> Dict[str, Union[Tuple[int], int]]: """Request what to scoop or whom to pass in the given step of the turn. In each turn the simulator calls this serve function mulitple times for each step for a single player, until the player has scooped 24 units of ice-cream or asked to pass to next player or made an invalid request. If you have scooped 24 units of ice-cream in a turn then you get one last step in that turn where you can specify to pass to a player. - Args: top_layer (np.ndarray): Numpy 2d array of size (24, 15) containing flavor at each cell location curr_level (np.ndarray): Numpy 2d array of size (24, 15) containing current level at each cell location from 8 to 0, where 8 is highest level at start and 0 means no icecream left at this level @@ -30,23 +31,25 @@ def serve(self, top_layer: np.ndarray, curr_level: np.ndarray, player_idx: int, get_player_count (Callable[[], int]): number of total players get_served (Callable[[], List[Dict[int, int]]]): returns a list of dictionaries corresponding to each player, each dictionary at index i tells how units of a flavor are present in the bowl of the player with index i. E.g. lets say the fourth element is {1: 0, 2: 8...} means the corresponding player with index 4 has 0 units of flavor 1 and 8 units of flavor get_turns_received (Callable[[], List[int]]): returns a list of integers corresponding to each player, each element at index i tells how many turns a player with index i has played so far. - Returns: Dict[str, Union[Tuple[int],int]]: Return a dictionary of action to take in next step. 2 possible return values {"action": "scoop", "values" : (i,j)} stating to scoop the 4 cells with index (i,j), (i+1,j), (i,j+1), (i+1,j+1) {"action": "pass", "values" : i} pass to next player with index i """ - x = self.rng.random() - if x < 0.95: - i = self.rng.integers(0, top_layer.shape[0]-1) - j = self.rng.integers(0, top_layer.shape[1]-1) + action = None + values = None + if (self.state["count"]<24): + pos,amount = self.greedy(self, top_layer, curr_level, 24-self.state["count"]) action = "scoop" - values = (i, j) + values = pos + self.state["count"]+=amount else: - other_player_list = list(range(0, get_player_count())) - other_player_list.remove(player_idx) - next_player = other_player_list[self.rng.integers(0, len(other_player_list))] - action = "pass" - values = next_player + self.state["count"] = 0 + #self.passNextPlayer(self, top_layer: np.ndarray, curr_level: np.ndarray, player_idx: int, get_flavors: + #Callable[[], List[int]], get_player_count: Callable[[], int], get_served: Callable[ + # [], List[Dict[int, int]]], get_turns_received: Callable[[], List[int]]) + return {"action": action, "values": values} + + From e6de9b1b6c78d0a4bd62702fe23a6bd65837b6ef Mon Sep 17 00:00:00 2001 From: baium Date: Sat, 9 Oct 2021 01:50:52 -0400 Subject: [PATCH 2/2] "finished greedy" --- players/g6_greedy.py | 4 ++-- players/g6_player.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/players/g6_greedy.py b/players/g6_greedy.py index b1e0169..6e6d758 100644 --- a/players/g6_greedy.py +++ b/players/g6_greedy.py @@ -24,8 +24,8 @@ def get_icecream(self,top_layer,curr_level,level,i,j): return value, amount def greedy(self, top_layer, curr_level,maxAmount): bestValue = -1.0 # best mean value of ice-cream - bestPos=(-1,-1) # the position of bestValue - bestAmount=0 # the amount of ice-cream of bestValue + bestPos=(-1,-1) # position of bestValue + bestAmount=0 # amount of ice-cream of bestValue for i in range(23): for j in range(14): level = get_level(self,curr_level,i,j) diff --git a/players/g6_player.py b/players/g6_player.py index 4c91d0e..62cec10 100644 --- a/players/g6_player.py +++ b/players/g6_player.py @@ -37,7 +37,7 @@ def serve(self, top_layer: np.ndarray, curr_level: np.ndarray, player_idx: int, {"action": "scoop", "values" : (i,j)} stating to scoop the 4 cells with index (i,j), (i+1,j), (i,j+1), (i+1,j+1) {"action": "pass", "values" : i} pass to next player with index i """ - action = None + action = "no action" values = None if (self.state["count"]<24): pos,amount = self.greedy(self, top_layer, curr_level, 24-self.state["count"])