-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.py
More file actions
119 lines (92 loc) · 3.14 KB
/
rules.py
File metadata and controls
119 lines (92 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#rules.py
from typing import List, Dict, Any
from geometry import Rectangle, edge_distance
def check_inside_site(buildings: List[Dict], site_width=200, site_height=140):
violations = []
for i, b in enumerate(buildings):
r = b["rect"]
if r.left < 0 or r.bottom < 0 or r.right > site_width or r.top > site_height:
violations.append(i)
return {
"passed": len(violations) == 0,
"violations": violations
}
def check_boundary_clearance(buildings: List[Dict], clearance=10, site_width=200, site_height=140):
violations = []
for i, b in enumerate(buildings):
r = b["rect"]
if (
r.left < clearance or
r.bottom < clearance or
r.right > site_width - clearance or
r.top > site_height - clearance
):
violations.append(i)
return {
"passed": len(violations) == 0,
"violations": violations
}
def check_min_distance(buildings: List[Dict], min_dist=15):
violations = []
for i in range(len(buildings)):
for j in range(i + 1, len(buildings)):
r1 = buildings[i]["rect"]
r2 = buildings[j]["rect"]
d = edge_distance(r1, r2)
if d < min_dist:
violations.append({
"pair": (i, j),
"distance": d
})
return {
"passed": len(violations) == 0,
"violations": violations
}
def check_plaza_overlap(buildings: List[Dict], plaza: Rectangle):
violations = []
for i, b in enumerate(buildings):
if b["rect"].overlaps(plaza):
violations.append(i)
return {
"passed": len(violations) == 0,
"violations": violations
}
def check_neighbor_mix(buildings: List[Dict], max_dist=60):
violations = []
for i, b in enumerate(buildings):
if b["type"] != "A":
continue
rA = b["rect"]
found_neighbor = False
for j, other in enumerate(buildings):
if other["type"] == "B":
d = edge_distance(rA, other["rect"])
if d <= max_dist:
found_neighbor = True
break
if not found_neighbor:
violations.append(i)
return {
"passed": len(violations) == 0,
"violations": violations
}
def evaluate_layout(buildings: List[Dict], plaza: Rectangle):
return {
"inside_site": check_inside_site(buildings),
"boundary_clearance": check_boundary_clearance(buildings),
"min_distance": check_min_distance(buildings),
"plaza_overlap": check_plaza_overlap(buildings, plaza),
"neighbor_mix": check_neighbor_mix(buildings)
}
def score_layout(buildings, plaza, rule_report):
score = 0
# HARD penalty for invalid layouts
for rule in rule_report.values():
if not rule["passed"]:
score -= 100 * len(rule["violations"])
# Reward compactness
score += len(buildings) * 10
# Small reward for built area
total_area = sum(b["rect"].width * b["rect"].height for b in buildings)
score += total_area * 0.01
return score