• Ingen resultater fundet

Konklusion

In document Planlægning med LEGO agenter (Sider 70-90)

I dette kapitel har vi kreeret en konceptløning p˚ a hvorledes multiagenter i LEGO verdenen kan h˚ andteres. Systemet har implementeret en modificeret B-DI arkitektur, s˚ aledes at agenterne kan operere i en dynamisk skiftende verden og stadig benytte POP* i planlægningsmodulet. Vi har analyseret systemet for hvorledes deadlocks og ressourcestyring vil kunne benyttes.

Til dette har vi lavet en overordnet system der uddelegere opgaver,

vedlighold-er en repræsentation af vvedlighold-erden, styrvedlighold-er ressourcvedlighold-er og ovvedlighold-erv˚ ager deadlocks.

Ud-videlser kan med fordel tilføjes systemet og af dem vi har nævnt, vil det ikke

umiddelbart kræve en revision af den basale platform.

Kapitel 5

Konklusion

Vi har udviklet og implementeret en kunstig intelligens, der kan generer en handlingssekvens til at opn˚ a givne betingelser. Til dette har vi designet og im-plementeret et delvist rangeret planlægningsmodul, der understøtter STRIPS.

Vi har undervejs afprøvet planlægningen og eksekvering p˚ a LEGO Mindstorm NXT robotter.

Vi har vist hvorledes planlægningsmodulet kan anvendes til at udgøre planlægn-ingsfasen i agentstyring. Det er blevet demonstreret ved at inkorporere POP i BDI arkitetktturen. Endvidere har vi undersøgt teoretiske aspekter ved flere agenter der opererer i samme miljø.

Vi har undervejs konstateret at det kræver meget nøje design af handlinger og betingelser for at planlægningen bliver konsistent. STRIPS er intuitivt at forst˚ a, men dets resulterende planrums grafer er ganske komplekse. Et højere abstraktionsniveauet ville udgøre en stor fordel for designeren.

LEGO verdenen har vist sig ikke at være id´ eel til STRIPS planlægning. Omvendt kunne man konkludere at STRIPS, uden nødvendige udvidelser, ikke er alsidig nok til let at p˚ aføre selv en simpel LEGO verden. Udvidelserne ville skulle starte med en revision af beskrivelsessproget.

Id´ eer og design af POP, BDI strukturen og agentkoordineringen kan med fordel

videreudvikles og benyttes i fremtidigt arbejde.

Litteratur

[1] P. B. Galvin A. Silberschatz, J. L. Peterson. Operating System Concepts trd. edi., pages 195–224.

[2] BenDi. Priority queue.

http://www.codeproject.com/csharp/PriorityQueue.asp.

[3] Tom Bylander. The computational complexity of propositional strips plan-ning. Technical report, Matematics, Computer Science, and Statstics, 1994.

[4] Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest. Introduc-tion to Algorithms. MIT Press/McGraw-Hill, second ediIntroduc-tion, 2001.

[5] Edsger W. Dijkstra. Bankers algorithm.

http://www.cs.utexas.edu/users/EWD/ewd06xx/EWD623.PDF.

[6] LEGO. Lego mindstorm nxt tribot.

http://mindstorms.lego.com/Overview/MTR_Tribot.aspx.

[7] Anders Lemke, Johan Lidlaw, and Lars Zilmer-Pedersen. Developing multi-agent lego robotics. Technical report, IMM, 2007.

[8] Steven Minton, John Bresina, and Mark Drummond. Total-order and partial-order planning: A comparative analysis. Technical report, NASA Ames Research Center, 1994.

[9] XuanLong Nguyen and Subbarao Kambhampati. Reviving partial order planning. In IJCAI, pages 459–466, 2001.

[10] Prabha Ramakrishnan. Intelligent control of autonomous underwater

vehi-cles - a partial order planner for the orca project. Master’s thesis, University

of Maine System, 1997.

[11] Stuart J. Russell and Peter Norvig. Artificial Intelligence - A Modern Ap-proach. Prentice Hall International Series in Artificial Intelligence. Prentice Hall, 2003.

