<commit_before><?php

namespace ScheduleBundle\Controller;

use ScheduleBundle\Entity\Individ;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use ScheduleBundle\Entity\Patient;
use ScheduleBundle\Entity\Population;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

/**
 * Patient controller.
 *
 * @Route("/reception/schedule")
 */
class PatientController extends Controller
{

    private $workTime;

    private $iterationNumber;

    /**
     * PatientController constructor.
     * @param $iterationNumber
     */
    public function __construct()
    {
        $this->iterationNumber = 4;
    }

    /**
     * @return mixed
     */
    public function getIterationNumber()
    {
        return $this->iterationNumber;
    }

    /**
     * @param mixed $iterationNumber
     */
    public function setIterationNumber($iterationNumber)
    {
        $this->iterationNumber = $iterationNumber;
    }

    /**
     * @return mixed
     */
    public function getWorkTime()
    {
        return $this->workTime;
    }

    /**
     * @param mixed $workTime
     */
    public function setWorkTime($workTime)
    {
        $this->workTime = $workTime;
    }

    /**
     * Lists all Patient entities.
     *
     * @Route("/", name="reception_schedule_index")
     * @Method({"GET", "POST"})
     */
    public function indexAction(Request $request)
    {
        if ($request->isXmlHttpRequest()) {
            $number = $request->request->get("number");
            if ($number < 4 || $number > 18) {
                return new JsonResponse("error", 500);
            }
            $i = 0;
            $last = false;
            $form = $this->createFormBuilder()
                ->add('name', EntityType::class, array(
                    'label' => 'Тип візиту',
                    'class' => 'GuideBundle:VisitTypes',
                    'choices_as_values' => true,
                    'choice_label' => 'name',
                ))
                ->getForm();
            while ($i != $number) {
                if ($i == $number - 1) {
                    $last = true;
                }
                $forms[] = $this->renderView('patient/workforms.html.twig', array(
                    "form" => $form->createView(),
                    "num" => $i,
                    "last" => $last
                ));
                $i++;
            }

            return new JsonResponse(array("content" => $forms));
        } else {
            $form = $this->createFormBuilder()
                ->add('number', IntegerType::class, array('label' => 'Кількість пацієнтів', 'attr' => array('class' => 'validate[required,max[18], min[4]]')))
                ->add('save', SubmitType::class, array('label' => 'Підтвердити'))
                ->getForm();
            return $this->render('patient/start.html.twig', array(
                'form' => $form->createView()
            ));
        }
    }

    /**
     * Lists all Patient entities.
     *
     * @Route("/submit", name="reception_schedule_submit")
     * @Method("POST")
     */
    public function submitAction(Request $request)
    {
        if ($request->isXmlHttpRequest()) {
            $works = $request->request->get('works');
            $patients = array();
            $em = $this->getDoctrine()->getEntityManager();
            //$em->getRepository('ScheduleBundle:Patient');
            $queryBuilder = $em
                ->createQueryBuilder()
                ->delete('ScheduleBundle:Patient', 'a');
            $queryBuilder->getQuery()->execute();
            foreach ($works as $id => $work) {
                $patient = new Patient($id, $work);
                $em->persist($patient);
                $em->flush();
                //$this->patients[] = $patient;
            }
            return new JsonResponse(array("process" => true));
        } else {
            return new JsonResponse("error", 500);
        }
    }

    private function reanimate($children, $selection)
    {

        $em = $this->getDoctrine()->getEntityManager();
        $childs = array();
        foreach ($children as $child) {
            if ($selection) {
                $pats = $child->getPatients()->toArray();
                $newPats = $this->changeGenes($pats);
                $times = $em->getRepository("ScheduleBundle:works")->findAll();
                $ch = $this->createIndivid($newPats, $times, true);
                $childs[] = $ch;
            } else {
                if (!$child->getSuitable()) {
                    $pats = $child->getPatients()->toArray();
                    $newPats = $this->changeGenes($pats);
                    $times = $em->getRepository("ScheduleBundle:works")->findAll();
                    $ch = $this->createIndivid($newPats, $times, true);
                    $childs[] = $ch;
                } else {
                    $childs[] = $child;
                }
            }

        }
        return $childs;
    }

