-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem.m
101 lines (87 loc) · 3.71 KB
/
Problem.m
1
2
3
4
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
classdef Problem < handle
properties
InitialState
GoalState
States = State.empty;
ArrayVersion
end
methods
function problem = Problem(numberOfRows, numberOfColumns)
problem.States(1:numberOfRows, 1: numberOfColumns) = State;
for i = 1:numberOfRows
for j = 1:numberOfColumns
if rand < 0.4
blocked = true;
else
blocked = false;
end
problem.States(i,j) = State(i,j,blocked);
problem.ArrayVersion(i,j) = blocked;
end
end
index = round(rand*(i*j-1))+1;
problem.InitialState = problem.States(index);
problem.InitialState.Blocked = false;
problem.ArrayVersion(index) = 0;
index = round(rand*(i*j-1))+1;
problem.GoalState = problem.States(index);
problem.GoalState.Blocked = false;
problem.ArrayVersion(index) = 0;
end
function result = GoalTest(problem, state)
if state == problem.GoalState
result = true;
else
result = false;
end
end
function [actions, results] = SuccessorFunction(problem, state)
[numberOfRows, numberOfColumns] = size(problem.States);
row = state.Row;
column = state.Column;
actions = double.empty;
results = State.empty;
index = 0;
if(column>1 && problem.States(row,column-1).Blocked == false)
index = index + 1;
actions(index) = 4; % left (according to keyboard's numeric keypad!)
results(index) = problem.States(row,column-1);
end
if(column<numberOfColumns && problem.States(row,column+1).Blocked == false)
index = index + 1;
actions(index) = 6; % right
results(index) = problem.States(row,column+1);
end
if(row>1 && problem.States(row-1,column).Blocked == false)
index = index + 1;
actions(index) = 8; % up
results(index) = problem.States(row-1,column);
end
if(row<numberOfRows && problem.States(row+1,column).Blocked == false)
index = index + 1;
actions(index) = 2; % down
results(index) = problem.States(row+1,column);
end
if(column>1 && row>1 && problem.States(row-1,column-1).Blocked == false)
index = index + 1;
actions(index) = 7; % upper left
results(index) = problem.States(row-1,column-1);
end
if(column<numberOfColumns && row>1 && problem.States(row-1,column+1).Blocked == false)
index = index + 1;
actions(index) = 9; % upperRight
results(index) = problem.States(row-1,column+1);
end
if(column>1 && row<numberOfRows && problem.States(row+1,column-1).Blocked == false)
index = index + 1;
actions(index) = 1; % bottomLeft
results(index) = problem.States(row+1,column-1);
end
if(column<numberOfColumns && row<numberOfRows && problem.States(row+1,column+1).Blocked == false)
index = index + 1;
actions(index) = 3; % bottomRight
results(index) = problem.States(row+1,column+1);
end
end
end
end