Commit 875201f6 authored by Jan Pokorný's avatar Jan Pokorný 💬
Browse files

measurig depth

parent ccea5a17
......@@ -38,20 +38,26 @@ def run_program(position_name):
start = time.time()
p = subprocess.Popen([SOLVER_PATH], stdin=input_game, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
program_output = [x.decode('UTF-8') for x in p.communicate()]
game_result = program_output[0]
program_output[1] = program_output[1].split('\n')
nodes_opened = program_output[1][0]
if len(program_output[1]) > 1:
depth_searched = program_output[1][1]
end = time.time()
global TOTAL_RUN_TIME
TOTAL_RUN_TIME += end - start
print("time: {}".format(end - start))
assert reference_output == program_output[0] or reference_output == None, "Program outputed {}, but in reference was {}".format(program_output[0], reference_output)
print("result: {}".format(program_output[0]))
print("reference: {} \t current: {}".format(reference_no_nodes, int(program_output[1])))
assert reference_output == game_result or reference_output == None, "Program outputed {}, but in reference was {}".format(game_result, reference_output)
print("result: {}".format(game_result))
print("reference: {} \t current: {}".format(reference_no_nodes, int(nodes_opened)))
if len(program_output[1]) > 1:
print("depth: ", depth_searched)
if(reference_output == None or reference_no_nodes == None):
write_file(position_name + ".ref", program_output[0])
write_file(position_name + ".meta", program_output[1])
write_file(position_name + ".ref", game_result)
write_file(position_name + ".meta", nodes_opened)
print("Writing results")
elif(int(reference_no_nodes) > int(program_output[1]) and UPDATE_COUNTS):
write_file(position_name + ".meta", program_output[1])
elif(int(reference_no_nodes) > int(nodes_opened) and UPDATE_COUNTS):
write_file(position_name + ".meta", nodes_opened)
print("Updating results")
......
......@@ -4,11 +4,12 @@
using namespace std;
TranspositionTableHashed<Qubic> tt;
TranspositionTableHashed<Qubic> tt(100'000'000);
// from GAME-TREE SEARCH USING PROOF NUMBERS: THE FIRST TWENTY YEARS
Node::Node(const Qubic &game, bool isAndNode, Node *parent) : pn(SaturatedUInt::INFTY), dn(SaturatedUInt::INFTY), game(game), isAndNode(isAndNode), parent(parent) {}
Node::Node(const Qubic &game, bool isAndNode, Node *parent) :
pn(SaturatedUInt::INFTY), dn(SaturatedUInt::INFTY), game(game), isAndNode(isAndNode), parent(parent), depth(-1) {}
Node::Node(const Qubic &game) : Node(game, !game.firstPlayerPlays(), nullptr) {}
......@@ -37,30 +38,51 @@ void Node::setProofAndDisproofNumbers() {
GameState tt_result = tt.getGameState(this->game);
if (tt_result != Open) {
setProofAndDisproofNumbers(tt_result);
depth =tt.getVal(this->game);
return;
}
if (this->isExpanded()) { /* interior node */
if (this->isAndNode) {
this->pn = 0;
this->dn = SaturatedUInt::INFTY;
int max_depth = -1;
for (Node &c : this->children) { // check speed
this->pn = this->pn + c.pn;
this->dn = std::min(this->dn, c.dn);
max_depth = std::max(max_depth, c.depth);
if(c.dn == 0){
this->depth = c.depth+1;
}
}
if(this->pn == 0){
this->depth = max_depth+1;
}
} else { /* OR node */
this->pn = SaturatedUInt::INFTY;
this->dn = 0;
int max_depth = -1;
for (Node &c : this->children) {
this->dn = this->dn + c.dn;
this->pn = std::min(this->pn, c.pn);
max_depth = std::max(max_depth, c.depth);
if(c.pn == 0){
this->depth = c.depth+1;
}
}
if(this->dn == 0){
this->depth = max_depth+1;
}
}
} else { /* terminal node or none terminal leaf */
} else { /* terminal node or non-terminal leaf */
if (this->parent != nullptr) {
setProofAndDisproofNumbers(this->game.getState(this->parent->game));
} else {
setProofAndDisproofNumbers(this->game.getState());
}
// it is terminal leaf
if(this->getGameState() != Open){
depth = 0;
}
}
}
......@@ -91,7 +113,7 @@ Node *Node::updateAncestors() {
current->setProofAndDisproofNumbers();
// adding to TT if the position was solved
if (current->getGameState() != Open) {
tt.add(current->game, current->getGameState());
tt.add(current->game, current->getGameState(), current->depth);
}
if ((current->pn == oldpn && current->dn == olddn) || current->parent == nullptr) {
return current;
......
......@@ -21,6 +21,7 @@ struct Node {
const bool isAndNode;
Node *const parent;
vector<Node> children;
int depth;
bool operator==(const Node &other) const;
......
......@@ -19,5 +19,6 @@ int main(void) {
Node root(game);
PNS(root);
cout << GameStateToString(root.getGameState()) << endl;
cerr << root.depth << endl;
return 0;
}
\ No newline at end of file
......@@ -2,6 +2,12 @@
int main(void) {
Qubic::generateLines();
Node gameNodeEasy = Node(Qubic(0x7, 0x70));
PNS(gameNodeEasy);
assert(gameNodeEasy.getGameState() == FirstWon);
assert(gameNodeEasy.depth == 1);
Node gameNode = Node(Qubic(0xc84081b180410000, 0xb01c4010909000));
gameNode.expand();
gameNode.setProofAndDisproofNumbers();
......
......@@ -6,18 +6,48 @@
template <typename Game>
class TranspositionTableHashed {
std::unordered_map<Game, GameState> tt;
std::unordered_map<Game, std::pair<GameState, double>> tt;
size_t max_size;
public:
void add(const Game& game, GameState state) {
tt.insert(make_pair(game, state));
TranspositionTableHashed(size_t max_size = SIZE_MAX):max_size(max_size){}
void add(const Game& game, GameState state, double score = 0) {
if(tt.size() > max_size){
eraseWorst(0.2);
}
tt.insert({game, {state, score}});
}
GameState getGameState(const Game& game) const {
if (tt.count(game) > 0) {
return tt.at(game);
return tt.at(game).first;
}
return Open;
}
double getVal(const Game& game) const {
if (tt.count(game) > 0) {
return tt.at(game).second;
}
return NAN;
}
void eraseWorst(double perc){
double minval = (*tt.begin()).second.second;
double maxval = minval;
for( const auto & p : tt){
minval = std::min(minval, p.second.second);
maxval = std::max(maxval, p.second.second);
}
double treshold = minval + perc*(maxval-minval);
vector<Game> to_delete;
for( const auto & p : tt){
if(p.second.second <= treshold){
to_delete.push_back(p.first);
}
}
for(const Game & game : to_delete){
tt.erase(game);
}
}
size_t size() const {
return tt.size();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment