Skip to content

Commit

Permalink
Start making solve iterative.
Browse files Browse the repository at this point in the history
  • Loading branch information
rokicki committed Aug 12, 2023
1 parent 53e619b commit edb653b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 51 deletions.
139 changes: 89 additions & 50 deletions src/cpp/solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,68 +41,106 @@ void solveworker::init(const puzdef &pd, int d_, int id_, const setval &p) {
d = d_ ;
id = id_ ;
}
int solveworker::solverecur(const puzdef &pd, prunetable &pt, int togo, int sp, int st) {
lookups++ ;
int v = pt.lookup(posns[sp], looktmp) ;
if (v > togo + 1)
return -1 ;
if (v > togo)
int solveworker::possibsolution(const puzdef &pd, int sp) {
if (callback) {
return callback(posns[sp], movehist, d, id) ;
}
if (pd.comparepos(posns[sp], pd.solved) == 0) {
int r = 1 ;
get_global_lock() ;
solutionsfound++ ;
lastsolution.clear() ;
if (d == 0) // allow null solution to trigger
cout << " " ;
for (int i=0; i<d; i++) {
cout << " " << pd.moves[movehist[i]].name ;
if (i > 0)
lastsolution += " " ;
lastsolution += pd.moves[movehist[i]].name ;
}
cout << endl << flush ;
if (solutionsfound < solutionsneeded)
r = 0 ;
release_global_lock() ;
return r ;
} else
return 0 ;
}
int solveworker::solveiter(const puzdef &pd, prunetable &pt, int togo, int sp, int st) {
int v, mi, m ;
ull mask, skipbase ;
top:
lookups++ ;
v = pt.lookup(posns[sp], looktmp) ;
if (v > togo + 1) {
v = -1 ; goto returnval ;
}
if (v > togo) {
v = 0 ; goto returnval ;
}
if (v == 0) {
if (togo == 1 && didprepass && pd.comparepos(posns[sp], pd.solved) == 0)
return 0 ;
if (togo == 1 && didprepass && pd.comparepos(posns[sp], pd.solved) == 0) {
v = 0 ;
goto returnval ;
}
if (togo > 0 && noearlysolutions &&
pd.comparepos(posns[sp], pd.solved) == 0)
return 0 ;
pd.comparepos(posns[sp], pd.solved) == 0) {
v = 0 ;
goto returnval ;
}
}
if (togo == 0) {
if (callback) {
return callback(posns[sp], movehist, d, id) ;
}
if (pd.comparepos(posns[sp], pd.solved) == 0) {
int r = 1 ;
get_global_lock() ;
solutionsfound++ ;
lastsolution.clear() ;
if (d == 0) // allow null solution to trigger
cout << " " ;
for (int i=0; i<d; i++) {
cout << " " << pd.moves[movehist[i]].name ;
if (i > 0)
lastsolution += " " ;
lastsolution += pd.moves[movehist[i]].name ;
}
cout << endl << flush ;
if (solutionsfound < solutionsneeded)
r = 0 ;
release_global_lock() ;
return r ;
} else
return 0 ;
v = possibsolution(pd, sp) ;
goto returnval ;
}
mask = canonmask[st] ;
skipbase = 0 ;
mi = 0 ;
topm:
if (mi >= (int)pd.moves.size()) {
v = 0 ;
goto returnval ;
}
ull mask = canonmask[st] ;
const vector<int> &ns = canonnext[st] ;
ull skipbase = 0 ;
for (int mi=0; mi<(int)pd.moves.size(); mi++) {
int m = randomstart ? randomized[togo][mi] : mi ;
m = randomstart ? randomized[togo][mi] : mi ;
{
const moove &mv = pd.moves[m] ;
if (!quarter && mv.base < 64 && ((skipbase >> mv.base) & 1))
continue ;
goto nextm ;
if ((mask >> mv.cs) & 1)
continue ;
goto nextm ;
pd.mul(posns[sp], mv.pos, posns[sp+1]) ;
if (!pd.legalstate(posns[sp+1]))
continue ;
goto nextm ;
movehist[sp] = m ;
v = solverecur(pd, pt, togo-1, sp+1, ns[mv.cs]) ;
if (v == 1)
return 1 ;
if (!quarter && v == -1) {
if (mv.base < 64)
skipbase |= 1LL << mv.base ;
}
solvestates.push_back({st, mi, mask, skipbase}) ;
togo-- ;
sp++ ;
st = canonnext[st][mv.cs] ;
goto top ;
}
return 0 ;
returnval:
if (solvestates.size() == 0)
return v ;
{
auto &ss = solvestates[solvestates.size()-1] ;
togo++ ;
sp-- ;
st = ss.st ;
mi = ss.mi ;
mask = ss.mask ;
skipbase = ss.skipbase ;
}
solvestates.pop_back() ;
if (v == 1)
goto returnval ;
if (!quarter && v == -1) {
m = randomstart ? randomized[togo][mi] : mi ;
if (pd.moves[m].base < 64)
skipbase |= 1LL << pd.moves[m].base ;
}
nextm:
mi++ ;
goto topm ;
}
int solveworker::solvestart(const puzdef &pd, prunetable &pt, int w) {
ull initmoves = workchunks[w] ;
Expand All @@ -121,7 +159,8 @@ int solveworker::solvestart(const puzdef &pd, prunetable &pt, int w) {
togo-- ;
initmoves /= nmoves ;
}
return solverecur(pd, pt, togo, sp, st) ;
solvestates.clear() ;
return solveiter(pd, pt, togo, sp, st) ;
}
void solveworker::dowork(const puzdef &pd, prunetable &pt) {
while (1) {
Expand Down
8 changes: 7 additions & 1 deletion src/cpp/solve.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ extern int optmindepth ;
extern int onlyimprovements ;
extern int randomstart ;
extern string lastsolution ;
struct solvestate {
int st, mi ;
ull mask, skipbase ;
} ;
struct solveworker {
vector<allocsetval> posns ;
vector<solvestate> solvestates ;
vector<int> movehist ;
long long lookups ;
int d, id ;
setval *looktmp ;
char padding[256] ; // kill false sharing
void init(const puzdef &pd, int d_, int id_, const setval &p) ;
int solverecur(const puzdef &pd, prunetable &pt, int togo, int sp, int st) ;
int solveiter(const puzdef &pd, prunetable &pt, int togo, int sp, int st) ;
int possibsolution(const puzdef &pd, int sp) ;
int solvestart(const puzdef &pd, prunetable &pt, int w) ;
void dowork(const puzdef &pd, prunetable &pt) ;
} ;
Expand Down

0 comments on commit edb653b

Please sign in to comment.