Tetris

Solve the small system with Tetris algorithm. For the pod priorities we will use frequencies.

For this tutorial we need following modules:

import prp.recorder as recorder
import prp.xy as xy
import prp.utils as utils
from prp.solvers.simple import CostsType
import prp.solvers.tetris as tetris

If you want to use logging call

import logging

Parameters

We define paths to JSON files of the small warehouse system

LAYOUT_FILE = "../data/10-layout.json"
INITIAL_STATE_FILE = "../data/10-initial-state.json"
DEPARTURES_FILE = "../data/10-departures.json"

and store the solution to a JSON file:

SOLUTION_FILE = "../data/solutions/10-testris-solution.json"

Calculate

Load the warehouse model from JSON files.

def load_problem():
    """Load a test system with 10 places and 10 pods randomly distributed among them."""
    layout = xy.Layout()
    with open(LAYOUT_FILE, 'r') as infile:
        layout.load_from_json(infile)
        warehouse = layout.get_empty_warehouse()
        costs = layout.get_costs()
        warehouse.set_costs(costs)
    with open(INITIAL_STATE_FILE, 'r') as infile:
        recorder.load_initial_state_from_json(infile, warehouse)
    with open(DEPARTURES_FILE, 'r') as infile:
        departures = recorder.load_departures_from_json(infile)
        warehouse.set_departure_generator(departures)
    return warehouse


warehouse = load_problem()

If you want to switch on logging, add

import logging

For the tetris algorithm we need to decide whether we want to consider only the costs from station or we want to consider the costs from the station and to the next station. In this tutorial we will use costs in both directions. For more information see cheapest-place algorithm.

We also need to decide how to calculate priority of the pods:

  • By frequency: That means, a pod which departs more frequently has a higher priority than pods which depart less frequently.

or

  • By sojourn time: That means, a pod with a shorter sojourn time in the storage have higher priority than pods with longer sojourn time. The sojourn time is the time between the arrival to the storage and departure to the storage.

We select full decision costs for the costs and pod frequency for the priority.

solution = tetris.solve(warehouse, costs_type=CostsType.DECISION, occ_priority=tetris.OccupationPriority.POD_FREQUENCY)

We test our final result and print the costs.

for place_id in solution:
    require_decision = warehouse.next(place_id)

if not warehouse.finished():
    print("Problem is not solved completely!")

print("Total costs: {} at time {}.".format(warehouse.total_costs, warehouse.t))

Finally we store the solution to a JSON file.

utils.create_missing_directories_of_file(SOLUTION_FILE)
with open(SOLUTION_FILE, 'w') as outfile:
    recorder.store_solution_to_json(solution, outfile)