> For the complete documentation index, see [llms.txt](https://finch-1.gitbook.io/version0.0.1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://finch-1.gitbook.io/version0.0.1/layers/universal-layers.md).

# Universal Layers

### Table of Contents

1. Introduction
2. Populate
3. SortByFitness
4. CapPopulation

### Introduction

Universal layers in Finch 3.5 are components that can be used in various genetic algorithm scenarios. They handle common operations such as populating the initial set of individuals, sorting individuals by fitness, and capping the population size.

### Populate

The `Populate` layer is responsible for creating new individuals to maintain the desired population size.

#### Usage

```python
from Finch.layers.universal_layers import Populate
from Finch.generic import GenePool

gene_pool = GenePool(...)  # Your specific gene pool
populate_layer = Populate(population=100, gene_pool=gene_pool)
```

#### Parameters

* `population`: An integer or a callable that returns an integer, representing the desired population size.
* `gene_pool`: An instance of `GenePool` used to generate new individuals.

#### Behavior

This layer checks the current population size and generates new individuals if needed to reach the desired population size.

### SortByFitness

The `SortByFitness` layer sorts the individuals in the population based on their fitness scores.

#### Usage

```python
from Finch.layers.universal_layers import SortByFitness

sort_layer = SortByFitness()
```

#### Behavior

This layer sorts the individuals in descending order of fitness (highest fitness first).

### CapPopulation

The `CapPopulation` layer limits the population size to a maximum value.

#### Usage

```python
from Finch.layers.universal_layers import CapPopulation

cap_layer = CapPopulation(max_population=100)
```

#### Parameters

* `max_population`: An integer or a callable that returns an integer, representing the maximum allowed population size.

#### Behavior

This layer trims the population to the specified maximum size, keeping only the top individuals based on their current order in the population.

### Example

Here's an example of how these layers might be used together in a Finch 3.5 environment:

```python
from Finch.generic import Environment, GenePool
from Finch.layers.universal_layers import Populate, SortByFitness, CapPopulation

# Assume you have defined your gene_pool and other necessary components

layers = [
    Populate(population=100, gene_pool=gene_pool),
    # Other genetic algorithm layers (mutation, crossover, etc.)
    SortByFitness(),
    CapPopulation(max_population=100)
]

env = Environment(layers=layers)
env.compile()
env.evolve(generations=100)
```

This setup will ensure that:

1. The population is maintained at 100 individuals.
2. After each generation, individuals are sorted by fitness.
3. The population is capped at 100 individuals, keeping only the fittest.

### Conclusion

These universal layers provide essential functionality for managing the population in genetic algorithms using Finch 3.5. By combining these layers with problem-specific layers and genetic operators, you can create efficient and flexible genetic algorithm implementations for a wide range of optimization problems.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://finch-1.gitbook.io/version0.0.1/layers/universal-layers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
