Skip to content

Commit 34fe319

Browse files
committed
Convert PathConditionVertices from GameState to tensor.
1 parent 0003b23 commit 34fe319

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed

VSharp.Explorer/AISearcher.fs

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
8686
s.Children |> Array.filter activeStates.Contains
8787
))
8888

89-
let pathConditionVertices =
90-
ResizeArray<PathConditionVertex> s.PathConditionVertices
89+
let pathConditionVertices = ResizeArray<PathConditionVertex> s.PathConditionVertices
9190

9291
pathConditionVertices.AddRange delta.PathConditionVertices
9392

@@ -182,7 +181,8 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
182181

183182
new(pathToONNX: string, useGPU: bool, optimize: bool) =
184183
let numOfVertexAttributes = 7
185-
let numOfStateAttributes = 7
184+
let numOfStateAttributes = 6
185+
let numOfPathConditionVertexAttributes = 49
186186
let numOfHistoryEdgeAttributes = 2
187187

188188
let createOracle (pathToONNX: string) =
@@ -205,10 +205,33 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
205205
let predict (gameState: GameState) =
206206
let stateIds = Dictionary<uint<stateId>, int>()
207207
let verticesIds = Dictionary<uint<basicBlockGlobalId>, int>()
208+
let pathConditionVerticesIds = Dictionary<uint<pathConditionVertexId>, int>()
208209

209210
let networkInput =
210211
let res = Dictionary<_, _>()
211212

213+
let pathConditionVertices, numOfPcToPcEdges =
214+
let mutable numOfPcToPcEdges = 0
215+
216+
let shape =
217+
[| int64 gameState.PathConditionVertices.Length
218+
numOfPathConditionVertexAttributes |]
219+
220+
let attributes =
221+
Array.zeroCreate (
222+
gameState.PathConditionVertices.Length * numOfPathConditionVertexAttributes
223+
)
224+
225+
for i in 0 .. gameState.PathConditionVertices.Length - 1 do
226+
let v = gameState.PathConditionVertices.[i]
227+
numOfPcToPcEdges <- numOfPcToPcEdges + v.Children.Length * 2
228+
pathConditionVerticesIds.Add(v.Id, i)
229+
let j = i * numOfPathConditionVertexAttributes
230+
attributes.[j + int v.Type] <- float32 1u
231+
232+
OrtValue.CreateTensorValueFromMemory(attributes, shape), numOfPcToPcEdges
233+
234+
212235
let gameVertices =
213236
let shape = [| int64 gameState.GraphVertices.Length; numOfVertexAttributes |]
214237

@@ -242,8 +265,6 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
242265
stateIds.Add(v.Id, i)
243266
let j = i * numOfStateAttributes
244267
attributes.[j] <- float32 v.Position
245-
// TODO: Support path condition
246-
// attributes.[j + 1] <- float32 v.PathConditionSize
247268
attributes.[j + 2] <- float32 v.VisitedAgainVertices
248269
attributes.[j + 3] <- float32 v.VisitedNotCoveredVerticesInZone
249270
attributes.[j + 4] <- float32 v.VisitedNotCoveredVerticesOutOfZone
@@ -252,6 +273,31 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
252273

253274
OrtValue.CreateTensorValueFromMemory(attributes, shape), numOfParentOfEdges, numOfHistoryEdges
254275

276+
let pcToPcEdgeIndex =
277+
let shapeOfIndex = [| 2L; numOfPcToPcEdges |]
278+
let index = Array.zeroCreate (2 * numOfPcToPcEdges)
279+
let mutable firstFreePositionOfIndex = 0
280+
281+
for v in gameState.PathConditionVertices do
282+
for child in v.Children do
283+
// from vertex to child
284+
index.[firstFreePositionOfIndex] <- pathConditionVerticesIds.[v.Id]
285+
286+
index.[firstFreePositionOfIndex + 2 * numOfPcToPcEdges] <-
287+
pathConditionVerticesIds.[child]
288+
289+
firstFreePositionOfIndex <- firstFreePositionOfIndex + 1
290+
// from child to vertex
291+
index.[firstFreePositionOfIndex] <- pathConditionVerticesIds.[child]
292+
293+
index.[firstFreePositionOfIndex + 2 * numOfPcToPcEdges] <-
294+
pathConditionVerticesIds.[v.Id]
295+
296+
firstFreePositionOfIndex <- firstFreePositionOfIndex + 1
297+
298+
OrtValue.CreateTensorValueFromMemory(index, shapeOfIndex)
299+
300+
255301
let vertexToVertexEdgesIndex, vertexToVertexEdgesAttributes =
256302
let shapeOfIndex = [| 2L; gameState.Map.Length |]
257303
let shapeOfAttributes = [| int64 gameState.Map.Length |]
@@ -267,11 +313,13 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
267313
OrtValue.CreateTensorValueFromMemory(index, shapeOfIndex),
268314
OrtValue.CreateTensorValueFromMemory(attributes, shapeOfAttributes)
269315

