Skip to main content

A reinforcement learning environment for the Multi Port Stowage Planning problem

Project description

This package implements the mathematical model of the "Multi Port Stowage Planning" (MPSP) problem, introduced in Avriel et al. 1998, as a reinforcement learning environment using the Gymnasium API.

The Env class has the following input parameters:

  • R (int): number of rows
  • C (int): number of columns
  • N (int): number of ports

The env observations consist of a dictionary with the one hot encoded bay, and the upper triangular part of the transportation matrix as a flattened array.

Screenshot from the environment. The left matrix is the bay, and the right matrix is the transportation matrix. "x" denotes blocking containers. The reward decreases by one for each reshuffle necessitated by the agents stowage plan.

Note that since the mathematical model disregards any stability constraints, the env automatically swaps the column order in a lexocographic manor, based on the containers in the columns. This reduces the state space, by a factor of almost $C!$. Furthmore, the transportation matrix, is reshifted up and to the left, for each port, to reduce the state space even further. This means that from the point of view of the agent, it never leaves the first port.

When using the environment, you should call env.close() before exiting your program, to avoid memory leaks.

Minimal usage with Stable Baselines:

from MPSPEnv import Env
from sb3_contrib.ppo_mask import MaskablePPO

train_env = Env(R=8, C=4, N=5)

model = MaskablePPO(
    "MultiInputPolicy",
    train_env
)

model.learn(
    total_timesteps=1e6,
    progress_bar=True
)

train_env.close()

If you wish to try the env yourself, placing containers using keys 0-3 and removing containers using keys 4-7, you can use the following script:

from MPSPEnv import Env
import pygame, numpy as np


def get_action_from_key(key):
    return key - pygame.K_0 if pygame.K_0 <= key <= pygame.K_7 else None


if __name__ == "__main__":
    env = Env(3, 4, 6)
    env.reset()

    while True:
        env.render()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                env.close()
                exit()
            elif (
                event.type == pygame.KEYDOWN
                and (action := get_action_from_key(event.key)) is not None
            ):
                _, reward, done, _, _ = env.step(action)
                if done:
                    env.render()
                    pygame.time.wait(1000)
                    env.close()
                    exit()

        pygame.time.wait(10)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpspenv-0.1.5.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

MPSPEnv-0.1.5-cp311-cp311-macosx_14_0_arm64.whl (24.0 kB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

File details

Details for the file mpspenv-0.1.5.tar.gz.

File metadata

  • Download URL: mpspenv-0.1.5.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for mpspenv-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f01bcfe408f402e4645a0434147dd5a28da1968aa864328785ac9b88d1936368
MD5 45e7e1b4070e1e72ae5720f603ab78a3
BLAKE2b-256 9e6c8f48ef839406538f092a4ef468776dc41f827081339353b06112fa58e8fd

See more details on using hashes here.

File details

Details for the file MPSPEnv-0.1.5-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for MPSPEnv-0.1.5-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 ecb42d24382140ce09adc4dfb8158798de941988d1b2667d70920dfcca1a75a1
MD5 6f380b4ed6ab341e3de7a1d3475d1746
BLAKE2b-256 ba12488a5604d9395dda21701f019d1e0d97e1b440b4d753def60883968a4db5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page