@@ -23,22 +23,22 @@ type ExplorationResult =
23
23
val TestsCount : uint < test >
24
24
val ErrorsCount : uint < error >
25
25
val StepsCount : uint < step >
26
+
26
27
new ( actualCoverage, testsCount, errorsCount, stepsCount) =
27
- {
28
- ActualCoverage = actualCoverage
29
- TestsCount = testsCount
30
- ErrorsCount = errorsCount
31
- StepsCount = stepsCount
32
- }
28
+ { ActualCoverage = actualCoverage
29
+ TestsCount = testsCount
30
+ ErrorsCount = errorsCount
31
+ StepsCount = stepsCount }
33
32
34
33
type Mode =
35
34
| Server = 0
36
35
| Generator = 1
36
+
37
37
type CliArguments =
38
38
| [<Unique>] Port of int
39
39
| [<Unique>] DatasetBasePath of string
40
40
| [<Unique>] DatasetDescription of string
41
- | [<Unique ; Mandatory>] Mode of Mode
41
+ | [<Unique; Mandatory>] Mode of Mode
42
42
| [<Unique>] OutFolder of string
43
43
| [<Unique>] StepsToSerialize of uint
44
44
| [<Unique>] UseGPU
@@ -62,21 +62,21 @@ type CliArguments =
62
62
let mutable inTrainMode = true
63
63
64
64
let explore ( gameMap : GameMap ) options =
65
- let assembly =
66
- RunnerProgram.TryLoadAssembly <| FileInfo gameMap.AssemblyFullName
67
- let method =
68
- RunnerProgram.ResolveMethod ( assembly, gameMap.NameOfObjectToCover)
69
- let statistics =
70
- TestGenerator.Cover ( method, options)
65
+ let assembly = RunnerProgram.TryLoadAssembly <| FileInfo gameMap.AssemblyFullName
66
+ let method = RunnerProgram.ResolveMethod( assembly, gameMap.NameOfObjectToCover)
67
+ let statistics = TestGenerator.Cover( method, options)
68
+
71
69
let actualCoverage =
72
70
try
73
71
let testsDir = statistics.OutputDir
74
72
let _expectedCoverage = 100
75
- let exploredMethodInfo =
76
- AssemblyManager.NormalizeMethod method
73
+ let exploredMethodInfo = AssemblyManager.NormalizeMethod method
74
+
77
75
let status , actualCoverage , message =
78
- VSharp.Test.TestResultChecker.Check ( testsDir, exploredMethodInfo :?> MethodInfo, _ expectedCoverage)
76
+ VSharp.Test.TestResultChecker.Check( testsDir, exploredMethodInfo :?> MethodInfo, _ expectedCoverage)
77
+
79
78
printfn $" Actual coverage for {gameMap.MapName}: {actualCoverage}"
79
+
80
80
if actualCoverage < 0 then
81
81
0 u< percent>
82
82
else
@@ -85,7 +85,7 @@ let explore (gameMap: GameMap) options =
85
85
printfn $" Coverage checking problem:{e.Message} \n {e.StackTrace}"
86
86
0 u< percent>
87
87
88
- ExplorationResult (
88
+ ExplorationResult(
89
89
actualCoverage,
90
90
statistics.TestsCount * 1 u< test>,
91
91
statistics.ErrorsCount * 1 u< error>,
@@ -94,11 +94,12 @@ let explore (gameMap: GameMap) options =
94
94
95
95
96
96
let loadGameMaps ( datasetDescriptionFilePath : string ) =
97
- let jsonString =
98
- File.ReadAllText datasetDescriptionFilePath
99
- let maps = ResizeArray < GameMap > ()
97
+ let jsonString = File.ReadAllText datasetDescriptionFilePath
98
+ let maps = ResizeArray < GameMap >()
99
+
100
100
for map in System.Text.Json.JsonSerializer.Deserialize< GameMap[]> jsonString do
101
101
maps.Add map
102
+
102
103
maps
103
104
104
105
let ws port outputDirectory ( webSocket : WebSocket ) ( context : HttpContext ) =
@@ -111,6 +112,7 @@ let ws port outputDirectory (webSocket: WebSocket) (context: HttpContext) =
111
112
serializeOutgoingMessage message
112
113
|> System.Text.Encoding.UTF8.GetBytes
113
114
|> ByteSegment
115
+
114
116
webSocket.send Text byteResponse true
115
117
116
118
let oracle =
@@ -123,87 +125,99 @@ let ws port outputDirectory (webSocket: WebSocket) (context: HttpContext) =
123
125
| Feedback.ServerError s -> OutgoingMessage.ServerError s
124
126
| Feedback.MoveReward reward -> OutgoingMessage.MoveReward reward
125
127
| Feedback.IncorrectPredictedStateId i -> OutgoingMessage.IncorrectPredictedStateId i
128
+
126
129
do ! sendResponse message
127
130
}
131
+
128
132
match Async.RunSynchronously res with
129
- | Choice1Of2 () -> ()
133
+ | Choice1Of2() -> ()
130
134
| Choice2Of2 error -> failwithf $" Error: %A {error}"
131
135
132
136
let predict =
133
137
let mutable cnt = 0 u
138
+
134
139
fun ( gameState : GameState ) ->
135
140
let toDot drawHistory =
136
- let file = Path.Join ( " dot" , $" {cnt}.dot" )
141
+ let file = Path.Join( " dot" , $" {cnt}.dot" )
137
142
gameState.ToDot file drawHistory
138
143
cnt <- cnt + 1 u
139
144
//toDot false
140
145
let res =
141
146
socket {
142
147
do ! sendResponse ( ReadyForNextStep gameState)
143
148
let! msg = webSocket.read ()
149
+
144
150
let res =
145
151
match msg with
146
152
| ( Text, data, true ) ->
147
153
let msg = deserializeInputMessage data
154
+
148
155
match msg with
149
156
| Step stepParams -> ( stepParams.StateId)
150
157
| _ -> failwithf $" Unexpected message: %A {msg}"
151
158
| _ -> failwithf $" Unexpected message: %A {msg}"
159
+
152
160
return res
153
161
}
162
+
154
163
match Async.RunSynchronously res with
155
164
| Choice1Of2 i -> i
156
165
| Choice2Of2 error -> failwithf $" Error: %A {error}"
157
166
158
- Oracle ( predict, feedback)
167
+ Oracle( predict, feedback)
159
168
160
169
while loop do
161
170
let! msg = webSocket.read ()
171
+
162
172
match msg with
163
173
| ( Text, data, true ) ->
164
174
let message = deserializeInputMessage data
175
+
165
176
match message with
166
177
| ServerStop -> loop <- false
167
178
| Start gameMap ->
168
179
printfn $" Start map {gameMap.MapName}, port {port}"
169
180
let stepsToStart = gameMap.StepsToStart
170
181
let stepsToPlay = gameMap.StepsToPlay
182
+
171
183
let aiTrainingOptions =
172
- {
173
- stepsToSwitchToAI = stepsToStart
174
- stepsToPlay = stepsToPlay
175
- defaultSearchStrategy =
176
- match gameMap.DefaultSearcher with
177
- | searcher.BFS -> BFSMode
178
- | searcher.DFS -> DFSMode
179
- | x -> failwithf $" Unexpected searcher {x}. Use DFS or BFS for now."
180
- serializeSteps = false
181
- mapName = gameMap.MapName
182
- oracle = Some oracle
183
- }
184
+ { stepsToSwitchToAI = stepsToStart
185
+ stepsToPlay = stepsToPlay
186
+ defaultSearchStrategy =
187
+ match gameMap.DefaultSearcher with
188
+ | searcher.BFS -> BFSMode
189
+ | searcher.DFS -> DFSMode
190
+ | x -> failwithf $" Unexpected searcher {x}. Use DFS or BFS for now."
191
+ serializeSteps = false
192
+ mapName = gameMap.MapName
193
+ oracle = Some oracle }
194
+
184
195
let options =
185
- VSharpOptions (
196
+ VSharpOptions(
186
197
timeout = 15 * 60 ,
187
198
outputDirectory = outputDirectory,
188
199
searchStrategy = SearchStrategy.AI,
189
200
aiAgentTrainingOptions = aiTrainingOptions,
190
201
stepsLimit = uint ( stepsToPlay + stepsToStart),
191
202
solverTimeout = 2
192
203
)
193
- let explorationResult =
194
- explore gameMap options
204
+
205
+ let explorationResult = explore gameMap options
195
206
196
207
Application.reset ()
197
- API.Reset ()
198
- HashMap.hashMap.Clear ()
208
+ API.Reset()
209
+ HashMap.hashMap.Clear()
210
+ Serializer.pathConditionVertices.Clear()
211
+
199
212
do !
200
213
sendResponse (
201
- GameOver (
214
+ GameOver(
202
215
explorationResult.ActualCoverage,
203
216
explorationResult.TestsCount,
204
217
explorationResult.ErrorsCount
205
218
)
206
219
)
220
+
207
221
printfn $" Finish map {gameMap.MapName}, port {port}"
208
222
| x -> failwithf $" Unexpected message: %A {x}"
209
223
@@ -215,65 +229,68 @@ let ws port outputDirectory (webSocket: WebSocket) (context: HttpContext) =
215
229
}
216
230
217
231
let app port outputDirectory : WebPart =
218
- choose
219
- [
220
- path " /gameServer" >=> handShake ( ws port outputDirectory)
221
- ]
232
+ choose [ path " /gameServer" >=> handShake ( ws port outputDirectory) ]
222
233
223
234
let generateDataForPretraining outputDirectory datasetBasePath ( maps : ResizeArray < GameMap >) stepsToSerialize =
224
235
for map in maps do
225
236
if map.StepsToStart = 0 u< step> then
226
237
printfn $" Generation for {map.MapName} started."
238
+
227
239
let map =
228
- GameMap (
240
+ GameMap(
229
241
map.StepsToPlay,
230
242
map.StepsToStart,
231
- Path.Combine ( datasetBasePath, map.AssemblyFullName),
243
+ Path.Combine( datasetBasePath, map.AssemblyFullName),
232
244
map.DefaultSearcher,
233
245
map.NameOfObjectToCover,
234
246
map.MapName
235
247
)
248
+
236
249
let aiTrainingOptions =
237
- {
238
- stepsToSwitchToAI = 0 u< step>
239
- stepsToPlay = 0 u< step>
240
- defaultSearchStrategy = searchMode.BFSMode
241
- serializeSteps = true
242
- mapName = map.MapName
243
- oracle = None
244
- }
250
+ { stepsToSwitchToAI = 0 u< step>
251
+ stepsToPlay = 0 u< step>
252
+ defaultSearchStrategy = searchMode.BFSMode
253
+ serializeSteps = true
254
+ mapName = map.MapName
255
+ oracle = None }
245
256
246
257
let options =
247
- VSharpOptions (
258
+ VSharpOptions(
248
259
timeout = 5 * 60 ,
249
260
outputDirectory = outputDirectory,
250
261
searchStrategy = SearchStrategy.ExecutionTreeContributedCoverage,
251
262
stepsLimit = stepsToSerialize,
252
263
solverTimeout = 2 ,
253
264
aiAgentTrainingOptions = aiTrainingOptions
254
265
)
266
+
255
267
let folderForResults =
256
268
Serializer.getFolderToStoreSerializationResult outputDirectory map.MapName
269
+
257
270
if Directory.Exists folderForResults then
258
- Directory.Delete ( folderForResults, true )
259
- let _ =
260
- Directory.CreateDirectory folderForResults
271
+ Directory.Delete( folderForResults, true )
272
+
273
+ let _ = Directory.CreateDirectory folderForResults
261
274
262
275
let explorationResult = explore map options
263
- File.WriteAllText (
264
- Path.Join ( folderForResults, " result" ),
276
+
277
+ File.WriteAllText(
278
+ Path.Join( folderForResults, " result" ),
265
279
$" {explorationResult.ActualCoverage} {explorationResult.TestsCount} {explorationResult.StepsCount} {explorationResult.ErrorsCount}"
266
280
)
281
+
267
282
printfn
268
283
$" Generation for {map.MapName} finished with coverage {explorationResult.ActualCoverage}, tests {explorationResult.TestsCount}, steps {explorationResult.StepsCount},errors {explorationResult.ErrorsCount}."
284
+
269
285
Application.reset ()
270
- API.Reset ()
271
- HashMap.hashMap.Clear ()
286
+ API.Reset()
287
+ HashMap.hashMap.Clear()
272
288
273
289
[<EntryPoint>]
274
290
let main args =
275
291
let parser =
276
- ArgumentParser.Create< CliArguments> ( programName = " VSharp.ML.GameServer.Runner.exe" )
292
+ ArgumentParser.Create< CliArguments>( programName = " VSharp.ML.GameServer.Runner.exe" )
293
+
277
294
let args = parser.Parse args
278
295
279
296
let mode = args.GetResult <@ Mode @>
@@ -298,19 +315,16 @@ let main args =
298
315
| Some steps -> steps
299
316
| None -> 500 u
300
317
301
- let useGPU =
302
- ( args.TryGetResult <@ UseGPU @>) .IsSome
318
+ let useGPU = ( args.TryGetResult <@ UseGPU @>) .IsSome
303
319
304
- let optimize =
305
- ( args.TryGetResult <@ Optimize @>) .IsSome
320
+ let optimize = ( args.TryGetResult <@ Optimize @>) .IsSome
306
321
307
- let outputDirectory =
308
- Path.Combine ( Directory.GetCurrentDirectory (), string port)
322
+ let outputDirectory = Path.Combine( Directory.GetCurrentDirectory(), string port)
309
323
310
324
if Directory.Exists outputDirectory then
311
- Directory.Delete ( outputDirectory, true )
312
- let testsDirInfo =
313
- Directory.CreateDirectory outputDirectory
325
+ Directory.Delete( outputDirectory, true )
326
+
327
+ let testsDirInfo = Directory.CreateDirectory outputDirectory
314
328
printfn $" outputDir: {outputDirectory}"
315
329
316
330
match mode with
@@ -319,11 +333,7 @@ let main args =
319
333
startWebServer
320
334
{ defaultConfig with
321
335
logger = Targets.create Verbose [||]
322
- bindings =
323
- [
324
- HttpBinding.createSimple HTTP " 127.0.0.1" port
325
- ]
326
- }
336
+ bindings = [ HttpBinding.createSimple HTTP " 127.0.0.1" port ] }
327
337
( app port outputDirectory)
328
338
with e ->
329
339
printfn $" Failed on port {port}"
0 commit comments