 |
CLIP Score: 26.74
from AIDL import *
def city_skyline(building_count: int = 10, max_building_width: float = 50*mm, max_building_height: float = 200*mm):
"""
Create a model of a city skyline with various buildings. The number of buildings, maximum building width,
and maximum building height can be adjusted.
"""
import random
# Main structure
skyline = Solid()
# Define a common anchor point for all buildings
anchor = Solid()
skyline.anchor = anchor
# Create buildings
skyline.buildings = []
for i in range(building_count):
building = Solid(shift=((i * (max_building_width + 5*mm)), 0)) # Use shift to position horizontally
width = random.uniform(max_building_width / 2, max_building_width)
height = random.uniform(max_building_height / 2, max_building_height)
# Define the building geometry as a simple rectangle
building.body = Rectangle((0,0), width, height)
# Add building to the skyline
skyline.buildings.append(building)
# Align the buildings at their base
skyline.AddConstraint(building.bb.bottom == anchor.bb.bottom)
return skyline
with AIDL_2D:
model = city_skyline()
print(model.Solve())
model.Render()
|
Please write an AIDL program to generate a city skyline
Creating a city skyline using AIDL will involve constructing various buildings with different heights and widths to represent different structures. We'll create a function that generates a city skyline filled with multiple buildings using simple geometric shapes such as rectangles.
Let's start by defining the structures and their hierarchical relationships.
### Step 1: Defining Structures
We'll create a main `Solid` structure named `skyline` and fill it with `Solid` structures representing individual buildings.
```python
from AIDL import *
def city_skyline(building_count: int = 10, max_building_width: float = 50*mm, max_building_height: float = 200*mm):
"""
Create a model of a city skyline with various buildings. The number of buildings, maximum building width,
and maximum building height can be adjusted.
"""
import random
# Main structure
skyline = Solid()
# Create buildings
skyline.buildings = []
for i in range(building_count):
building = Solid()
width = random.uniform(max_building_width / 2, max_building_width)
height = random.uniform(max_building_height / 2, max_building_height)
# Define the building geometry as a simple rectangle
building.body = Rectangle((0,0), width, height)
# Position the buildings next to each other (horizontally)
if i > 0:
previous_building = skyline.buildings[i-1]
building.AddConstraint(building.bb.left == previous_building.bb.right)
skyline.buildings.append(building)
# Align all buildings at their base
for bld in skyline.buildings:
skyline.AddConstraint(bld.bb.bottom == skyline.bb.bottom)
return skyline
with AIDL_2D:
model = city_skyline()
print(model.Solve())
model.Render()
```
### Explanation
1. **Main Structure**: We define the main structure of the skyline as a `Solid`.
2. **Building Generation**: We generate a specified number of buildings (`building_count`) with random widths and heights within specified ranges. Each building is added as a `Solid` with a `Rectangle` geometry.
3. **Positioning**: Buildings are positioned horizontally next to each other. To do this, we ensure the left side of each building aligns with the right side of the previous building. Additionally, we align the bottom of all buildings ensuring they start from the same base.
4. **Parameter Control**: We allow users to control the number of buildings, maximum widths, and maximum heights using function parameters.
This sample code allows for a simple representation of a city skyline with horizontal placement of buildings. Each run of the function will produce a different skyline due to the use of randomization in building dimensions. You can adjust the appearance by modifying the parameters passed to the function.
Traceback (most recent call last):
File "/net/projects/ranalab/jz/aidl/AIDL/generation/backend.py", line 21, in dumbrun
exec(code, env)
File "", line 38, in
File "/net/projects/ranalab/jz/aidl/AIDL/structure.py", line 968, in Solve
self._pre_solve(system)
File "/net/projects/ranalab/jz/aidl/AIDL/structure.py", line 917, in _pre_solve
structure._pre_solve(system)
File "/net/projects/ranalab/jz/aidl/AIDL/structure.py", line 925, in _pre_solve
constraint._pre_solve(system)
File "/net/projects/ranalab/jz/aidl/AIDL/constraints.py", line 182, in _pre_solve
self.expression._e.constrain(system, name=self.name)
File "/net/projects/ranalab/jz/aidl/AIDL/expression.py", line 133, in _e
return self._solver_expr()
File "/net/projects/ranalab/jz/aidl/AIDL/expression.py", line 175, in _solver_expr
return self.val[0]._e == self.val[1]._e
File "/net/projects/ranalab/jz/aidl/AIDL/expression.py", line 133, in _e
return self._solver_expr()
File "/net/projects/ranalab/jz/aidl/AIDL/expression.py", line 207, in _solver_expr
return self.val[0]()._solver_expr()
File "/net/projects/ranalab/jz/aidl/AIDL/structure.py", line 376, in _right
return self._max_u()
File "/net/projects/ranalab/jz/aidl/AIDL/structure.py", line 347, in _max_u
bb = self._bounding_box()
File "/net/projects/ranalab/jz/aidl/AIDL/structure.py", line 319, in _bounding_box
raise NonLocalContext(context, self._structure)
AIDL.feedback.NonLocalContext: .buildings[0] referenced from .buildings[1].
The error message indicates that there is a context issue with referencing bounding box properties of sibling structures. To resolve this, we need to ensure that constraints between buildings are handled correctly within a common parent context.
One way to handle this is to maintain a reference to the overall skyline width and use a common anchoring line for horizontal placement.
Let's adjust the code to address these issues:
```python
from AIDL import *
def city_skyline(building_count: int = 10, max_building_width: float = 50*mm, max_building_height: float = 200*mm):
"""
Create a model of a city skyline with various buildings. The number of buildings, maximum building width,
and maximum building height can be adjusted.
"""
import random
# Main structure
skyline = Solid()
# Define a common anchor point for all buildings
anchor = Solid()
skyline.anchor = anchor
# Create buildings
skyline.buildings = []
for i in range(building_count):
building = Solid(shift=((i * (max_building_width + 5*mm)), 0)) # Use shift to position horizontally
width = random.uniform(max_building_width / 2, max_building_width)
height = random.uniform(max_building_height / 2, max_building_height)
# Define the building geometry as a simple rectangle
building.body = Rectangle((0,0), width, height)
# Add building to the skyline
skyline.buildings.append(building)
# Align the buildings at their base
skyline.AddConstraint(building.bb.bottom == anchor.bb.bottom)
return skyline
with AIDL_2D:
model = city_skyline()
print(model.Solve())
model.Render()
```
### Explanation of Changes
1. **Common Anchor**: We introduce a common `Solid` named `anchor` within the `skyline` structure. This anchor serves as a base reference point for aligning the bottom edges of all buildings.
2. **Horizontal Positioning Using Shift**: Instead of using constraints to position buildings horizontally relative to one another, we use the `shift` parameter when creating each building. This avoids the context issue by ensuring each building has its own local coordinate system while maintaining the correct relative positions.
3. **Adding Base Alignment**: We align the bottom of each building to the bottom of the anchor to keep the skyline's base level.
This approach should resolve the `NonLocalContext` error by ensuring constraints stay within the appropriate context.
|