[12] Daniel S. Weld. An introduction to least commitment planning. AI Maga-zine, 15(4):27–61, 1994.

[13] Wikipedia. Partially ordered set.

http://en.wikipedia.org/wiki/Partially_ordered_set.

[14] Wikipedia. Strips.

http://en.wikipedia.org/w/index.php?title=STRIPS&oldid=

121703234.

[15] Wikipedia. Unit testing.

http://en.wikipedia.org/wiki/Unit_testing.

[16] Michael Wooldridge. Introduction to MultiAgent Systems. John Wiley and Sons, 2002.

[17] Michael Wooldridge and Nicholas R. Jennings. Intelligent agents: Theory and practice. Knowledge Engineering Review, 10(2):115–152, 1995.

[18] Michael J. Wooldridge. Reasoning about Rational Agents. The MIT Press,

Cambridge, Massachusetts, 2000.

Bilag A

Pseudokode

I følgende listes pseudokode for forskellige algoritmer der har været benyttet i

programmet.

1:

procedure DFS (G)

2:

for each vertex u V[G] do

3:

color[u] := WHITE;

4:

π[u] := NIL; . π is node u’s parent

5:

end for

6:

for each vertex u V[G] do

7:

if color[u] = WHITE then

8:

DFS-VISIT(u);

9:

end if

10:

end for

11:

end procedure

1:

procedure DFS-VISIT (u)

2:

color[u] := GREY; . White vertex has just been discovered

3:

for each v Adj[u] do . Explore edge(u, v)

4:

if color[v] = GREY then

5:

PRINT ”Back-Edge Found”;

6:

terminate DFS;

7:

else if color[v] = WHITE then

8:

π[v] := u;

9:

DFS-VISIT(v);

10:

end if

11:

end for

12:

color[u] := BLACK; . Blacken u; it is finished

13:

end procedure

Depth First Search

67

1:

procedure Topological Sort (Q)

2:

Q ← Set of all nodes with no incoming edges

3:

while Q is non-empty do

4:

remove a node n from Q

5:

output n

6:

for each node m with an edge e from n to m do

7:

remove edge e from the graph

8:

if m has no other incoming edges then

9:

insert m into Q

10:

end if

11:

end for

12:

end while

13:

if graph has edges then

14:

output error message (graph has a cycle)

15:

end if

16:

end procedure

Topological Sort

1:

procedure Max-heapify (A, i)

2:

l ← LEF T (i);

3:

r ← RIGHT (i);

4:

if l ≤ heap-size[A] and A[l] > A[i] then

5:

largest ← l;

6:

elselargest ← i;

7:

end if

8:

if r ≤ heap-size[A] and A[r] > A[largest] then

9:

largest ← r;;

10:

end if

11:

if largest 6= i then

12:

exchange A[i] ↔ A[largest];

13:

end if

14:

Max-Heapify(A, largest);

15:

end procedure

1:

procedure Heap-Maximum (A)

2:

return A[1];

3:

end procedure

1:

procedure Heap-Extract-Max (A)

2:

if heap-size[A] < 1 then

3:

print error ’heap underflow’;

4:

end if

5:

max ← A[1];

6:

A[1] ← A[heap-size[A]];

7:

heap-size[A] ← heap-size[A] − 1;

8:

Max-Heapify(A, 1);

9:

return max;

10:

end procedure

1:

procedure Heap-increase-key (A, i, key)

2:

if key < A[i] then

3:

print error ’new key is smaller than current key’;

4:

end if

5:

A[i] ← key;

6:

while i > 1 and A[P AREN T (i)] < A[i] do

7:

exchange A[i] ↔ A[P AREN T (i)];

8:

i ← P AREN T(i);

9:

end while

10:

end procedure

1:

procedure Max-Heap-Insert (A, key)

2:

heap-size[A] ← heap-size[A] + 1;

3:

A[heap-size[A]] ← −∞

4:

Heap-Increase-Key(A, heap-size[A], key);

5:

end procedure

Max Priority Queue

Bilag B

Grafisk brugergrænseflade

