Skip to content
14 changes: 9 additions & 5 deletions environments/MNPuzzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

#include <stdint.h>
#include <iostream>
#include "SearchEnvironment.h"
#include "../search/SearchEnvironment.h"
#include "PermutationPuzzleEnvironment.h"
#include "UnitSimulation.h"
#include "../simulation/UnitSimulation.h"
#include "GraphEnvironment.h"
#include "Graph.h"
#include "../graph/Graph.h"
#include "GraphEnvironment.h"
#include <sstream>
#include <array>
#include "LexPermutationPDB.h"
#include "../search/LexPermutationPDB.h"

enum puzzleWeight {
kDWSTP,
Expand Down Expand Up @@ -839,6 +839,7 @@ double MNPuzzle<width, height>::DCost(const MNPuzzleState<width, height> &state1
}
}
hval = std::max(hval, man_dist);
return hval;
}

template <int width, int height>
Expand Down Expand Up @@ -1012,8 +1013,11 @@ void MNPuzzle<width, height>::GetStateFromPDBHash(uint64_t hash, MNPuzzleState<w
dual[y]++;
}
}
s.puzzle.resize(count);
//std::array;
std::fill(s.puzzle.begin(), s.puzzle.end(), -1);
// הגנה (לא חובה): ודא/י שלא נחרוג מהגודל הקבוע
if (count > static_cast<int>(s.puzzle.size()))
count = static_cast<int>(s.puzzle.size());
for (int x = 0; x < dual.size(); x++)
{
s.puzzle[dual[x]] = pattern[x];
Expand Down
49 changes: 24 additions & 25 deletions environments/PermutationPuzzleEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include <cstdio>
#include <thread>
#include <deque>
#include "SearchEnvironment.h"
#include "Timer.h"
#include "SharedQueue.h"
#include "RangeCompression.h"
#include "MR1Permutation.h"
#include "../search/SearchEnvironment.h"
#include "../utils/Timer.h"
#include "../utils/SharedQueue.h"
#include "../utils/RangeCompression.h"
#include "../utils/MR1Permutation.h"

#ifndef PERMPUZZ_H
#define PERMPUZZ_H
Expand Down Expand Up @@ -61,7 +61,8 @@ namespace PermutationPuzzle {

double HCost(const state &s) const;
virtual double AdditiveGCost(const state &s, const action &d)
{ assert(!"Additive Gost used but not defined for this class\n"); }
{ assert(!"Additive Gost used but not defined for this class\n");
return 0.0; }

void GetStateFromHash(state &s, uint64_t hash) const;
uint64_t GetStateHash(const state &s) const;
Expand Down Expand Up @@ -1357,30 +1358,28 @@ namespace PermutationPuzzle {
n, each of the integers from 0 to n-1 occurs exactly once.
**/
template <class state, class action>
bool PermutationPuzzleEnvironment<state, action>::Check_Permutation(const std::vector<int> &to_check)
bool PermutationPuzzleEnvironment<state, action>::Check_Permutation(const std::vector<int>& to_check)
{
const auto size = to_check.size();

bool in_puzzle[size];

for (size_t i = 0; i < size; i++)
{
in_puzzle[i] = false;
}

for (size_t i = 0; i < size; i++)
{
in_puzzle[to_check[i]] = true;
}

for (size_t i = 0; i < size; i++)
{
if (!in_puzzle[i])
const size_t n = to_check.size();

// היה: bool in_puzzle[n]; // לא חוקי ב-MSVC
std::vector<char> in_puzzle(n, 0); // "בדגל" לכל ערך 0..n-1

for (size_t i = 0; i < n; ++i) {
const int v = to_check[i];
if (v < 0 || v >= static_cast<int>(n)) // ערך מחוץ לטווח
return false;
if (in_puzzle[static_cast<size_t>(v)]) // כפילות
return false;
in_puzzle[static_cast<size_t>(v)] = 1;
}

// לוודא שכל הערכים 0..n-1 הופיעו
for (size_t v = 0; v < n; ++v)
if (!in_puzzle[v]) return false;

return true;
}


/**
Outputs the set of puzzles in puzzle_vector to standard output. The output is of the
Expand Down
29 changes: 25 additions & 4 deletions search/PDBHeuristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ void PDBHeuristic<abstractState, abstractAction, abstractEnvironment, state, pdb

Timer t;
t.StartTimer();

for (auto &i : goalState)
{
PDB.Set(GetPDBHash(i), 0);
Expand All @@ -305,10 +306,12 @@ void PDBHeuristic<abstractState, abstractAction, abstractEnvironment, state, pdb
newEntries = 0;
Timer s;
s.StartTimer();

// std::cout << "Im here 8 \n";
// TODO: clean up interface
if (!useCoarseOpen)
{
// std::cout << "Im here 9 \n";

uint64_t smallestDepth = (1<<pdbBits)-1;
for (uint64_t x = 0; x < COUNT; x++)
{
Expand All @@ -326,15 +329,20 @@ void PDBHeuristic<abstractState, abstractAction, abstractEnvironment, state, pdb
x, depth, std::ref(PDB), std::ref(coarseOpenNext),
&workQueue, &resultQueue, &lock);
}

// std::cout << "Im here 10 \n";
int added = 0;

for (uint64_t x = 0; x < COUNT; x+=coarseSize)
{
if ((useCoarseOpen && coarseOpenCurr[x/coarseSize]) || !useCoarseOpen)
{
workQueue.WaitAdd({x, std::min(COUNT, x+coarseSize)});
added++;
}
coarseOpenCurr[x/coarseSize] = false;
}
std::cout << "Added " << added << " work ranges to queue\n";

for (int x = 0; x < numThreads; x++)
{
workQueue.WaitAdd({0,0});
Expand All @@ -346,17 +354,20 @@ void PDBHeuristic<abstractState, abstractAction, abstractEnvironment, state, pdb
threads[x] = 0;
}
// read out node counts
// std::cout << "Im here 11 \n";

uint64_t total = 0;
{
uint64_t val;
while (resultQueue.Remove(val))
{
// std::cout << "Im here 15 \n";
total+=val;
}
}

entries += total;//newEntries;
if (verbose)
// if (verbose)
printf("Depth %d complete; %1.2fs elapsed. %" PRId64 " new states written; %" PRId64 " of %" PRId64 " total\n",
depth, s.EndTimer(), total, entries, COUNT);
depth++;
Expand Down Expand Up @@ -561,7 +572,7 @@ void PDBHeuristic<abstractState, abstractAction, abstractEnvironment, state, pdb
}
entries += total;//newEntries;
distribution.push_back(total);
if (verbose)
// if (verbose)
printf("Depth %d complete; %1.2fs elapsed. %" PRId64 " new states written; %" PRId64 " of %" PRId64 " total [%s]\n",
depth, s.EndTimer(), total, entries, COUNT, searchForward?"forward":"backward");
if (double(total)*double(total)*0.4 > double(COUNT-entries)*double(distribution[distribution.size()-2]))// || depth == 8)
Expand Down Expand Up @@ -641,18 +652,28 @@ void PDBHeuristic<abstractState, abstractAction, abstractEnvironment, state, pdb
lock->lock();
for (auto d : cache)
{
// std::cout << "im hereeeeeeeeee " << "\n";

if (d.newGCost < DB.Get(d.rank)) // shorter path
{
if (DB.Get(d.rank) == (1<<pdbBits)-1)
{
count++;
// std::cout << "count " << count << "\n";
}
coarse[d.rank/coarseSize] = true;
DB.Set(d.rank, d.newGCost);
}

}
lock->unlock();
// std::cout << "countttttttttttttttttt " << count << "\n";

cache.resize(0);
}
// std::cout << "im hereeeeee countttt" << count << "\n";
results->Add(count);

// lock->lock();
// printf("Thread %d offline\n", threadNum);
// lock->unlock();
Expand Down
6 changes: 3 additions & 3 deletions search/TreePermutationPDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class TreePermutationPDB : public PermutationPDB<state, action, environment, bit
virtual uint64_t GetPDBHash(const state &s, int threadID = 0) const;
virtual void GetStateFromPDBHash(uint64_t hash, state &s, int threadID = 0) const;
virtual uint64_t GetAbstractHash(const state &s, int threadID = 0) const { return GetPDBHash(s); }
virtual state GetStateFromAbstractState(state &s) { return s; }

virtual state GetStateFromAbstractState(state &s) const override { return s; }
std::string GetFileName(const char *prefix);
private:
using PermutationPDB<state, action, environment, bits>::example;
Expand All @@ -42,7 +42,7 @@ class TreePermutationPDB : public PermutationPDB<state, action, environment, bit
mutable std::vector<std::vector<int> > locsCache;
mutable std::vector<std::vector<int> > tempCache;

state example;
state new_example;
};

inline int mylog2(int val)
Expand Down