Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Course Schedule (Topological Sort) #62

Merged
merged 2 commits into from
Feb 5, 2025

Conversation

zafstojano
Copy link
Collaborator

What does this PR do?

This PR implements a reasoning environment for performing topological sort. In particular, it borrows the scenario from the popular Course Schedule exercise, where one has to figure out if it is possible to complete all courses, given the prerequisite relationships.

The solution is performed using topological sorting. In order to generate the scenarios, I first create a valid course schedule, and in the case the current draw selects an unsolvable scenario, I add an unobvious cycle (of min length 3) to the solution.

For sanity check, we can verify that the solution is always same as our intended outcome of the scenario before we even construct it, i.e. assert item["metadata"]["solution"] == item["metadata"]["solvable"]

Example

import reasoning_gym

# Defaults:
# num_courses: int = 5  # Total number of courses (ranging from 0 to num_courses - 1)
# max_num_prerequisites: int = 2  # Maximum number of prerequisites (per course)
# p_solvable: float = 0.5  # Probability that the course schedule is solvable
# min_cycle_length: int = 3  # Minimum length of a cycle in the prerequisites (if unsolvable)
# max_cycle_length: int = 5  # Maximum length of a cycle in the prerequisites (if unsolvable)

data = reasoning_gym.create_dataset('course_schedule', size=3, seed=42)

for i, item in enumerate(data):
    print(f"Example {i+1}:")
    print(f"Question: {item['question']}")
    print(f"Answer: {item['answer']}\n")
    print(f"Metadata: {item['metadata']}\n")
    assert item["metadata"]["solution"] == item["metadata"]["solvable"]
    print("--------------------------------------------------\n")
Example 1:
Question: There are a total of 5 courses you have to take, labeled from 0 to 4.

You are given the following list of prerequisites, where prerequisites[i] = (a_i, b_i) indicates that you must first take course b_i first if you want to take course a_i:
[(2, 1), (4, 2), (4, 3), (2, 3)]

Return True if you can finish all courses considering the prerequisites, or False otherwise.

Answer: True

Metadata: {'courses': [3, 1, 2, 4, 0], 'prerequisites': [(2, 1), (4, 2), (4, 3), (2, 3)], 'solution': True, 'solvable': True}

--------------------------------------------------

Example 2:
Question: There are a total of 5 courses you have to take, labeled from 0 to 4.

You are given the following list of prerequisites, where prerequisites[i] = (a_i, b_i) indicates that you must first take course b_i first if you want to take course a_i:
[(3, 0), (2, 4), (2, 3), (4, 1), (3, 1), (0, 1), (0, 2), (1, 3)]

Return True if you can finish all courses considering the prerequisites, or False otherwise.

Answer: False

Metadata: {'courses': [1, 4, 3, 2, 0], 'prerequisites': [(3, 0), (2, 4), (2, 3), (4, 1), (3, 1), (0, 1), (0, 2), (1, 3)], 'solution': False, 'solvable': False}

--------------------------------------------------

Example 3:
Question: There are a total of 5 courses you have to take, labeled from 0 to 4.

You are given the following list of prerequisites, where prerequisites[i] = (a_i, b_i) indicates that you must first take course b_i first if you want to take course a_i:
[]

Return True if you can finish all courses considering the prerequisites, or False otherwise.

Answer: True

Metadata: {'courses': [2, 1, 4, 0, 3], 'prerequisites': [], 'solution': True, 'solvable': True}

--------------------------------------------------

Copy link
Contributor

@andreaskoepf andreaskoepf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! We'll see how RL does with such binary tasks. Do you want to update the question template or should I do it after merge?

@andreaskoepf andreaskoepf merged commit b84e29a into open-thought:main Feb 5, 2025
3 checks passed
@andreaskoepf
Copy link
Contributor

@zafstojano Thanks a lot for your contribution!

@zafstojano zafstojano deleted the env/course-schedule branch February 5, 2025 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants