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

Fix incorrect mapping update for routing env from grid size (#68). #69

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions qgym/utils/input_parsing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This module contains function which parse user input.

With parsing we mean that the user input is validated and transformed to a predictable
format. In this way, user can give different input formats, but internally we are
format. In this way, user can give different input formats, but internally we are
assured that the data has the same format."""

from __future__ import annotations
Expand Down Expand Up @@ -105,7 +105,11 @@ def parse_connection_graph(

if grid_size is not None:
# Generate connection grid graph
return nx.grid_graph(grid_size)
graph = nx.grid_graph(grid_size)

# Relabel the nodes to be integers
graph = nx.convert_node_labels_to_integers(graph)
return graph

raise ValueError("No valid arguments for a connection graph were given")

Expand Down
7 changes: 5 additions & 2 deletions qgym/utils/input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def check_adjacency_matrix(adjacency_matrix: ArrayLike) -> NDArray[Any]:


def check_graph_is_valid_topology(graph: nx.Graph, name: str) -> None:
"""Check if `graph` with name 'name' is an instance of ``networkx.Graph`` and check
if the graph is valid topology graph.
"""Check if `graph` with name 'name' is an instance of ``networkx.Graph``, check
if the graph is valid topology graph and check if the nodes are integers.

Args:
graph: Graph to check.
Expand All @@ -226,6 +226,9 @@ def check_graph_is_valid_topology(graph: nx.Graph, name: str) -> None:
if len(graph) == 0:
raise ValueError(f"'{name}' has no nodes")

if not all(isinstance(node, int) for node in graph.nodes()):
raise TypeError(f"'{name}' has nodes that are not integers")


def check_instance(x: Any, name: str, dtype: type) -> None:
"""Check if `x` with name 'name' is an instance of dtype.
Expand Down
13 changes: 10 additions & 3 deletions tests/envs/routing/test_routing_env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import numpy as np
import pytest
from stable_baselines3.common.env_checker import check_env

Expand All @@ -19,6 +20,12 @@
},
],
)
def test_validity(kwargs: dict[str, tuple[int, int] | bool]) -> None:
env = Routing(**kwargs) # type: ignore[arg-type]
check_env(env, warn=True) # todo: maybe switch this to the gym env checker
class TestEnvironment:
def test_validity(self, kwargs: dict[str, tuple[int, int] | bool]) -> None:
env = Routing(**kwargs) # type: ignore[arg-type]
check_env(env, warn=True) # todo: maybe switch this to the gym env checker

def test_step(self, kwargs):
env = Routing(**kwargs) # type: ignore[arg-type]
obs = env.step(0)[0]
assert np.array_equal(obs["mapping"], [2, 1, 0, 3])
29 changes: 19 additions & 10 deletions tests/utils/test_input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,28 @@ def test_check_adjacency_matrix_errors(self, arg: Any) -> None:
check_adjacency_matrix(arg)


def test_check_graph_is_valid_topology() -> None:
graph = nx.Graph()
msg = "'test' has no nodes"
with pytest.raises(ValueError, match=msg):
class TestGraphValidTopology:
def test_check_graph_is_valid_topology(self) -> None:
graph = nx.Graph()
msg = "'test' has no nodes"
with pytest.raises(ValueError, match=msg):
check_graph_is_valid_topology(graph, "test")

graph.add_edge(1, 2)
check_graph_is_valid_topology(graph, "test")

graph.add_edge(1, 2)
check_graph_is_valid_topology(graph, "test")
graph.add_edge(1, 1)
msg = "'test' contains self-loops"
with pytest.raises(ValueError, match=msg):
check_graph_is_valid_topology(graph, "test")

def test_check_graph_is_valid_topology_nodes(self) -> None:
graph = nx.Graph()
graph.add_node((0, 0))

graph.add_edge(1, 1)
msg = "'test' contains self-loops"
with pytest.raises(ValueError, match=msg):
check_graph_is_valid_topology(graph, "test")
msg = "'test' has nodes that are not integers"
with pytest.raises(TypeError, match=msg):
check_graph_is_valid_topology(graph, "test")


class TestCheckInstance:
Expand Down