Assign the algorithm to a resource once it is ready.

## Check if the algorithm is ready

An algorithm model must train on a [minimum amount of data](https://support.google.com/displayvideo/answer/9723477#training-your-custom-algorithm)
before it is ready.

Algorithms train a model for each advertiser that can use them. Models train on
existing impressions data. It can take 1-3 days to train a model after the
[data requirements](https://support.google.com/displayvideo/answer/9723477#training-your-custom-algorithm) are met by the advertiser.

Retrieve the readiness of each [model](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingAlgorithm.FIELDS.model_details) from the algorithm. The
[`ReadinessState`](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#ReadinessState) determines next steps:

| `ReadinessState` ||
|---|---|
| `READINESS_STATE_NO_VALID_SCRIPT` | There is no valid script. Upload a new [script](https://developers.google.com/display-video/api/guides/tasks/implement-custom-bidding/upload-script) or [rules file](https://developers.google.com/display-video/api/guides/tasks/implement-custom-bidding/upload-rules). |
| `READINESS_STATE_EVALUATION_FAILURE` | There is no script that could be evaluated in the allotted time. Upload a new [script](https://developers.google.com/display-video/api/guides/tasks/implement-custom-bidding/upload-script) or [rules file](https://developers.google.com/display-video/api/guides/tasks/implement-custom-bidding/upload-rules). |
| `READINESS_STATE_INSUFFICIENT_DATA` | There is not a sufficient amount of data available under the advertiser to train the model. Continue to run campaigns under the advertiser to meet the minimum data requirements. |
| `READINESS_STATE_TRAINING` | The model is training on existing data and not ready for serving. Wait 12-24 hours before checking to see if the model is ready for serving. |
| `READINESS_STATE_ACTIVE` | The model is fully trained and ready to be assigned to campaigns under the advertiser. |

> [!TIP]
> **Tip:** If you are retrieving an algorithm with an advertiser ID, only that advertiser's model will be included in the response.

## Assign algorithm to line item

An algorithm is used to measure and adjust bidding performance. It can be used
with bid strategies that optimize towards [spending the whole
budget](https://developers.google.com/display-video/api/reference/rest/current/BiddingStrategy#MaximizeSpendBidStrategy) or [meeting a goal](https://developers.google.com/display-video/api/reference/rest/current/BiddingStrategy#PerformanceGoalBidStrategy). In these cases, the
[strategy performance goal type](https://developers.google.com/display-video/api/reference/rest/current/BiddingStrategy#BiddingStrategyPerformanceGoalType) would be
`BIDDING_STRATEGY_PERFORMANCE_GOAL_TYPE_CUSTOM_ALGO`.

> [!WARNING]
> **Warning:** If no spend is associated with a model for 21 days, it is suspended. Suspended models don't train on new data. [Suspension
> status](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingModelDetails.FIELDS.suspension_state) can be found in the advertiser's [model
> details](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingAlgorithm.FIELDS.model_details). To use a suspended model, reactivate it by assigning the algorithm to an active line item. The line item will need to begin spending within 21 days.

Here's how to update a line item to use an algorithm with a bid strategy that
optimizes for spending the whole budget:

### Java

```java
// Provide the ID of the advertiser that owns the parent algorithm.
long advertiserId = advertiser-id;

// Provide the ID of the parent algorithm.
long algorithmId = algorithm-id;

// Provide the ID of the line item to assign the algorithm to.
long lineItemId = line-item-id;

// Create the line item structure.
LineItem lineItem =
    new LineItem()
        .setBidStrategy(
            new BiddingStrategy()
                .setMaximizeSpendAutoBid(
                    new MaximizeSpendBidStrategy()
                        .setPerformanceGoalType(
                            "BIDDING_STRATEGY_PERFORMANCE_GOAL_TYPE_CUSTOM_ALGO")
                        .setCustomBiddingAlgorithmId(algorithmId)));

// Configure the patch request and set update mask to only update the bid
// strategy.
LineItems.Patch request =
    service
        .advertisers()
        .lineItems()
        .patch(advertiserId, lineItemId, lineItem)
        .setUpdateMask("bidStrategy");

// Update the line item.
LineItem response = request.execute();

// Display the new algorithm ID used by the line item.
System.out.printf(
    "Line item %s now uses algorithm ID %s in its bidding strategy.",
    response.getName(),
    response.getBidStrategy().getMaximizeSpendAutoBid().getCustomBiddingAlgorithmId());
```

### Python

```python
# Provide the parent advertiser ID of the line item and creative.
advertiser_id = advertiser-id

# Provide the ID of the creative to assign.
algorithm_id = algorithm-id

# Provide the ID of the line item to assign the creative to.
line_item_id = line-item-id

# Build bidding strategy object.
bidding_strategy_obj = {
    "maximizeSpendAutoBid": {
        "performanceGoalType": (
            "BIDDING_STRATEGY_PERFORMANCE_GOAL_TYPE_CUSTOM_ALGO"
        ),
        "customBiddingAlgorithmId": algorithm_id,
    }
}

# Build line item object.
line_item_obj = {"bidStrategy": bidding_strategy_obj}

# Build and execute request.
line_item_resp = (
    service.advertisers()
    .lineItems()
    .patch(
        advertiserId=advertiser_id,
        lineItemId=line_item_id,
        updateMask="bidStrategy",
        body=line_item_obj,
    )
    .execute()
)

# Print the algorithm ID now assigned to the line item.
print(
    f'Line Item {line_item_resp["name"]} now uses algorithm ID'
    f' {line_item_resp["bidStrategy"]["maximizeSpendAutoBid"]["customBiddingAlgorithmId"]}'
    " in its bidding strategy."
)
```

### PHP

```php
// Provide the ID of the advertiser that owns the parent algorithm.
$advertiserId = advertiser-id;

// Provide the ID of the parent algorithm.
$algorithmId = algorithm-id;

// Provide the ID of the line item to assign the algorithm to.
$lineItemId = line-item-id;

// Create the bidding strategy structure.
$maxSpendBidStrategy = new Google_ServiceDisplayVideo_MaximizeSpendBidStrategy();
$maxSpendBidStrategy->setPerformanceGoalType('BIDDING_STRATEGY_PERFORMANCE_GOAL_TYPE_CUSTOM_ALGO');
$maxSpendBidStrategy->setCustomBiddingAlgorithmId($algorithmId);
$biddingStrategy = new Google_ServiceDisplayVideo_BiddingStrategy();
$biddingStrategy->setMaximizeSpendAutoBid($maxSpendBidStrategy);

// Create the line item structure.
$lineItem = new Google_Service_DisplayVideo_LineItem();
$lineItem->setBidStrategy($biddingStrategy);
$optParams = array('updateMask' => 'bidStrategy');

// Call the API, updating the bid strategy for the identified line
// item.
try {
    $result = $this->service->advertisers_lineItems->patch(
        $advertiserId,
        $lineItemId,
        $lineItem,
        $optParams
    );
} catch (\Exception $e) {
    $this->renderError($e);
    return;
}

printf(
    '<p>Line Item %s now uses algorithm ID %s in its bid strategy.</p>',
    $result['name'],
    $result['bidStrategy']['maximizeSpendAutoBid']['customBiddingAlgorithmId']
);
```