-
Notifications
You must be signed in to change notification settings - Fork 2
/
heuristics.cc
1867 lines (1638 loc) · 54.1 KB
/
heuristics.cc
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "global.h"
#include "actions.h"
#include "algorithms.h"
#include "exceptions.h"
#include "graph.h"
#include "hash.h"
#include "heuristics.h"
#include "queue.h"
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <values.h>
#include <iostream>
#include <sstream>
/*******************************************************************************
*
* codification for atom lists up to a fixed size
*
******************************************************************************/
class codification_t
{
public:
static unsigned f_p( unsigned w )
{
return( ((w+1)*(w+1) - (w+1)) >> 1 );
}
static unsigned f_w( unsigned z )
{
double t = (1 + sqrt( 1 + 8*z )) / 2;
return( (unsigned)floor( t ) - 1 );
}
static unsigned f_x( unsigned z )
{
return( z - f_p( f_w( z ) ) );
}
static unsigned f_y( unsigned z )
{
return( f_p( f_w( z ) ) - z + f_w( z ) );
}
static unsigned code( unsigned x, unsigned y )
{
unsigned z = x*x + 2*x*y + y*y + 3*x + y;
return( z >> 1 );
}
static unsigned code( const atomList_t &alist );
static unsigned code( const ushort_t *array, size_t size );
static void decode( unsigned ucode, atomList_t &alist );
};
inline unsigned
codification_t::code( const atomList_t &alist )
{
assert( alist.size() < 8 );
unsigned ucode = 0;
if( alist.size() > 0 )
{
ucode = alist.atom( 0 );
for( size_t i = 1; i < alist.size(); ++i )
ucode = code( ucode, alist.atom( i ) );
if( ((ucode<<3)>>3) != ucode )
throw Exception( "precision overflow in codification_t::code(const atomList_t&)" );
ucode = alist.size() + (ucode<<3);
}
return( ucode );
}
inline unsigned
codification_t::code( const ushort_t *array, size_t size )
{
assert( size < 8 );
unsigned ucode = 0;
if( size > 0 )
{
ucode = array[0];
for( size_t i = 1; i < size; ++i )
ucode = code( ucode, array[i] );
if( ((ucode<<3)>>3) != ucode )
throw Exception( "precision overflow in codification_t::code(const ushort_t*,size_t)" );
ucode = size + (ucode<<3);
}
return( ucode );
}
inline void
codification_t::decode( unsigned ucode, atomList_t &alist )
{
alist.clear();
size_t size = ucode & 0x7;
ucode = ucode >> 3;
if( size == 1 )
alist.insert( ucode );
else if( size > 1 )
{
for( size_t i = 0; i < size - 1; ++i )
{
alist.insert( f_y( ucode ) );
ucode = f_x( ucode );
}
alist.insert( ucode );
}
}
/*******************************************************************************
*
* zero heuristic
*
******************************************************************************/
zeroHeuristic_t::zeroHeuristic_t( const problem_t &problem )
: heuristic_t(problem)
{
if( gpt::verbosity >= 500 )
std::cout << "<zero>: new" << std::endl;
}
zeroHeuristic_t::~zeroHeuristic_t()
{
if( gpt::verbosity >= 500 )
std::cout << "<zero>: deleted" << std::endl;
}
/*******************************************************************************
*
* min-min-lrtdp heuristic
*
******************************************************************************/
minMinLRTDPHeuristic_t::minMinLRTDPHeuristic_t( const problem_t &problem,
heuristic_t &heur )
: heuristic_t(problem), relaxation_(problem.weak_relaxation()), heur_(&heur)
{
hash_table_ = new hash_t( gpt::initial_hash_size, *heur_ );
algorithm_ = new lrtdp_t( relaxation_, *hash_table_, 0 );
if( gpt::verbosity >= 500 )
std::cout << "<min-min-lrtdp>: new" << std::endl;
}
minMinLRTDPHeuristic_t::~minMinLRTDPHeuristic_t()
{
delete algorithm_;
delete hash_table_;
problem_t::unregister_use( &relaxation_ );
if( gpt::verbosity >= 500 )
std::cout << "<min-min-lrtdp>: deleted" << std::endl;
}
double
minMinLRTDPHeuristic_t::value( const state_t &state )
{
double result = algorithm_->value( state );
if( gpt::verbosity >= 450 )
{
std::cout << "<min-min-lrtdp>: heuristic for " << state << " = " << result << std::endl;
}
return( result );
}
void
minMinLRTDPHeuristic_t::statistics( std::ostream &os ) const
{
}
/*******************************************************************************
*
* min-min-ida* heuristic
*
******************************************************************************/
minMinIDAHeuristic_t::minMinIDAHeuristic_t( const problem_t &problem,
heuristic_t &heur, bool useTT )
: heuristic_t(problem), relaxation_(problem.weak_relaxation()), heur_(&heur)
{
hash_table_ = new hash_t( gpt::initial_hash_size, *heur_ );
algorithm_ = new IDA_t( relaxation_, *hash_table_, useTT );
if( gpt::verbosity >= 500 )
std::cout << "<min-min-ida*>: new" << std::endl;
}
minMinIDAHeuristic_t::~minMinIDAHeuristic_t()
{
delete algorithm_;
delete hash_table_;
problem_t::unregister_use( &relaxation_ );
if( gpt::verbosity >= 500 )
std::cout << "<min-min-ida*>: deleted" << std::endl;
}
double
minMinIDAHeuristic_t::value( const state_t &state )
{
double result = algorithm_->value( state );
if( gpt::verbosity >= 450 )
{
std::cout << "<min-min-ida*>: heuristic for " << state << " = " << result << std::endl;
}
return( result );
}
void
minMinIDAHeuristic_t::statistics( std::ostream &os ) const
{
}
/*******************************************************************************
*
* dijkstra heuristic
*
******************************************************************************/
dijkstraHeuristic_t::dijkstraHeuristic_t( const problem_t &problem,
heuristic_t &heur, unsigned size )
: heuristic_t(problem), relaxation_(problem.weak_relaxation()), heur_(&heur), size_(size)
{
hash_table_ = new hash_t( gpt::initial_hash_size, *heur_ );
algorithm_ = new boundedDijkstra_t( relaxation_, *hash_table_, size );
if( gpt::verbosity >= 500 )
std::cout << "<dijkstra-" << size_ << ">: new" << std::endl;
}
dijkstraHeuristic_t::~dijkstraHeuristic_t()
{
delete algorithm_;
delete hash_table_;
problem_t::unregister_use( &relaxation_ );
if( gpt::verbosity >= 500 )
std::cout << "<dijkstra-" << size_ << ">: deleted" << std::endl;
}
double
dijkstraHeuristic_t::value( const state_t &state )
{
double result = algorithm_->value( state );
if( gpt::verbosity >= 450 )
{
std::cout << "<dijkstra-" << size_ << ">: heuristic for " << state << " = " << result << std::endl;
}
return( result );
}
void
dijkstraHeuristic_t::statistics( std::ostream &os ) const
{
}
/*******************************************************************************
*
* atom-min-1-forward heuristic
*
******************************************************************************/
atomMin1ForwardHeuristic_t::atomMin1ForwardHeuristic_t( const problem_t &problem )
: heuristic_t(problem), relaxation_(problem.strong_relaxation())
{
if( gpt::verbosity >= 500 )
std::cout << "<atom-min-1-forward>: new" << std::endl;
// generate random states and compute their heuristic
if( gpt::xtra > 0 )
{
// recover one initial states
const problem_t &relax = problem_.medium_relaxation();
std::pair<state_t*,Rational> *initial = new std::pair<state_t*,Rational>[DISP_INT_SIZE];
for( size_t i = 0; i < DISP_INT_SIZE; ++i )
initial[i].first = new state_t;
problem_.initial_states( initial );
for( size_t i = 0; i < gpt::xtra; ++i )
{
// set initial state
state_t state;
for( state_t::const_predicate_iterator ai = initial[0].first->predicate_begin(); ai != initial[0].first->predicate_end(); ++ai )
if( *ai % 2 == 0 )
state.add( *ai );
// apply random operators
for( size_t steps = 0; steps < 1250; ++steps )
for( actionList_t::const_iterator ai = relax.actionsT().begin(); ai != relax.actionsT().end(); ++ai )
if( (*ai)->enabled( state ) && (drand48() > 0.5) )
{
(*ai)->affect( state );
break;
}
// output init description and value
std::cout << " (:init";
for( state_t::const_predicate_iterator pi = state.predicate_begin(); pi != state.predicate_end(); ++pi )
{
std::cout << " ";
const Atom *atom = problem_t::atom_inv_hash_get( *pi );
if( !atom )
std::cout << "(int-" << (*pi) << ")";
else
problem_.print( std::cout, *atom );
}
std::cout << ")" << std::endl;
std::cout << "<state-value>: " << value( state ) << std::endl;
}
// clean
for( size_t i = 0; i < DISP_INT_SIZE; ++i )
delete initial[i].first;
delete[] initial;
exit( 0 );
}
}
atomMin1ForwardHeuristic_t::~atomMin1ForwardHeuristic_t()
{
problem_t::unregister_use( &relaxation_ );
if( gpt::verbosity >= 500 )
std::cout << "<atom-min-1-forward>: deleted" << std::endl;
}
double
atomMin1ForwardHeuristic_t::value( const state_t &state )
{
actionList_t::const_iterator it;
double result;
state_t current( state );
size_t steps = 0;
bool change = true;
relaxation_.complete_state( current );
while ( change && !relaxation_.goalT().holds( current ) )
{
++steps;
change = false;
for( it = relaxation_.actionsT().begin(); it != relaxation_.actionsT().end(); ++it )
if( (*it)->enabled( current, relaxation_.nprec() ) )
{
change = (*it)->affect( current, relaxation_.nprec() ) || change;
}
}
if( relaxation_.goalT().holds( current, relaxation_.nprec() ) )
result = (double)steps;
else
result = gpt::dead_end_value;
if( gpt::verbosity >= 450 )
{
std::cout << "<atom-min-1-forward>: heuristic for " << state << " = " << result << std::endl;
}
if( (result < gpt::dead_end_value) && problem_.goal_atom() )
result = result - 1;
if( result < gpt::dead_end_value )
result = result * gpt::heuristic_weight;
return( result );
}
void
atomMin1ForwardHeuristic_t::statistics( std::ostream &os ) const
{
}
/*******************************************************************************
*
* atom-min-<m>-forward heuristic
*
******************************************************************************/
atomMinMForwardHeuristic_t::atomMinMForwardHeuristic_t( const problem_t &problem,
size_t m )
: heuristic_t(problem), m_(m), relaxation_(problem.medium_relaxation())
{
array_ = (ushort_t*)malloc( m_ * sizeof(ushort_t) );
if( gpt::verbosity >= 500 )
std::cout << "<atom-min-" << m_ << "-forward>: new" << std::endl;
}
atomMinMForwardHeuristic_t::~atomMinMForwardHeuristic_t()
{
free( array_ );
problem_t::unregister_use( &relaxation_ );
if( gpt::verbosity >= 500 )
std::cout << "<atom-min-" << m_ << "-forward>: deleted" << std::endl;
}
double
atomMinMForwardHeuristic_t::value( const state_t &state )
{
static atomList_t alist, slist;
static actionList_t aset;
static std::vector<unsigned> ssets, gssets;
static priorityQueue_t<std::pair<unsigned,ushort_t>,pairINDEX> PQ;
static std::map<unsigned,ushort_t> map;
static std::map<unsigned,ushort_t>::const_iterator it;
static state_t::const_predicate_iterator ai;
// base case
if( problem_.goalT().holds( state ) ) return( 0 );
// initialize goal subsets
gssets.clear();
subsets( m_, relaxation_.goalT().atom_list( 0 ), 0, array_, 0, gssets );
// transform state into atom list
slist.clear();
for( ai = state.predicate_begin(); ai != state.predicate_end(); ++ai )
slist.insert( *ai );
// insert initial m-subsets into priority queue
map.clear();
ssets.clear();
subsets( m_, slist, 0, array_, 0, ssets );
for( size_t i = 0; i < ssets.size(); ++i )
{
std::pair<unsigned,ushort_t> p( ssets[i], 0 | OPEN );
PQ.push( p );
map[p.first] = p.second;
}
// apply Dijkstra's to atom space
size_t count = 0;
while( !PQ.empty() )
{
std::pair<unsigned,ushort_t> p = PQ.top();
PQ.pop();
p.second = (p.second & 0xFF) | CLOSED;
map[p.first] = p.second;
codification_t::decode( p.first, alist );
if( gpt::verbosity >= 500 )
{
std::cout << "<atom-min-" << m_ << "-forward>: " << m_
<< "-subset = " << p.first << alist
<< ", value = " << (p.second & 0xFF) << std::endl;
}
// check if done
for( size_t i = 0; i < gssets.size(); ++i )
if( gssets[i] == p.first )
{
++count;
break;
}
if( count == gssets.size() ) break;
// expand
actionList_t::const_iterator ai = relaxation_.actionsT().begin();
for( ; ai != relaxation_.actionsT().end(); ++ai )
{
// check precondition
size_t i = 0;
assert( (*ai)->precondition().size() == 1 );
while( i < (*ai)->precondition().atom_list( 0 ).size() )
if( !alist.holds( (*ai)->precondition().atom_list( 0 ).atom( i++ ) ) )
break;
if( i < (*ai)->precondition().atom_list( 0 ).size() ) continue;
const deterministicAction_t *action = (const deterministicAction_t*)(*ai);
ushort_t cost = (p.second & 0xFF) + 1; // 1 should be (*ai)'s cost
atomList_t progress( alist );
// add add-list
for( i = 0; i < action->effect().s_effect().add_list().size(); ++i )
progress.insert( action->effect().s_effect().add_list().atom( i ) );
// remove del-list
for( i = 0; i < action->effect().s_effect().del_list().size(); ++i )
progress.remove( action->effect().s_effect().del_list().atom( i ) );
// insert m-subsets into priority queue
ssets.clear();
subsets( m_, progress, 0, array_, 0, ssets );
for( i = 0; i < ssets.size(); ++i )
{
it = map.find( ssets[i] );
if( (it == map.end()) ||
((((*it).second & 0xFF00) == OPEN) && (cost < ((*it).second & 0xFF))) )
{
if( it != map.end() ) PQ.remove( *it );
std::pair<unsigned,ushort_t> q( ssets[i], cost | OPEN );
PQ.push( q );
map[q.first] = q.second;
}
}
}
}
// compute value
double result = 0;
for( size_t i = 0; i < gssets.size(); ++i )
if( (it = map.find( gssets[i] )) != map.end() )
{
result = MAX(result,((*it).second & 0xFF));
}
else
{
result = gpt::dead_end_value;
break;
}
if( gpt::verbosity >= 450 )
{
std::cout << "<atom-min-" << m_ << "-forward>: heuristic for " << state << " = " << result << std::endl;
}
if( (result < gpt::dead_end_value) && problem_.goal_atom() )
result = result - 1;
if( result < gpt::dead_end_value )
result = result * gpt::heuristic_weight;
return( result );
}
void
atomMinMForwardHeuristic_t::statistics( std::ostream &os ) const
{
}
void
atomMinMForwardHeuristic_t::subsets( size_t m, const atomList_t &alist,
size_t i, ushort_t *array, size_t j,
std::vector<unsigned> &ssets ) const
{
if( (j == m) || (i == alist.size()) )
{
if( (j == m) || (j == alist.size()) )
{
unsigned ucode = codification_t::code( array, j );
ssets.push_back( ucode );
}
}
else
{
// insert ith atom into subset
array[j] = alist.atom( i );
subsets( m, alist, i+1, array, j+1, ssets );
// skip ith atom
subsets( m, alist, i+1, array, j, ssets );
}
}
/*******************************************************************************
*
* atom-min-<m>-backward heuristic
*
******************************************************************************/
#define _MTX(n,i,j) (n*(i) + (j))
#define MTX(n,i,j) _MTX(n,MIN(i,j),MAX(i,j))
#define LOW(n,m) ((m)/n)
#define HIGH(n,m) ((m)%n)
static const atomMinMBackwardHeuristic_t *heuristic_ = NULL;
static const problem_t *problem_ = NULL;
static const atomList_t *seeds_ = NULL;
static bool
is_cover( atomList_t &clique )
{
static atomList_t orbit;
orbit.clear();
for( size_t i = 0; i < clique.size(); ++i )
orbit.insert( clique.atom( i ) << 1 );
return( !orbit.empty_intersection( *seeds_ ) && heuristic_->is_cover( *problem_, orbit ) );
}
void
atomMinMBackwardHeuristic_t::system_t::compute_fmask( void )
{
fmask_.clear();
for( size_t i = 0; i < focus().size(); ++i )
fmask_.insert( focus().atom_list( i ) );
}
void
atomMinMBackwardHeuristic_t::system_t::compute_bmask( void )
{
compute_fmask();
bmask_.insert( fmask_ );
for( size_t i = 0; i < base().size(); ++i )
bmask_.insert( base().atom_list( i ) );
}
unsigned
atomMinMBackwardHeuristic_t::system_t::value( const atomList_t &alist ) const
{
atomList_t proj = alist;
proj.intersect( bmask() );
// complete projection (if necessary)
unsigned partial = gpt::dead_end_value;
bool completion = false;
for( size_t i = 0; i < base().size(); ++i )
if( proj.empty_intersection( base().atom_list( i ) ) )
{
if( completion )
throw Exception( "sorry, more than one background orbit not yet supported" );
for( size_t j = 0; j < base().atom_list(i).size(); ++j )
{
completion = true;
proj.insert( base().atom_list(i).atom( j ) );
unsigned val = value_aux( proj );
partial = MIN(partial,val);
proj.remove( base().atom_list(i).atom( j ) );
}
}
return( completion ? partial : value_aux( proj ) );
}
void
atomMinMBackwardHeuristic_t::system_t::print( std::ostream &os ) const
{
os << "[ {";
for( size_t i = 0; i < focus().size(); ++i )
os << " " << focus().atom_list( i );
os << " } {";
for( size_t i = 0; i < base().size(); ++i )
os << " " << base().atom_list( i );
os << " } {";
for( size_t i = 0; i < frame().size(); ++i )
os << " " << frame().atom_list( i );
os << " } ]";
}
atomMinMBackwardHeuristic_t::atomMinMBackwardHeuristic_t( const problem_t &problem, size_t m )
: heuristic_t(problem), m_(m), relaxation_(problem.medium_relaxation())
{
std::vector<std::pair<const atomList_t*,ushort_t> >::const_iterator ei;
std::map<const std::string,system_t*>::iterator si;
problem_t::no_more_atoms();
generate_systems();
compute_databases();
// compute heuristic using Dijkstra's
std::vector<std::pair<const atomList_t*,ushort_t> > expansion;
priorityQueue_t<std::pair<const atomList_t*,ushort_t>,pairINDEX> PQ;
for( si = systems_.begin(); si != systems_.end(); ++si )
if( (*si).second->support() )
{
system_t *system = (*si).second;
std::cout << "computing pattern database for " << (*si).first << " ... ";
std::cout.flush();
// seed is goal
atomList_t state = relaxation_.goalT().atom_list( 0 );
state.intersect( system->bmask() );
// complete seed with base atoms and insert into PQ
assert( PQ.empty() );
bool completion = false;
for( size_t i = 0; i < system->base().size(); ++i )
if( state.empty_intersection( system->base().atom_list( i ) ) )
{
if( completion )
throw Exception( "sorry, more than one background orbit not yet supported" );
for( size_t j = 0; j < system->base().atom_list(i).size(); ++j )
{
completion = true;
atomList_t *seed = new atomList_t( state );
seed->insert( system->base().atom_list(i).atom( j ) );
std::pair<const atomList_t*,ushort_t> p( seed, 0 | OPEN );
PQ.push( p );
system->hash().insert( p );
}
}
if( !completion )
{
const atomList_t *seed = new atomList_t( state );
std::pair<const atomList_t*,ushort_t> p( seed, 0 | OPEN );
PQ.push( p );
system->hash().insert( p );
}
// apply Dijkstra's
while( !PQ.empty() )
{
std::pair<const atomList_t*,ushort_t> p = PQ.top();
PQ.pop();
p.second = (p.second & 0xFF) | CLOSED;
hash_t::iterator it = system->hash().find( p.first );
(*it).second = p.second;
if( gpt::verbosity >= 500 )
{
std::cout << "<atom-min-" << m_ << "-backward>: " << *p.first
<< ", value = " << (p.second & 0xFF) << std::endl;
}
// expand
regression( *p.first, p.second & 0xFF, *system, expansion );
for( ei = expansion.begin(); ei != expansion.end(); ++ei )
{
hash_t::iterator it = system->hash().find( (*ei).first );
if( it == system->hash().end() )
{
std::pair<const atomList_t*,ushort_t> q( (*ei).first, (*ei).second | OPEN );
PQ.push( q );
system->hash().insert( q );
}
else if( (((*it).second & 0xFF00) == OPEN) && ((*ei).second < ((*it).second & 0xFF)) )
{
PQ.remove( *it );
system->hash().erase( it, ++it );
std::pair<const atomList_t*,ushort_t> q( (*ei).first, (*ei).second | OPEN );
PQ.push( q );
system->hash().insert( q );
}
}
}
std::cout << "size = " << system->hash().size() << std::endl;
}
// generate random states and compute their heuristic
if( gpt::xtra > 0 )
{
// recover one initial states
std::pair<state_t*,Rational> *initial = new std::pair<state_t*,Rational>[DISP_INT_SIZE];
for( size_t i = 0; i < DISP_INT_SIZE; ++i )
initial[i].first = new state_t;
problem_.initial_states( initial );
::hash_t *htable = new ::hash_t( gpt::initial_hash_size, *this );
algorithm_t *ida = new IDA_t( relaxation_, *htable, true );
for( size_t i = 0; i < gpt::xtra; ++i )
{
// set initial state
state_t state;
for( state_t::const_predicate_iterator ai = initial[0].first->predicate_begin(); ai != initial[0].first->predicate_end(); ++ai )
if( *ai % 2 == 0 )
state.add( *ai );
// apply random operators
for( size_t steps = 0; steps < 1250; ++steps )
for( actionList_t::const_iterator ai = relaxation_.actionsT().begin(); ai != relaxation_.actionsT().end(); ++ai )
if( (*ai)->enabled( state ) && (drand48() > 0.5) )
{
(*ai)->affect( state );
break;
}
// output init description and value
std::cout << " (:init";
for( state_t::const_predicate_iterator pi = state.predicate_begin(); pi != state.predicate_end(); ++pi )
{
std::cout << " ";
const Atom *atom = problem_t::atom_inv_hash_get( *pi );
if( !atom )
std::cout << "(int-" << (*pi) << ")";
else
problem_.print( std::cout, *atom );
}
std::cout << ")" << std::endl;
std::cout << "<state-value>: " << value( state ) << std::endl;
std::cout << "<solution*>: " << ida->value( state ) << std::endl;
}
// clean
for( size_t i = 0; i < DISP_INT_SIZE; ++i )
delete initial[i].first;
delete[] initial;
}
}
atomMinMBackwardHeuristic_t::~atomMinMBackwardHeuristic_t()
{
problem_t::unregister_use( &relaxation_ );
for( std::map<const std::string,system_t*>::const_iterator si = systems_.begin(); si != systems_.end(); ++si )
delete (*si).second;
for( std::map<const std::string,std::pair<const atomList_t*,bool> >::const_iterator oi = orbits_.begin(); oi != orbits_.end(); ++oi )
delete (*oi).second.first;
if( gpt::verbosity >= 500 )
std::cout << "<atom-min-" << m_ << "-backward>: deleted" << std::endl;
}
double
atomMinMBackwardHeuristic_t::value( const state_t &state )
{
atomList_t alist;
for( state_t::const_predicate_iterator pi = state.predicate_begin(); pi != state.predicate_end(); ++pi )
alist.insert( *pi );
unsigned result = 0;
std::map<const std::string,std::vector<system_t*>*>::const_iterator dbi;
for( dbi = databases_.begin(); dbi != databases_.end(); ++dbi )
{
unsigned val = value( *(*dbi).second, alist );
result = MAX(result,val);
if( result == gpt::dead_end_value ) break;
}
if( gpt::verbosity >= 450 )
{
std::cout << "<atom-min-" << m_ << "-backward>: heuristic for " << state << " = " << result << std::endl;
}
if( (result < gpt::dead_end_value) && problem_.goal_atom() )
result = result - 1;
if( result < gpt::dead_end_value )
result = result * (unsigned)gpt::heuristic_weight;
return( result );
}
void
atomMinMBackwardHeuristic_t::statistics( std::ostream &os ) const
{
}
unsigned
atomMinMBackwardHeuristic_t::value( const std::vector<system_t*> &database, const atomList_t &alist ) const
{
unsigned result = 0;
for( size_t i = 0; i < database.size(); ++i )
{
unsigned val = database[i]->value( alist );
if( val == gpt::dead_end_value )
return( gpt::dead_end_value );
else
result += val;
}
return( result );
}
bool
atomMinMBackwardHeuristic_t::is_cover( const problem_t &problem, const atomList_t &orbit ) const
{
// check that if an atom from orbit is removed by an action, another is added
for( actionList_t::const_iterator ai = problem.actionsT().begin(); ai != problem.actionsT().end(); ++ai )
{
const deterministicAction_t *a = (const deterministicAction_t*)(*ai);
if( !orbit.empty_intersection( a->effect().s_effect().del_list() ) &&
orbit.empty_intersection( a->effect().s_effect().add_list() ) )
return( false );
}
return( true );
}
bool
atomMinMBackwardHeuristic_t::prune( const atomList_t &alist, const system_t &system ) const
{
for( size_t i = 0; i < system.frame().size(); ++i )
if( alist.intersection_size( system.frame().atom_list( i ) ) > 1 )
return( true );
return( false );
}
void
atomMinMBackwardHeuristic_t::regression( const atomList_t &alist,
ushort_t cost,
const system_t &system,
std::vector<std::pair<const atomList_t*,ushort_t> > &expansion ) const
{
// find all actions such that: (1) add(a) \cap alist != empty,
// (2) del(a) \cap alist = empty
//std::cout << "begin expansion of " << alist << std::endl;
expansion.clear();
atomList_t prec, add, del, result;
for( actionList_t::const_iterator ai = relaxation_.actionsT().begin(); ai != relaxation_.actionsT().end(); ++ai )
{
const deterministicAction_t *action = (const deterministicAction_t*)(*ai);
prec = action->precondition().atom_list( 0 );
add = action->effect().s_effect().add_list();
del = action->effect().s_effect().del_list();
prec.intersect( system.bmask() );
add.intersect( system.bmask() );
del.intersect( system.bmask() );
if( !alist.empty_intersection( add ) && alist.empty_intersection( del ) )
{
result = alist;
result.insert( prec );
result.remove( add );
if( !prune( result, system ) )
{
ushort_t ncost = cost;
if( !add.empty_intersection( system.fmask() ) || !del.empty_intersection( system.fmask() ) )
++ncost;
//std::cout << " " << (*ai)->name() << ":" << result << ":" << ncost << std::endl;
std::pair<const atomList_t*,ushort_t> p( new atomList_t( result ), ncost );
expansion.push_back( p );
}
}
}
//std::cout << "end" << std::endl;
}
void
atomMinMBackwardHeuristic_t::compute_mutexes( atomList_t &reachable,
std::vector<unsigned> &mutexes,
std::pair<state_t*,Rational> *initial ) const
{
actionList_t::const_iterator ai;
// initialize bitmap
unsigned natoms = problem_t::number_atoms();
unsigned size = natoms * (natoms - 1);
char *bitmap = (char*)calloc( size/8, sizeof(char) );
// initialization
std::vector<unsigned> initial_mutexes;
#if 1
// method 1: include all pairs not in s_0
for( size_t i = 0; i < reachable.size(); ++i )
for( size_t j = i+1; j < reachable.size(); ++j )
{
bool found = false;
ushort_t p = reachable.atom( i );
ushort_t q = reachable.atom( j );
for( size_t k = 0; initial[k].second != Rational( -1 ); ++k )
if( initial[k].first->holds(p) && initial[k].first->holds(q) )
{
found = true;
break;
}
if( !found )
{
unsigned m = MTX(natoms,p,q);
initial_mutexes.push_back( m );
bitmap[m/8] = bitmap[m/8] | (1<<(m%8));
}
}
#else
// method 2: unions of two sets M_a and M_b.
// M_a = initial_mutexes = pairs {p,q} not in s_0 such some action add p and deletes q
for( ai = relaxation_.actionsT().begin(); ai != relaxation_.actionsT().end(); ++ai )
{
const deterministicAction_t *a = (const deterministicAction_t*)(*ai);
for( size_t i = 0; i < a->effect().s_effect().add_list().size(); ++i )
{
ushort_t p = a->effect().s_effect().add_list().atom( i );
if( (p % 2 == 0) && reachable.find(p) )
{
for( size_t j = 0; j < a->effect().s_effect().del_list().size(); ++j )
{
ushort_t q = a->effect().s_effect().del_list().atom( j );
unsigned m = MTX(natoms,p,q);
if( (q % 2 == 0) && reachable.find(q) && !(bitmap[m/8] & (1<<(m%8))) )
{
bool found = false;
for( size_t k = 0; initial[k].second != Rational( -1 ); ++k )
if( initial[k].first->holds(p) && initial[k].first->holds(q) )
{
found = true;
break;
}
if( !found )
{
initial_mutexes.push_back( m );
bitmap[m/8] = bitmap[m/8] | (1<<(m%8));
}
}
}
}
}
}
#if 0
// M_b = pairs {r,q} such that a {p,q} is in M_a and exists action with r in PREC and p in ADD
std::vector<unsigned> mutexes_b;
for( ai = relaxation_.actionsT().begin(); ai != relaxation_.actionsT().end(); ++ai )
{
const deterministicAction_t *a = (const deterministicAction_t*)(*ai);
for( size_t i = 0; i < a->precondition().atom_list(0).size(); ++i )
{
ushort_t r = a->precondition().atom_list(0).atom( i );
if( (r % 2 == 0) && reachable.find(r) )
{