|
| 1 | +#' Simulate the run of a PCS model |
| 2 | +#' |
| 3 | +#' \code{PCS_run} simulates a PCS network given a pre-specified interconnection |
| 4 | +#' matrix and model parameters, according to the mechanism outlines by |
| 5 | +#' McClelland and Rumelhart (1981). |
| 6 | +#' |
| 7 | +#' @param interconnection_matrix A square, matrix representing the link weights |
| 8 | +#' between nodes, such that each entry w_ij represents the link strength |
| 9 | +#' between nodes i and j. Accordingly, for a network of n nodes, the matrix |
| 10 | +#' must be of six n*n. In most applications, the matrix will be symmetric, |
| 11 | +#' meaning that links are bidirectional. |
| 12 | +#' |
| 13 | +#' @param initial_state Initial node activations before the first iteration is |
| 14 | +#' run. In most cases, this will be a vector of zeros, with the length |
| 15 | +#' corresponding to the number of nodes in the network. |
| 16 | +#' |
| 17 | +#' @param resting_levels Resting activation level for each node. In most cases, |
| 18 | +#' this will be a vector of zeros, with its length corresponding to the number |
| 19 | +#' of nodes in the network. |
| 20 | +#' |
| 21 | +#' @param reset Vector denoting nodes with stable activation values. The vector |
| 22 | +#' contains a value for each node; if it is unequal to zero, the node |
| 23 | +#' activation will be reset to this value after each iteration. |
| 24 | +#' |
| 25 | +#' @param node_names Vector specifying human-readable labels for every node, or |
| 26 | +#' \code{'default'}, in which case nodes are automatically named. |
| 27 | +#' |
| 28 | +#' @param stability_criterion Stability theshold for convergence criteria. If |
| 29 | +#' energy changes across iterations fall below this threshold, the model is |
| 30 | +#' considered to have converged. |
| 31 | +#' |
| 32 | +#' @param max_iterations Maximum number of iterations to run before terminating |
| 33 | +#' the simulation. |
| 34 | +#' |
| 35 | +#' @param convergence_criteria Array of convergence criteria to apply. This PCS |
| 36 | +#' implementation allows users to define and observe multiple convergence |
| 37 | +#' criteria in one model. Each entry in this array is a convergence criterion, |
| 38 | +#' which is representated as a function that receives the current iteration, |
| 39 | +#' energy, model state history and the \code{stability_criterion} defined |
| 40 | +#' above and returns a boolean value representing whether the particular |
| 41 | +#' criterion is met given the model's current state. |
| 42 | +#' |
| 43 | +#' @param convergence_names Human-readable labels for the convergence criteria, |
| 44 | +#' or \code{'default'}, in which case the criteria are numbered automatically, |
| 45 | +#' in which case the criteria are numbered automatically. |
| 46 | +#' |
| 47 | +#' @return A list representing the model state after all convergence criteria |
| 48 | +#' have been fullfilled. The key \code{iterations} contains the model state |
| 49 | +#' over its entire run, while the key \code{convergence} defines which |
| 50 | +#' convergence criteria have been met at which iteration. Together, these |
| 51 | +#' provide an exhaustive summary of the model's behavior. |
| 52 | +#' |
| 53 | +#' @export |
| 54 | +PCS_run<-function(interconnection_matrix,initial_state,resting_levels,reset, |
| 55 | +node_names=NULL,stability_criterion=10^-6,max_iterations=Inf, |
| 56 | +convergence_criteria=c(PCS_convergence_McCandR),convergence_names=NULL) { |
| 57 | +# A note on the iteration counter: |
| 58 | +# The counter reflects the current line of the |
| 59 | +# model output, but the iterations start at zero. |
| 60 | +# Therefore, whenever iterations are output, |
| 61 | +# one is subtracted from this counter. |
| 62 | +# |
| 63 | +# This may seem silly, but it makes sense because |
| 64 | +# a) It is equivalent to the python output |
| 65 | +# b) The first output is not actually from the |
| 66 | +# first iteration, rather nothing has happened |
| 67 | +# at that point. |
| 68 | +iteration<-1 |
| 69 | + |
| 70 | +# How many convergence criteria are going to be |
| 71 | +# applied? |
| 72 | +n_criteria<- length(convergence_criteria) |
| 73 | + |
| 74 | +# Name the criteria, if that has not already happened |
| 75 | +if (is.null(convergence_names)) { |
| 76 | +convergence_names<- paste("criterion_",1:n_criteria,sep="") |
| 77 | + } |
| 78 | + |
| 79 | +# Initialize the model state |
| 80 | +state<-initial_state |
| 81 | +state<- PCS_reset(state,reset) |
| 82 | +nodes<- length(state) |
| 83 | +energy<- PCS_energy(interconnection_matrix,state) |
| 84 | + |
| 85 | +# Create the matrix in which we will save |
| 86 | +# the data from the model iterations |
| 87 | +memory.ma<- PCS_memory_create(nodes,node_names) |
| 88 | +memory.ma[iteration,]<- c(iteration-1,energy,state) |
| 89 | + |
| 90 | +# Create the matrix in which we will save |
| 91 | +# convergence data |
| 92 | +convergence.ma<-matrix(ncol=n_criteria,nrow=nrow(memory.ma)) |
| 93 | + colnames(convergence.ma)<-convergence_names |
| 94 | +convergence.ma[1, ]<-TRUE |
| 95 | + |
| 96 | +# This is the main model evaluation loop |
| 97 | +continue=TRUE |
| 98 | +while (continue==TRUE&iteration<=max_iterations) { |
| 99 | +# Increment the counter |
| 100 | +iteration=iteration+1 |
| 101 | + |
| 102 | +# Compute the new model state and energy |
| 103 | +state<- PCS_iterate(interconnection_matrix,state,resting_levels)[1:nodes] |
| 104 | +state<- PCS_reset(state,reset) |
| 105 | +energy<- PCS_energy(interconnection_matrix,state) |
| 106 | + |
| 107 | +# Write the current state into the matrix |
| 108 | +memory.ma[iteration, ]<- c(iteration-1,energy,state) |
| 109 | + |
| 110 | +# Expand the output matrix if necessary |
| 111 | +if (PCS_memory_needs_expansion(memory.ma,iteration)) { |
| 112 | +memory.ma<- PCS_matrix_expand(memory.ma,iteration) |
| 113 | +convergence.ma<- PCS_matrix_expand(convergence.ma,iteration) |
| 114 | + } |
| 115 | + |
| 116 | +# Check if the model has converged yet, using |
| 117 | +# the given criterion functions |
| 118 | +for (fin1:n_criteria) { |
| 119 | +convergence.ma[iteration,f]<-convergence_criteria[[f]]( |
| 120 | +iteration=iteration, |
| 121 | +current_energy=energy, |
| 122 | +memory.matrix=memory.ma, |
| 123 | +stability_criterion=stability_criterion, |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | +# Continue until all criteria are converged |
| 128 | +continue<- (sum(convergence.ma[iteration,]*1)>0) |
| 129 | + } |
| 130 | + |
| 131 | +# Prepare and pass along the model output |
| 132 | +memory.ma<- PCS_matrix_trunc(memory.ma) |
| 133 | +memory.df<- as.data.frame(memory.ma) |
| 134 | + |
| 135 | +convergence.ma<- PCS_matrix_trunc(convergence.ma) |
| 136 | + |
| 137 | +output<-list() |
| 138 | +output$iterations<-memory.df |
| 139 | + |
| 140 | +# Note that the MPI Coll implementation will always assume |
| 141 | +# one more iteration. To match their simulations, |
| 142 | +# add "+ 1" in the next line |
| 143 | +output$convergence<- colSums(convergence.ma) |
| 144 | + |
| 145 | +return(output) |
| 146 | +} |
| 147 | + |
| 148 | +#' Simulate the run of a PCS model based on only the interconnection matrix |
| 149 | +#' |
| 150 | +#' \code{PCS_run_from_interconnections} simulates a PCS network given \emph{only} |
| 151 | +#' the pre-specified interconnection matrix and convergence criteria, substituting |
| 152 | +#' default values from the literature for all other parameters. Thereby, it |
| 153 | +#' provides a convenient shorthand for the \link{PCS_run} function that covers |
| 154 | +#' the vast majority of applications. |
| 155 | +#' |
| 156 | +#' @inheritParams PCS_run |
| 157 | +#' |
| 158 | +#' @export |
| 159 | +PCS_run_from_interconnections<-function(interconnection_matrix, |
| 160 | +convergence_criteria=c(PCS_convergence_McCandR),convergence_names="default") { |
| 161 | +# This function is just a simplification to speed up my work |
| 162 | + |
| 163 | +# Infer the number of nodes from the matrix size |
| 164 | +nodes<- nrow(interconnection_matrix) |
| 165 | + |
| 166 | +# This function assumes that the initial state, |
| 167 | +# as well as the resting levels, are all set to zero |
| 168 | +resting_levels<- rep(0,nodes) |
| 169 | +state<- rep(0,nodes) |
| 170 | + |
| 171 | +# We assume that the first node (and only that) |
| 172 | +# has a constant activation |
| 173 | +reset<- c(1, rep(0,nodes-1)) |
| 174 | + |
| 175 | +# Good to go! :-) |
| 176 | +return(PCS_run(interconnection_matrix,state,resting_levels,reset, |
| 177 | +convergence_criteria=convergence_criteria,convergence_names=convergence_names)) |
| 178 | +} |