Esempio n. 1
0
def test_get_dict():
    pizza = Pizza(['vegetarian', 'medium', ['chicken', 'beef']])
    pizza_dict = pizza.get_dict()
    assert "type" in pizza_dict
    assert "size" in pizza_dict
    assert "toppings" in pizza_dict
    assert "price" in pizza_dict and isinstance(pizza_dict["price"], int)
Esempio n. 2
0
 def __init__(self, pizza_config):
     self.pizza = Pizza(pizza_config['pizza_lines'])
     self.min_each_ingredient_per_slice = pizza_config['l']
     self.max_ingredients_per_slice = pizza_config['h']
     self.cursor_position = (0,0)
     self.slice_mode = False
     self.valid_slices = []
     self.score = 0
Esempio n. 3
0
def manual_price_update():
    pizza = Pizza(['vegetarian', 'medium', ['chicken', 'beef']])
    rand1 = random.randint(1, 100)
    price1 = pizza.manual_price_update(rand1)
    assert price1 == rand1

    rand2 = random.randint(1, 100)
    price2 = pizza.manual_price_update(rand2)
    assert price2 == rand2
Esempio n. 4
0
def test_check_inputs():
    pizza = Pizza(['pepperoni', 'medium', ['olives', 'beef']])
    assert pizza.check_inputs(['pepperoni', 'medium', ['olives',
                                                       'beef']]) == True
    assert pizza.check_inputs(['vegetarian', 'small', ['chicken']]) == True
    assert pizza.check_inputs(['pepperoni', 'medium', []]) == True
    assert pizza.check_inputs(['invalid', 'medium', ['olives',
                                                     'beef']]) == False
    assert pizza.check_inputs(['pepperoni', 'invalid', ['olives',
                                                        'beef']]) == False
    assert pizza.check_inputs(['pepperoni', 'medium', ['invalid',
                                                       'beef']]) == False
    assert pizza.check_inputs(['pepperoni', 'medium', 'invalid']) == False
Esempio n. 5
0
def test_get_price():
    with open(os.path.dirname(__file__) + '/../data.json') as f:
        data = json.load(f)
    pizza = Pizza(['vegetarian', 'medium', ['beef', 'olives']])
    price = data['pizza']['vegetarian']['medium']
    price += data['topping']['beef']
    price += data['topping']['olives']
    assert pizza.get_price() == price

    pizza = Pizza(['vegetarian', 'small', ['chicken', 'beef']])
    price = data['pizza']['vegetarian']['small']
    price += data['topping']['chicken']
    price += data['topping']['beef']
    assert pizza.get_price() == price
