def measureExecutionTime(runsNeeded, threadCount): startTime = int(round(time.time() * 1000)) runsDone = 0 runsStarted = 0 allResults = [] # Create and reuse threadCount number of workspaces for i in range(0, threadCount): workspace = nl4py.newNetLogoHeadlessWorkspace() workspace.openModel('models/Wolf Sheep Predation.nlogo') simulate(workspace) runsStarted = runsStarted + 1 # repeat until runsNeeded is satisfied while (runsDone < runsNeeded): # check if workspaces are done. If so, get results and # start another model run on the workspace, keeping the # number of workspaces constant for workspace in nl4py.getAllHeadlessWorkspaces(): newResults = workspace.getScheduledReporterResults() # getScheduledReporterResults() is non-blocking # and can return an empty list if the model is # still running. Check that the model is done by # checking if the list is not empty. if len(newResults) > 0: allResults.extend(newResults) runsDone = runsDone + 1 if runsStarted < runsNeeded: # start another simulation if required. simulate(workspace) runsStarted = runsStarted + 1 stopTime = int(round(time.time() * 1000)) return (stopTime - startTime)
print('\n2) Starting the model runs... ') model = "C://Users//Mateen Qureshi//Documents//CSCI378//AFM Initial Migration.nlogo" n = None print( '\n Creating ' + str(n) + ' NetLogo HeadlessWorkspaces with: workspace = nl4py.newNetLogoHeadlessWorkspace()\n and clearing any old workspaces with nl4py.deleteAllHeadlessWorkspaces()' ) print( '\n Opening the ' + model + ' model on the NetLogo HeadlessWorkspace with: workspace.openModel("model")' ) nl4py.deleteAllHeadlessWorkspaces() workspace = nl4py.newNetLogoHeadlessWorkspace() workspace.openModel(model) print( "\n Setting the parameters to random values with workspace.setParamsRandom()" ) workspace.setParamsRandom() # workspace.command('set model-version "sheep-wolves-grass"') print('\n Send setup command to model using: workspace.command("setup")') workspace.command("setup") print( '\n Schedule reporters to the model to report the ticks passed, the model\'s two stop conditions and number of sheep and wolves for each tick for 100 ticks,\n using: workspace.scheduleReportersAndRun(reporters,0,1,100,"go")' )