Skip to content

Commit

Permalink
feat(instance): use Numpy random generator
Browse files Browse the repository at this point in the history
  • Loading branch information
netotz committed Aug 16, 2022
1 parent c24d334 commit f0b0d24
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions anpcp/models/instance.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from dataclasses import dataclass, field
import json
import os
from typing import List, Set, Tuple
from typing import List, Optional, Set, Tuple
from random import randint

from scipy import spatial
import numpy as np

from models.vertex import VertexType, Vertex

Expand Down Expand Up @@ -56,13 +57,23 @@ def __post_init__(self) -> None:
# self.farthests = self.get_farthest_indexes()

@classmethod
def random(cls, n: int, m: int, x_max: int = 1000, y_max: int = 1000) -> "Instance":
def random(
cls,
n: int,
m: int,
x_max: int = 1000,
y_max: int = 1000,
seed: Optional[int] = None,
) -> "Instance":
distinct_coords = set()
total = n + m

# use seeded random generator for reproducibility
rng = np.random.default_rng(seed)

while len(distinct_coords) < total:
distinct_coords |= {
(randint(0, x_max), randint(0, y_max))
(rng.integers(x_max, endpoint=True), rng.integers(y_max, endpoint=True))
for _ in range(total - len(distinct_coords))
}

Expand Down

1 comment on commit f0b0d24

@netotz
Copy link
Owner Author

@netotz netotz commented on f0b0d24 Aug 16, 2022

Choose a reason for hiding this comment

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

Related to #13

Please sign in to comment.