forked from VSharp-team/VSharp
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMain.fs
More file actions
468 lines (380 loc) · 17.3 KB
/
Main.fs
File metadata and controls
468 lines (380 loc) · 17.3 KB
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
open System
open System.IO
open System.Text.Json
open System.Net.Sockets
open System.Reflection
open Argu
open Microsoft.FSharp.Core
open Suave
open Suave.Operators
open Suave.Filters
open Suave.Logging
open Suave.Sockets
open Suave.Sockets.Control
open Suave.WebSocket
open VSharp
open VSharp.Core
open VSharp.Explorer
open VSharp.IL
open VSharp.ML.GameServer.Messages
open VSharp.Runner
[<Struct>]
type ExplorationResult =
val ActualCoverage: uint<percent>
val TestsCount: uint<test>
val ErrorsCount: uint<error>
val StepsCount: uint<step>
new(actualCoverage, testsCount, errorsCount, stepsCount) =
{ ActualCoverage = actualCoverage
TestsCount = testsCount
ErrorsCount = errorsCount
StepsCount = stepsCount }
type Mode =
| Server = 0
| Generator = 1
| SendModel = 2
let TIMEOUT_FOR_TRAINING = 15 * 60
let SOLVER_TIMEOUT_FOR_TRAINING = 2
type CliArguments =
| [<Unique>] Port of int
| [<Unique>] DatasetBasePath of string
| [<Unique>] DatasetDescription of string
| [<Unique; Mandatory>] Mode of Mode
| [<Unique>] OutFolder of string
| [<Unique>] StepsToSerialize of uint
| [<Unique>] Model of string
| [<Unique>] StepsToPlay of uint<step>
| [<Unique>] DefaultSearcher of string
| [<Unique>] StepsToStart of uint<step>
| [<Unique>] AssemblyFullName of string
| [<Unique>] NameOfObjectToCover of string
| [<Unique>] MapName of string
| [<Unique>] UseGPU of bool
| [<Unique>] Optimize of bool
interface IArgParserTemplate with
member s.Usage =
match s with
| Port _ -> "Port to communicate with game client."
| DatasetBasePath _ ->
"Full path to dataset root directory. Dll location is <DatasetBasePath>/<AssemblyFullName>"
| DatasetDescription _ -> "Full paths to JSON-file with dataset description."
| Mode _ ->
"Mode to run application. Server --- to train network, Generator --- to generate data for training."
| OutFolder _ -> "Folder to store generated data."
| StepsToSerialize _ -> "Maximal number of steps for each method to serialize."
| Model _ -> """Path to ONNX model (use it for training in mode "SendModel")"""
| StepsToPlay _ -> """Steps required to play (after `StepsToStart` steps)"""
| DefaultSearcher _ -> """Defines the default searcher algorithm (BFS | DFS)"""
| StepsToStart _ -> """Steps required to start the game"""
| AssemblyFullName _ -> """Path to the DLL that contains the game map implementation"""
| NameOfObjectToCover _ -> """The name of the object that needs to be covered"""
| MapName _ -> """The name of the map used in the game"""
| UseGPU _ -> "Specifies whether the ONNX execution session should use a CUDA-enabled GPU."
| Optimize _ ->
"Enabling options like parallel execution and various graph transformations to enhance performance of ONNX."
let mutable inTrainMode = true
let explore (gameMap: GameMap) options =
let assembly = RunnerProgram.TryLoadAssembly <| FileInfo gameMap.AssemblyFullName
let method = RunnerProgram.ResolveMethod(assembly, gameMap.NameOfObjectToCover)
let statistics = TestGenerator.Cover(method, options)
let actualCoverage =
try
let testsDir = statistics.OutputDir
let _expectedCoverage = 100
let exploredMethodInfo = AssemblyManager.NormalizeMethod method
let status, actualCoverage, message =
VSharp.Test.TestResultChecker.Check(testsDir, exploredMethodInfo :?> MethodInfo, _expectedCoverage)
printfn $"Actual coverage for {gameMap.MapName}: {actualCoverage}"
if actualCoverage < 0 then
0u<percent>
else
uint actualCoverage * 1u<percent>
with e ->
printfn $"Coverage checking problem:{e.Message} \n {e.StackTrace}"
0u<percent>
ExplorationResult(
actualCoverage,
statistics.TestsCount * 1u<test>,
statistics.ErrorsCount * 1u<error>,
statistics.StepsCount * 1u<step>
)
let loadGameMaps (datasetDescriptionFilePath: string) =
let jsonString = File.ReadAllText datasetDescriptionFilePath
let maps = ResizeArray<GameMap>()
for map in System.Text.Json.JsonSerializer.Deserialize<GameMap[]> jsonString do
maps.Add map
maps
let ws port outputDirectory (webSocket: WebSocket) (context: HttpContext) =
let mutable loop = true
socket {
let sendResponse (message: OutgoingMessage) =
let byteResponse =
serializeOutgoingMessage message
|> System.Text.Encoding.UTF8.GetBytes
|> ByteSegment
webSocket.send Text byteResponse true
let oracle =
let feedback =
fun (feedback: Feedback) ->
let res =
socket {
let message =
match feedback with
| Feedback.ServerError s -> OutgoingMessage.ServerError s
| Feedback.MoveReward reward -> OutgoingMessage.MoveReward reward
| Feedback.IncorrectPredictedStateId i -> OutgoingMessage.IncorrectPredictedStateId i
do! sendResponse message
}
match Async.RunSynchronously res with
| Choice1Of2() -> ()
| Choice2Of2 error -> failwithf $"Error: %A{error}"
let predict =
let mutable cnt = 0u
fun (gameState: GameState) ->
let toDot drawHistory =
let file = Path.Join("dot", $"{cnt}.dot")
gameState.ToDot file drawHistory
cnt <- cnt + 1u
//toDot false
let res =
socket {
do! sendResponse (ReadyForNextStep gameState)
let! msg = webSocket.read ()
let res =
match msg with
| (Text, data, true) ->
let msg = deserializeInputMessage data
match msg with
| Step stepParams -> (stepParams.StateId)
| _ -> failwithf $"Unexpected message: %A{msg}"
| _ -> failwithf $"Unexpected message: %A{msg}"
return res
}
match Async.RunSynchronously res with
| Choice1Of2 i -> i
| Choice2Of2 error -> failwithf $"Error: %A{error}"
Oracle(predict, feedback)
while loop do
let! msg = webSocket.read ()
match msg with
| (Text, data, true) ->
let message = deserializeInputMessage data
match message with
| ServerStop -> loop <- false
| Start gameMap ->
printfn $"Start map {gameMap.MapName}, port {port}"
let stepsToStart = gameMap.StepsToStart
let stepsToPlay = gameMap.StepsToPlay
let aiTrainingOptions =
{ aiBaseOptions =
{ defaultSearchStrategy =
match gameMap.DefaultSearcher with
| searcher.BFS -> BFSMode
| searcher.DFS -> DFSMode
| x -> failwithf $"Unexpected searcher {x}. Use DFS or BFS for now."
mapName = gameMap.MapName }
stepsToSwitchToAI = stepsToStart
stepsToPlay = stepsToPlay
oracle = Some oracle }
let aiOptions: AIOptions =
(Training(SendEachStep { aiAgentTrainingOptions = aiTrainingOptions }))
let options =
VSharpOptions(
timeout = TIMEOUT_FOR_TRAINING,
outputDirectory = outputDirectory,
searchStrategy = SearchStrategy.AI,
aiOptions = aiOptions,
stepsLimit = uint (stepsToPlay + stepsToStart),
solverTimeout = SOLVER_TIMEOUT_FOR_TRAINING
)
let explorationResult = explore gameMap options
Application.reset ()
API.Reset()
HashMap.hashMap.Clear()
Serializer.pathConditionVertices.Clear()
Serializer.resetPathConditionVertexIdCounter ()
Serializer.termsWithId.Clear()
do!
sendResponse (
GameOver(
explorationResult.ActualCoverage,
explorationResult.TestsCount,
explorationResult.StepsCount,
explorationResult.ErrorsCount
)
)
printfn $"Finish map {gameMap.MapName}, port {port}"
| x -> failwithf $"Unexpected message: %A{x}"
| (Close, _, _) ->
let emptyResponse = [||] |> ByteSegment
do! webSocket.send Close emptyResponse true
loop <- false
| _ -> ()
}
let app port outputDirectory : WebPart =
choose [ path "/gameServer" >=> handShake (ws port outputDirectory) ]
let serializeExplorationResult (explorationResult: ExplorationResult) =
$"{explorationResult.ActualCoverage} {explorationResult.TestsCount} {explorationResult.StepsCount} {explorationResult.ErrorsCount}"
let generateDataForPretraining outputDirectory datasetBasePath (maps: ResizeArray<GameMap>) stepsToSerialize =
for map in maps do
if map.StepsToStart = 0u<step> then
printfn $"Generation for {map.MapName} started."
let map =
GameMap(
map.StepsToPlay,
map.StepsToStart,
Path.Combine(datasetBasePath, map.AssemblyFullName),
map.DefaultSearcher,
map.NameOfObjectToCover,
map.MapName
)
let aiBaseOptions =
{ defaultSearchStrategy = BFSMode
mapName = map.MapName }
let options =
VSharpOptions(
timeout = 5 * 60,
outputDirectory = outputDirectory,
searchStrategy = SearchStrategy.ExecutionTreeContributedCoverage,
stepsLimit = stepsToSerialize,
solverTimeout = 2,
aiOptions = DatasetGenerator aiBaseOptions
)
let folderForResults =
Serializer.getFolderToStoreSerializationResult outputDirectory map.MapName
if Directory.Exists folderForResults then
Directory.Delete(folderForResults, true)
let _ = Directory.CreateDirectory folderForResults
let explorationResult = explore map options
File.WriteAllText(Path.Join(folderForResults, "result"), serializeExplorationResult explorationResult)
printfn
$"Generation for {map.MapName} finished with coverage {explorationResult.ActualCoverage}, tests {explorationResult.TestsCount}, steps {explorationResult.StepsCount},errors {explorationResult.ErrorsCount}."
Application.reset ()
API.Reset()
HashMap.hashMap.Clear()
let runTrainingSendModelMode
outputDirectory
(gameMap: GameMap)
(pathToModel: string)
(useGPU: bool)
(optimize: bool)
(port: int)
=
printfn $"Run infer on {gameMap.MapName} have started."
let stepsToStart = gameMap.StepsToStart
let stepsToPlay = gameMap.StepsToPlay
let aiTrainingOptions =
{ aiBaseOptions =
{ defaultSearchStrategy =
match gameMap.DefaultSearcher with
| searcher.BFS -> BFSMode
| searcher.DFS -> DFSMode
| x -> failwithf $"Unexpected searcher {x}. Use DFS or BFS for now."
mapName = gameMap.MapName }
stepsToSwitchToAI = stepsToStart
stepsToPlay = stepsToPlay
oracle = None }
let steps = ResizeArray()
let stepSaver (aiGameStep: AIGameStep) = steps.Add aiGameStep
let aiOptions: AIOptions =
Training(
SendModel
{ aiAgentTrainingOptions = aiTrainingOptions
outputDirectory = outputDirectory
stepSaver = stepSaver }
)
let options =
VSharpOptions(
timeout = TIMEOUT_FOR_TRAINING,
outputDirectory = outputDirectory,
searchStrategy = SearchStrategy.AI,
solverTimeout = SOLVER_TIMEOUT_FOR_TRAINING,
stepsLimit = uint (stepsToPlay + stepsToStart),
aiOptions = aiOptions,
pathToModel = pathToModel,
useGPU = useGPU,
optimize = optimize
)
let explorationResult = explore gameMap options
File.WriteAllText(
Path.Join(outputDirectory, gameMap.MapName + "result"),
serializeExplorationResult explorationResult
)
printfn
$"Running for {gameMap.MapName} finished with coverage {explorationResult.ActualCoverage}, tests {explorationResult.TestsCount}, steps {explorationResult.StepsCount},errors {explorationResult.ErrorsCount}."
let stream =
let host = "localhost" // TODO: working within a local network
let client = new TcpClient()
client.Connect(host, port)
client.SendBufferSize <- 4096
client.GetStream()
let needToSendSteps =
let buffer = Array.zeroCreate<byte> 1
let bytesRead = stream.Read(buffer, 0, 1)
if bytesRead = 0 then
failwith "Connection is closed?!"
stream.Close()
buffer.[0] <> byte 0
if needToSendSteps then
File.WriteAllText(Path.Join(outputDirectory, gameMap.MapName + "_steps"), JsonSerializer.Serialize steps)
[<EntryPoint>]
let main args =
let parser =
ArgumentParser.Create<CliArguments>(programName = "VSharp.ML.GameServer.Runner.exe")
let args = parser.Parse args
let mode = args.GetResult <@ Mode @>
let port = args.GetResult(Port, defaultValue = 8100)
let outputDirectory =
args.GetResult(OutFolder, defaultValue = Path.Combine(Directory.GetCurrentDirectory(), string port))
let cleanOutputDirectory () =
if Directory.Exists outputDirectory then
Directory.Delete(outputDirectory, true)
Directory.CreateDirectory outputDirectory
printfn $"outputDir: {outputDirectory}"
match mode with
| Mode.Server ->
let _ = cleanOutputDirectory ()
try
startWebServer
{ defaultConfig with
logger = Targets.create Verbose [||]
bindings = [ HttpBinding.createSimple HTTP "127.0.0.1" port ] }
(app port outputDirectory)
with e ->
printfn $"Failed on port {port}"
printfn $"{e.Message}"
| Mode.SendModel ->
let model = args.GetResult(Model, defaultValue = "models/model.onnx")
let stepsToPlay = args.GetResult <@ StepsToPlay @>
let defaultSearcher =
let s = args.GetResult <@ DefaultSearcher @>
let upperedS = String.map System.Char.ToUpper s
match upperedS with
| "BFS" -> searcher.BFS
| "DFS" -> searcher.DFS
| _ -> failwith "Use BFS or DFS as a default searcher"
let stepsToStart = args.GetResult <@ StepsToStart @>
let assemblyFullName = args.GetResult <@ AssemblyFullName @>
let nameOfObjectToCover = args.GetResult <@ NameOfObjectToCover @>
let mapName = args.GetResult <@ MapName @>
let gameMap =
GameMap(
stepsToPlay = stepsToPlay,
stepsToStart = stepsToStart,
assemblyFullName = assemblyFullName,
defaultSearcher = defaultSearcher,
nameOfObjectToCover = nameOfObjectToCover,
mapName = mapName
)
let useGPU = args.GetResult(UseGPU, defaultValue = false)
let optimize = args.GetResult(Optimize, defaultValue = false)
runTrainingSendModelMode outputDirectory gameMap model useGPU optimize port
| Mode.Generator ->
let datasetDescription = args.GetResult <@ DatasetDescription @>
let datasetBasePath = args.GetResult <@ DatasetBasePath @>
let stepsToSerialize = args.GetResult(StepsToSerialize, defaultValue = 500u)
let _ = cleanOutputDirectory ()
let maps = loadGameMaps datasetDescription
generateDataForPretraining outputDirectory datasetBasePath maps stepsToSerialize
| x -> failwithf $"Unexpected mode {x}."
0