Portuguese law fixes the smallest area each room in a dwelling may have, and the smallest gross area for each typology, from the studio (T0) to the six-bedroom flat (T6). This study asked whether those rules, written down as data, are enough to generate a legal flat plan inside Grasshopper, without external plugins.
The short answer is no, not yet. The plan below is cut live in your browser with the same algorithm the study arrived at; the schedule beside it checks every space against the law and shows where random cutting breaks. Part two turns the law into the generator itself, with doors, and a solver that cannot produce an illegal plan.
…
Running 500 plans…
| # | Space | m² | Min | RGEU |
|---|
How to read it. The gross area comes from RGEU art. 67 and is offset 35 cm inwards for the external walls (the dark band). A list of directions and split points is consumed one cut at a time, first in, first out, as the Python split_several loop does. Naming the pieces (largest piece to largest requirement) and checking them against art. 66, 68, 69 and 70 is done by this page only: the Grasshopper definition never connected names to geometry. Hatched spaces fail at least one rule.
In 2023 almost 13% of people in Portugal lived in an overcrowded home, against about 9% a year earlier; in the Azores and Madeira the rate passed 20%. Overcrowding is a symptom of many things, but one of them is simply that there are not enough dwellings, and that each new one costs too much to build.
The study had, therefore, two objectives. The internal one was to build a random generator of minimal legal flats in Grasshopper with no external plugins, as a way to learn how to attach textual data (names, rules, adjacencies) to 2D shapes and to manipulate the shapes through those tags, a little like IFC binds properties to geometry. The external one was to contribute, however modestly, to the housing shortage: a tool of this kind, coupled with prefabrication, could help municipal architects produce affordable housing faster.
The aim is the legal minimum, so that the number of dwellings per plot and per euro goes up. Whether a minimal flat is a good place to live, or a cultural shock for those who move into one, is a real question, but not one this system tries to answer.
The RGEU (Regulamento Geral das Edificações Urbanas, in the 1975 wording of its housing articles) defines a dwelling almost entirely by numbers. These were typed into Grasshopper panels, one list per article, so that every room generated would be born inside its legal limits.
| Typology | Gross area art. 67 | Bedrooms art. 66 | Living art. 66 | Kitchen art. 66 | Extra area art. 66 | Sanitary art. 68 | Spaces in the program |
|---|
Areas in m². The extra area is a mandatory surplus the designer may place anywhere. Above T6 the gross area becomes 1.6 times the habitable area.
Area alone would allow a 10.5 m² bedroom 1 m wide. Article 69 prevents it with a rule that changes with size, and article 70 sets the width of corridors:
Clear ceiling heights complete the set: 2.40 m in habitable rooms and 2.20 m in halls and bathrooms (art. 65). The proportion limit of article 69 (length at most twice the width) became the first design rule of the generator.
The first idea was the intuitive one: generate each room at its legal area, with random proportions inside the 2:1 limit, and then pack the rooms as tightly as possible. A small Python component draws one rectangle per required area, with a width between √A and 1.5√A.

Packing was entrusted to Galapagos, Grasshopper's evolutionary solver. Rooms received random positions (0 to 30 m) and rotations (0° or 90°); the fitness was the difference between the area of the bounding box around all rooms and the sum of their areas, which is zero when the rooms fill their box exactly.

The rooms overlapped, which makes the fitness trivially good. A collision test was added (true or false for each pair, turned into 1 or 0) and multiplied by the fitness. However, multiplying by zero gives zero: as soon as the rooms stopped colliding the fitness was already perfect, whatever the gaps between them, and Galapagos was content to stop shuffling. Adding the collision term with different weights, instead of multiplying, did not change the outcome. No fitness definition that combined both goals was found, and the exploration was abandoned at this point.
The problem was then turned around. Instead of assembling rooms into a flat, the flat is taken whole and divided, the way a sheet of plywood is cut on a panel saw. In the vocabulary of operations research (Wäscher, Haußner and Schumann, 2007) this moves from a packing problem to a cutting problem with guillotine cuts, where every cut crosses the piece from side to side. The definition was drawn first in Grasshopper components and then rewritten in Python, which is where the recursion became possible.
Take the bounding box of the net flat area (the gross area of art. 67, offset 35 cm inwards). Working on the bounding box means the flat can be rotated and the same logic still applies, because every split line crosses the rectangle in its full extent.
Deconstruct the box into its two domains, x and y, and their lengths.

A 0/1 slider picks the direction d of the cut: it selects the unit vector (x or y) and, at the same time, the length along that direction. A second slider, from 0 to 1, is the split point s.
Multiplying vector, length and s places a point along the chosen side, anywhere from 0% to 100% of it.