Esempio n. 6
0
class GoogleEngineer:
    delta_position = {
        Direction.right: (0,1),
        Direction.down:  (1,0),
        Direction.left:  (0,-1),
        Direction.up:    (-1,0),
    }

    def __init__(self, pizza_config):
        self.pizza = Pizza(pizza_config['pizza_lines'])
        self.min_each_ingredient_per_slice = pizza_config['l']
        self.max_ingredients_per_slice = pizza_config['h']
        self.cursor_position = (0,0)
        self.slice_mode = False
        self.valid_slices = []
        self.score = 0

    def score_of(self, slice):
        if min(self.pizza.ingredients.of(slice)) >= self.min_each_ingredient_per_slice:
            return slice.ingredients
        return 0

    def move(self, direction):
        next_cursor_position = tuple(x0+x1 for x0,x1 in zip(self.cursor_position,self.delta_position[direction]))
        if (next_cursor_position[0] >= 0 and next_cursor_position[0] < self.pizza.r and
            next_cursor_position[1] >= 0 and next_cursor_position[1] < self.pizza.c):

            self.cursor_position = next_cursor_position
            return NEUTRAL_REWARD
        return NEGATIVE_REWARD

    def increase(self, direction):
        slice = self.pizza.slice_at(self.cursor_position)
        new_slice = self.pizza.increase(slice, direction, self.max_ingredients_per_slice)
        if (new_slice is not None and min(self.pizza.ingredients.of(new_slice)) >=
            self.min_each_ingredient_per_slice):

            if slice in self.valid_slices:
                self.valid_slices.remove(slice)
            self.valid_slices.append(new_slice)
            score = self.score_of(new_slice) - self.score_of(slice)
            self.score += score
            return score * POSITIVE_REWARD
        return NEUTRAL_REWARD if new_slice is not None else NEGATIVE_REWARD

    def do(self, action):
        r, c = self.cursor_position
        slice_map = self.pizza._map
        if action == 'toggle':
            self.slice_mode = not self.slice_mode
            return NEUTRAL_REWARD if slice_map[r][c] == -1 else NEGATIVE_REWARD

        if action not in Direction.__members__:
            raise ActionNotFoundException('Action \'{}\' is not recognised.'.format(action))

        if self.slice_mode:
            reward = self.increase(Direction[action])
            return reward
        reward = self.move(Direction[action])
        return reward

    def state(self):
        return {
            'ingredients_map': self.pizza.ingredients._map.tolist(),
            'slices_map': self.pizza._map.tolist(),
            'cursor_position': self.cursor_position,
            'slice_mode': self.slice_mode,
            'min_each_ingredient_per_slice': self.min_each_ingredient_per_slice,
            'max_ingredients_per_slice': self.max_ingredients_per_slice,
        }
Esempio n. 7
0
def test_get_size():
    pizza = Pizza(['pepperoni', 'small', ['chicken', 'olives']])
    assert pizza.get_type() == 'pepperoni'

    pizza = Pizza(['vegetarian', 'medium', ['beef', 'olives']])
    assert pizza.get_type() == 'vegetarian'
Esempio n. 8
0
def test_update():
    pizza = Pizza(['pepperoni', 'small', ['chicken', 'olives']])
    assert pizza.get_size() == 'small'
    pizza.update([-1, 'medium', -1])
    assert pizza.get_size() == 'medium'

    pizza = Pizza(['vegetarian', 'medium', ['beef', 'olives']])
    assert pizza.get_type() == 'vegetarian'
    pizza.update(['pepperoni', -1, -1])
    assert pizza.get_type() == 'pepperoni'

    pizza = Pizza(['vegetarian', 'small', ['chicken', 'beef']])
    assert pizza.get_toppings() == ['chicken', 'beef']
    pizza.update([-1, -1, ['olives']])
    assert pizza.get_toppings() == ['olives']

    pizza = Pizza(['pepperoni', 'small', ['chicken', 'olives']])
    assert not pizza.update([-1, 'invalid', -1])

    pizza = Pizza(['vegetarian', 'medium', ['beef', 'olives']])
    assert not pizza.update(['invalid', -1, -1])

    pizza = Pizza(['vegetarian', 'small', ['chicken', 'beef']])
    assert not pizza.update([-1, -1, 'invalid'])
Esempio n. 9
0
def test_get_toppings():
    pizza = Pizza(['pepperoni', 'small', ['chicken', 'olives']])
    assert pizza.get_toppings() == ['chicken', 'olives']

    pizza = Pizza(['vegetarian', 'medium', ['beef', 'olives']])
    assert pizza.get_toppings() == ['beef', 'olives']
Esempio n. 10
0
 def add_pizza(self, pizza_info):
     """Creates a pizza object using pizza_info and appends this pizza to the self.pizzas list.
     May throw exception if pizza_info is invalid"""
     pizza = Pizza(pizza_info)
     self.pizzas.append(pizza)
