-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
86 lines (62 loc) · 2.26 KB
/
main.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
import 'dart:convert';
import 'package:collection/collection.dart';
import 'node.dart';
import 'utils.dart';
enum Algorithm { AStar, SimpleAStar, UniformCost }
List<dynamic> readStateFromProgramParameters(List<String> arguments) {
if (arguments.isNotEmpty) {
return jsonDecode(arguments[0]);
}
throw Exception("No arguments passed");
}
Algorithm readAlgorithmFromProgramParameters(List<String> arguments) {
if (arguments.length > 1) {
switch (arguments[1]) {
case 'AStar':
return Algorithm.AStar;
case 'SimpleAStar':
return Algorithm.SimpleAStar;
case 'UniformCost':
return Algorithm.UniformCost;
default:
throw Exception("Invalid algorithm passed");
}
}
return Algorithm.AStar;
}
var PROGRAM_ALGORITHM = Algorithm.AStar;
void main(List<String> arguments) {
Stopwatch stopwatch = Stopwatch()..start();
PROGRAM_ALGORITHM = readAlgorithmFromProgramParameters(arguments);
final initialState = readStateFromProgramParameters(arguments);
final node = Node.fromParameters(initialState);
var currentNode = node;
Map<String, List<Node>> visitedNodes = {};
final openedNodes = SortedList<Node>((a, b) => a.cost.compareTo(b.cost));
while (true) {
if (currentNode.isGoal()) {
break;
}
if (visitedNodes[currentNode.stateToString()] == null) {
visitedNodes[currentNode.stateToString()] = [];
}
visitedNodes[currentNode.stateToString()]!.add(currentNode);
final children = currentNode.generateChildren();
children.forEach((child) {
final hasBetterNode = visitedNodes[child.stateToString()]?.any((element) => element.cost < child.cost) ?? false;
if (!hasBetterNode) {
openedNodes.add(child);
}
});
currentNode = openedNodes.first;
openedNodes.removeAt(0);
}
print('Tempo de execução: ${stopwatch.elapsed}');
print('Quantidade de nodos visitados: ${visitedNodes.length}');
print('Quantidade de nodos abertos: ${openedNodes.length}');
print('Quantidade de passos para resolver o problema: ${currentNode.goalPath.length}');
print('\nLista de passos para resolver o problema:');
currentNode.goalPath.forEachIndexed((index, node) {
print("Passo ${index + 1}: ${node.getDescriptionOfThePerformedStep()}");
});
}