Beispiel #1
0
def grid_search(floor):
    best_score = 999999999
    best_args = None
    decay = .2
    k = 10
    start_pheromone = 0
    iters = 15
    alpha = 2
    for k in range(1, 20, 2):
        args = {
            'alpha': alpha,
            'iters': iters,
            'k': k,
            'pheromone_decay': decay,
            'start_pheromone': start_pheromone
        }
        create_hallways(floor, verbose=False, **args)
        floor.hallway_geometry = create_geometry(floor)
        floor.stats = fps.calculate_all(floor)
        print('alpha=', round(alpha, 2), 'k=', k, 'decay=', decay, 'ha=',
              floor.stats['ha'])

        if floor.stats['fe'] < best_score:
            best_args = args
            best_score = floor.stats['fe']
    create_hallways(floor, verbose=False, **best_args)
    floor.hallway_geometry = create_geometry(floor)
    floor.stats = fps.calculate_all(floor)
    smooth(floor)
    return floor
Beispiel #2
0
def grid_search(floor):
    best_score = 999999999
    best_args = None
    # loor = FloorPlan.from_genome(genome, view=None, cache=False)
    decay = .2
    k = 10
    start_pheromone = 0
    iters = 15
    alpha = 2
    for k in range(1, 20, 2):
        # for start_pheromone in np.linspace(.1, 2)
        # for alpha in np.linspace(2, 10, 20):
        args = {
            'alpha': alpha,
            'iters': iters,
            'k': k,
            'pheromone_decay': decay,
            'start_pheromone': start_pheromone
        }
        create_hallways(floor, verbose=False, **args)
        floor.hallway_geometry = create_geometry(floor)
        floor.stats = fps.calculate_all(floor)
        print('alpha=', round(alpha, 2), 'k=', k, 'decay=', decay, 'ha=',
              floor.stats['ha'])

        if floor.stats['fe'] < best_score:
            best_args = args
            best_score = floor.stats['fe']
    create_hallways(floor, verbose=False, **best_args)
    floor.hallway_geometry = create_geometry(floor)
    floor.stats = fps.calculate_all(floor)
    smooth(floor)
    return floor
Beispiel #3
0
    return new_room_id

""" Merge administration room clusters together.
"""
# admin1_id = len(floor.rooms) # create new room ID.
# # IDs of rooms to merge
admin1_rooms = [113, 87, 88, 89, 90, 93, 92, 117, 114, 112, 4, 5, 6, 7, 22, 21, 23]
admin1_verts = [156, 170, 172, 174, 176, 178, 179, 183, 182, 180, 157, 168,
                167, 187, 229, 159, 185, 200, 198, 196, 194, 192, 190, 158,
                204, 254, 166, 169]

admin2_rooms = [25, 16, 17, 18, 19, 20, 80, 79, 84, 15, 14, 13, 12, 11, 24, 86]
admin2_verts = [133, 128, 129, 130, 165, 225, 131, 121, 107, 227, 226, 104,
                108, 122, 114, 113, 112, 111, 110, 109, 134, 230, 135, 132]
admin1_id = merge_rooms('administration', admin1_rooms, admin1_verts)
admin2_id = merge_rooms('administration', admin2_rooms, admin2_verts)

floor.add_edge(floor.room_centers[admin1_id], floor.room_centers[119], outside=False, inner=True, width=10)
floor.add_edge(floor.room_centers[admin2_id], floor.room_centers[81], outside=False, inner=True, width=10)

# try:
# print(fps.calculate_all(floor))
for k, v in fps.calculate_all(floor).items():
    print(k, v)
# except Exception as e:
#     print(e)