Bilag C

Kausal link graf

Følgende graf viser et hvordan kausal link dannes i eksemplet. Bindingerne til

Start og alternative bindinger, vises som sm˚ a pile for at lette overskueligheden.

Bilag D

Kildekode

D.1 Plan.cs

1 using System ;

2 using System . C o l l e c t i o n s . G e n e r i c ;

3 using System . Text ;

4 using M u l t i R o b o t i c A g e n t s P l a n n i n g ;

5

6 namespace M u l t i R o b o t i c A g e n t s P l a n n i n g

7 {

8 public c l a s s Plan : IComparable , IComparable<Plan>

9 {

10 // L i s t o f a c t i o n s u s e d i n t h e p l a n

11 public L i s t<A c t i o n> a c t i o n s = new MyList<A c t i o n>() ;

12

13 // L i s t o f p r e v i o u s a c t i o n s a s a l i n k

14 public L i s t<Link> p r e v i o u s A c t i o n s = new MyList<Link>() ;

15

16 // The S t a r t A c t i o n

17 public A c t i o n S t a r t A c t i o n ;

18

19 // L i s t o f c a u s a l l i n k s

20 public L i s t<C a u s a l L i n k> C a u s a l L i n k s = new MyList<C a u s a l L i n k>() ;

21

22 // L i s t o f o r d e r i n g p a i r s .

23 // The k e y node h a s a d i r e c t e d e d g e t o a l l o f t h e n o d e s i n t h e l i s t

24 public D i c t i o n a r y<A c t i o n , L i s t<A c t i o n>> o r d e r s = new D i c t i o n a r y

<A c t i o n , L i s t<A c t i o n>>() ;

25

26 // A l l c o n d i t i o n s t h e r e a r e bound t o S t a r t A c t i o n ’ s c o n d i t i o n s

27 public D i c t i o n a r y<A c t i o n , L i s t<C o n d i t i o n>>

E s s e n t i e l S t a r t C o n d i t i o n s = new D i c t i o n a r y<A c t i o n , L i s t<

C o n d i t i o n>>() ;

28

29 /∗

30 ∗ A s e t o f open p r e c o n d i t i o n s . A p r e c o n d i t i o n i s open i f i t i s n o t a c h i e v e d by some a c t i o n PRECONDITIONS i n t h e p l a n .

31 ∗ P l a n n e r s w i l l work t o r e d u c e t h e s e t o f open p r e c o n d i t i o n s t o t h e empty s e t , w i t h o u t i n t r o d u c i n g a c o n t r a d i c t i o n .

32 ∗/

33 public L i s t<Link> O p e n P r e c o n d i t i o n s =new MyList<Link>() ;

34

35 // C o n s t r u c t o r

36 public Plan ( A c t i o n S t a r t A c t i o n , A c t i o n F i n i s h A c t i o n , L i s t<

C o n d i t i o n> F i n i s h C o n d i t i o n s )

37 {

38 t h i s. S t a r t A c t i o n = S t a r t A c t i o n ;

39

40 // Add t h e f i r s t a c t i o n t o a c t i o n s

41 a c t i o n s . Add ( F i n i s h A c t i o n ) ;

42

43 // P l a c e a l l c o n d i t i o n s from F i n i s h i n t h e O p e n P r e c o n d i t i o n l i s t

44 foreach ( C o n d i t i o n c in F i n i s h C o n d i t i o n s )

45 {

46 Lin k F i n i s h L i n k =new Lin k (null, c , F i n i s h A c t i o n ) ;

47 O p e n P r e c o n d i t i o n s . Add ( F i n i s h L i n k ) ;

48 }

49

50 // Add t h e i n i t i a l o r d e r S t a r t −> F i n i s h

51 AddOrder ( S t a r t A c t i o n , F i n i s h A c t i o n ) ;

52 }

53

54 // P r i v a t e c o n s t r u c t o r , u s e d by Clone ( )

55 private Plan ( )

56 {

57 }

58

59 /∗

60 ∗ Add a o r d e r i n g c o n s t r a i n t s o n l y i f t h e r e d o e s n o t e x i s t s a c y c l e a f t e r

61 ∗ There i s u s i n g a DFS t o f i n d i f t h e r e i s a p o t e n t i a l c y c l e

62 ∗/

63 public bool AddOrder ( A c t i o n b e f o r e , A c t i o n a f t e r )

64 {

65 i f ( a f t e r i s S t a r t A c t i o n ) return f a l s e;

66

67 // S e a r c h t h e g r a p h f o r any c y c l e s

68 Queue<A c t i o n>Q = new Queue<A c t i o n>() ;

69 D i c t i o n a r y<A c t i o n , bool> v i s i t e d = new D i c t i o n a r y<A c t i o n ,

bool>() ;

D.1 Plan.cs 75

70 Q. Enqueue ( a f t e r ) ;

71 i n t i = 0 ;

72 while (Q. Count != 0 )

73 {

74 i ++;

75 A c t i o n a = Q. Dequeue ( ) ;

76

77 i f ( a . E q u a l s ( b e f o r e ) )

78 {

79 return f a l s e;

80 }

81

82 v i s i t e d . Add ( a , true) ;

83 try

84 {

85 foreach ( A c t i o n ap in o r d e r s [ a ] )

86 {

87 i f ( ! v i s i t e d . ContainsKey ( ap ) && !Q. C o n t a i n s ( ap ) )

88 {

89 Q. Enqueue ( ap ) ;

90 }

91 }

92 }

93 catch ( KeyNotFoundException )

94 {

95 }

96 }

97

98 L i s t<A c t i o n> l i s t ;

99 try

100 {

101 l i s t = o r d e r s [ b e f o r e ] ;

102 }

103 catch ( KeyNotFoundException )

104 {

105 l i s t = new L i s t<A c t i o n>() ;

106 o r d e r s . Add ( b e f o r e , l i s t ) ;

107 }

108 i f ( ! l i s t . C o n t a i n s ( a f t e r ) )

109 {

110 l i s t . Add ( a f t e r ) ;

111 }

112

113 return true;

114 }

115

116 /∗ ∗

117 ∗ Check i f t h e p l a n i s a s o l u t i o n t o t h e w h o l e p r o b l e m

118 ∗ Use a t o p o l i g i c a l s o r t t o d e t e r m i n e i f t h e r e e x i s t a l i n e a r e x t e n s i o n

119 ∗/

120 public bool I s S o l u t i o n ( )

121 {

122 i f ( O p e n P r e c o n d i t i o n s . Count == 0 && G e t O r d e r s T o p o l o g i c a l S o r t ( ) != n u l l)

123 {

124 return true;

125 }

126 return f a l s e;

127 }

128

129 // Add an a c t i o n t o t h e p l a n .

130 public bool AddAction ( Link p r e A c t i o n L i n k , C o n d i t i o n s u b g o a l C o n d i t i o n , A c t i o n s u b g o a l A c t i o n )

131 {

132 A c t i o n p r e A c t i o n = p r e A c t i o n L i n k . A c t i o n P r e ;

133

134 // Add o r d e r i n g c o n s t r a i n s

135 i f ( ! AddOrder ( p r e A c t i o n , s u b g o a l A c t i o n ) )

136 {

137 return f a l s e;

138 }

139

140 // Add t h e c a u s a l l i n k

141 C a u s a l L i n k n e w l i n k = new C a u s a l L i n k ( p r e A c t i o n , s u b g o a l C o n d i t i o n , s u b g o a l A c t i o n ) ;

142 C a u s a l L i n k s . Add ( n e w l i n k ) ;

143

144 // Update p l a n :

145 C o n s o l e . W r i t e L i n e ( ”Use : ” + p r e A c t i o n ) ;

146 i f ( p r e A c t i o n L i n k . C o n d i t i o n == n u l l)

147 {

148 O p e n P r e c o n d i t i o n s = p r e A c t i o n . N e w O p e n P r e c o n d i t i o n s ( O p e n P r e c o n d i t i o n s , n e w l i n k , t h i s) ;

149 a c t i o n s . Add ( p r e A c t i o n ) ;

150 }

151 e l s e

152 {

153 C o n s o l e . W r i t e L i n e ( ” P r e v i o u s a c t i o n u s e d ” ) ;

154 // p r e v i o u s A c t i o n s . Remove ( p r e A c t i o n L i n k ) ;

155 foreach ( Li nk l i n k in O p e n P r e c o n d i t i o n s )

156 {

157 i f ( l i n k . C o n d i t i o n . E q u a l s ( s u b g o a l C o n d i t i o n ) )

158 {

159 O p e n P r e c o n d i t i o n s . Remove ( l i n k ) ;

160 break;

161 }

162 }

163 }

164 return true;

165 }

166

167 // R e t u r n s a l i s t o f t h r e a t s

168 public L i s t<C a u s a l L i n k> G e t T h r e a t L i n k s ( Lin k u n r e s o l v e d L i n k , Lin k s e l e c t e d P r e A c t i o n L i n k )

169 {

170 L i s t<C a u s a l L i n k> Threads = new MyList<C a u s a l L i n k>() ;

171 A c t i o n s e l e c t e d P r e A c t i o n = s e l e c t e d P r e A c t i o n L i n k . A c t i o n P r e ;

172

173 C o n d i t i o n u n r e s o l v e d C o n d i t i o n = u n r e s o l v e d L i n k . C o n d i t i o n ;

D.1 Plan.cs 77

174 foreach ( C a u s a l L i n k c a u s a l l i n k in new L i s t<C a u s a l L i n k>(

C a u s a l L i n k s ) )

175 {

176 i f ( u n r e s o l v e d C o n d i t i o n . T h r e a t e n s ( c a u s a l l i n k . C o n d i t i o n ) &&

! s e l e c t e d P r e A c t i o n . E q u a l s ( c a u s a l l i n k . P o s t A c t i o n )

177 && ! s e l e c t e d P r e A c t i o n . E q u a l s ( c a u s a l l i n k . P r e A c t i o n ) )

178 {

179 Threads . Add ( c a u s a l l i n k ) ;

180 }

181 e l s e i f( //Make s u r e t h e c o n d i t i o n C1 u s e d by an a c t i o n A1 t h a t c h a n g e s i t , i s n o t u s e d by a n o t h e r a c t i o n A2 .

182 c a u s a l l i n k . P r e A c t i o n . E q u a l s ( s e l e c t e d P r e A c t i o n ) // s t a r t ==

s t a r t

183 && ! c a u s a l l i n k . P o s t A c t i o n . E q u a l s ( u n r e s o l v e d L i n k . A c t i o n P o s t ) // p i c k u p 1 != p i c k u p 2

184 && c a u s a l l i n k . C o n d i t i o n . GetType ( ) . E q u a l s (

u n r e s o l v e d C o n d i t i o n . GetType ( ) ) // n o t h a v e b a l l ==

n o t h a v e b a l l

185 && u n r e s o l v e d L i n k . A c t i o n P o s t . G e t A l t e r P r e L i s t ( ) . C o n t a i n s ( u n r e s o l v e d C o n d i t i o n . GetType ( ) ) // p i c k u p 2 . a l t e r s ( n o t h a v e b a l l c o n d i t i o n )

186 && ( u n r e s o l v e d C o n d i t i o n i s C o n d i t i o n V a r i a b l e

187 && ( ( C o n d i t i o n V a r i a b l e ) u n r e s o l v e d C o n d i t i o n ) . I s S a m e P l a c e ( ( C o n d i t i o n V a r i a b l e ) c a u s a l l i n k . C o n d i t i o n )

188 | | ! ( u n r e s o l v e d C o n d i t i o n i s C o n d i t i o n V a r i a b l e ) ) ) //

b o x a t ( 1 , 0 ) == b o x a t ( 1 , 0 ) e l . n o t h a v e b a l l og i k k e b o x a t ( 1 , 0 ) == b o x a t ( 0 , 0 )

189 {

190 C a u s a l L i n k s . Remove ( c a u s a l l i n k ) ;

191 O p e n P r e c o n d i t i o n s . Add (new Link ( c a u s a l l i n k . P r e A c t i o n , c a u s a l l i n k . C o n d i t i o n , c a u s a l l i n k . P o s t A c t i o n ) ) ;

192 }

193 }

194

195 return Threads ;

196 }

197

198 // Make a copy o f t h e p l a n

199 public Plan C lo ne ( )

200 {

201 Plan p l a n = new Plan ( ) ;

202 p l a n . a c t i o n s = new MyList<A c t i o n>(t h i s. a c t i o n s ) ;

203 p l a n . C a u s a l L i n k s = new MyList<C a u s a l L i n k>(t h i s. C a u s a l L i n k s ) ;

204 p l a n . o r d e r s = new D i c t i o n a r y<A c t i o n , L i s t<A c t i o n>>() ;

205 foreach ( KeyValuePair<A c t i o n , L i s t<A c t i o n>> k in t h i s. o r d e r s )

206 p l a n . o r d e r s . Add ( k . Key , new L i s t<A c t i o n>(k . Value ) ) ;

207 p l a n . O p e n P r e c o n d i t i o n s = new MyList<Link>(t h i s. O p e n P r e c o n d i t i o n s ) ;

208 p l a n . p r e v i o u s A c t i o n s = new MyList<Link>(t h i s. p r e v i o u s A c t i o n s )

;

209 p l a n . S t a r t A c t i o n = t h i s. S t a r t A c t i o n ;

210

211 return p l a n ;

212 }

213

214

215 public override s t r i n g T o S t r i n g ( )

216 {

217 S t r i n g B u i l d e r s =new S t r i n g B u i l d e r ( ) ;

218 foreach ( A c t i o n a in a c t i o n s )

219 {

220 s . Append ( a+”\n ” ) ;

221 }

222

223 return s . T o S t r i n g ( ) ;

224 }

225

226 // Make a l i n e a r e x t e n s i o n from t h e o r d e r i n g c o n s t r a i n t s v i a a t o p o l o g i c a l s o r t a l g o r i t h m

227 public L i n k e d L i s t<A c t i o n> G e t O r d e r s T o p o l o g i c a l S o r t ( )

228 {

229 D i c t i o n a r y<A c t i o n , L i s t<A c t i o n>> o r d e r c o p y =new D i c t i o n a r y<

A c t i o n , L i s t<A c t i o n>>() ;

230 foreach ( KeyValuePair<A c t i o n , L i s t<A c t i o n>> k in t h i s. o r d e r s )

231 {

232 o r d e r c o p y . Add ( k . Key , new L i s t<A c t i o n>(k . Value ) ) ;

233 }

234

235 L i n k e d L i s t<A c t i o n> r e s u l t =new L i n k e d L i s t<A c t i o n>() ;

236

237 Queue<A c t i o n>Q = new Queue<A c t i o n>() ;

238 Q. Enqueue ( S t a r t A c t i o n ) ;

239

240 while (Q. Count > 0 )

241 {

242 A c t i o n n = Q. Dequeue ( ) ;

243 r e s u l t . AddLast ( n ) ;

244

245 try

246 {

247 foreach ( A c t i o n m in new L i s t<A c t i o n>( o r d e r c o p y [ n ] ) )

248 {

249 o r d e r c o p y [ n ] . Remove (m) ;

250 i f ( o r d e r c o p y [ n ] . Count == 0 )

251 o r d e r c o p y . Remove ( n ) ;

252

253 bool h a v e o t h e r s = f a l s e;

254 foreach ( KeyValuePair<A c t i o n , L i s t<A c t i o n>> l in o r d e r c o p y )

255 {

256 i f ( l . Value . C o n t a i n s (m) )

257 h a v e o t h e r s = true;

258 }

259 i f ( ! h a v e o t h e r s ) Q. Enqueue (m) ;

260 }

261 }

262 catch ( KeyNotFoundException ) {

263 }

264 }

265

D.1 Plan.cs 79

266 i f ( o r d e r c o p y . Count != 0 )

267 return n u l l;

268

269 return r e s u l t ;

270 }

271

272 public i n t CompareTo (object p )

273 {

274 i f ( p i s Plan )

275 return t h i s. CompareTo ( ( Plan ) p ) ;

276 return 0 ;

277 }

278

279 // Compare two p l a n s and r a n k t h e p l a n w i t h t h e l o w e s t h e u r i s t i c s f i r s t

280 public i n t CompareTo ( Plan p )

281 {

282 return t h i s. H e u r i s t i c . CompareTo ( p . H e u r i s t i c ) ;

283 }

284

285 // C a l c u l a t e d h e u r i s t i c

286 private i n t c a l c u l a t e d H e u r i s t i c = −1;

287

288 // Return t h e h e u r i s t i c v a l u e .

289 // T h i s v a l u e i s o n l y c a l c u l a t e d once and s t o r e d i n t h e p r i v a t e v a r i a b l e c a l c u l a t e d H e u r i s t i c

290 public i n t H e u r i s t i c

291 {

292 g e t

293 {

294 i f ( c a l c u l a t e d H e u r i s t i c == −1)

295 {

296 c a l c u l a t e d H e u r i s t i c = 0 ;

297

298 // The w e i g h t o f t h e c u r r e n t p l a n

299 foreach ( A c t i o n a in a c t i o n s )

300 c a l c u l a t e d H e u r i s t i c += a . Weight ;

301

302 // E s t i m a t e a w e i g h t o f how many a c t i o n s t h e r e i s n e e d e d f o r a l l c o n d i t i o n s a r e r e s o l v e d

303 foreach ( Li nk c 1 in O p e n P r e c o n d i t i o n s )

304 {

305 // C a l c u l a t e t h e d i s t a n c e t o a o b j e c t and l e t t h i s w e i g h t i n c l u d e i n t h e h e u r i s t i c

306 i f ( c 1 . C o n d i t i o n i s A t C o n d i t i o n )

307 foreach ( C o n d i t i o n c 2 in ( ( S t a r t A c t i o n ) S t a r t A c t i o n ) . P o s t C o n d i t i o n s )

308 i f ( c 2 i s A t C o n d i t i o n )

309 {

310 i n t d i s t a n c e = ( ( A t C o n d i t i o n ) c 1 . C o n d i t i o n ) . D i s t a n c e T o ( ( ( A t C o n d i t i o n ) c 2 ) ) ;

311 i f ( d i s t a n c e != i n t. MaxValue )

312 {

313 c a l c u l a t e d H e u r i s t i c += d i s t a n c e ;

314 break;

315 }

316 }

317 c a l c u l a t e d H e u r i s t i c ++;

318 }

319 }

320 return c a l c u l a t e d H e u r i s t i c ;

321 }

322 }

323

324 // Find a l l e s s e n t i a l s t a r t c o n d i t i o n s

325 i n t e r n a l void C a l c E s s e n t i a l S t a r t C o n d i t i o n s ( )

326 {

327 foreach ( C a u s a l L i n k c a l i n k in C a u s a l L i n k s )

328 {

329 i f ( c a l i n k . P r e A c t i o n i s S t a r t A c t i o n )

330 {

331 i f ( E s s e n t i e l S t a r t C o n d i t i o n s . ContainsKey ( c a l i n k . P o s t A c t i o n ) )

332 {

333 E s s e n t i e l S t a r t C o n d i t i o n s [ c a l i n k . P o s t A c t i o n ] . Add ( c a l i n k . C o n d i t i o n ) ;

334 }

335 e l s e

336 {

337 L i s t<C o n d i t i o n> temp = new L i s t<C o n d i t i o n>() ;

338 temp . Add ( c a l i n k . C o n d i t i o n ) ;

339 E s s e n t i e l S t a r t C o n d i t i o n s . Add ( c a l i n k . P o s t A c t i o n , temp ) ;

340 }

341 }

342 }

343 }

344 }

345 }

In document Planlægning med LEGO agenter (Sider 70-90)