Skip to content

Commit cbd4b19

Browse files
Rishav159redblobgames
authored andcommitted
Fix bugs in ch3 - animation interval and ucs restart
Cost detail initial node fixed
1 parent 58b0efd commit cbd4b19

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

3-Solving-Problems-By-Searching/c_breadthFirstSearch.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ $(document).ready(function() {
22
var w = 600,
33
h = 350;
44
var DELAY = 2000;
5+
var intervalFunction = null;
56

67
function init() {
8+
clearInterval(intervalFunction, DELAY);
79
var graph = new DefaultGraph();
810
var graphProblem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A');
911
var graphAgent = new GraphAgent(graphProblem);
@@ -29,7 +31,7 @@ $(document).ready(function() {
2931
clearInterval(intervalFunction, DELAY);
3032
}
3133
}
32-
var intervalFunction = setInterval(updateFunction, DELAY);
34+
intervalFunction = setInterval(updateFunction, DELAY);
3335
};
3436
$('#bfsRestartButton').click(init);
3537
$('#fifoWaiting').css('background-color', 'hsl(200,50%,70%)');

3-Solving-Problems-By-Searching/c_costDetails.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ $(document).ready(function() {
3939
two.clear();
4040
path.path = path.path.reverse();
4141
let runningCost = 0;
42-
var i, x1, x2, y;
42+
var i, x1, x2, y = 20;
4343
for (i = 0; i < path.path.length - 1; i++) {
4444
x1 = i * 65 + 20;
4545
x2 = (i + 1) * 65 + 20;
@@ -113,6 +113,5 @@ $(document).ready(function() {
113113
bfsGraphDrawAgent = new GraphDrawAgent(graphProblem, 'no-costGraphCanvas', options, h, w);
114114
ucsGraphDrawAgent = new GraphDrawAgent(graphProblem, 'costGraphCanvas', options, h, w);
115115
};
116-
$('#ucsRestartButton').click(init);
117116
init();
118117
});

3-Solving-Problems-By-Searching/c_depthFirstSearch.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ $(document).ready(function() {
22
var w = 600,
33
h = 350;
44
var DELAY = 2000;
5+
var intervalFunction = null;
56

67
function init() {
8+
clearInterval(intervalFunction, DELAY);
79
var graph = new DefaultGraph();
810
var graphProblem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A');
911
var graphAgent = new GraphAgent(graphProblem);
@@ -26,7 +28,7 @@ $(document).ready(function() {
2628
clearInterval(intervalFunction, DELAY);
2729
}
2830
}
29-
var intervalFunction = setInterval(updateFunction, DELAY);
31+
intervalFunction = setInterval(updateFunction, DELAY);
3032
};
3133
$('#dfsRestartButton').click(init);
3234
$('#lifoWaiting').css('background-color', 'hsl(200,50%,70%)');

3-Solving-Problems-By-Searching/c_uniformCostSearch.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ $(document).ready(function() {
22
var w = 600,
33
h = 350;
44
var DELAY = 2000;
5+
var intervalFunction = null;
56

67
function init() {
7-
8+
clearInterval(intervalFunction, DELAY);
89
var priorityQueueCanvas = document.getElementById('priorityQueueCanvas');
910
var exploredQueueCanvas = document.getElementById('exploredPriorityQueueCanvas');
1011
priorityQueueCanvas.innerHTML = '';
@@ -43,13 +44,16 @@ $(document).ready(function() {
4344
graphDrawAgent.iterate();
4445
drawList(priorityTwo, graphProblem.frontier, graphProblem, options, costMap);
4546
drawList(exploredTwo, graphProblem.explored, graphProblem, options, costMap);
46-
let maxCost = graphProblem.nodes[graphProblem.nextToExpand].cost;
47+
let maxCost = 0;
48+
if (graphProblem.nextToExpand) {
49+
maxCost = graphProblem.nodes[graphProblem.nextToExpand].cost;
50+
}
4751
$('.ucsSeparation').html(maxCost);
4852
} else {
4953
clearInterval(intervalFunction, DELAY);
5054
}
5155
}
52-
var intervalFunction = setInterval(updateFunction, DELAY);
56+
intervalFunction = setInterval(updateFunction, DELAY);
5357
};
5458
$('#ucsRestartButton').click(init);
5559
$('#ucsWaiting').css('background-color', 'hsl(200,50%,70%)');

3-Solving-Problems-By-Searching/helpers.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var GraphNode = function(x, y, id, text) {
66
this.state = 'unexplored';
77
this.cost = Number.POSITIVE_INFINITY;
88
this.parent = null;
9+
this.depth = Number.POSITIVE_INFINITY;
910
};
1011

1112
function getEdgeCostLocation(x1, y1, x2, y2) {
@@ -115,6 +116,7 @@ var GraphProblem = function(nodes, edges, initialKey, nextToExpand) {
115116
this.nodes[initialKey].state = 'frontier';
116117
this.nodes[initialKey].cost = 0;
117118
this.nodes[initialKey].parent = null;
119+
this.nodes[initialKey].depth = 0;
118120
this.initialKey = initialKey;
119121
this.frontier = [initialKey];
120122
this.explored = [];
@@ -158,13 +160,12 @@ var GraphProblem = function(nodes, edges, initialKey, nextToExpand) {
158160
};
159161
};
160162

161-
var GraphAgent = function(problem) {
163+
var GraphAgent = function(problem,algo) {
162164
this.problem = problem;
163-
165+
this.algo = algo;
164166
this.expand = function(nodeKey) {
165167
this.problem.removeFromFrontier(nodeKey);
166168
this.problem.addToExplored(nodeKey);
167-
168169
let adjacent = this.problem.getAdjacent(nodeKey);
169170
for (var i = 0; i < adjacent.length; i++) {
170171
let nextNodeKey = adjacent[i].nodeKey;
@@ -173,10 +174,19 @@ var GraphAgent = function(problem) {
173174
this.problem.addToFrontier(nextNodeKey);
174175
nextNode.cost = adjacent[i].cost + this.problem.nodes[nodeKey].cost;
175176
nextNode.parent = nodeKey;
177+
nextNode.depth = this.problem.nodes[nodeKey].depth + 1;
176178
}
177-
if (nextNode.state == 'frontier' && nextNode.cost > adjacent[i].cost + this.problem.nodes[nodeKey].cost) {
178-
nextNode.cost = adjacent[i].cost + this.problem.nodes[nodeKey].cost;
179-
nextNode.parent = nodeKey;
179+
if(this.algo == 'ucs') {
180+
if (nextNode.state == 'frontier' && nextNode.cost > adjacent[i].cost + this.problem.nodes[nodeKey].cost) {
181+
nextNode.cost = adjacent[i].cost + this.problem.nodes[nodeKey].cost;
182+
nextNode.parent = nodeKey;
183+
}
184+
}
185+
if(this.algo == 'dfs') {
186+
if (nextNode.state == 'frontier' && nextNode.depth < (this.problem.nodes[nodeKey].depth + 1)) {
187+
nextNode.depth = this.problem.nodes[nodeKey].depth + 1;
188+
nextNode.parent = nodeKey;
189+
}
180190
}
181191
}
182192
};

0 commit comments

Comments
 (0)