Skip to content

Commit fceae74

Browse files
committed
added successors18 and successors 915 for checking if the movement made is really important, that means, if the tile moved is in the range { 1, .. , 8 } or { 9 , .. , 15 }
1 parent 4be6a76 commit fceae74

File tree

7 files changed

+102
-55
lines changed

7 files changed

+102
-55
lines changed

15-puzzle/15puzzle.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "lib.h"
22
#include <queue>
3+
#include <stdio.h>
4+
#include <stdlib.h>
35

46
#define CZERO '0'
57

@@ -16,7 +18,7 @@ using namespace std;
1618
int construct_initial(char ** input, state15_t *state) {
1719
int nums[NUM_TILES];
1820
for (int i = ZERO; i < NUM_TILES; i++) {
19-
nums[i] = *input[i+1] - CZERO;
21+
nums[i] = atoi(input[i+1]);
2022
if (nums[i] < 0 || nums[i] > 15)
2123
return(0);
2224
}
@@ -76,12 +78,14 @@ bool can_be_resolved(state15_t state) {
7678
int main(int argc, char **argv) {
7779
state15_t state;
7880
state15_t *scs[BR];
81+
bool important[BR];
7982

8083
if (!construct_initial(argv, &state)) { std::cout << "Error initializing" << std::endl; return(0); }
8184

82-
state.successors(scs);
85+
cout << state << " ---- " << endl << endl;
86+
successors18(state, scs, important);
8387
for (int i = 0; scs[i] != NULL; i++)
84-
cout << *scs[i] << endl;
88+
cout << *scs[i] << important[i] << endl;
8589

8690
return(0);
8791
}

15-puzzle/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
S_HELPERS = search.cpp heuristics.cpp
1+
S_HELPERS = search.cpp heuristics.cpp pdb-generator.cc
22
O_HELPERS = $(S_HELPERS:.cpp=.o)
33
HEADER = lib.h
44

5-
S_MAIN = pdb-generator.cc
5+
S_MAIN = 15puzzle.cpp
66

77
CC = g++
88
CFLAGS = -g -o

15-puzzle/lib.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class node_t {
127127

128128
if (i == 0) { clone_st->left(); clone_nd->set_m(*LEFT); }
129129
else if (i == 1) { clone_st->right(); clone_nd->set_m(*RIGHT); }
130-
else if (i == 2) { clone_st->up(); clone_nd->set_m(*UP); }
130+
else if (i == 2) { clone_st->up(); clone_nd->set_m(*UP); }
131131
else if (i == 3) { clone_st->down(); clone_nd->set_m(*DOWN); }
132132
successors[k] = clone_nd; k++;
133133
}
@@ -138,3 +138,12 @@ class node_t {
138138
};
139139

140140
inline std::ostream& operator<<( std::ostream &os, const node_t &n ) { n.print(os); return(os); }
141+
142+
namespace __gnu_cxx {
143+
template<> class hash<state15_t> {
144+
public:
145+
size_t operator()( const state15_t &s ) const { return(s.p1_^s.p2_); }
146+
};
147+
};
148+
149+
class hash_t : public __gnu_cxx::hash_map<state15_t, node_t> { }; // class

15-puzzle/lib.h

+7
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ unsigned manhattan(state15_t state);
1111
// Search Algorithms
1212
bool informed_search(state15_t initial_state, node_t * node, int alg, int heu);
1313
bool iterative_deepening_search(state15_t initial_state, node_t *root, int heu);
14+
15+
// pattern database
16+
void pdb_gen18(state15_t state, unsigned *pt1, unsigned *pt2);
17+
void pdb_gen915(state15_t state, unsigned *pt1, unsigned *pt2);
18+
int pdb_bfs(state15_t *state, int cost, hash_t *closed);
19+
void successors18(state15_t state, state15_t ** scs, bool * important);
20+
void successors915(state15_t state, state15_t ** scs, bool * important);

15-puzzle/pdb-generator.cc

+75-39
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55

66
using namespace std;
77

8-
namespace __gnu_cxx {
9-
template<> class hash<state15_t> {
10-
public:
11-
size_t operator()( const state15_t &s ) const { return(s.p1_^s.p2_); }
12-
};
13-
};
14-
15-
class hash_t : public __gnu_cxx::hash_map<state15_t, node_t> { }; // class
16-
178
void pdb_gen18(state15_t state, unsigned *pt1, unsigned *pt2) {
189
*pt1 = ZERO; *pt2 = ZERO;
1910
for (int i = 7; i >= ZERO; --i) {
@@ -32,17 +23,6 @@ void pdb_gen915(state15_t state, unsigned *pt1, unsigned *pt2) {
3223
}
3324
}
3425

35-
int construct_initial(char ** input, state15_t *state) {
36-
int nums[NUM_TILES];
37-
for (int i = ZERO; i < NUM_TILES; i++) {
38-
nums[i] = atoi(input[i+1]);
39-
if (nums[i] < 0 || nums[i] > 15)
40-
return(0);
41-
}
42-
state->set_state(nums);
43-
return(1);
44-
}
45-
4626
int pdb_bfs(state15_t *state, int cost, hash_t *closed) {
4727
// Insert this state into the pattern database
4828
unsigned int pt_state1 = ZERO, pt_state2 = ZERO;
@@ -63,27 +43,83 @@ int pdb_bfs(state15_t *state, int cost, hash_t *closed) {
6343
return(0);
6444
}
6545

66-
void print(unsigned p1_, unsigned p2_) {
67-
unsigned p = p1_;
68-
for( int i = ZERO; i < NUM_TILES; ++i ) {
69-
cout << std::setw(2) << (p&0xF) << ' ';
70-
p = p>>TILE_SIZE;
71-
if( i%4 == 3 ) cout << endl;
72-
if( i == 7 ) p = p2_;
46+
void successors18(state15_t state, state15_t ** scs, bool * is_important) {
47+
memset(scs, ZERO, sizeof(scs));
48+
memset(is_important, ZERO, sizeof(bool));
49+
50+
short as = state.allowed_steps(), k = ZERO;
51+
for (int i = ZERO; i < BR; i++, as = as >> 1) {
52+
scs[i] = NULL;
53+
if (as & 1 == 1) {
54+
// Clone the current state
55+
state15_t * clone = (state15_t *)malloc(sizeof(state15_t));
56+
bool important = false;
57+
clone->p1_ = state.p1_; clone->p2_ = state.p2_;
58+
if (i == 0) {
59+
is_important[k] = (clone->cont(clone->bpos()-HSTEP) <= HALF_NUM_TILES ? true : false);
60+
clone->left();
61+
}
62+
else if (i == 1) {
63+
is_important[k] = (clone->cont(clone->bpos()+HSTEP) <= HALF_NUM_TILES ? true : false);
64+
clone->right();
65+
}
66+
else if (i == 2) {
67+
is_important[k] = (clone->cont(clone->bpos()-VSTEP) <= HALF_NUM_TILES ? true : false);
68+
clone->up();
69+
}
70+
else if (i == 3) {
71+
is_important[k] = (clone->cont(clone->bpos()+VSTEP) <= HALF_NUM_TILES ? true : false);
72+
clone->down();
73+
}
74+
scs[k] = clone; k++;
75+
}
76+
}
77+
}
78+
79+
void successors915(state15_t state, state15_t ** scs, bool *is_important) {
80+
memset(scs, ZERO, sizeof(scs));
81+
memset(is_important, ZERO, sizeof(bool));
82+
83+
short as = state.allowed_steps(), k = ZERO;
84+
for (int i = ZERO; i < BR; i++, as = as >> 1) {
85+
scs[i] = NULL;
86+
if (as & 1 == 1) {
87+
// Clone the current state
88+
state15_t * clone = (state15_t *)malloc(sizeof(state15_t));
89+
bool important = false;
90+
clone->p1_ = state.p1_; clone->p2_ = state.p2_;
91+
if (i == 0) {
92+
is_important[k] = (clone->cont(clone->bpos()-HSTEP) > HALF_NUM_TILES ? true : false);
93+
clone->left();
94+
}
95+
else if (i == 1) {
96+
is_important[k] = (clone->cont(clone->bpos()+HSTEP) > HALF_NUM_TILES ? true : false);
97+
clone->right();
98+
}
99+
else if (i == 2) {
100+
is_important[k] = (clone->cont(clone->bpos()-VSTEP) > HALF_NUM_TILES ? true : false);
101+
clone->up();
102+
}
103+
else if (i == 3) {
104+
is_important[k] = (clone->cont(clone->bpos()+VSTEP) > HALF_NUM_TILES ? true : false);
105+
clone->down();
106+
}
107+
scs[k] = clone; k++;
108+
}
73109
}
74110
}
75111

76-
int main(int argc, char **argv) {
77-
state15_t state;
78-
hash_t closed;
112+
// int main(int argc, char **argv) {
113+
// state15_t state;
114+
// hash_t closed;
79115

80-
if (!construct_initial(argv, &state)) { std::cout << "Error initializing" << std::endl; return(0); }
116+
// if (!construct_initial(argv, &state)) { std::cout << "Error initializing" << std::endl; return(0); }
81117

82-
//pdb_bfs(&state, 0, &closed);
83-
unsigned pt1, pt2;
84-
pdb_gen915(state, &pt1, &pt2);
85-
print(pt1, pt2);
86-
cout << endl << endl;
87-
cout << state;
88-
return(0);
89-
}
118+
// //pdb_bfs(&state, 0, &closed);
119+
// unsigned pt1, pt2;
120+
// pdb_gen915(state, &pt1, &pt2);
121+
// print(pt1, pt2);
122+
// cout << endl << endl;
123+
// cout << state;
124+
// return(0);
125+
// }

15-puzzle/search.cpp

-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ class value_comparison{
1414
};
1515
typedef priority_queue<node_t*,vector<node_t*>, value_comparison> pq_t;
1616

17-
namespace __gnu_cxx {
18-
template<> class hash<state15_t> {
19-
public:
20-
size_t operator()( const state15_t &s ) const { return(s.p1_^s.p2_); }
21-
};
22-
};
23-
24-
class hash_t : public __gnu_cxx::hash_map<state15_t, node_t> { }; // class
25-
2617
unsigned (*heuristics[2]) (state15_t state) = { misplaced_tiles, manhattan };
2718

2819
bool informed_search(state15_t initial_state, node_t *root, int alg, int heu) {

TO-DO

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ Disjoint patterns DB
1212
--------------------
1313
- patterns generator for a given state: 1 7 5 4 2 6 8 0 3 -> 1 0 0 4 2 0 0 0 3
1414
- read about switch optimization
15-
* successors should tell if the movement touch a *tile or a important tile
15+
- successors should tell if the movement touch a *tile or a important tile
1616
* complete pdb_bfs: insert state's pattern into db, get successors, get their patterns,
1717
check if they're alredy into the db, recursive call to pdb_bfs

0 commit comments

Comments
 (0)