Spaces:
Sleeping
Sleeping
Create json_handler.py
Browse files- json_handler.py +101 -0
json_handler.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import random
|
| 3 |
+
import networkx as nx
|
| 4 |
+
import os
|
| 5 |
+
from visualizer import get_sorted_nodes
|
| 6 |
+
|
| 7 |
+
def prepare_edges_for_json(G):
|
| 8 |
+
nodes_list = get_sorted_nodes(G)
|
| 9 |
+
nodes_list_dict = {str(i+1): node for i, node in enumerate(nodes_list)}
|
| 10 |
+
coord_to_id = {v: k for k, v in nodes_list_dict.items()}
|
| 11 |
+
edges_formatted = []
|
| 12 |
+
for u, v in G.edges():
|
| 13 |
+
if u in coord_to_id and v in coord_to_id:
|
| 14 |
+
edges_formatted.append({"room1": coord_to_id[u], "room2": coord_to_id[v]})
|
| 15 |
+
return edges_formatted, list(nodes_list_dict.keys()), nodes_list_dict
|
| 16 |
+
|
| 17 |
+
def prepare_parameter_for_json(G, I, nodes_list_dict):
|
| 18 |
+
n_count = len(G.nodes())
|
| 19 |
+
if n_count == 0:
|
| 20 |
+
return [], [], [], [], [], [], [], [], [], []
|
| 21 |
+
|
| 22 |
+
weights = [n_count / (n_count * (1 + (((i + 1) * 2) / 30))) for i in range(n_count)]
|
| 23 |
+
m_weights = random.choices(I, weights=weights, k=5)
|
| 24 |
+
t_weights_probs = [n_count / (n_count * (1 + (((i + 1) * 2) / 5))) for i in range(10)]
|
| 25 |
+
t_weights = random.choices(range(1, 11), weights=t_weights_probs, k=5)
|
| 26 |
+
|
| 27 |
+
dismantled, conditioningDuration, assignment, help_list = [], [], [], []
|
| 28 |
+
|
| 29 |
+
for m in range(5):
|
| 30 |
+
dismantled.append({"m": str(m + 1), "i": str(m_weights[m]), "t": t_weights[m], "value": 1})
|
| 31 |
+
conditioningDuration.append({"m": str(m + 1), "value": 1})
|
| 32 |
+
x = random.randint(1, 3)
|
| 33 |
+
if m > 2:
|
| 34 |
+
if 1 not in help_list: x = 1
|
| 35 |
+
if 2 not in help_list: x = 2
|
| 36 |
+
if 3 not in help_list: x = 3
|
| 37 |
+
help_list.append(x)
|
| 38 |
+
assignment.append({"m": str(m + 1), "r": str(x), "value": 1})
|
| 39 |
+
|
| 40 |
+
t_weights_del = random.choices(range(1, 11), weights=t_weights_probs[:10], k=3)
|
| 41 |
+
delivered = [{"r": str(r+1), "i": "1", "t": t_weights_del[r], "value": 1} for r in range(3)]
|
| 42 |
+
conditioningCapacity = [{"r": str(r+1), "value": 1} for r in range(3)]
|
| 43 |
+
|
| 44 |
+
CostMT, CostMB, CostRT, CostRB, Coord = [], [], [], [], []
|
| 45 |
+
for i in range(n_count):
|
| 46 |
+
s_id = str(i + 1)
|
| 47 |
+
CostMT.append({"i": s_id, "value": random.choice([2, 5])})
|
| 48 |
+
CostMB.append({"i": s_id, "value": random.choice([5, 10, 30])})
|
| 49 |
+
CostRT.append({"i": s_id, "value": random.choice([4, 10])})
|
| 50 |
+
CostRB.append({"i": s_id, "value": 1000 if i==0 else random.choice([20, 30, 100])})
|
| 51 |
+
if s_id in nodes_list_dict:
|
| 52 |
+
Coord.append({"i": s_id, "Coordinates": nodes_list_dict[s_id]})
|
| 53 |
+
|
| 54 |
+
return dismantled, assignment, delivered, conditioningCapacity, conditioningDuration, CostMT, CostMB, CostRT, CostRB, Coord
|
| 55 |
+
|
| 56 |
+
def generate_full_json_dict(G, loop=0):
|
| 57 |
+
edges, I, nodes_list_dict = prepare_edges_for_json(G)
|
| 58 |
+
dismantled, assignment, delivered, condCap, condDur, CostMT, CostMB, CostRT, CostRB, Coord = prepare_parameter_for_json(G, I, nodes_list_dict)
|
| 59 |
+
sets = {
|
| 60 |
+
"I": I, "E": {"bidirectional": True, "seed": 1, "edges": edges},
|
| 61 |
+
"M": ["1", "2", "3", "4", "5"], "R": ["1", "2", "3"]
|
| 62 |
+
}
|
| 63 |
+
params = {
|
| 64 |
+
"defaults": { "V": 1000, "CostMB": 100, "CostMT": 20, "CostRB": 300, "CostRT": 50 },
|
| 65 |
+
"t_max": 100, "V": [{"m": "1", "i": "1", "value": 42}],
|
| 66 |
+
"dismantled": dismantled, "delivered": delivered,
|
| 67 |
+
"conditioningCapacity": condCap, "conditioningDuration": condDur,
|
| 68 |
+
"assignment": assignment, "Coord": Coord,
|
| 69 |
+
"CostMT": CostMT, "CostMB": CostMB, "CostRT": CostRT, "CostRB": CostRB, "CostZR": 9, "CostZH": 5
|
| 70 |
+
}
|
| 71 |
+
return {"description": "Generated by Gradio", "sets": sets, "params": params}
|
| 72 |
+
|
| 73 |
+
def load_graph_from_data(data, name):
|
| 74 |
+
"""Core function to parse loaded JSON data into a NetworkX graph."""
|
| 75 |
+
G = nx.Graph()
|
| 76 |
+
id_to_coord = {}
|
| 77 |
+
|
| 78 |
+
if "params" in data and "Coord" in data["params"]:
|
| 79 |
+
for item in data["params"]["Coord"]:
|
| 80 |
+
coord = tuple(item["Coordinates"])
|
| 81 |
+
id_to_coord[item["i"]] = coord
|
| 82 |
+
G.add_node(coord)
|
| 83 |
+
|
| 84 |
+
if "sets" in data and "E" in data["sets"] and "edges" in data["sets"]["E"]:
|
| 85 |
+
for edge in data["sets"]["E"]["edges"]:
|
| 86 |
+
r1 = edge["room1"]
|
| 87 |
+
r2 = edge["room2"]
|
| 88 |
+
if r1 in id_to_coord and r2 in id_to_coord:
|
| 89 |
+
G.add_edge(id_to_coord[r1], id_to_coord[r2])
|
| 90 |
+
|
| 91 |
+
width = max([n[0] for n in G.nodes()]) if len(G.nodes()) > 0 else 10
|
| 92 |
+
height = max([n[1] for n in G.nodes()]) if len(G.nodes()) > 0 else 10
|
| 93 |
+
width = int(width + max(2, width * 0.1))
|
| 94 |
+
height = int(height + max(2, height * 0.1))
|
| 95 |
+
|
| 96 |
+
return {"name": name, "graph": G, "width": width, "height": height}
|
| 97 |
+
|
| 98 |
+
def load_graph_from_json(filepath):
|
| 99 |
+
with open(filepath, 'r') as f:
|
| 100 |
+
data = json.load(f)
|
| 101 |
+
return load_graph_from_data(data, os.path.basename(filepath))
|