graph jsp env

2025-12-07 0 399

Graph Job Shop Problem Gym Environment

About The Project

A Gymnasium Environment implementation
of the Job Shop Scheduling Problem (JSP) using the disjunctive graph approach.

  • Github: https://g*ithub.co**m/Alexander-Nasuta/graph-jspenv
  • PyPi: https://py*p*i.org*/project/graph-jsp-env/
  • Documentation: https://graph-jsp-env.readt*hed*o*cs.io/en/latest/

This environment is inspired by the
The disjunctive graph machine representation of the job shop scheduling problem
by Jacek Błażewicz and
Learning to Dispatch for Job Shop Scheduling via Deep Reinforcement Learning
by Zhang et al.

This environment does not explicitly include disjunctive edges, like specified by Jacek Błażewicz,
only conjunctive edges.
Additional information is saved in the edges and nodes, such that one could construct the disjunctive edges, so the is no loss in information.

This environment is more similar to the Zhang, Cong, et al. implementation.
Zhang, Cong, et al. seems to store exclusively time-information exclusively inside nodes
(see Figure 2: Example of state transition) and no additional information inside the edges (like weights in the representation of Jacek Błażewicz).

The DisjunctiveGraphJssEnv uses the networkx library for graph structure and graph visualization.
It is highly configurable and offers various rendering options.

Quick Start

Install the package with pip:

   pip install graph-jsp-env

Minimal Working Example: Random Actions

The code below shows a minimal working example without any reinforcement learning

import numpy as np
from graph_jsp_env.disjunctive_graph_jsp_env import DisjunctiveGraphJspEnv

jsp = np.array([
    [[1, 2, 0],  # job 0
     [0, 2, 1]],  # job 1
    [[17, 12, 19],  # task durations of job 0
     [8, 6, 2]]  # task durations of job 1
])

env = DisjunctiveGraphJspEnv(
    jps_instance=jsp,
    perform_left_shift_if_possible=True, 
    normalize_observation_space=True,  # see documentation of DisjunctiveGraphJspEnv::get_state for more information
    flat_observation_space=True,  # see documentation of DisjunctiveGraphJspEnv::get_state for more information
    action_mode=\'task\',  # alternative \'job\'
    dtype=\'float32\'  # dtype of the observation space
)

