WowaJob.php 4.21 KiB
<?php
namespace App\Jobs;
use App\Feedback;
use App\Mark;
use App\Wowa;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use RUCD\Training\SolutionDistance;
use RUCD\Training\Trainer;
use RUCD\Training\TrainerParameters;
class WowaJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $feedback_number;
protected $wowa;
protected $is_true_alert = [];
protected $evidences = [];
/**
* Create a new job instance.
*
* @param Wowa $wowa
*/
public function __construct(Wowa $wowa)
{
$this->wowa = $wowa;
$this->feedback_number = Feedback::count();
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$logger = new Logger('wowa-training-test');
$logger->pushHandler(new StreamHandler(__DIR__.'/../../storage/logs/training.log', Logger::DEBUG));
$this->wowa->status = Wowa::STATE_RUNNING;
$this->wowa->start_time = time();
$this->wowa->save();
if ($this->feedback_number == 0) {
$this->wowa->status = Wowa::STATE_FAILED;
$this->wowa->end_time = time();
$this->wowa->save();
return;
}
$logger->debug("Begin find all related evidences");
try {
error_log('Just after try ', 3, __DIR__.'/../../storage/logs/training.log');
$test = Mark::get()->findEvidenceById('604fe648e1f447672dbfc064');
error_log(
'Test find evidences by report id' . $test['label'],
3,
__DIR__.'/../../storage/logs/training.log'
);
Feedback::chunk(100, function ($feedbacks) {
foreach ($feedbacks as $feed) {
error_log('Begin for each', 3, __DIR__.'/../../storage/logs/training.log');
$evidence_references = $feed->report()->references;
error_log('References ', 3, __DIR__.'/../../storage/logs/training.log');
$this->is_true_alert[] = $feed->is_true_alert;
error_log('True Alert ', 3, __DIR__.'/../../storage/logs/training.log');
$scores_references = [];
foreach ($evidence_references as $evidence_reference) {
error_log('Second foreach', 3, __DIR__.'/../../storage/logs/training.log');
$scores_references[] = Mark::get()->findEvidenceById($evidence_reference)->score;
}
error_log('Score references stored', 3, __DIR__.'/../../storage/logs/training.log');
$this->evidences[] = $scores_references;
}
});
} catch (\Exception $e) {
$this->wowa->status = Wowa::STATE_FAILED;
$logger->error("Error during retrieving evidences");
$this->wowa->save();
}
$logger->debug("End find all related evidences");
$training_prameters = new TrainerParameters(
$logger,
$this->wowa->population,
$this->wowa->crossover_rate,
$this->wowa->mutation_rate,
TrainerParameters::SELECTION_METHOD_RWS,
$this->wowa->generation_number,
TrainerParameters::INITIAL_POPULATION_GENERATION_RANDOM
);
$trainer = new Trainer($training_prameters, new SolutionDistance(count($this->evidences[0])));
try {
$solution = $trainer->run($this->evidences, $this->is_true_alert);
} catch (\Exception $e) {
$this->wowa->status = Wowa::STATE_FAILED;
$logger->error("Error during wowa training");
$logger->error($e->getMessage());
$this->wowa->save();
}
$this->wowa->status = Wowa::STATE_SUCCESS;
$this->wowa->w_weights = $solution->weights_w;
$this->wowa->p_weights = $solution->weights_p;
$this->wowa->score = $solution->distance;
$this->wowa->end_time = time();
$this->wowa->save();
}
}