    /**
     * Lists all Patient entities.
     *
     * @Route("/result", name="reception_schedule_result")
     * @Method({"GET", "POST"})
     */
    public function resultAction()
    {
        $iterations = array();
        $iterate = array();
        $population = $this->buildPopulation();
        for ($i = 0; $i < $this->getIterationNumber(); $i++) {
            $parents = $this->getNewParents($population);
            $children = $this->getNewChildren($parents);
            if (rand(1, 100) === 5) {
                $mutated = $this->reanimate($children, true);
                $childSelection = $this->reanimate($mutated, true);
                $childReanimate = $this->reanimate($childSelection, false);
            } else {
                $childSelection = $this->reanimate($children, true);
                $childReanimate = $this->reanimate($childSelection, false);
            }
            $newPopulation = $this->getNewPopulation($population, $childReanimate);
            $iterations[$i][] = array("children" => $children);
            $iterations[$i][] = array(
                "newPopulation" => $newPopulation->getIndivids(),
                "parents" => $parents,
                "selection" => $childSelection,
                "reanimation" => $childReanimate,
                //"newPopulation" => $newPopulation,
                "mutated" => $mutated
            );
        }
        // $view = $this->renderView('patient/iterations.html.twig', $iterations);
        return $this->render('patient/result.html.twig', array(
            "population" => $population->getIndivids(),
            "maxMorLength" => $population->maxMorLength,<commit_msg>New iteration on new population<commit_after><?php

namespace ScheduleBundle\Controller;

use ScheduleBundle\Entity\Individ;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use ScheduleBundle\Entity\Patient;
use ScheduleBundle\Entity\Population;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

/**
 * Patient controller.
 *
 * @Route("/reception/schedule")
 */
class PatientController extends Controller
{

    private $workTime;

    private $iterationNumber;

    /**
     * PatientController constructor.
     * @param $iterationNumber
     */
    public function __construct()
    {
        $this->iterationNumber = 4;
    }

    /**
     * @return mixed
     */
    public function getIterationNumber()
    {
        return $this->iterationNumber;
    }

    /**
     * @param mixed $iterationNumber
     */
    public function setIterationNumber($iterationNumber)
    {
        $this->iterationNumber = $iterationNumber;
    }

    /**
     * @return mixed
     */
    public function getWorkTime()
    {
        return $this->workTime;
    }

    /**
     * @param mixed $workTime
     */
    public function setWorkTime($workTime)
    {
        $this->workTime = $workTime;
    }

    /**
     * Lists all Patient entities.
     *
     * @Route("/", name="reception_schedule_index")
     * @Method({"GET", "POST"})
     */
    public function indexAction(Request $request)
    {
        if ($request->isXmlHttpRequest()) {
            $number = $request->request->get("number");
            if ($number < 4 || $number > 18) {
                return new JsonResponse("error", 500);
            }
            $i = 0;
            $last = false;
            $form = $this->createFormBuilder()
                ->add('name', EntityType::class, array(
                    'label' => 'Тип візиту',
                    'class' => 'GuideBundle:VisitTypes',
                    'choices_as_values' => true,
                    'choice_label' => 'name',
                ))
                ->getForm();
            while ($i != $number) {
                if ($i == $number - 1) {
                    $last = true;
                }
                $forms[] = $this->renderView('patient/workforms.html.twig', array(
                    "form" => $form->createView(),
                    "num" => $i,
                    "last" => $last
                ));
                $i++;
            }

            return new JsonResponse(array("content" => $forms));
        } else {
            $form = $this->createFormBuilder()
                ->add('number', IntegerType::class, array('label' => 'Кількість пацієнтів', 'attr' => array('class' => 'validate[required,max[18], min[4]]')))
                ->add('save', SubmitType::class, array('label' => 'Підтвердити'))
                ->getForm();
            return $this->render('patient/start.html.twig', array(
                'form' => $form->createView()
            ));
        }
    }

    /**
     * Lists all Patient entities.
     *
     * @Route("/submit", name="reception_schedule_submit")
     * @Method("POST")
     */
    public function submitAction(Request $request)
    {
        if ($request->isXmlHttpRequest()) {
            $works = $request->request->get('works');
            $patients = array();
            $em = $this->getDoctrine()->getEntityManager();
            //$em->getRepository('ScheduleBundle:Patient');
            $queryBuilder = $em
                ->createQueryBuilder()
                ->delete('ScheduleBundle:Patient', 'a');
            $queryBuilder->getQuery()->execute();
            foreach ($works as $id => $work) {
                $patient = new Patient($id, $work);
                $em->persist($patient);
                $em->flush();
                //$this->patients[] = $patient;
            }
            return new JsonResponse(array("process" => true));
        } else {
            return new JsonResponse("error", 500);
        }
    }

    private function reanimate($children, $selection)
    {

        $em = $this->getDoctrine()->getEntityManager();
        $childs = array();
        foreach ($children as $child) {
            if ($selection) {
                $pats = $child->getPatients()->toArray();
                $newPats = $this->changeGenes($pats);
                $times = $em->getRepository("ScheduleBundle:works")->findAll();
                $ch = $this->createIndivid($newPats, $times, true);
                $childs[] = $ch;
            } else {
                if (!$child->getSuitable()) {
                    $pats = $child->getPatients()->toArray();
                    $newPats = $this->changeGenes($pats);
                    $times = $em->getRepository("ScheduleBundle:works")->findAll();
                    $ch = $this->createIndivid($newPats, $times, true);
                    $childs[] = $ch;
                } else {
                    $childs[] = $child;
                }
            }

        }
        return $childs;
    }

    /**
     * Lists all Patient entities.
     *
     * @Route("/result", name="reception_schedule_result")
     * @Method({"GET", "POST"})
     */
    public function resultAction()
    {
        $iterations = array();
        $iterate = array();
        $population = $this->buildPopulation();
        $iterPopulation = $population;
        for ($i = 0; $i < $this->getIterationNumber(); $i++) {
            $parents = $this->getNewParents($iterPopulation);
            $children = $this->getNewChildren($parents);
            if (rand(1, 100) === 5) {
                $mutated = $this->reanimate($children, true);
                $childSelection = $this->reanimate($mutated, true);
                $childReanimate = $this->reanimate($childSelection, false);
            } else {
                $childSelection = $this->reanimate($children, true);
                $childReanimate = $this->reanimate($childSelection, false);
            }
            $newPopulation = $this->getNewPopulation($iterPopulation, $childReanimate);
            $iterPopulation = $newPopulation;
            $iterations[$i][] = array("children" => $children);
            $iterations[$i][] = array(
                "newPopulation" => $newPopulation->getIndivids(),
                "parents" => $parents,
                "selection" => $childSelection,
                "reanimation" => $childReanimate,
                //"newPopulation" => $newPopulation,
                "mutated" => $mutated
            );
        }
        // $view = $this->renderView('patient/iterations.html.twig', $iterations);
        return $this->render('patient/result.html.twig', array(
            "population" => $population->getIndivids(),
            "maxMorLength" => $population->maxMorLength,
{'fraction_non_alphanumeric': 0.15225604105780882, 'fraction_numerical': 0.0029937985601254545, 'mean_word_length': 1.8383572729111874, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 177, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6015492', 'n_tokens_mistral': 4004, 'n_tokens_neox': 3763, 'n_words': 677}