270-
let historyEdgesIndex_vertexToState, historyEdgesAttributes, parentOfEdges =
316+
let historyEdgesIndex_vertexToState, historyEdgesAttributes, parentOfEdges, edgeIndex_pcToState =
271317
let shapeOfParentOf = [| 2L; numOfParentOfEdges |]
272318
let parentOf = Array.zeroCreate (2 * numOfParentOfEdges)
273319
let shapeOfHistory = [| 2L; numOfHistoryEdges |]
274320
let historyIndex_vertexToState = Array.zeroCreate (2 * numOfHistoryEdges)
321+
let shapeOfPcToState = [| 2L; gameState.States.Length |]
322+
let index_pcToState = Array.zeroCreate (2 * gameState.States.Length)
275323

276324
let shapeOfHistoryAttributes =
277325
[| int64 numOfHistoryEdges; int64 numOfHistoryEdgeAttributes |]
@@ -280,6 +328,7 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
280328
let mutable firstFreePositionInParentsOf = 0
281329
let mutable firstFreePositionInHistoryIndex = 0
282330
let mutable firstFreePositionInHistoryAttributes = 0
331+
let mutable firstFreePositionInPcToState = 0
283332

284333
gameState.States
285334
|> Array.iter (fun state ->
@@ -291,6 +340,14 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
291340

292341
firstFreePositionInParentsOf <- firstFreePositionInParentsOf + state.Children.Length
293342

343+
index_pcToState.[firstFreePositionInPcToState] <-
344+
int64 pathConditionVerticesIds[state.PathCondition.Id]
345+
346+
index_pcToState.[firstFreePositionInPcToState + gameState.States.Length] <-
347+
int64 stateIds[state.Id]
348+
349+
firstFreePositionInPcToState <- firstFreePositionInPcToState + 1
350+
294351
state.History
295352
|> Array.iteri (fun i historyElem ->
296353
let j = firstFreePositionInHistoryIndex + i
@@ -309,7 +366,8 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
309366

310367
OrtValue.CreateTensorValueFromMemory(historyIndex_vertexToState, shapeOfHistory),
311368
OrtValue.CreateTensorValueFromMemory(historyAttributes, shapeOfHistoryAttributes),
312-
OrtValue.CreateTensorValueFromMemory(parentOf, shapeOfParentOf)
369+
OrtValue.CreateTensorValueFromMemory(parentOf, shapeOfParentOf),
370+
OrtValue.CreateTensorValueFromMemory(index_pcToState, shapeOfPcToState)
313371

314372
let statePosition_stateToVertex, statePosition_vertexToState =
315373
let data_stateToVertex = Array.zeroCreate (2 * gameState.States.Length)
@@ -337,6 +395,7 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
337395

338396
res.Add("game_vertex", gameVertices)
339397
res.Add("state_vertex", states)
398+
res.Add("path_condition_vertex", pathConditionVertices)
340399

341400
res.Add("gamevertex_to_gamevertex_index", vertexToVertexEdgesIndex)
342401
res.Add("gamevertex_to_gamevertex_type", vertexToVertexEdgesAttributes)
@@ -347,6 +406,9 @@ type internal AISearcher(oracle: Oracle, aiAgentTrainingOptions: Option<AIAgentT
347406
res.Add("gamevertex_in_statevertex", statePosition_vertexToState)
348407
res.Add("statevertex_parentof_statevertex", parentOfEdges)
349408

409+
res.Add("pathconditionvertex_to_pathconditionvertex", pcToPcEdgeIndex)
410+
res.Add("pathconditionvertex_to_statevertex", edgeIndex_pcToState)
411+
350412
res
351413

352414
let output = session.Run(runOptions, networkInput, session.OutputNames)

0 commit comments

Comments
 (0)