This project uses a Graph Neural Network (GNN) to solve the Josephus problem. It includes training, validation, and inference capabilities.
model.py: Defines the GNN model structure.dataset.py: Handles data loading and preprocessing.utils.py: Contains utility functions for training and evaluation.train.py: Main script for training the model.inference.py: Script for making predictions using a trained model.requirements.txt: Lists all required Python packages.josephus_train.csv: Training dataset.josephus_test.csv: Testing dataset.
-
Ensure you have Python 3.7+ installed.
-
Install the required packages:
pip install -r requirements.txt
To train the model:
python train.py
This script will train the model using the data in josephus_train.csv and validate it using josephus_test.csv. The trained model will be saved as josephus_model.pth.
To make predictions using a trained model:
python inference.py
This script will load the trained model and allow you to input values for N and M to predict the survivor's position.
The train.py script supports distributed training. To run on multiple GPUs:
torchrun --nproc_per_node=NUM_GPUS train.py
Replace NUM_GPUS with the number of GPUs you want to use.
The josephus_train.csv and josephus_test.csv files contain the training and testing data respectively. Each row represents a Josephus problem instance with columns for N (number of people), M (skip count), and Result (survivor's position).
Ensure you have sufficient GPU memory for training, especially for large datasets. Adjust batch sizes in train.py if needed.