• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/ml/scoring.php
  • Класс: Bitrix\Crm\Ml\Scoring
  • Вызов: Scoring::startModelTraining
static function startModelTraining(Model\Base $model): Result
{
	$result = new Result();
	if (!Loader::includeModule('ml'))
	{
		return $result->addError(new Error('ML module is not installed'));
	}

	if (!static::isEnabled())
	{
		return $result->addError(new Error('Scoring is not enabled for your tariff'));
	}

	if ($model->getMlModel())
	{
		return $result->addError(new Error('ML model should be deleted prior to starting learning process'));
	}

	$lastTraining = static::getLastTraining($model);
	if (
		$lastTraining &&
		!in_array(
			$lastTraining['STATE'],
			[TrainingState::FINISHED, TrainingState::CANCELED],
			true
		)
	)
	{
		return $result->addError(
			new Error('Model ' . $model->getName() . ' is already in training')
		);
	}

	[$successfulRecords, $failedRecords] = $model->getTrainingSetSize();
	$totalRecords = $successfulRecords + $failedRecords;

	if (
		$totalRecords < static::getMinimalTrainingSetSize()
		|| $successfulRecords < static::getMinimalClassSize()
		|| $failedRecords < static::getMinimalClassSize()
	)
	{
		return $result->addError(new Error('Not enough data to start model training'));
	}

	// check model training state

	$scheduleResult = ModelTrainer::scheduleTraining($model);
	if (!$scheduleResult->isSuccess())
	{
		return $result->addErrors($scheduleResult->getErrors());
	}

	return $result;
}