<commit_before>#include "voraceAlgorithm.h"
#include <random>
#include <ctime>

VoraceAlgorithm::VoraceAlgorithm()
{
    std::srand(unsigned int(std::time(nullptr)));
}

Solution VoraceAlgorithm::concreteSolve(Problem &problem)
{
    std::vector<Student*> students;
    for(auto& student : problem.students)
    {
        students.push_back(&student);
    }
    while(true)
    {
        std::vector<Student*> students;
        for(auto& student : problem.students)
        {
            student.inSolution = false;
            students.push_back(&student);
        }
        Solution tempSolution(problem);

        Student* lastStudentAdded = chooseStudent(students);
        tempSolution.students.push_back(lastStudentAdded);
        lastStudentAdded->inSolution = true;
        while(true)
        {
            Student* nextStudent = chooseStudent(lastStudentAdded->friends);
            if(nextStudent != nullptr)
            {
                lastStudentAdded = nextStudent;
                tempSolution.students.push_back(lastStudentAdded);
                lastStudentAdded->inSolution = true;
            }
            else
            {
                break;
            }
        }
        if(tempSolution.isValid())
        {
           return tempSolution;
        }
    }
}

Student* VoraceAlgorithm::chooseStudent(std::vector<Student*> students)
{
    int totalHeight = 0;
    for(auto& student : students)
    {
        if(!student->inSolution)
        {
            totalHeight += student->height;
        }
    }
    std::vector<double> probabilities;
    for(auto& student : students)
    {
        if(!student->inSolution)
        {
            probabilities.push_back(double(student->height) / totalHeight);
        }
        else
        {
            probabilities.push_back(0.0);
        }
    }
    double choosenProb = double(std::rand()) / RAND_MAX;
    double totalProb = 0.0;
    for(size_t i = 0; i < probabilities.size(); ++i)
    {
        totalProb += probabilities[i];
        if(totalProb > choosenProb)
        {
            return students[i];
        }
    }
    return nullptr;
}<commit_msg>Amelioration vorace<commit_after>#include "voraceAlgorithm.h"
#include <random>
#include <ctime>

VoraceAlgorithm::VoraceAlgorithm()
{
    std::srand(unsigned int(std::time(nullptr)));
}

Solution VoraceAlgorithm::concreteSolve(Problem &problem)
{
    while (true)
    {
        std::vector<Student*> students;
        for (auto& student : problem.students)
        {
            student.inSolution = false;
            students.push_back(&student);
        }
        Solution tempSolution(problem);

        Student* lastStudentAdded = chooseStudent(students);
        tempSolution.students.push_back(lastStudentAdded);
        lastStudentAdded->inSolution = true;
        while (true)
        {
            Student* nextStudent = chooseStudent(lastStudentAdded->friends);
            if (nextStudent != nullptr)
            {
                lastStudentAdded = nextStudent;
                tempSolution.students.push_back(lastStudentAdded);
                lastStudentAdded->inSolution = true;
            }
            else
            {
                break;
            }
        }
        if (tempSolution.isValid())
        {
            std::reverse(tempSolution.students.begin(), tempSolution.students.end());
            return tempSolution;
        }
    }
}

Student* VoraceAlgorithm::chooseStudent(std::vector<Student*> students)
{
    int totalHeight = 0;
    for (auto& student : students)
    {
        if (!student->inSolution)
        {
            totalHeight += student->height;
        }
    }
    std::vector<double> probabilities;
    for (auto& student : students)
    {
        if (!student->inSolution)
        {
            probabilities.push_back(double(student->height) / totalHeight);
        }
        else
        {
            probabilities.push_back(0.0);
        }
    }
    double choosenProb = double(std::rand() - 1) / RAND_MAX;
    double totalProb = 0.0;
    for (size_t i = 0; i < probabilities.size(); ++i)
    {
        totalProb += probabilities[i];
        if (totalProb > choosenProb)
        {
            return students[i];
        }
    }
    int numberOfStudent = probabilities.size();
    return nullptr;
}
{'fraction_non_alphanumeric': 0.11282051282051282, 'fraction_numerical': 0.0030303030303030303, 'mean_word_length': 1.961352657004831, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 30, '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': '6401558', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1184, 'n_words': 200}