@@ -20,45 +20,51 @@ $(document).ready(function() {
2020 } ) . appendTo ( exploredQueueCanvas ) ;
2121 //Intial value for separation is 0
2222 $ ( '.ucsSeparation' ) . html ( 0 ) ;
23- var graph = new DefaultGraph ( ) ;
24- //Precompute costs of all nodes from the initial node
2523 var costMap = precomputedCosts ( ) ;
26- var graphProblem = new GraphProblem ( graph . nodes , graph . edges , 'A' , 'A' ) ;
27- //Change the text of all the nodes to its cost
28- for ( key in graphProblem . nodes ) {
29- graphProblem . nodes [ key ] . text = costMap [ graphProblem . nodes [ key ] . id ] ;
30- }
31- var graphAgent = new GraphAgent ( graphProblem , 'ucs' ) ;
24+
3225 var options = new DefaultOptions ( ) ;
3326 options . nodes . next . fill = 'hsla(126, 100%, 69%, 1)' ;
3427 options . edges . showCost = true ;
35- var graphDrawAgent = new GraphDrawAgent ( graphProblem , 'uniformCostSearchCanvas' , options , h , w ) ;
36- drawList ( priorityTwo , graphProblem . frontier , graphProblem , options , costMap ) ;
37- drawList ( exploredTwo , graphProblem . explored , graphProblem , options , costMap ) ;
38- var updateFunction = function ( ) {
39- if ( graphProblem . frontier . length > 0 ) {
40- var nextNode = uniformCostSearch ( graphProblem ) ;
41- graphAgent . expand ( nextNode ) ;
28+
29+ var drawState = function ( n ) {
30+ var graph = new DefaultGraph ( ) ;
31+ var graphProblem = new GraphProblem ( graph . nodes , graph . edges , 'A' , 'A' ) ;
32+ var graphAgent = new GraphAgent ( graphProblem ) ;
33+ for ( key in graphProblem . nodes ) {
34+ graphProblem . nodes [ key ] . text = costMap [ graphProblem . nodes [ key ] . id ] ;
35+ }
36+
37+ var graphDrawAgent = new GraphDrawAgent ( graphProblem , 'uniformCostSearchCanvas' , options , h , w ) ;
38+ var maxCost ;
39+ while ( n -- ) {
4240 if ( graphProblem . frontier . length > 0 ) {
43- graphProblem . nextToExpand = uniformCostSearch ( graphProblem ) ;
41+ var nextNode = uniformCostSearch ( graphProblem ) ;
42+ graphAgent . expand ( nextNode ) ;
43+ //If frontier is still present, find the next node to be expanded so it
44+ //could be colored differently
45+ if ( graphProblem . frontier . length > 0 ) {
46+ graphProblem . nextToExpand = uniformCostSearch ( graphProblem ) ;
47+ maxCost = graphProblem . nodes [ graphProblem . nextToExpand ] . cost ;
48+ } else {
49+ graphProblem . nextToExpand = null ;
50+ }
4451 } else {
45- graphProblem . nextToExpand = null ;
46- }
47- graphDrawAgent . iterate ( ) ;
48- drawList ( priorityTwo , graphProblem . frontier , graphProblem , options , costMap ) ;
49- drawList ( exploredTwo , graphProblem . explored , graphProblem , options , costMap ) ;
50- let maxCost = 0 ;
51- //Find the max cost which separates the explored from frontier nodes
52- if ( graphProblem . nextToExpand ) {
53- maxCost = graphProblem . nodes [ graphProblem . nextToExpand ] . cost ;
52+ break ;
5453 }
55- //Draw it in the front end
56- $ ( '.ucsSeparation' ) . html ( maxCost ) ;
57- } else {
58- clearInterval ( intervalFunction , DELAY ) ;
5954 }
55+ graphDrawAgent . iterate ( ) ;
56+ drawList ( priorityTwo , graphProblem . frontier , graphProblem , options , costMap ) ;
57+ drawList ( exploredTwo , graphProblem . explored , graphProblem , options , costMap ) ;
58+ //Draw it in the front end
59+ $ ( '.ucsSeparation' ) . html ( maxCost ) ;
6060 }
61- intervalFunction = setInterval ( updateFunction , DELAY ) ;
61+ let ac = new AnimationController ( {
62+ selector : '#ucsAC' ,
63+ min : 0 ,
64+ max : 15 ,
65+ renderer : drawState
66+ } ) ;
67+ ac . renderFirst ( ) ;
6268 } ;
6369 $ ( '#ucsRestartButton' ) . click ( init ) ;
6470 $ ( '#ucsWaiting' ) . css ( 'background-color' , 'hsl(0,50%,75%)' ) ;
@@ -81,7 +87,6 @@ function drawList(two, list, problem, options, costMap) {
8187 let text = two . makeText ( costMap [ node . id ] , x , y ) ;
8288 let fillColor = options . nodes [ state ] . fill ;
8389 circle . fill = fillColor ;
84-
8590 }
8691 two . update ( ) ;
8792}
0 commit comments