@@ -239,7 +239,7 @@ def get_root(parents):
239
239
# return State(world)
240
240
241
241
242
- def show_search_tree (tree , fringe , explored , costs = None ):
242
+ def show_search_tree (tree , fringe , explored , costs = None , heuristic = None ):
243
243
state = explored [- 1 ] if explored else get_root (tree )
244
244
show_state (state )
245
245
for child , parent in tree .items ():
@@ -250,9 +250,18 @@ def show_search_tree(tree, fringe, explored, costs=None):
250
250
xs = [s .spaceship .col + 0.5 for s in explored ]
251
251
ys = [state .n - s .spaceship .row - 0.5 for s in explored ]
252
252
if costs :
253
- labels = [
254
- '{order}\n c={cost}' .format (order = i , cost = costs [s ])
255
- for i , s in enumerate (explored , start = 1 )]
253
+ if heuristic :
254
+ labels = [
255
+ '{order}\n {g}+{h}={f}' .format (
256
+ order = i ,
257
+ g = costs [s ],
258
+ h = heuristic [s ],
259
+ f = costs [s ]+ heuristic [s ])
260
+ for i , s in enumerate (explored , start = 1 )]
261
+ else :
262
+ labels = [
263
+ '{order}\n c={cost}' .format (order = i , cost = costs [s ])
264
+ for i , s in enumerate (explored , start = 1 )]
256
265
else :
257
266
labels = [str (i ) for i in range (1 ,len (explored )+ 1 )]
258
267
for label , x , y in zip (labels , xs , ys ):
@@ -269,6 +278,14 @@ def show_search_tree(tree, fringe, explored, costs=None):
269
278
labels = [
270
279
'{order}\n c={cost}' .format (order = '?' , cost = costs [s ])
271
280
for s in fringe ]
281
+ if heuristic :
282
+ labels = [
283
+ '{order}\n {g}+{h}={f}' .format (
284
+ order = '?' ,
285
+ g = costs [s ],
286
+ h = heuristic [s ],
287
+ f = costs [s ]+ heuristic [s ])
288
+ for s in fringe ]
272
289
for label , x , y in zip (labels , xs , ys ):
273
290
plt .text (
274
291
x , y , label ,
@@ -280,7 +297,8 @@ def show_search_tree(tree, fringe, explored, costs=None):
280
297
281
298
282
299
def create_tree_search_widget (explored_states , trees , fringes ,
283
- costs = None , interactive = False ):
300
+ costs = None , heuristic = None ,
301
+ interactive = False ):
284
302
if not trees :
285
303
print ('Zadne stromy k zobrazeni.' )
286
304
return
@@ -292,6 +310,7 @@ def show_search_tree_at(step):
292
310
tree ,
293
311
fringe = fringe ,
294
312
costs = costs [step ] if costs else None ,
313
+ heuristic = heuristic [step ] if heuristic else None ,
295
314
explored = explored_states [:step ])
296
315
297
316
if interactive :
@@ -313,17 +332,20 @@ def set_output(self, text=False, widget=False):
313
332
self .output_text = text
314
333
self .output_widget = widget
315
334
316
- def start_search (self , state , costs = False ):
335
+ def start_search (self , state , costs = False , heuristic = False ):
317
336
self .explored_states = []
318
337
self .trees = [{state : None }]
319
338
self .fringes = [set ([state ])]
320
339
self .costs = [{state : 0 }] if costs else None
340
+ # TODO: fix hodnota heuristiky v pocatecnim stavu
341
+ # (neni jasne jak, mozna post-fix po prvnim zalognuti)
342
+ self .heuristic = [{state : 0 }] if heuristic else None
321
343
self .log ('start search' )
322
344
323
345
def end_search (self , interactive = False ):
324
346
create_tree_search_widget (
325
347
self .explored_states , self .trees , self .fringes ,
326
- costs = self .costs ,
348
+ costs = self .costs , heuristic = self . heuristic ,
327
349
interactive = interactive )
328
350
329
351
def log (self , message ):
@@ -332,7 +354,7 @@ def log(self, message):
332
354
print ('{step}: {message}' .format (
333
355
step = step , message = message ))
334
356
335
- def log_search_step (self , explored_state , fringe , costs = None ):
357
+ def log_search_step (self , explored_state , fringe , costs = None , heuristic = None ):
336
358
# TODO: check type of states from fringe
337
359
fringe = set (state for state in fringe )
338
360
last_tree = self .trees [- 1 ]
@@ -350,18 +372,23 @@ def log_search_step(self, explored_state, fringe, costs=None):
350
372
if costs is None :
351
373
raise ValueError ('Zadejte i ceny stavu.' )
352
374
self .costs .append (deepcopy (costs ))
375
+ if self .heuristic :
376
+ if heuristic is None :
377
+ raise ValueError ('Zadejte i heuristiky.' )
378
+ self .heuristic .append (deepcopy (heuristic ))
353
379
self .log (str (fringe ))
354
380
355
381
356
382
LOGGER = Logger ()
357
383
LOGGER .set_output (text = False , widget = True )
358
384
359
385
@contextmanager
360
- def visualize_search (state , interactive = False , costs = False ):
361
- LOGGER .start_search (state , costs = costs )
386
+ def visualize_search (state , interactive = False , costs = False , heuristic = False ):
387
+ LOGGER .start_search (state , costs = costs , heuristic = heuristic )
362
388
yield
363
389
LOGGER .end_search (interactive = interactive )
364
390
365
391
366
- def log_search_step (explored_state , fringe , costs = None ):
367
- LOGGER .log_search_step (explored_state , fringe , costs = costs )
392
+ def log_search_step (explored_state , fringe , costs = None , heuristic = None ):
393
+ LOGGER .log_search_step (
394
+ explored_state , fringe , costs = costs , heuristic = heuristic )
0 commit comments