view.draw_floorplan(floor)
view.hold()
Beispiel #4
0
    def from_genome(cls, genome, firescapes=False, view=None, cache=True):
        if genome.phenotype is not None and cache:
            return genome.phenotype
        G = genome_to_graph(genome)  # First create a graph representation.
        pos = nx.spectral_layout(G)  # Create with spectral layout of graph.
        scale_offset(pos, dx=75, dy=75, scale=600)
        fix_overlapping(pos)  # Prevent overlapping points.
        center(pos, dimensions=(750, 750))
        pos = physics_layout(G, pos, view)  # Apply physics simulation.

        center(pos, dimensions=(750, 750))
        if 'concave_alpha' in genome.attribute_genes:
            a = genome.attribute_genes['concave_alpha'].value
        else:
            a = 40

        cells, debug_data = voronoi_cells(
            pos, genome.node_genes, a)  # Room walls by voronoi tesselation.
        vert_to_id = defaultdict(
            lambda: len(vert_to_id))  # Map vertices to global indexes.
        rooms = defaultdict(list)
        vert_to_room = defaultdict(set)
        room_names = dict()
        room_centers = dict()
        edges = []

        no_outside = set()

        for ID, verts in cells.items():
            gene = genome.node_genes[ID]
            # print(gene.name)
            if gene.name == 'empty' or gene.name == 'playground':
                no_outside.update(set([vert_to_id[vert] for vert in verts]))
                if gene.name == 'empty':
                    continue

            center_v = polygon.center(verts)
            center_i = vert_to_id[center_v]
            room_centers[ID] = center_i
            vert_to_room[center_i].add(ID)
            room_names[ID] = gene.name

            # if gene.name == 'playground':
            #     continue

            for i, vert in enumerate(verts):
                vid = vert_to_id[
                    vert]  # voronoi points are integers and thus hashable.
                rooms[ID].append(vid)

                vert_to_room[vid].add(ID)
                if gene.name != 'playground':
                    edges.append((vid, vert_to_id[verts[i - 1]], {
                        'inner': False
                    }))
                edges.append((vid, center_i, {
                    'inner': True,
                    'directed': None
                }))

        vertices = sorted(vert_to_id.keys(), key=lambda v: vert_to_id[v])
        floor = cls(vertices,
                    dict(rooms),
                    room_names,
                    vert_to_room,
                    room_centers,
                    entrances=[])
        floor.debug_data = debug_data  # temporary

        for i, j, data in edges:  # add edges.
            floor.add_edge(i, j, **data)

        floor.calc_outisde_edges(no_outside)

        # Remove edges that connect centers to outside
        # for ri, vc in floor.room_centers.items():
        #     if room_names[ri] in ['cafeteria', 'library', 'gym']:
        #         continue
        #     verts = rooms[ri]
        #     out = [v for v in verts if v in floor.outside_verts]
        #     if len(out) == len(verts):
        #         continue
        #     for v in out:
        #         if floor.has_edge(vc, v):
        #             floor.remove_edge(vc, v)

        # for v in floor.outside_verts:
        #     for ri in vert_to_room[v]:
        #         if room_names[ri] not in ['cafeteria', 'library', 'gym'] and \
        #             len(room_centers[ri]):
        #             vc = room_centers[ri]
        #             floor.remove_edge(v, vc)

        # new_polygons = []
        # for ID, face in floor.rooms:
        #     polygon = [floor.vertices[vi] for vi in face]
        #     clipped = poylgon.clip(polygon, floor.hallway_geometry)
        #     new_polygons.append(clipped)

        # self.topologize(new_polygons)
        """ Prevent adjacency requirements from having a hallway between them.
        """
        for conn in genome.conn_genes.values():
            if conn.fixed:
                room1_id = conn.in_node_id
                room2_id = conn.out_node_id
                edge = floor.get_rooms_edge(room1_id, room2_id)
                if edge:
                    i, j, data = edge
                    data['between_fixed'] = True
                else:
                    raise InvalidContraintException()

        alpha = genome.attribute_genes['hallway_alpha'].value
        decay = genome.attribute_genes['hallway_decay'].value
        if 'fe_weight' in genome.attribute_genes:
            fe_weight = genome.attribute_genes['fe_weight'].value
        else:
            fe_weight = .2
        create_hallways(floor, alpha=alpha, pheromone_decay=decay, iters=10, fe_weight=fe_weight,\
                        verbose=False, firescapes=firescapes)

        for i, j, d in floor.edges(data=True):
            if d['between_fixed'] and d['width']:
                raise InvalidContraintException()

        cx = mean([x for x, y in floor.vertices])
        cy = mean([y for x, y in floor.vertices])
        x1, y1 = floor.vertices[floor.entrances[0]]
        angle = -math.atan2(y1 - cy, x1 - cx) + math.pi / 2
        floor.vertices = [
            geometry.rotate_point((cx, cy), p, angle) for p in floor.vertices
        ]

        floor.add_adjacent_room_edges([(c.in_node_id, c.out_node_id)
                                       for c in genome.conn_genes.values()
                                       if c.fixed])
        smooth(floor)
        floor.hallway_geometry = create_geometry(floor)
        floor.stats = fps.calculate_all(floor)
        return floor