Академический Документы
Профессиональный Документы
Культура Документы
PHP
Resource on Implementing Articial Intelligence using PHP
Home
About
Blog
Contact
Home
Email: bhatmahesht@gmail.com
Skype : bhatmahesht
Snow Software
snowsoftware.c om
Recent Posts
Solving n queens problem using
Genetic Algorithm
Formation of n queens problem in
PHP
Solving 8 puzzle problem using
simulated annealing search
freeregistryc leanerapp.c om
8 puzzle
In a 3X3 box all the boxes are filled with numbers from 1 to 8, except one box
The blank can be moved to left, right, top and down.
In the initial state the numbers are unordered, in the final state the numbers are ordered.
Our goal is to find out the sequence of moving blank states such that final goal is reached.
In this problem we tried to solve it by breadth first search. We used the special Que available
in PHP to store states of puzzle.
As the initial state become unordered and if it requires number of steps to reach the goal
state, itll take lots of memory and your PHP limit may be exceeded. Try keeping as much as
memory possible in the PHP
Here is the PHP code that implemented breadth first search on 8 Puzzle problem.
1
2
3
4
class Puzzle {
var $pos;
var $sequence;
Recent Comments
Solving n queens problem using
Genetic Algorithm | Artificial
Intelligence and PHP on Formation of
n queens problem in PHP
Solving 8 puzzle problem using
simulated annealing search | Artificial
Intelligence and PHP on Formation of
8 puzzle problem in PHP
Solving 8 puzzle problem using
recursive best first search ( RBFS) |
Artificial Intelligence and PHP on
Formation of 8 puzzle problem in PHP
Solving 8 puzzle problem using depth
first search (Recursive function) |
Artificial Intelligence and PHP on
Solving 8 puzzle problem using depth
first search (No repeated states)
Solving 8 puzzle problem using depth
first search (Recursive function) |
Artificial Intelligence and PHP on
Formation of 8 puzzle problem in PHP
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
var $depth;
function __construct($current_pos) {
$this>pos = $current_pos;
$this>sequence = array();
$this>depth = 1;
}
Tags
function goalTest($goal) {
if ($this>pos === $goal) {
return True;
} else {
return False;
}
}
bidirectional s earch
8-puzzle
$j,
$j,
$j,
$j,
$i 1, $j,
$i + 1, $j,
$i, $j 1,
$i, $j + 1,
breadth first
queens
function possibleMoves() {
$Moves = array();
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
if ($this>pos[$i][$j] == 0) {
break 2;
}
}
}
$this>checkMove($i,
$this>checkMove($i,
$this>checkMove($i,
$this>checkMove($i,
A* ai ai php 8 puzzle a
$Moves);
$Moves);
$Moves);
$Moves);
return $Moves;
}
function moveBlank($srcRow, $srcCol, $destRow, $destCol) {
$newpos = $this>pos;
$tmp = $newpos[$destRow][$destCol];
$newpos[$destRow][$destCol] = $newpos[$srcRow][$srcCol];
$newpos[$srcRow][$srcCol] = $tmp;
return $newpos;
}
function InSequence($pos) {
for ($i = 0; $i < count($this>sequence); $i++) {
if ($this>sequence === $pos) {
return TRUE;
}
}
return FALSE;
traverse city
54
}
55
56
function canMove($srcRow, $srcCol, $destRow, $destCol) {
57
if ($srcRow < 0 or $srcCol < 0 or $destRow < 0 or $destCol < 0) {
58
return FALSE;
59
}
60
if ($srcRow > 2 or $srcCol > 2 or $destRow > 2 or $destCol > 2) {
61
return FALSE;
62
}
63
return TRUE;
64
}
65
66
function checkMove($srcRow, $srcCol, $destRow, $destCol, & $Moves) {
67
if ($this>canMove($srcRow, $srcCol, $destRow, $destCol)) {
68
$newpos = $this>moveBlank($srcRow, $srcCol, $destRow, $destCol);
69
if (!$this>InSequence($newpos)) {
70
$newMove = new Puzzle($newpos);
71
$newMove>sequence = array_merge($this>sequence);
72
$newMove>sequence[] = $newpos;
73
$Moves[] = $newMove;
74
}
75
}
76
}
77
78
function printSequence() {
79
for ($i = 0; $i < count($this>sequence); $i++) {
80
print ("Step $i <br> ");
81
$this>printPos($this>sequence[$i]);
82
print("<br>");
83
}
84
}
85
86
function printPos($pos) {
87
for ($i = 0; $i < 3; $i++) {
88
for ($j = 0; $j < 3; $j++) {
89
print(" " . $pos[$i][$j] . " ");
90
}
91
print("<br>");
92
}
93
}
94
95 }
96
97 $start_time = microtime();
98
99 $initial_pos = array(
100
array(1, 2, 5),
101
array(3, 4, 0),
102
array(6, 8, 7)
Use our professional PDF creation service at http://www.htm2pdf.co.uk!
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
);
$goal_pos = array(
array(0, 1, 2),
array(3, 4, 5),
array(6, 7, 8)
);
$initial_state = new Puzzle($initial_pos);
$initial_state>sequence[] = $initial_pos;
$nodeQue = new SplQueue();
$nodeQue>enqueue($initial_state);
$nodeQue>rewind();
$i = 1;
while ($nodeQue>valid()) {
$i++;
if ($i % 10000 == 0) {
print("$i steps have been finished<br>");
print("Unopened Nodes left in the Que = " . $nodeQue>count() . "<br>");
$end_time = microtime();
$exec_time = $end_time $start_time;
print ("Time executed to $i steps is $end_time <br>");
}
$current_state = new Puzzle();
$current_state = $nodeQue>dequeue();
if ($current_state>goalTest($goal_pos) == TRUE) {
print("Solution found in $i steps<br>");
print("Unopened Nodes left in the Que=" . $nodeQue>count() . "<br>");
$current_state>printSequence();
break;
}
$moves = $current_state>possibleMoves();
foreach ($moves as $move) {
$nodeQue>enqueue($move);
}
$nodeQue>rewind();
}
print ("<br>Maximum Memory used " . memory_get_peak_usage());
print ("<br>Current Memory used" . memory_get_usage());
$end_time = microtime();
$time_exec = $end_time $start_time;
print("<br>Execution time used =" . $time_exec);
Free Genealogy
Search
genealogy.com/Records
Tweet
This entry was pos ted in Searching and tagged 8-puzzle, breadth firs t. Bookmark the permalink.
Phones
Leave a reply
Name required
Website
Moving
Numbers
Submit comment
RSS - Posts
RSS - Comments
Subscribe