# Handles IO of network files (archiving/dearchiving) # NetIO.tz (c) 2010 Jacob Schrum. # See Simulation.tz for more copyright information @use Abstract. @use File. Abstract : NetIO { + variables: file-path (string). last-file-saved (string). last-path-saved (string). num-loaded-networks (int). unloaded-networks (int). # Trivial archiving, since state is simple + to archive: return 1. + to dearchive: return 1. + to init: file-path = "". last-file-saved = "". last-path-saved = "". num-loaded-networks = 0. unloaded-networks = 0. + to set-file-path to fp (string): file-path = fp. + to set-last-file-saved to file (string): last-file-saved = file. + to set-last-path-saved to fp (string): last-path-saved = fp. + to get-last-file-saved: return last-file-saved. + to get-last-path-saved: return last-path-saved. # name: filename without extension # commandline: Command line handler for the simulation # fitness-info-list: list containing all fitness info # additional-input-info: list containing neural network inputs + to write-save-file with-identifier name (string) commandline-options commandline (object) fitnesses fitness-info-list (list) inputs additional-input-info (list) logs log (object): i (int). save-file (object). key (string). val (double). valString (string). valList (list). string-options (hash). numeric-options (hash). on-off-options (hash). list-options (hash). log-objects (list). string-options = (commandline get-command-line-string-options). numeric-options = (commandline get-command-line-numeric-options). on-off-options = (commandline get-command-line-on-off-options). list-options = (commandline get-command-line-list-options). save-file = new File. save-file open-for-appending with-file "$file-path/$name.save". controller set-last-file-saved to name. controller set-last-path-saved to file-path. foreach key in keys( string-options ) : { valString = string-options{key}{COMMAND_LINE_VAL}. save-file write-line text "$key". save-file write-line text "$valString". } foreach key in keys( numeric-options ) : { val = numeric-options{key}{COMMAND_LINE_VAL}. save-file write-line text "$key". save-file write-line text "$val". } foreach key in keys( on-off-options ) : { val = on-off-options{key}{COMMAND_LINE_VAL}. save-file write-line text "$key". save-file write-line text "$val". } foreach key in keys( list-options ) : { valList = list-options{key}{COMMAND_LINE_VAL}. save-file write-line text "$key". save-file write-line text "list start". for i = 0, i < |valList|, i++ : { valString = valList{i}. if (valString{0} == ":") : { save-file write-line text "$valString". } else { val = valList{i}. save-file write-line text "$val". } } save-file write-line text "list end". } save-file write-line text "Fitnesses". for i = 0, i < |fitness-info-list|, i++ : { save-file write-line text (fitness-info-list{i}{SLOT_DISPLAY_LABEL}). } save-file write-line text "End". save-file write-line text "ObjectiveUsage". for i = 0, i < |fitness-info-list|, i++ : { save-file write-line text (fitness-info-list{i}{SLOT_USE_OBJECTIVE}). } save-file write-line text "End". save-file write-line text "Goals". for i = 0, i < |fitness-info-list|, i++ : { save-file write-line text (fitness-info-list{i}{SLOT_GOAL}). } save-file write-line text "End". save-file write-line text "Inputs". for i = 0, i < |additional-input-info|, i++ : { if (additional-input-info{i}{INPUT_SLOT_USE} == INPUT_ON) : { save-file write-line text additional-input-info{i}{INPUT_SLOT_METHOD}. } } save-file write-line text "End". save-file write-line text "InputArgs". for i = 0, i < |additional-input-info|, i++ : { if (additional-input-info{i}{INPUT_SLOT_USE} == INPUT_ON) : { save-file write-line text additional-input-info{i}{INPUT_SLOT_ARGS}. } } save-file write-line text "End". save-file write-line text "InputRange". for i = 0, i < |additional-input-info|, i++ : { if (additional-input-info{i}{INPUT_SLOT_USE} == INPUT_ON) : { valList = additional-input-info{i}{INPUT_SLOT_RANGE}. if (|valList| == 0) : save-file write-line text "None". else { save-file write-line text (valList{0}). #save-file write-line text (valList{1}). } } } save-file write-line text "End". log-objects = (log get-log-objects). save-file write-line text "LogObjects". for i = 0, i < |log-objects|, i++ : { save-file write-line text (log-objects{i}{LOG_SLOT_LABEL}). } save-file write-line text "End". save-file write-line text "LogArguments". for i = 0, i < |log-objects|, i++ : { save-file write-line text (log-objects{i}{LOG_SLOT_ARGS}). } save-file write-line text "End". save-file close. free save-file. # name: filename without extension # commandline: The Command line handler for the simulation # fitness-info-list: list containing all fitness info # additional-input-info: list containing neural network inputs # parent-fitnesses: fitness scores for parents # child-fitnesses: fitness scores for children # child-brains: neural network controllers for children + to write-networks with-identifier name (string) commandline-options commandline (object) fitnesses fitness-info-list (list) inputs additional-input-info (list) parent-fitness-list parent-fitnesses (list) child-fitness-list child-fitnesses (list) children child-brains (list) parents parent-brains (list) logs log (object): k (int). self write-save-file with-identifier name commandline-options commandline fitnesses fitness-info-list inputs additional-input-info logs log. self write-current-scores with-identifier "$name" extension "roster" fitnesses fitness-info-list parent-fitness-list parent-fitnesses child-fitness-list child-fitnesses parents parent-brains children child-brains. for k = 0, k < |child-fitnesses|, k++ : { self write-networks from (child-brains{k}) with name and "$k.children". } for k = 0, k < |parent-fitnesses|, k++ : { self write-networks from (parent-brains{k}) with name and "$k.parents". } # name: filename without extension # fitness-info-list: list containing all fitness info # parent-fitnesses: fitness scores for parents # child-fitnesses: fitness scores for children + to write-current-scores with-identifier name (string) extension ext (string) fitnesses fitness-info-list (list) parent-fitness-list parent-fitnesses (list) child-fitness-list child-fitnesses (list) parents parent-brains (list) children child-brains (list) write-children bit = 1 (int): i (int). j (int). k (int). temp (string). temp2 (string). tempDouble (string). tempList (list). pop-file (object). true-pop-file (object). history-file (object). pop-file = new File. true-pop-file = new File. history-file = new File. pop-file open-for-appending with-file "$file-path/$name.$ext". true-pop-file open-for-appending with-file "$file-path/OldScores-$name.$ext". history-file open-for-appending with-file "$file-path/$name.history". temp = "#". for i = 0, i < |fitness-info-list|, i++ : { temp2 = fitness-info-list{i}{SLOT_DISPLAY_LABEL}. temp = "$temp$temp2\t". } pop-file write-line text "$temp". true-pop-file write-line text "$temp". pop-file write-line text "#Parents". true-pop-file write-line text "#Parents". history-file write-line text "#Parents". for k = 0, k < |parent-fitnesses|, k++ : { pop-file write-line text "#Sub-pop: $k". true-pop-file write-line text "#Sub-pop: $k". history-file write-line text "#Sub-pop: $k". for i = 0, i < |parent-fitnesses{k}|, i++ : { temp = "". temp2 = "". tempList = ((parent-brains{k}{i}) get-average-scores). for j = 0, j < |parent-fitnesses{k}{i}|, j++ : { tempDouble = parent-fitnesses{k}{i}{j}. temp = "$temp$tempDouble\t". if (|tempList| > j) : { tempDouble = tempList{j}. temp2 = "$temp2$tempDouble\t". } } tempList = ((parent-brains{k}{i}) get-number-evaluations). temp2 = "$temp2$tempList\t". tempList = ((parent-brains{k}{i}) get-standard-devs). temp2 = "$temp2$tempList\t". pop-file write-line text "$temp". true-pop-file write-line text "$temp2". # Write history info temp = ((parent-brains{k}{i}) get-id). tempList = ((parent-brains{k}{i}) get-parent-ids). for j = 0, j < |tempList|, j++ : { temp2 = tempList{j}. temp = "$temp\t$temp2". } history-file write-line text "$temp". } } if bit: { pop-file write-line text "#Children". true-pop-file write-line text "#Children". history-file write-line text "#Children". for k = 0, k < |child-fitnesses|, k++ : { pop-file write-line text "#Sub-pop: $k". true-pop-file write-line text "#Sub-pop: $k". history-file write-line text "#Sub-pop: $k". for i = 0, i < |child-fitnesses{k}|, i++ : { temp = "". temp2 = "". tempList = ((child-brains{k}{i}) get-average-scores). for j = 0, j < |child-fitnesses{k}{i}|, j++ : { tempDouble = child-fitnesses{k}{i}{j}. temp = "$temp$tempDouble\t". if (|tempList| > j) : { tempDouble = tempList{j}. temp2 = "$temp2$tempDouble\t". } } tempList = ((child-brains{k}{i}) get-number-evaluations). temp2 = "$temp2$tempList\t". tempList = ((child-brains{k}{i}) get-standard-devs). temp2 = "$temp2$tempList\t". pop-file write-line text "$temp". true-pop-file write-line text "$temp2". # Write history info temp = ((child-brains{k}{i}) get-id). tempList = ((child-brains{k}{i}) get-parent-ids). for j = 0, j < |tempList|, j++ : { temp2 = tempList{j}. temp = "$temp\t$temp2". } history-file write-line text "$temp". } } } pop-file close. true-pop-file close. history-file close. free pop-file. free true-pop-file. free history-file. print "Scores written to file: $name.$ext". # brains: parent-brains or child-brains # name: filename without extension # extension: filename extension + to write-networks from brains (list) with name (string) and extension (string): i (int). pop-file (object). dash (string). dash = "-". pop-file = new File. pop-file open-for-appending with-file "$file-path/$name.$extension". for i = 0, i < |brains|, i++ : { if (brains{i}) : # protect against null pointers { brains{i} archive-as-xml file "$file-path/$name$dash$extension$i.xml". } else { print "Error! Tried to write an empty network". print "in write-networks". print "i = $i". print "name = $name". print "extension = $extension". print "brains{i} =", brains{i}. print "brains =", brains. controller end-simulation. return. } # Reclaim space used to archive brains{i} blank-archive. pop-file write-line text "$name$dash$extension$i.xml". } pop-file close. free pop-file. # fileSet: filename without extension # loaded-fitnesses: list to contain the fitnesses to load # loaded-inputs: list to contain inputs to load # loaded-inputs-args: list to contain arguments for each input # commandline: Command line handler for the simulation # - this method loads settings into the appropriate containers via side-effects # - loaded-fitnesses should be set to {} before calling this method # - loaded-inputs should be set to {} before calling this method + to load-settings from fileSet (string) fitnesses loaded-fitnesses (list) inputs loaded-inputs (list) input-args loaded-input-args (list) input-ranges loaded-input-ranges (list) commandline-options commandline (object) log-objects loaded-log-objects (list) log-args loaded-log-args (list): save-file (object). var (string). val (double). valString (string). valList (list). string-options (hash). numeric-options (hash). on-off-options (hash). list-options (hash). i (int). string-options = (commandline get-command-line-string-options). numeric-options = (commandline get-command-line-numeric-options). on-off-options = (commandline get-command-line-on-off-options). list-options = (commandline get-command-line-list-options). print "Loading from save file: $fileSet.save". save-file = new File. save-file open-for-appending with-file "$fileSet.save". save-file close. save-file open-for-reading with-file "$fileSet.save". while ((save-file is-end-of-file) == 0) : { var = (save-file read-line). if (var == "Fitnesses") : { var = (save-file read-line). while (var != "End") : { # Objectives are set ON for backwards compatibility. # Whether or not the objective is actually on is controlled # by the ObjectiveUsage list below, if it is present. push {var,FITNESS_OBJECTIVE_ON,0} onto loaded-fitnesses. var = (save-file read-line). } } else if (var == "LogObjects") : { var = (save-file read-line). while (var != "End") : { push var onto loaded-log-objects. var = (save-file read-line). } } else if (var == "LogArguments") : { var = (save-file read-line). while (var != "End") : { valList = {}. if var != "{ }" : { # Assumes only one argument # remove braces var{0} = "". var { (|var| - 1) } = "". #print "Loading args for:", (loaded-log-objects{ (|loaded-log-args|) }). if ((loaded-log-objects{ (|loaded-log-args|) } == "PartitionSize") || (loaded-log-objects{ (|loaded-log-args|) } == "PartitionTaken")) : { var{0} = "". var { (|var| - 1) } = "". valList = {var}. } else { val = var. valList = {val}. } } push valList onto loaded-log-args. var = (save-file read-line). } } else if (var == "ObjectiveUsage") : { i = 0. var = (save-file read-line). while (var != "End") : { val = var. loaded-fitnesses{i}{LOAD_FITNESS_USE_OBJECTIVE} = val. var = (save-file read-line). i++. } } else if (var == "Goals") : { i = 0. var = (save-file read-line). while (var != "End") : { val = var. loaded-fitnesses{i}{LOAD_FITNESS_GOAL} = val. var = (save-file read-line). i++. } } else if (var == "Inputs") : { var = (save-file read-line). while (var != "End") : { push var onto loaded-inputs. var = (save-file read-line). } } else if (var == "InputArgs") : { var = (save-file read-line). while (var != "End") : { valList = {}. if var != "{ }" : { # Assumes only one argument # remove braces var{0} = "". var { (|var| - 1) } = "". val = var. valList = {val}. } push valList onto loaded-input-args. var = (save-file read-line). } } else if (var == "InputRange") : { var = (save-file read-line). while (var != "End") : { valList = {}. if var != "None" : { val = var. push val onto valList. #var = (save-file read-line). #val = var. #push val onto valList. } push valList onto loaded-input-ranges. var = (save-file read-line). } } else { valString = (save-file read-line). if numeric-options{var} : { val = valString. numeric-options{var}{COMMAND_LINE_VAL} = val. } else if string-options{var} : string-options{var}{COMMAND_LINE_VAL} = valString. else if on-off-options{var} : { val = valString. on-off-options{var}{COMMAND_LINE_VAL} = val. } else if list-options{var} : { # valString should equal "list start" valList = {}. valString = (save-file read-line). while (valString != "list end") : { if (valString{0} == ":") : { push valString onto valList. } else { val = valString. push val onto valList. } valString = (save-file read-line). } list-options{var}{COMMAND_LINE_VAL} = valList. valString = valList. } if (|var| > 1) : print "$var = $valString". } } save-file close. free save-file. # filePath: directory with files # fileSet: name of files to load # group: "parents" or "children" (group to load) # number-of-subpops: the number of subpopulations to divide loaded networks into # subpop-size: size of each subpopulation # parent-brains: where to store the loaded networks # - parent-brains is modified via side-effects # - parent-brains must have the appropriate dimensions to store all loaded networks + to load-population from-path filePath (string) files fileSet (string) type group (string) subpops number-of-subpops (int) sub-size subpop-size (int) parent-list parent-brains (list): i (int). j (int). pop-file (object). net-file (string). pop-file = new File. for i = 0, i < number-of-subpops, i++ : { pop-file open-for-reading with-file "$filePath/$fileSet.$i.$group". for j = 0, j < subpop-size, j++ : { net-file = (pop-file read-line). if (|net-file| != 0) : { num-loaded-networks++. print "Loading network $filePath/$net-file into slot {$i}{$j}". if parent-brains{i}{j}: free parent-brains{i}{j}. parent-brains{i}{j} = (controller dearchive-xml file "$filePath/$net-file"). print "Input connectivity:", (parent-brains{i}{j} get-input-connectivity). } else { unloaded-networks = 1. } } pop-file close. } free pop-file. + to get-unloaded-networks: return unloaded-networks. + to get-num-loaded-networks: return num-loaded-networks. }