Skip to content

Commit 4d8a4ea

Browse files
committed
[fix] Normalize default CVRP demands with configured capacity
1 parent d4e8eef commit 4d8a4ea

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

envpool/jumanji/cvrp_env.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class CVRPEnv : public Env<CVRPEnvSpec>, public RenderableEnv {
195195
coordinates_[node * 2 + 1] = 0.0f;
196196
}
197197
if (spec_.config["cvrp_demands"_].empty()) {
198-
demands_[node] = node == 0 ? 0.0f : 0.05f;
198+
demands_[node] = node == 0 ? 0.0f : 1.0f / kMaxCapacity;
199199
} else {
200200
demands_[node] = configured_demands_[node];
201201
}

envpool/jumanji/jumanji_cvrp_test.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616
from __future__ import annotations
1717

1818
import numpy as np
19-
from absl.testing import absltest
19+
from absl.testing import absltest, parameterized
2020

2121
import envpool.jumanji.registration # noqa: F401
2222
from envpool.registration import make_gymnasium
2323

2424

25-
class JumanjiCVRPTest(absltest.TestCase):
25+
class JumanjiCVRPTest(parameterized.TestCase):
2626
"""Checks native CVRP transitions."""
2727

28-
def test_visit_customer_and_return_to_depot(self) -> None:
28+
@parameterized.parameters(3, 20, 30)
29+
def test_visit_customer_and_return_to_depot(self, capacity: int) -> None:
2930
"""Checks visiting a customer then returning to depot."""
3031
env = make_gymnasium(
31-
"CVRP-v1", num_envs=1, seed=0, render_mode="rgb_array"
32+
"CVRP-v1",
33+
num_envs=1,
34+
seed=0,
35+
cvrp_max_capacity=capacity,
36+
render_mode="rgb_array",
3237
)
3338
try:
3439
obs, _ = env.reset()
@@ -37,6 +42,7 @@ def test_visit_customer_and_return_to_depot(self) -> None:
3742
self.assertTrue(bool(obs["action_mask"][0, 1]))
3843
self.assertAlmostEqual(float(obs["coordinates"][0, 1, 0]), 0.05)
3944
self.assertAlmostEqual(float(obs["capacity"][0]), 1.0)
45+
self.assertAlmostEqual(float(obs["demands"][0, 1]), 1 / capacity)
4046

4147
obs, reward, terminated, truncated, _ = env.step(
4248
np.asarray([1], dtype=np.int32)
@@ -48,7 +54,9 @@ def test_visit_customer_and_return_to_depot(self) -> None:
4854
self.assertFalse(bool(obs["unvisited_nodes"][0, 1]))
4955
self.assertFalse(bool(obs["action_mask"][0, 1]))
5056
self.assertTrue(bool(obs["action_mask"][0, 0]))
51-
self.assertAlmostEqual(float(obs["capacity"][0]), 0.95, places=6)
57+
self.assertAlmostEqual(
58+
float(obs["capacity"][0]), (capacity - 1) / capacity, places=6
59+
)
5260
self.assertEqual(int(obs["trajectory"][0, 0]), 0)
5361
self.assertEqual(int(obs["trajectory"][0, 1]), 1)
5462

0 commit comments

Comments
 (0)