automater
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
This is a simple example of automating operation of a model by reading in a list of commands from a file and then executing them.
It illustrates the use of one input and two output files.
This might be used for running validation on a model. It is not intended to replace BehaviorSpace.
HOW IT WORKS
This example just creates some turtles. It
- reads commands from an input file,
- executes the commands,
- logs the steps into a log file as they are done, and
- saves output into an output file
Useful header information, such as date-and-time and the names of the files used for commands, logging, and output is written into the log and output files.
HOW TO USE IT
make a text file with a list of commands you want executed
set the names of your working-directory, command-file, log-file, and input-file in the set-up-files section
If all files are in the local directory, you can set working-directory to either the full path name, or to ""
run setup. In the example, setup also runs the go step, leaving the go step clean for you to use.
Important notes:
The log file and output file are destroyed each run to start clean.
You can set the run-title in the setup section
You can suppress the printing of lines in the automate-run section by commenting out the obvious print statements
THINGS TO NOTICE
- by default, the model is verbose, since you might want to confirm that it is doing what you think it is doing. It's easy to comment out the print statements to make the output cleaner.
- you can run the go command once or many times from the command file, using a repeat command.
- The example runs the go command 4 times as soon as you run setup,
because "automate-run" is called in the setup section
THINGS TO TRY
EXTENDING THE MODEL
NETLOGO FEATURES
Illustrates File I/O with multiple files open at once
RELATED MODELS
CREDITS AND REFERENCES
Model was created by R. Wade Schuette, 10-November-2019. No copyright is claimed.
Comments and Questions
;; Please read HOW TO USE IT in the INFO tab! ;; the sample command file is called test-01-input.txt and contains the following lines, without the ;; semicolons ;; ;; print "This is just a test command file" ;; repeat 4 [go] ;; clean-up-files stop ;; ;; this code will generate such a file if you want it to, over-writing any old one of that name globals [ working-directory ;; the operating system working directory command-file ;; where the commands will be read from output-file ;; optional, where output from the go step can be written log-file ;; a log of what commands were executed run-title ;; whatever you want to name this run show-commands-as-run? ;; controls whether you want to see commands as they are run use-sample-input-file? ;; if true, generates a file called test-01-input.txt to use ;; as input for this model. ;; This will destroy an existing file of that name! ] to setup clear-all set run-title "Automated validation example" print (word run-title "\n" "at " date-and-time "\n" ) set show-commands-as-run? false ;; shows each command as it is read in from the command file set-up-files init-log reset-ticks automate-run end to automate-run if not file-exists? command-file [ type "Can't find the command file: " print command-file stop] print " " print (word ".....show-commands-as-run? is set to " show-commands-as-run? ) print ".....Reading command input now..." repeat 10000 ;; reads this many lines from the command-file, if available. [ ;; read the next command from the command file file-open command-file if file-at-end? [ clean-up-files stop] let line file-read-line ;; track progress in the log file file-open log-file ;; ok to comment out the next line file-print (word "Executing: " line) ;; track the upcoming command in the Command Center if you want to ;; ok to comment out the next line if show-commands-as-run? = true [print (word "..................................Executing: " line) ] ;; execute that command run line ;; track completion of the command in the Command Center if you want to ;; print (word "...............................Executed: " line) ] end to go create-turtles 1 [ set size 3 setxy random-xcor random-ycor] ;; your code may produce output. Here's how to do that file-open output-file file-print "executed the go command once" tick end to set-up-files file-close-all ;; replace these with the path name and file-names of your own files set working-directory "C:\\Users\\condor\\Google Drive\\ABModels\\models\\netlogo\\caveman\\versions\\automater" if (working-directory = "C:\\Users\\condor\\Google Drive\\ABModels\\models\\netlogo\\caveman\\versions\\automater") [ user-message "You MUST set your own working directory in the set-up-files section!"] ;; possibly ;; set working-directory "" ;; use the directory containing the model engine itself set command-file "test-01-input.txt" ;; this file should exist set output-file "test-01-output.txt" ;; this can be missing. If it exists it will be replaced. set log-file "test-01-log.txt" ;; this can be missing. If it exists it will be replaced. print ("Using these files defined in set-up-files ") print (word "working directory: " working-directory) print (word "commands : " command-file ) print (word "output : " output-file) print (word "log : " log-file ) set-current-directory working-directory ;; has to be done where file-at-end? is trested ;; open files for writing if file-exists? log-file [ file-delete log-file ] file-open log-file file-print run-title file-print date-and-time file-print " " if file-exists? output-file [ file-delete output-file ] file-open output-file file-print run-title file-print date-and-time file-print " " end to init-log file-open log-file file-print (word "current directory: " working-directory) file-print (word "commands : " command-file) file-print (word "output :" output-file) file-print (word "log :" log-file) file-print " " end to clean-up-files file-close-all print ".....Finished reading control input" print ".....Cleaning up" end ;; Written by R. Wade Schuette 10-November-2019 ;; no copyright is claimed.
There is only one version of this model, created almost 6 years ago by R. Wade Schuette.
Attached files
No files
This model does not have any ancestors.
This model does not have any descendants.
R. Wade Schuette
This won't run over the web
This model uses File I/O which means you cannot run it over the web. If you download it, it should run just fine
Posted almost 6 years ago
R. Wade Schuette
Help! The command file is missing!
You can generate your own command file, such as the one called test-01-input.txt in the model. Three sample lines are shown at the top of the code in the notes. Just copy and paste those lines into a new text file, remove the semicolons, and save the file.
Posted almost 6 years ago