-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathuniformCostSearch.js
More file actions
31 lines (30 loc) · 921 Bytes
/
uniformCostSearch.js
File metadata and controls
31 lines (30 loc) · 921 Bytes
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
// Code for Uniform Cost Search
var uniformCostSearch = function(problem) {
var frontier = problem.frontier;
var minNode = frontier[0];
var minCost = Number.POSITIVE_INFINITY;
for (var i = 0; i < frontier.length; i++) {
if (problem.nodes[frontier[i]].cost < minCost) {
minCost = problem.nodes[frontier[i]].cost;
minNode = frontier[i];
}
}
return minNode;
}
//Calculate the costs of the default graph and return a dictionary
//with the costs of all the nodes
var precomputedCosts = function() {
var graph = new DefaultGraph();
var problem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A');
var agent = new GraphAgent(problem);
while (problem.frontier.length > 0) {
let next = uniformCostSearch(problem);
agent.expand(next);
}
var costMap = {};
for (key in problem.nodes) {
let node = problem.nodes[key];
costMap[key] = node.cost;
}
return costMap;
}