Вы находитесь на странице: 1из 7

Articial Intelligence and

PHP
Resource on Implementing Articial Intelligence using PHP
Home

About

Blog

Contact

Home

8 puzzle problem solving using


breadth rst search

I'm a programmer in Artificial


Intelligence using PHP. Building
intelligent websites and services by
implementing Artificial Intelligence
using PHP. Message me if I could be
any help to you

Pos ted on December 4, 2013 by admin

Email: bhatmahesht@gmail.com
Skype : bhatmahesht

Concealed Carry Guide


usc onc ealedc arry.net

Do You Know Your Rights ? Get Your Free Concealed Carry


Guide Today!

Snow Software
snowsoftware.c om

Need a Succes s ful SAM Strategy? Download Our Free


Whitepaper!

NTT Com / Nexcenter


ntt.c om

130 data centers worldwide provide Qlty., cos t-efficiency,


flexibility

Use our professional PDF creation service at http://www.htm2pdf.co.uk!

Phone: +91 90087 27280

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

Fix and repair your s low PC in a few minutes and clicks .

Solving 8 puzzle problem using


recursive best first search ( RBFS)

8 puzzle is one of the classic problems that is used to test intelligence.

Solving 8 puzzle problem using depth


first search (Recursive function)

Here is how classic 8 puzzle problem looks like

Solving 8 puzzle problem by using


iterative deepening A* search.
Formation of 8 puzzle problem in PHP
Solving 8 puzzle problem using
iterative deepening search
Solving 8 puzzle search using local
beam search
Solving 8 puzzle problem using
bidirectional search

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;

Use our professional PDF creation service at http://www.htm2pdf.co.uk!

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

Formation of 8 puzzle problem in PHP

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

s tar beam s earch bes t firs t bidirectional

$j,
$j,
$j,
$j,

$i 1, $j,
$i + 1, $j,
$i, $j 1,
$i, $j + 1,

breadth first

depth first depth firs t s earch formation


in ph GA Genetic Algorithms heauris tics hill
climbing IDA* IDDFS iterative deepening A*
iterative deepening s earch local beam
s earch

no repeat n queen puzzle n

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

problem formation que

datas tructure RBFS recurs ive bes t firs t


s earch recurs ive function Simulated
annealing traveling s ales man

$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;

Use our professional PDF creation service at http://www.htm2pdf.co.uk!

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);

Use our professional PDF creation service at http://www.htm2pdf.co.uk!

Free Genealogy
Search
genealogy.com/Records

1) Simply enter their name. 2)


View genealogy record online!

Tweet

This entry was pos ted in Searching and tagged 8-puzzle, breadth firs t. Bookmark the permalink.

Travelling city problem using depth first search


8 puzzle problem solved using depth first search
Phone Number

Phones

Reverse phone number look up

Leave a reply
Name required

Email(will not be published)(required)

Website

Use our professional PDF creation service at http://www.htm2pdf.co.uk!

Moving

Numbers

Submit comment

Notify me of follow-up comments by email.


Notify me of new posts by email.

Subscribe to Blog via Email

RSS - Posts

Enter your email address to subscribe to this blog and receive

RSS - Comments

notifications of new posts by email.


Email Address

Subscribe

Use our professional PDF creation service at http://www.htm2pdf.co.uk!

Вам также может понравиться