The other direction is found with the index |d − 1|, which always returns the one not chosen. Moving the point by the full length in that direction gives the second end of the split line.
Intersect the line with the outline, split the outline at those parameters, close each piece with a line from its start to its end point, and join. Lines have to be converted to curves first, or the join refuses them.

Recursion: keep all pieces in a list. Take the first one out with pop(0), split it, append both halves to the end, and repeat while there are directions and split points left.
Because the oldest piece is always cut first, the flat is divided in rounds, large pieces before small ones.


The report considers this the main result of the study: the Grasshopper logic, component by component, became a short Python function that can be read, debugged and reused. The plan at the top of this page runs a JavaScript port of the same two functions.
Cutting produces pieces, but a flat is a set of named spaces that must connect in a certain way. The program was written as one master list, from which each typology takes its first n items: a T0 has a hall, a living room, a kitchen and a bathroom; each step up adds a private hall, bedrooms, and from T3 a second bathroom.
The connections were written as an adjacency map, one entry per space with the spaces it should open onto: a small graph, in text, of where the doors go.
These sketches follow the work of Sara Eloy and José Pinto Duarte on transformation grammars for housing, where a body of existing flats (the Lisbon "rabo-de-bacalhau" buildings, Siza's Malagueira houses) is described by rules that can then generate new or rehabilitated plans.
The cutting definition applied the same number of cuts to every flat. The plan was to read the area of the starting rectangle and derive the typology, and from it the number of cuts: in a T0 the first division separates the open living, kitchen and sleeping space and needs no wall, the second one encloses the bathroom; a T1 makes the same cuts but walls the first one; a T2 adds one more; a T3 adds two (a bedroom and a bathroom), and so on.
An elif chain is the natural form for this, since each branch excludes the ones below it even when they are also true. The areas were read correctly, but with placeholder values for direction and split point the component returned an empty data tree, and more time would have been needed to find out why.
The generator on this page does what this attempt was meant to do, by taking the number of cuts from the program (one fewer than the number of spaces) instead of from the area.
Random cuts produce long, narrow spaces that no one would call a room. Galapagos was brought back to minimise two things for each division: the difference between the x and y dimensions of the pieces (within bounds) and the difference between the smaller and the larger area, pushing towards square pieces and cuts near half.

The logic is sound in principle, but the geometry stayed wrong, with the same long spaces as before. A likely cause is the randomiser of initial conditions, where several consecutive cuts in the same direction produce strips; a fixed sequence of horizontal and vertical cuts would probably help. There is also a deeper conflict: once the starting point is the whole flat, hitting the exact areas of the law becomes a matter of chance.

The result is not a working generator, and the reasons can be listed:
The Milestone 1 report predicted that the weak point would be keeping the graph of spatial relations coherent while the geometry is being packed. That proved right, only earlier than expected: the graph never reached the geometry at all. The study set out to understand, from the inside, tools such as Planfinder and the Magnetizing Floor Plan Generator, and it leaves a clearer picture of where their difficulty lives: less in drawing rectangles than in keeping a set of very simple rules true at the same time. It is a restrictive problem, but one whose rules are still open enough to leave a generative algorithm some agency, and a starting point rather than a conclusion.

The study generated first and checked afterwards, and random cuts produced a legal plan in fewer than 1% of the attempts. Part two inverts the order. The rules stop being a filter at the end and become the set of moves the generator is allowed to make: a width that would break the law is never on the table, so there is nothing to reject.
Three questions shaped it: how to place the doors, how to make sure every WC can be reached from the common space, and whether the law is better written in Gherkin and Python. A fourth came first: do we have all the rules?
Not quite, and some of the ones the study used have changed. In December 2026 the RGEU is still in force, but Decree-Law 10/2024 rewrote articles 66 and 68 and revoked article 70, and its full revocation now waits for the technical norms that are to replace it (DL 108/2026). Accessibility (DL 163/2006, section 3.3) applies to every new dwelling and adds its own plan rules. The table lists what constrains a flat's plan and what the solver does with each.
| Rule | What it asks | In the solver |
|---|
Sources: RGEU consolidated text (PGDL), DL 10/2024, DL 108/2026, DL 163/2006 annex as amended. The numeric tables of art. 66 and 67 were taken from the study's sources and not re-checked against the official images.
The WC rule is a rule of that graph. Among the sanitary spaces, at least one must have its door on the hall; with a single WC, that one. A second WC may open onto a bedroom as an en-suite, the accepted exception to art. 86, which says WCs should not as a rule open onto habitable rooms and never onto the kitchen. Because every room in the band grammar touches the hall along its whole width, a WC can always reach it; the solver still checks the edge, since in the compact layouts that is not guaranteed.
In T0 and T1 the kitchen is a kitchenette inside the living room (art. 66.6), and the merged room must be at least the sum of both table areas (art. 66.7), here with the supplement on top. That is also why a T0 keeps a small entrance niche instead of no hall at all: without it, the WC would open onto the room that holds the kitchenette, and art. 86 says a WC never opens onto the kitchen. The niche is as small as the rules allow, 1.10 m wide, or 1.50 m when the accessibility rules apply.
Light follows art. 71: every habitable room needs glazing of at least a tenth of its floor, and at least 1.08 m², in an exterior wall. This is a constraint on the widths, not a check at the end, so a room too narrow for its window is never drawn. On the street facade every habitable room is glazed across its whole wall; on the other facade windows are sized to the law. Art. 72 asks for cross ventilation of the dwelling through two opposite facades, read here as windows of any room, so a WC window on the back facade counts.
A legal plan can still be a bad one: a bedroom by the front door, a living room at the end of the corridor. The study's first sketches already had the rule (bedrooms far from the entrance, day spaces near it), but nothing measured it. Space syntax does, by reading the plan as a graph of rooms joined by doors (Hillier and Hanson, 1984; applied to BIM models by Aghabayli, 2021). Three measures are computed for every plan:
In the band grammar almost every room is one door from the hall, so depth alone hardly tells them apart; walking distance does. Left to chance, the living room came before every bedroom in only 24% of the plans, and bedrooms ended up on average 0.30 m nearer the entrance than the day spaces. The solver now draws twelve legal variants of each layout and keeps the one with the best privacy gradient. In the 247 sample plans with bedrooms, the living room now comes first in 66% of them, and bedrooms lie on average 2.7 m further from the entrance than the day spaces. The remaining third are mostly compact T1 and T2 plans and corridor plans whose band assignment puts the living room at the far end; making the gradient a rule, rather than a preference, is the next step, and would raise some of the minimum areas in the table above.
…
| Space | m² | Rules |
|---|
How to read it. Doors swing into the room they serve; the gap in the dark exterior wall on the hall is the entrance. Windows are the light bars in the facade walls: full width on the street (top), sized to art. 71 elsewhere. The gross area starts at the smallest value for which the solver found a plan at this proportion, never below art. 67. Every check in the schedule is recomputed on the finished plan.
Running the solver for every typology and footprint gives a question the study could not ask: is the minimum gross area of art. 67 actually enough for a flat that meets every other rule? Under the RGEU alone, from T1 to T6 it is, across most proportions. For T0 it is not: with 35 cm walls all round, the smallest studio found needs 36 m² gross, and up to 39.5 m² depending on the proportion. With a separate kitchen it needed 37.5 to 62 m²; the kitchenette and the studio layout save up to 26 m² on a stretched footprint. Accessibility (turning circles at the entrance and in a 2.00 × 2.20 m bathroom) pushes T0 to 39 to 43 m², T1 to at least 55.5 m², and, depending on the proportion, T2 and the larger flats over art. 67 too.
Smallest gross area (m², 0.5 m² steps) at which a legal plan was found, by facade : depth proportion. Red cells exceed art. 67; a dot means no plan within 30 m² above it, which in this grammar happens when the facade is too short for every habitable room to have a window. Assumptions: 35 cm exterior and party walls, 10 cm partitions, facades on two opposite sides only.
Yes, in layers, because the two do different jobs. Gherkin is a language for describing behaviour in sentences that a non-programmer can read and a machine can run; buildingSMART already uses it for the normative rules of its IFC validation service. It is a good form for the law as a contract: each article becomes a feature, quoted in Portuguese, with scenarios that must hold for every plan. It cannot generate anything, though; it only audits.
So the rules live once, as data (the numbers, each with its article or marked as an assumption), and are read by two independent programs: the JavaScript solver that builds plans inside them, and a Python module that re-implements every check on the finished plan. The Gherkin features call the Python checks. Because the two programs share the numbers but not the code, a bug in either one shows up as a failing scenario.
Run against 295 solver plans (all typologies, six proportions, half of them with accessibility), the 65 scenarios and 210 steps all pass, including a check that Python, computing the space syntax measures on its own, gets the same depths and walking distances as the solver. Two scenarios break good plans on purpose, a squeezed bedroom and a WC opening onto the kitchen, to prove the checks are not passing everything.
The study ended by locating the difficulty in keeping a graph of relations true while the geometry changes. Part two keeps it true by never letting the geometry leave the space the graph and the law allow, which turns the rules from a verdict at the end into the material the plan is made of.
The code in part one was recovered from the original Grasshopper definitions (Assignment02 Milestone 2 and 3, Rhino 8 Python components) and is reproduced as written. The first interactive plan is a JavaScript port of split_curve and split_several; its labelling and RGEU checks are additions of this page. Part two (the solver, the rules data, the Python checks and the Gherkin features) was written in December 2026.