Esempio n. 11
0
class GoogleEngineer:
    delta_position = {
        Direction.right: (0, 1),
        Direction.down: (1, 0),
        Direction.left: (0, -1),
        Direction.up: (-1, 0),
    }

    def __init__(self, pizza_config):
        self.pizza = Pizza(pizza_config['pizza_lines'])
        self.min_each_ingredient_per_slice = pizza_config['l']
        self.max_ingredients_per_slice = pizza_config['h']
        self.cursor_position = (0, 0)
        self.slice_mode = False
        self.valid_slices = []
        self.score = 0

    def score_of(self, slice):
        if min(self.pizza.ingredients.of(
                slice)) >= self.min_each_ingredient_per_slice:
            return slice.ingredients
        return 0

    def move(self, direction):
        next_cursor_position = tuple(x0 + x1 for x0, x1 in zip(
            self.cursor_position, self.delta_position[direction]))
        if (next_cursor_position[0] >= 0
                and next_cursor_position[0] < self.pizza.r
                and next_cursor_position[1] >= 0
                and next_cursor_position[1] < self.pizza.c):

            self.cursor_position = next_cursor_position
            return NEUTRAL_REWARD
        return NEGATIVE_REWARD

    def increase(self, direction):
        slice = self.pizza.slice_at(self.cursor_position)
        if slice is None:
            return -0.1
        new_slice = self.pizza.increase(slice, direction,
                                        self.max_ingredients_per_slice)
        if (new_slice is not None and min(self.pizza.ingredients.of(new_slice))
                >= self.min_each_ingredient_per_slice):

            if slice in self.valid_slices:
                self.valid_slices.remove(slice)
            self.valid_slices.append(new_slice)
            score = self.score_of(new_slice) - self.score_of(slice)
            self.score += score
            return score * POSITIVE_REWARD
        return NEUTRAL_REWARD if new_slice is not None else NEGATIVE_REWARD

    def increase_neg(self, direction):
        slice = self.pizza.slice_at(self.cursor_position)
        #new_slice = self.pizza.increase(slice, direction, self.max_ingredients_per_slice)
        #if (new_slice is not None and min(self.pizza.ingredients.of(new_slice)) <
        #   self.min_each_ingredient_per_slice):
        if (slice is not None and min(self.pizza.ingredients.of(slice)) <
                self.min_each_ingredient_per_slice):
            #if slice in self.valid_slices:
            #   self.valid_slices.remove(slice)
            #self.valid_slices.append(slice)
            score = slice.ingredients  #- self.score_of(slice)
            #self.score += score
            return POSITIVE_REWARD * 0.5 * score
        return NEUTRAL_REWARD  #NEUTRAL_REWARD if new_slice is not None else NEGATIVE_REWARD

    def do(self, action):
        #cut = 0
        if action in ['cut_up', 'cut_left', 'cut_down', 'cut_right']:
            self.slice_mode = True
            action = 'up' if action == 'cut_up' else action
            action = 'down' if action == 'cut_down' else action
            action = 'left' if action == 'cut_left' else action
            action = 'right' if action == 'cut_right' else action
        elif self.slice_mode == True:
            self.slice_mode = False
            reward = -self.increase_neg(Direction[action])
            slice = self.pizza.slice_at(self.cursor_position)
            for direction in Direction:
                self.pizza.disable_increase_around(slice, direction, 1)
            self.move(Direction[action])
            return reward
        else:
            self.slice_mode = False
            #reward = -self.increase_neg(Direction[action])
            #return reward

        if action == 'toggle':
            self.slice_mode = not self.slice_mode
            return 0
        if action not in Direction.__members__:
            raise ActionNotFoundException(
                'Action \'{}\' is not recognised.'.format(action))

        if self.slice_mode:
            reward = self.increase(Direction[action])
            self.cut = 1
            return reward
        reward = self.move(Direction[action])
        return reward

    def state(self):
        return {
            'ingredients_map': self.pizza.ingredients._map.tolist(),
            'slices_map': self.pizza._map.tolist(),
            'cursor_position': self.cursor_position,
            'slice_mode': self.slice_mode,
            'min_each_ingredient_per_slice':
            self.min_each_ingredient_per_slice,
            'max_ingredients_per_slice': self.max_ingredients_per_slice,
        }