# Ensemble of neural networks # BrainPool.tz (c) 2010 Jacob Schrum. # See Simulation.tz for more copyright information @include "Abstract.tz". @include "File.tz". @include "Constants.tz". @include "FreeNetwork.tz". Abstract : BrainPool { + variables: brain-pool (list). nodes-per-policy (int). choose-one-representative (int). + to init: brain-pool = {}. if ((controller get-commandline) bit-param named "action-selector-net") : nodes-per-policy = OUTPUTS_ACTION_SELECTOR. else nodes-per-policy = OUTPUTS_STANDARD. choose-one-representative = ((controller get-commandline) bit-param named "best-of-brain-pool"). # The other option is to simply create new outputs using # the outputs of the other networks as inputs. + to chooses-one-representative: return choose-one-representative. + to count-file-lines from-path filePath (string) files fileSet (string) type group = "parents" (string): lines (int). pop-file (object). # Don't count last line lines = -1. pop-file = new File. pop-file open-for-reading with-file "$filePath/$fileSet.0.$group". while !(pop-file is-end-of-file) : { pop-file read-line. lines++. } pop-file close. free pop-file. print "Load file contains $lines networks". return lines. # Load pool of NNs to be used in ensemble # filePath: directory with files # fileSet: name of files to load # group: "parents" or "children" (group to load) # size: number of networks in population (pareto front) to load + to load-brain-pool loader netIO (object) from-path filePath (string) files fileSet (string) type group = "parents" (string): i (int). size (int). size = (self count-file-lines from-path filePath files fileSet type group). free brain-pool. brain-pool = { {} }. for i = 0, i < size, i++ : { push {} onto (brain-pool{0}). } netIO load-population from-path filePath files fileSet type group subpops 1 sub-size size parent-list brain-pool. brain-pool = (brain-pool{0}). # Have each network process inputs. # - Size of inputs must equal number of inputs to network + to activate-brain-pool with inputs (list): i (int). for i = 0, i < |brain-pool|, i++ : { brain-pool{i} run-with inputs inputs. } # Outputs from a specific member. Should only call # after "activate-brain-pool" + to get-outputs-of-member at position (int): # SMEG: May have to tweak this to work with ModeMutation in the future return (brain-pool{position} get-all-outputs). + to get-brain-pool-size: return ( |brain-pool| ). + to get-number-brain-pool-outputs: #print "Brain pool size:", |brain-pool|. #print "nodes-per-policy:", nodes-per-policy. return ( |brain-pool| * nodes-per-policy ). + to get-all-brain-pool-outputs: i (int). j (int). all-outputs (list). individual-outputs (list). all-outputs = {}. for i = 0, i < |brain-pool|, i++ : { individual-outputs = (self get-outputs-of-member at i). for j = 0, j < |individual-outputs|, j++ : { push ( individual-outputs{j} ) onto all-outputs. } } return all-outputs. + to get-representative outputs out (list): i (int). best-value (double). rep (int). temp (double). best-value = -INFINITY. for i = 0, i < |out|, i++ : { temp = (out{i}). if (temp > best-value) : { best-value = temp. rep = i. } } return rep. }