|
| 1 | +""" |
| 2 | +Example: Using Specialized Problem Classes |
| 3 | +=========================================== |
| 4 | +
|
| 5 | +This example demonstrates the new specialized Problem classes in compas_tno. |
| 6 | +""" |
| 7 | + |
| 8 | +from compas_tna.diagrams import FormDiagram |
| 9 | +from compas_tno.problems import FixedProblem |
| 10 | +from compas_tno.problems import FixedSymmetricProblem |
| 11 | +from compas_tno.problems import Problem |
| 12 | +from compas_tno.problems import SymmetricProblem |
| 13 | + |
| 14 | +# ============================================================================= |
| 15 | +# Example 1: Base Problem (No constraints) |
| 16 | +# ============================================================================= |
| 17 | + |
| 18 | +print("=" * 70) |
| 19 | +print("Example 1: Base Problem - All edges are independent") |
| 20 | +print("=" * 70) |
| 21 | + |
| 22 | +x_span = (0, 10.0) |
| 23 | +y_span = (0, 10.0) |
| 24 | +n = 5 |
| 25 | + |
| 26 | +form = FormDiagram.create_cross(x_span=x_span, y_span=y_span, n=n) |
| 27 | +problem = Problem.from_formdiagram(form) |
| 28 | + |
| 29 | +print(f"Vertices: {problem.n}") |
| 30 | +print(f"Edges: {problem.m}") |
| 31 | +print(f"Independent variables: {problem.k}") |
| 32 | +print(f"All edges are independent: {problem.k == problem.m}") |
| 33 | +print() |
| 34 | + |
| 35 | +# ============================================================================= |
| 36 | +# Example 2: Fixed Problem (Diagram fixed in plan) |
| 37 | +# ============================================================================= |
| 38 | + |
| 39 | +print("=" * 70) |
| 40 | +print("Example 2: FixedProblem - Independent edges computed") |
| 41 | +print("=" * 70) |
| 42 | + |
| 43 | +form = FormDiagram.create_cross(x_span=x_span, y_span=y_span, n=n) |
| 44 | +problem = FixedProblem.from_formdiagram(form, method="QR", printout=False) |
| 45 | + |
| 46 | +print(f"Vertices: {problem.n}") |
| 47 | +print(f"Edges: {problem.m}") |
| 48 | +print(f"Independent variables: {problem.k}") |
| 49 | +print(f"Dependent variables: {len(problem.dep)}") |
| 50 | +print(f"Reduction: {100 * (1 - problem.k / problem.m):.1f}%") |
| 51 | +print() |
| 52 | + |
| 53 | +# ============================================================================= |
| 54 | +# Example 3: Symmetric Problem (Exploits symmetry) |
| 55 | +# ============================================================================= |
| 56 | + |
| 57 | +print("=" * 70) |
| 58 | +print("Example 3: SymmetricProblem - Symmetry reduces variables") |
| 59 | +print("=" * 70) |
| 60 | + |
| 61 | +center = (5.0, 5.0) |
| 62 | +radius = 5.0 |
| 63 | +n_hoops = 6 |
| 64 | +n_parallels = 12 |
| 65 | +r_oculus = 0.5 |
| 66 | + |
| 67 | +form = FormDiagram.create_circular_radial_spaced( |
| 68 | + center=center, |
| 69 | + radius=radius, |
| 70 | + n_hoops=n_hoops, |
| 71 | + n_parallels=n_parallels, |
| 72 | + r_oculus=r_oculus, |
| 73 | +) |
| 74 | + |
| 75 | +# Remove boundary edges |
| 76 | +for edge in form.edges_on_boundary(): |
| 77 | + form.edge_attribute(edge, "_is_edge", False) |
| 78 | + |
| 79 | +problem = SymmetricProblem.from_formdiagram(form, center=center, printout=False) |
| 80 | + |
| 81 | +print(f"Vertices: {problem.n}") |
| 82 | +print(f"Edges: {problem.m}") |
| 83 | +print(f"Independent variables: {problem.k}") |
| 84 | +print(f"Reduction through symmetry: {100 * (1 - problem.k / problem.m):.1f}%") |
| 85 | +print() |
| 86 | + |
| 87 | +# ============================================================================= |
| 88 | +# Example 4: Fixed + Symmetric Problem (Maximum reduction) |
| 89 | +# ============================================================================= |
| 90 | + |
| 91 | +print("=" * 70) |
| 92 | +print("Example 4: FixedSymmetricProblem - Both constraints applied") |
| 93 | +print("=" * 70) |
| 94 | + |
| 95 | +form = FormDiagram.create_circular_radial_spaced( |
| 96 | + center=center, |
| 97 | + radius=radius, |
| 98 | + n_hoops=n_hoops, |
| 99 | + n_parallels=n_parallels, |
| 100 | + r_oculus=r_oculus, |
| 101 | +) |
| 102 | + |
| 103 | +for edge in form.edges_on_boundary(): |
| 104 | + form.edge_attribute(edge, "_is_edge", False) |
| 105 | + |
| 106 | +problem = FixedSymmetricProblem.from_formdiagram(form, method="QR", center=center, printout=False) |
| 107 | + |
| 108 | +print(f"Vertices: {problem.n}") |
| 109 | +print(f"Edges: {problem.m}") |
| 110 | +print(f"Independent variables: {problem.k}") |
| 111 | +print(f"Combined reduction: {100 * (1 - problem.k / problem.m):.1f}%") |
| 112 | +print() |
| 113 | + |
| 114 | +# ============================================================================= |
| 115 | +# Comparison Table |
| 116 | +# ============================================================================= |
| 117 | + |
| 118 | +print("=" * 70) |
| 119 | +print("Comparison Table") |
| 120 | +print("=" * 70) |
| 121 | +print(f"{'Problem Type':<25} {'Edges':<10} {'Variables':<12} {'Reduction':<12}") |
| 122 | +print("-" * 70) |
| 123 | + |
| 124 | +# Recreate all problems for fair comparison |
| 125 | +form1 = FormDiagram.create_cross(x_span=x_span, y_span=y_span, n=n) |
| 126 | +p1 = Problem.from_formdiagram(form1) |
| 127 | +print(f"{'Problem':<25} {p1.m:<10} {p1.k:<12} {'-':<12}") |
| 128 | + |
| 129 | +form2 = FormDiagram.create_cross(x_span=x_span, y_span=y_span, n=n) |
| 130 | +p2 = FixedProblem.from_formdiagram(form2, method="QR") |
| 131 | +print(f"{'FixedProblem':<25} {p2.m:<10} {p2.k:<12} {f'{100 * (1 - p2.k / p2.m):.1f}%':<12}") |
| 132 | + |
| 133 | +form3 = FormDiagram.create_circular_radial_spaced(center=center, radius=radius, n_hoops=n_hoops, n_parallels=n_parallels, r_oculus=r_oculus) |
| 134 | +for edge in form3.edges_on_boundary(): |
| 135 | + form3.edge_attribute(edge, "_is_edge", False) |
| 136 | +p3 = SymmetricProblem.from_formdiagram(form3, center=center) |
| 137 | +print(f"{'SymmetricProblem':<25} {p3.m:<10} {p3.k:<12} {f'{100 * (1 - p3.k / p3.m):.1f}%':<12}") |
| 138 | + |
| 139 | +form4 = FormDiagram.create_circular_radial_spaced(center=center, radius=radius, n_hoops=n_hoops, n_parallels=n_parallels, r_oculus=r_oculus) |
| 140 | +for edge in form4.edges_on_boundary(): |
| 141 | + form4.edge_attribute(edge, "_is_edge", False) |
| 142 | +p4 = FixedSymmetricProblem.from_formdiagram(form4, method="QR", center=center) |
| 143 | +print(f"{'FixedSymmetricProblem':<25} {p4.m:<10} {p4.k:<12} {f'{100 * (1 - p4.k / p4.m):.1f}%':<12}") |
| 144 | + |
| 145 | +print() |
| 146 | +print("=" * 70) |
| 147 | +print("✓ All specialized problem classes demonstrated successfully!") |
| 148 | +print("=" * 70) |
0 commit comments