Skip to content

Commit

Permalink
Merge pull request #1 from thomasbai98/yuyang-greedy
Browse files Browse the repository at this point in the history
Yuyang greedy
  • Loading branch information
thomasbai98 authored Oct 9, 2021
2 parents 071d799 + e09c27e commit aa48ed4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
38 changes: 38 additions & 0 deletions players/g6_greedy.py
Original file line number Diff line number Diff line change
@@ -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) # 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)
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
28 changes: 17 additions & 11 deletions players/g6_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -17,9 +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 multiple 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:
Expand All @@ -37,16 +40,19 @@ 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
"""
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 = "no action"
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}


0 comments on commit aa48ed4

Please sign in to comment.