terminated = False
info = {}
for i in range(6):
    # get valid action mask. sample expects it to be a numpy array of type int8
    mask = np.array(env.valid_action_mask()).astype(np.int8)
    action = env.action_space.sample(mask=mask)
    state, reward, terminated, truncated, info = env.step(action)
    # chose the visualisation you want to see using the show parameter
    # console rendering
    env.render(show=[\"gantt_console\", \"graph_console\"])
    
print(f\"makespan: {info[\'makespan\']}\")

Stable Baselines3

To run the example below you need to install the following packages:

pip install stable_baselines3

pip install sb3_contrib

It is recommended to use the MaskablePPO algorithm from the sb3_contrib package.

import gymnasium as gym
import sb3_contrib
import numpy as np
from stable_baselines3.common.monitor import Monitor

from graph_jsp_env.disjunctive_graph_jsp_env import DisjunctiveGraphJspEnv
from graph_jsp_env.disjunctive_graph_logger import log
from sb3_contrib.common.wrappers import ActionMasker
from sb3_contrib.common.maskable.policies import MaskableActorCriticPolicy

jsp = np.array([
   [[1, 2, 0],  # job 0
    [0, 2, 1]],  # job 1
   [[17, 12, 19],  # task durations of job 0
    [8, 6, 2]]  # task durations of job 1
])

env = DisjunctiveGraphJspEnv(
   jps_instance=jsp,
   perform_left_shift_if_possible=True,
   normalize_observation_space=True,
   flat_observation_space=True,
   action_mode=\'task\',  # alternative \'job\'
)
env = Monitor(env)


def mask_fn(env: gym.Env) -> np.ndarray:
   return env.unwrapped.valid_action_mask()


env = ActionMasker(env, mask_fn)

model = sb3_contrib.MaskablePPO(MaskableActorCriticPolicy, env, verbose=1)

# Train the agent
log.info(\"training the model\")
model.learn(total_timesteps=10_000)

Ray rllib

The following example was provided by @nhuet.
To run the example below you need to install the following packages:

pip install \"ray[rllib]\" torch \"gymnasium[atari,accept-rom-license,mujoco]\"

import numpy as np
import ray
from graph_jsp_env.disjunctive_graph_jsp_env import DisjunctiveGraphJspEnv
from ray.rllib.algorithms import PPO
from ray.tune import register_env

jsp = np.array(
    [
        [
            [0, 1, 2],  # machines for job 0
            [0, 2, 1],  # machines for job 1
            [0, 1, 2],  # machines for job 2
        ],
        [
            [3, 2, 2],  # task durations of job 0
            [2, 1, 4],  # task durations of job 1
            [0, 4, 3],  # task durations of job 2
        ],
    ]
)

register_env(
    \"jsp\",
    lambda env_config: DisjunctiveGraphJspEnv(
        jps_instance=jsp,
        visualizer_kwargs=dict(handle_stop_signals=False)
    ),
)

ray.init()
algo = PPO(config=PPO.get_default_config().environment(\"jsp\"))
algo.train()

Visualisations

The environment offers multiple visualisation options.
There are four visualisations that can be mixed and matched:

  • gantt_window: a gantt chart visualisation in a separate window
  • graph_window: a graph visualisation in a separate window. This visualisation is computationally expensive.
  • gantt_console: a gantt chart visualisation in the console
  • graph_console: a graph visualisation in the console

The desired visualisation can be defaulted in the constructor of the environment with the argument default_visualisations.
To enable all visualisation specify default_visualisations=[\"gantt_window\", \"gantt_console\", \"graph_window\", \"graph_console\"].
The default visualisations are the used by the render() method if no visualisations are specified (using the show argument).

Visualisation in OpenCV Window

This visualisation can enabled by setting render_mode=\'window\' or setting the argument default_visualisations=[\"gantt_window\", \"graph_window\"] in the constructor of the environment.
Additional parameters for OpencCV will be passed to the cv2.imshow() function.
Example:

env.render(wait=1_000)  # render window closes automatically after 1 seconds
env.render(wait=None) # render window closes when any button is pressed (when the render window is focused)

Console Visualisation

This visualisation can enabled by setting render_mode=\'window\' or setting the argument default_visualisations=[\"gantt_console\", \"graph_console\"] in the constructor of the environment.

More Examples

Various examples can be found in the graph-jsp-examples repo.

State of the Project

This project is complementary material for a research paper.
It will not be frequently updated.
Minor updates might occur.

Dependencies

This project specifies multiple requirements files.
requirements.txt contains the dependencies for the environment to work. These requirements will be installed automatically when installing the environment via pip.
requirements_dev.txt contains the dependencies for development purposes. It includes the dependencies for testing, linting, and building the project on top of the dependencies in requirements.txt.

In this Project the dependencies are specified in the pyproject.toml file with as little version constraints as possible.
The tool pip-compile translates the pyproject.toml file into a requirements.txt file with pinned versions.
That way version conflicts can be avoided (as much as possible) and the project can be built in a reproducible way.

Development Setup

If you want to check out the code and implement new features or fix bugs, you can set up the project as follows:

Clone the Repository

clone the repository in your favorite code editor (for example PyCharm, VSCode, Neovim, etc.)

using https:

git clone https://g*ithub.co**m/Alexander-Nasuta/graph-jsp-env

or by using the GitHub CLI:

gh repo clone Alexander-Nasuta/graph-jsp-env

if you are using PyCharm, I recommend doing the following additional steps:

  • mark the src folder as source root (by right-clicking on the folder and selecting Mark Directory as -> Sources Root)
  • mark the tests folder as test root (by right-clicking on the folder and selecting Mark Directory as -> Test Sources Root)
  • mark the resources folder as resources root (by right-clicking on the folder and selecting Mark Directory as -> Resources Root)

at the end your project structure should look like this:

todo

Create a Virtual Environment (optional)

Most Developers use a virtual environment to manage the dependencies of their projects.
I personally use conda for this purpose.

When using conda, you can create a new environment with the name \’my-graph-jsp-env\’ following command:

conda create -n my-graph-jsp-env python=3.11

Feel free to use any other name for the environment or an more recent version of python.
Activate the environment with the following command:

conda activate my-graph-jsp-env

Replace my-graph-jsp-env with the name of your environment, if you used a different name.

You can also use venv or virtualenv to create a virtual environment. In that case please refer to the respective documentation.

Install the Dependencies

To install the dependencies for development purposes, run the following command:

pip install -r requirements_dev.txt
pip install tox

The testing package tox is not included in the requirements_dev.txt file, because it sometimes causes issues when
using github actions.
Github Actions uses an own tox environment (namely \’tox-gh-actions\’), which can cause conflicts with the tox environment on your local machine.

Reference: Automated Testing in Python with pytest, tox, and GitHub Actions.

Install the Project in Editable Mode

To install the project in editable mode, run the following command:

pip install -e .

This will install the project in editable mode, so you can make changes to the code and test them immediately.

Run the Tests

This project uses pytest for testing. To run the tests, run the following command:

pytest

For testing with tox run the following command:

tox

Tox will run the tests in a separate environment and will also check if the requirements are installed correctly.

Building and Publishing the Project to PyPi

In order to publish the project to PyPi, the project needs to be built and then uploaded to PyPi.

To build the project, run the following command:

python -m build

It is considered good practice use the tool twine for checking the build and uploading the project to PyPi.
By default the build command creates a dist folder with the built project files.
To check all the files in the dist folder, run the following command:

twine check dist/**

If the check is successful, you can upload the project to PyPi with the following command:

twine upload dist/**

Documentation

This project uses sphinx for generating the documentation.
It also uses a lot of sphinx extensions to make the documentation more readable and interactive.
For example the extension myst-parser is used to enable markdown support in the documentation (instead of the usual .rst-files).
It also uses the sphinx-autobuild extension to automatically rebuild the documentation when changes are made.
By running the following command, the documentation will be automatically built and served, when changes are made (make sure to run this command in the root directory of the project):

sphinx-autobuild ./docs/source/ ./docs/build/html/

This project features most of the extensions featured in this Tutorial: Document Your Scientific Project With Markdown, Sphinx, and Read the Docs | PyData Global 2021.

Contact

If you have any questions or feedback, feel free to contact me via email or open an issue on repository.

下载源码

通过命令行克隆项目:

git clone https://github.com/Alexander-Nasuta/graph-jsp-env.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 graph jsp env https://www.zuozi.net/31291.html

cakeshop
上一篇: cakeshop
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务