-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.dart
215 lines (173 loc) · 6.02 KB
/
node.dart
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
101
102
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import 'package:equatable/equatable.dart';
import 'heuristics.dart';
import 'main.dart';
import 'utils.dart';
class Node extends Equatable {
final List<List<int>> state;
final int depth;
final Node? parent;
final List<Node> currentPath;
List<Node> get goalPath {
if (isGoal()) {
currentPath.removeWhere((element) => element.isRoot);
return currentPath + [this];
}
throw Exception('This node is not a goal node');
}
final List<Node> children = [];
late final int cost;
late final bool isRoot;
Node({
required this.state,
this.parent = null,
this.depth = 1,
this.currentPath = const [],
}) {
isRoot = parent == null;
if (parent != null) {
switch (PROGRAM_ALGORITHM) {
case Algorithm.AStar:
cost = calculateDistanceToSucess(this) + calculateNumOfMisplacedParts(this) + calculateLinearConflict(this) + parent!.cost + 1;
break;
case Algorithm.SimpleAStar:
cost = calculateNumOfMisplacedParts(this) + parent!.cost + 1;
break;
case Algorithm.UniformCost:
cost = parent!.depth + depth;
break;
}
} else {
cost = 1;
}
}
@override
List<Object> get props {
return [state, depth, cost];
}
factory Node.fromParameters(List<dynamic> value) {
return Node(
state: [
for (var row in value)
[
for (var cell in row) cell as int,
],
],
);
}
bool isGoal() {
return deepListEquals(state, [
[1, 2, 3],
[4, 5, 6],
[7, 8, 0],
]);
}
String getDescriptionOfThePerformedStep() {
if (isRoot) return "";
final emptyPosition = _getEmptyPosition();
final parentEmptyPosition = parent!._getEmptyPosition();
if (emptyPosition.values.first > parentEmptyPosition.values.first) {
return "Mova o número ${_getValueLeftFromEmptySpace(emptyPosition)}";
} else if (emptyPosition.values.first < parentEmptyPosition.values.first) {
return "Mova o número ${_getValueRightFromEmptySpace(emptyPosition)}";
} else if (emptyPosition.keys.first > parentEmptyPosition.keys.first) {
return "Mova o número ${_getValueUpFromEmptySpace(emptyPosition)}";
} else if (emptyPosition.keys.first < parentEmptyPosition.keys.first) {
return "Mova o número ${_getValueDownFromEmptySpace(emptyPosition)}";
}
return "";
}
String stateToString() {
return state.map((row) => row.join('')).join('');
}
List<Node> generateChildren() {
final emptyPosition = _getEmptyPosition();
if (_canMoveEmptySpaceToLeft(emptyPosition)) {
_moveEmptySpaceToLeft(emptyPosition);
}
if (_canMoveEmptySpaceToRight(emptyPosition)) {
_moveEmptySpaceToRight(emptyPosition);
}
if (_canMoveEmptySpaceToUp(emptyPosition)) {
_moveEmptySpaceToUp(emptyPosition);
}
if (_canMoveEmptySpaceToDown(emptyPosition)) {
_moveEmptySpaceToDown(emptyPosition);
}
return children;
}
List<List<int>> _deepCloneState() {
return [
for (var row in state)
[
for (var cell in row) cell,
],
];
}
bool _canMoveEmptySpaceToLeft(Map<int, int> emptyPosition) {
return emptyPosition.values.first > 0;
}
bool _canMoveEmptySpaceToRight(Map<int, int> emptyPosition) {
return emptyPosition.values.first < state.length - 1;
}
bool _canMoveEmptySpaceToUp(Map<int, int> emptyPosition) {
return emptyPosition.keys.first > 0;
}
bool _canMoveEmptySpaceToDown(Map<int, int> emptyPosition) {
return emptyPosition.keys.first < state.length - 1;
}
void _moveEmptySpaceToLeft(Map<int, int> emptyPosition) {
final newState = _deepCloneState();
newState[emptyPosition.keys.first][emptyPosition.values.first] = newState[emptyPosition.keys.first][emptyPosition.values.first - 1];
newState[emptyPosition.keys.first][emptyPosition.values.first - 1] = 0;
_createNewChild(newState);
}
void _moveEmptySpaceToRight(Map<int, int> emptyPosition) {
final newState = _deepCloneState();
newState[emptyPosition.keys.first][emptyPosition.values.first] = newState[emptyPosition.keys.first][emptyPosition.values.first + 1];
newState[emptyPosition.keys.first][emptyPosition.values.first + 1] = 0;
_createNewChild(newState);
}
void _moveEmptySpaceToUp(Map<int, int> emptyPosition) {
final newState = _deepCloneState();
newState[emptyPosition.keys.first][emptyPosition.values.first] = newState[emptyPosition.keys.first - 1][emptyPosition.values.first];
newState[emptyPosition.keys.first - 1][emptyPosition.values.first] = 0;
_createNewChild(newState);
}
void _moveEmptySpaceToDown(Map<int, int> emptyPosition) {
final newState = _deepCloneState();
newState[emptyPosition.keys.first][emptyPosition.values.first] = newState[emptyPosition.keys.first + 1][emptyPosition.values.first];
newState[emptyPosition.keys.first + 1][emptyPosition.values.first] = 0;
_createNewChild(newState);
}
void _createNewChild(List<List<int>> newState) {
final currentPath = this.currentPath + [this];
children.add(Node(
state: newState,
parent: this,
depth: depth + 1,
currentPath: currentPath,
));
}
Map<int, int> _getEmptyPosition() {
for (var row = 0; row < state.length; row++) {
for (var column = 0; column < state[row].length; column++) {
if (state[row][column] == 0) {
return {row: column};
}
}
}
throw Exception("Empty space not found");
}
int _getValueLeftFromEmptySpace(Map<int, int> emptyPosition) {
return state[emptyPosition.keys.first][emptyPosition.values.first - 1];
}
int _getValueRightFromEmptySpace(Map<int, int> emptyPosition) {
return state[emptyPosition.keys.first][emptyPosition.values.first + 1];
}
int _getValueUpFromEmptySpace(Map<int, int> emptyPosition) {
return state[emptyPosition.keys.first - 1][emptyPosition.values.first];
}
int _getValueDownFromEmptySpace(Map<int, int> emptyPosition) {
return state[emptyPosition.keys.first + 1][emptyPosition.values.first];
}
}