> ## Documentation Index
> Fetch the complete documentation index at: https://docs.solvice.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Shift Patterns

> Define preferred or prohibited sequences of shifts across days

Patterns control the sequence of shift assignments, either within a single day or across multiple days. Use patterns to enforce or discourage specific shift combinations.

## Pattern Types

| Type         | Scope                          | Use Case                               |
| ------------ | ------------------------------ | -------------------------------------- |
| `SINGLE_DAY` | Multiple shifts on one day     | Prevent double-booking, enforce breaks |
| `MULTI_DAY`  | Shifts across consecutive days | Rotation rules, recovery time          |

## Satisfiability Levels

| Level         | Effect                   | Constraint Type |
| ------------- | ------------------------ | --------------- |
| `PREFERRED`   | Encourage this pattern   | Soft constraint |
| `UNPREFERRED` | Discourage this pattern  | Soft constraint |
| `PROHIBITED`  | Never allow this pattern | Hard constraint |

## Single-Day Patterns

Prevent an employee from working both early and late shifts on the same day:

```json theme={null}
{
  "patterns": [
    {
      "type": "SINGLE_DAY",
      "satisfy": "PROHIBITED",
      "elements": [
        { "type": "ON", "tags": ["EARLY"] },
        { "type": "ON", "tags": ["LATE"] }
      ]
    }
  ]
}
```

<Warning>
  `PROHIBITED` patterns create hard constraints. If unavoidable, the solution becomes infeasible.
</Warning>

Prefer no late shift after an early shift:

```json theme={null}
{
  "patterns": [
    {
      "type": "SINGLE_DAY",
      "satisfy": "PREFERRED",
      "elements": [
        { "type": "ON", "tags": ["EARLY"] },
        { "type": "OFF", "tags": ["LATE"] }
      ]
    }
  ]
}
```

## Multi-Day Patterns

Prohibit early shifts immediately after late shifts (next day):

```json theme={null}
{
  "patterns": [
    {
      "type": "MULTI_DAY",
      "satisfy": "PROHIBITED",
      "elements": [
        { "type": "ON", "tags": ["LATE"] },
        { "type": "ON", "tags": ["EARLY"] }
      ]
    }
  ]
}
```

<Info>
  This prevents "clopening" scenarios where an employee closes late and opens early the next day.
</Info>

Prefer a rotation of early → late → late:

```json theme={null}
{
  "patterns": [
    {
      "type": "MULTI_DAY",
      "satisfy": "PREFERRED",
      "elements": [
        { "type": "ON", "tags": ["EARLY"] },
        { "type": "ON", "tags": ["LATE"] },
        { "type": "ON", "tags": ["LATE"] }
      ]
    }
  ]
}
```

## Pattern Elements

<ParamField body="type" type="string" required>
  `ON` - Employee works a shift with matching tags
  `OFF` - Employee does not work a shift with matching tags
</ParamField>

<ParamField body="tags" type="array">
  List of shift tags that this element matches (e.g., `["EARLY"]`, `["NIGHT"]`)
</ParamField>

## Setting Up Shift Tags

Patterns use tags defined on shifts. Add tags to group shifts:

```json theme={null}
{
  "shifts": [
    {
      "name": "morning-1",
      "from": "2024-01-01T06:00:00",
      "to": "2024-01-01T14:00:00",
      "tags": [{ "name": "EARLY" }],
      "min": 1,
      "max": 1
    },
    {
      "name": "evening-1",
      "from": "2024-01-01T14:00:00",
      "to": "2024-01-01T22:00:00",
      "tags": [{ "name": "LATE" }],
      "min": 1,
      "max": 1
    },
    {
      "name": "night-1",
      "from": "2024-01-01T22:00:00",
      "to": "2024-01-02T06:00:00",
      "tags": [{ "name": "NIGHT" }],
      "min": 1,
      "max": 1
    }
  ]
}
```

## Pattern Weight

Adjust the importance of preferred patterns with `weight`:

```json theme={null}
{
  "patterns": [
    {
      "type": "MULTI_DAY",
      "satisfy": "PREFERRED",
      "weight": 10,
      "elements": [
        { "type": "ON", "tags": ["EARLY"] },
        { "type": "ON", "tags": ["LATE"] }
      ]
    }
  ]
}
```

<Tip>
  Higher weights make the solver try harder to satisfy the pattern. Use weights to prioritize between multiple preferred patterns.
</Tip>

## Common Patterns

<AccordionGroup>
  <Accordion title="No back-to-back shifts">
    ```json theme={null}
    {
      "type": "SINGLE_DAY",
      "satisfy": "PROHIBITED",
      "elements": [
        { "type": "ON", "tags": ["ALL"] },
        { "type": "ON", "tags": ["ALL"] }
      ]
    }
    ```
  </Accordion>

  <Accordion title="Day off after night shift">
    ```json theme={null}
    {
      "type": "MULTI_DAY",
      "satisfy": "PREFERRED",
      "elements": [
        { "type": "ON", "tags": ["NIGHT"] },
        { "type": "OFF", "tags": ["ALL"] }
      ]
    }
    ```
  </Accordion>

  <Accordion title="Consistent shift type per week">
    ```json theme={null}
    {
      "type": "MULTI_DAY",
      "satisfy": "PREFERRED",
      "elements": [
        { "type": "ON", "tags": ["EARLY"] },
        { "type": "ON", "tags": ["EARLY"] },
        { "type": "ON", "tags": ["EARLY"] }
      ]
    }
    ```
  </Accordion>
</AccordionGroup>
