Skip to content
Snippets Groups Projects
Commit 510b3155 authored by Rhydian Brown's avatar Rhydian Brown
Browse files

add generate.py

parent 59df6d2d
No related branches found
No related tags found
No related merge requests found
import argparse
import math
import itertools
import random
from pathlib import Path
from datetime import datetime
def fully_connected(n):
return int((n * (n - 1)) / 2)
def default_filename():
# the current time in ISO format (ish)
return datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f") + ".txt"
parser = argparse.ArgumentParser()
parser.add_argument("nodes", type=int)
parser.add_argument("-o", dest="file", default=default_filename(), help="output file")
parser.add_argument("-c", "--charging", type=int, default=0, help="number of charging points")
parser.add_argument("-e", "--edges", type=int, help="number of edges, defaults to a complete graph")
parser.add_argument("--min", type=int, default=1, help="minimum distance")
parser.add_argument("--max", type=int, default=10, help="maximum distance")
args = parser.parse_args()
out = Path(args.file)
num_nodes = args.nodes
num_charging = args.charging
num_connections = args.edges or fully_connected(num_nodes)
min_distance = args.min
max_distance = args.max
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
repeat = 1 + int(math.log(num_nodes, len(alphabet)))
node_names = list(itertools.islice(itertools.product(alphabet, repeat=repeat), num_nodes))
nodes = {''.join(n): [] for n in node_names} # map node name to list of nodes connected to it
# generate paths
connected = list(itertools.combinations(nodes.keys(), 2))
random.shuffle(connected)
connected = connected[:num_connections]
for a, b in connected:
nodes[a].append(b)
charging = list(nodes.keys())
random.shuffle(charging)
charging = charging[:num_charging]
with out.open("w") as f:
f.write(f"{num_nodes} {num_connections}\n")
# write nodes and charging status
for n in nodes:
f.write(f"{n} {int(n in charging)}\n")
# write connections
keys = list(nodes.keys())
for a, v in nodes.items():
distance = random.randint(min_distance, max_distance)
for b in v:
f.write(f"{keys.index(a)} {keys.index(b)} {distance}\n")
print("saved to", out.name)
......@@ -9,4 +9,5 @@ packaging==23.2
pillow==10.2.0
pyparsing==3.1.1
python-dateutil==2.8.2
scipy==1.12.0
six==1.16.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment