Skip to content

solver class

Paweł Waligóra edited this page Dec 10, 2023 · 15 revisions

Solver Class

Generic Declatation

namespace knapsack_solver{

class Solver{
    public:
    Solver() = delete;

    struct Options{

        // fields

        Options() = default;
        explicit Options(std::vector<std::string> & args);
    };

    // Solver specific methods
    static Solution Algorithm(PackagedProblem & problem, const Options & options);

    static PackagedSolution Solve(PackagedProblem & problem, const Options & options);
    static std::string GetAlgorithmName(const Options & options);
    static bool expect_perfection;
};

}

Algorithm can have any name, can take any other configuration of arguments, there can be many of them, but names of other methods have to match.

Solve method

Solve method should select apropriate Algorithm if has many of them and call it in following fashion:

PackagedSolution Solver::Solve(PackagedProblem & problem, const Options & options){
    PackagedSolution ps;
    
    // run with time measure
    std::chrono::steady_clock::time_point start, end;
    start = std::chrono::steady_clock::now();
    ps.solution = Algorithm(problem, options);
    end = std::chrono::steady_clock::now();
    const std::chrono::duration<double> elapsed_seconds{end - start};
    ps.solve_time = std::chrono::duration<double>(elapsed_seconds).count();

    return ps;
}

Implemented Solvers