aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver/solver.h
blob: fec3f4b2a4a8c193c412a267cef796dd7c302796 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Created by thecharlesblake on 10/24/17.
//

#ifndef SOLVITAIRE_SOLVER_H
#define SOLVITAIRE_SOLVER_H

#include <string>
#include <vector>
#include <atomic>
#include <chrono>

#include "../game/global_cache.h"
#include "../game/sol_rules.h"
#include "../input-output/input/command_line_helper.h"

class solver {
public:
    lru_cache cache;

    struct node {
        node(move);
        const move mv;
        std::vector<move> child_moves;
        boost::optional<lru_cache::item_list::iterator> cache_state; // Optional, as dominance moves aren't cached
    };

    struct result {
#ifdef C2NIM
	enum type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT, TERMINATED };
#else
        enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT, TERMINATED };
#endif

        type sol_type;
        uint64_t states_searched;
        uint64_t unique_states_searched;
        uint64_t backtracks;
        uint64_t dominance_moves;
        uint64_t states_removed_from_cache;
        lru_cache::item_list::size_type cache_size;
        lru_cache::item_list::size_type cache_bucket_count;
        uint64_t max_depth;
        uint64_t depth;
        std::chrono::milliseconds time;
    };

    explicit solver(const game_state&, uint64_t);

    result run(boost::optional<std::chrono::milliseconds> = boost::none);

    void print_solution() const;
    static void print_header(long, command_line_helper::streamliner_opt);
    static void print_result_csv(solver::result);
    static void print_null_seed_info();
    const std::vector<node> get_frontier() const;

    const game_state init_state;

private:
    typedef std::chrono::high_resolution_clock clock;
    typedef std::chrono::milliseconds millisec;

    result::type dfs(boost::optional<clock::time_point> = boost::none);

    bool revert_to_last_node_with_children(boost::optional<lru_cache::item_list::iterator> = boost::none);
    void set_to_child();

    game_state state;
    std::vector<node> frontier;

    result res;

    node root;
    std::vector<node>::iterator current_node;
};

std::ostream& operator<< (std::ostream&, const solver::result::type&);
std::ostream& operator<< (std::ostream&, const solver::result&);
void sigint_handler(int i);

#endif //SOLVITAIRE_SOLVER_H