<commit_before>// Copyright 2014 <NAME>, <NAME>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <boost/program_options.hpp>
#include <string>
#include <ctime>
#include <iostream>

#include "./instance.h"
#include "./solution.h"
#include "./greedy.h"
#include "./randomGreedy.h"
#include "./complexMoveHeuristic.h"

static Solution* best_solution;
static time_t last_interrupt = 0;
static float alpha = 0.4;
static int timeout = 10;
static bool randomized = false;
static bool best_improvement = false;
static time_t global_start_time;

void interrupt_handler(int signum) {
    fprintf(stderr, "Caught signal %d\n", signum);
    bool stop = (time(0) - last_interrupt) < 1;
    if (best_solution) {
        best_solution->print(stop ? stdout : stderr);
    }
    if (stop) exit(0);
    last_interrupt = time(0);
}

void set_best_solution(Solution* solution) {
    if (best_solution && best_solution != solution) {
        delete best_solution;
    }
    best_solution = solution->clone();
}

Solution* vnd(Solution* solution) {
    // Perform Variable Neighborhood Descent
    int current_length = solution->length;
    fprintf(stderr, "Current solution length: %i\n", current_length);
    time_t start_time = time(0);
    time_t time_to_stop = start_time + timeout;

    while (complexMove(solution, time_to_stop, best_improvement)) {
        solution->trim();
        current_length = solution->length;
        fprintf(stderr, "Current solution length: %i (%lus)\n",
            current_length, time(0)-start_time);
        time_to_stop = time(0) + timeout;

        set_best_solution(solution);
    }
    return best_solution;
}

Solution* grasp(Instance* instance) {
    for (int i = 0; i < 10; i++) {
        Solution* solution = randomGreedy(instance, alpha);

        fprintf(stderr, "Greedy Solution Nr. %i:\n", i);
        solution->print(stderr);

        solution = vnd(solution);
        solution->print(stderr);
        if (solution->length < best_solution->length) {
            set_best_solution(solution);
        }
    }
    fprintf(stderr, "Best solution found in %lus:\n", time(0)-global_start_time);
    best_solution->print();
    return best_solution;
}

/// Normal old solving, no GRASP
void solve(Instance* instance) {
    Solution* solution;

    if (randomized) {
        solution = randomGreedy(instance, alpha);
    } else {
        solution = greedy(instance);
    }

    fprintf(stderr, "Greedy Solution:\n");
    solution->print(stderr);
    signal(SIGINT, interrupt_handler);

    solution = vnd(solution);
    solution->print();
}

int main(int argc, char** argv) {
    namespace po = boost::program_options;
    po::options_description description
        ("Time-Constrained Bepartite Vehicle Routing Problem Solver");

    description.add_options()
        ("timeout,t", po::value<int>()->implicit_value(10),
            "Timeout after new solution has been found")
        ("randomized,r", "Use a randomized construction heuristic")
        ("alpha,a", po::value<float>(), "Alpha value for random construction heuristic [0-1]")
        ("grasp,g", "Use GRASP")
        ("best-improvement,b", "Use best-improvement instead of first-improvement")
        ("help,h", "Display this help message")
        ("instance,i", po::value<std::string>(), "Instance file")
        ("version,v", "Display the version number");

    po::positional_options_description p;
    p.add("instance", 1);
    po::variables_map vm;
    try {
        po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);<commit_msg>[GRASP] Fix last solution always reported as best<commit_after>// Copyright 2014 <NAME>, <NAME>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <boost/program_options.hpp>
#include <string>
#include <ctime>
#include <iostream>

#include "./instance.h"
#include "./solution.h"
#include "./greedy.h"
#include "./randomGreedy.h"
#include "./complexMoveHeuristic.h"

static Solution* best_solution;
static int best_solution_time = 0;
static time_t last_interrupt = 0;
static float alpha = 0.4;
static int timeout = 10;
static bool randomized = false;
static bool best_improvement = false;
static time_t global_start_time;

void interrupt_handler(int signum) {
    fprintf(stderr, "Caught signal %d\n", signum);
    bool stop = (time(0) - last_interrupt) < 1;
    if (best_solution) {
        best_solution->print(stop ? stdout : stderr);
    }
    if (stop) exit(0);
    last_interrupt = time(0);
}

void set_best_solution(Solution* solution) {
    if (best_solution && best_solution->length < solution->length) {
        return;
    }
    best_solution_time = time(0);
    if (best_solution && best_solution != solution) {
        delete best_solution;
    }
    best_solution = solution->clone();
}

Solution* vnd(Solution* solution) {
    // Perform Variable Neighborhood Descent
    int current_length = solution->length;
    fprintf(stderr, "Current solution length: %i\n", current_length);
    time_t start_time = time(0);
    time_t time_to_stop = start_time + timeout;

    while (complexMove(solution, time_to_stop, best_improvement)) {
        solution->trim();
        current_length = solution->length;
        fprintf(stderr, "Current solution length: %i (%lus / %lus)\n",
            current_length, time(0)-start_time, time(0)-global_start_time);
        time_to_stop = time(0) + timeout;

        set_best_solution(solution);
    }
    return best_solution;
}

Solution* grasp(Instance* instance) {
    for (int i = 0; i < 30; i++) {
        Solution* solution = randomGreedy(instance, alpha);

        fprintf(stderr, "Greedy Solution Nr. %i:\n", i);
        solution->print(stderr);

        solution = vnd(solution);
        solution->print(stderr);
    }
    fprintf(stderr, "Best solution found in %lus, total runtime %lus:\n",
        time(0)-best_solution_time, time(0)-global_start_time);
    best_solution->print();
    return best_solution;
}

/// Normal old solving, no GRASP
void solve(Instance* instance) {
    Solution* solution;

    if (randomized) {
        solution = randomGreedy(instance, alpha);
    } else {
        solution = greedy(instance);
    }

    fprintf(stderr, "Greedy Solution:\n");
    solution->print(stderr);
    set_best_solution(solution);
    signal(SIGINT, interrupt_handler);

    solution = vnd(solution);
    solution->print();
}

int main(int argc, char** argv) {
    namespace po = boost::program_options;
    po::options_description description
        ("Time-Constrained Bepartite Vehicle Routing Problem Solver");

    description.add_options()
        ("timeout,t", po::value<int>()->implicit_value(10),
            "Timeout after new solution has been found")
        ("randomized,r", "Use a randomized construction heuristic")
        ("alpha,a", po::value<float>(), "Alpha value for random construction heuristic [0-1]")
        ("grasp,g", "Use GRASP")
        ("best-improvement,b", "Use best-improvement instead of first-improvement")
        ("help,h", "Display this help message")
        ("instance,i", po::value<std::string>(), "Instance file")
        ("version,v", "Display the version number");

    po::positional_options_description p;
    p.add("instance", 1);
    po::variables_map vm;
    try {
        po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
{'fraction_non_alphanumeric': 0.13757115749525617, 'fraction_numerical': 0.007319056654920032, 'mean_word_length': 3.5021354484441733, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 53, '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': '6457949', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2236, 'n_words': 564}