aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver/solver.h
blob: 3aa0d95663eb7058d65a9da7437ebb91f2e843f3 (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
//
// Created by thecharlesblake on 10/24/17.
//

#ifndef SOLVITAIRE_SOLVER_H
#define SOLVITAIRE_SOLVER_H

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

#include "../game/global_cache.h"
#include "../game/sol_rules.h"

class solver {
public:
    enum class sol_state {solved, unsolvable, timed_out};
    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 solution_info {
        int states_searched;
        int unique_states_searched;
        int backtracks;
        int 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;
        int max_depth;
        int depth;
    };
    friend std::ostream& operator<< (std::ostream&, const solution_info&);

    explicit solver(const game_state&, uint64_t);

    sol_state run(boost::optional<std::atomic<bool> &> = boost::none);

    void print_solution() const;
    int get_states_searched() const;
    int get_unique_states_searched() const;
    int get_cache_size() const;
    int get_states_rem_from_cache() const;
    const std::vector<node> get_frontier() const;
    solution_info get_solution_info();

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

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

    int states_searched;
    int unique_states_searched;
    int backtracks;
    int dominance_moves;
    int depth;
    int max_depth;

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


#endif //SOLVITAIRE_SOLVER_H