@@ -117,7 +117,7 @@ check_prob_vector <- function(vec, name) {
117117 if (! is.numeric(vec )) {
118118 stop(' Routing vector "' , name , ' " must be numeric.' , call. = FALSE )
119119 }
120- if (any(vec < 0 | vec > 1 )) {
120+ if (any(vec < 0L | vec > 1L )) {
121121 stop(' All values in routing vector "' , name , ' " must be between 0 and 1.' ,
122122 call. = FALSE )
123123 }
@@ -128,54 +128,88 @@ check_prob_vector <- function(vec, name) {
128128}
129129
130130
131- # ' Validate parameter values.
131+ # ' Check if a value is a positive integer
132132# '
133- # ' Ensure that specific parameters are positive numbers, or non-negative
134- # ' integers, and that probability vectors are in [0, 1] and sum to 1 (within
135- # ' tolerance).
133+ # ' Throws an error if the value is not a positive integer (> 0).
136134# '
137- # ' @param param List containing parameters for the simulation.
135+ # ' @param x The value to check.
136+ # ' @param name The name of the parameter (for error messages).
138137# '
139- # ' @return Throws an error if any specified parameter value is invalid .
138+ # ' @return None. Throws an error if the check fails .
140139# ' @export
141140
142- check_param_values <- function (param ) {
143-
144- # Check that number of runs is positive integer
145- if (
146- ! is.null(" number_of_runs" ) &&
147- " number_of_runs" < = 0L &&
148- " number_of_runs" %% 1L == 0L
149- ) {
150- stop(' The parameter "number_of_runs" must be integer greater than 0.' ,
151- call. = FALSE )
141+ check_positive_integer <- function (x , name ) {
142+ if (is.null(x ) || x < = 0L || x %% 1L != 0L ) {
143+ stop(
144+ sprintf(' The parameter "%s" must be an integer greater than 0.' , name ),
145+ call. = FALSE
146+ )
152147 }
148+ }
153149
154- # Check that listed parameters are not negative (i.e. must be 0+)
155- non_neg <- c(" asu_arrivals" , " rehab_arrivals" , " asu_los" , " rehab_los" )
156- for (sub in non_neg ) {
157- if (! is.null(param [[sub ]])) {
158- vals <- unlist(param [[sub ]])
159- if (any(vals < = 0L )) {
160- stop(' All values in "' , sub , ' " must be greater than 0.' , call. = FALSE )
161- }
162- }
150+ # ' Check if all values are positive
151+ # '
152+ # ' Throws an error if any value in the input is not positive (> 0).
153+ # '
154+ # ' @param x The vector or list of values to check.
155+ # ' @param name The name of the parameter (for error messages).
156+ # '
157+ # ' @return None. Throws an error if the check fails.
158+ # ' @export
159+
160+ check_all_positive <- function (x , name ) {
161+ if (! is.null(x ) && any(unlist(x ) < = 0L )) {
162+ stop(
163+ sprintf(' All values in "%s" must be greater than 0.' , name ),
164+ call. = FALSE
165+ )
163166 }
167+ }
164168
165- # Check that listed parameters are non-negative integers
166- n_list <- c(" warm_up_period" , " data_collection_period" )
167- for (n in n_list ) {
168- if (param [[n ]] < 0L || param [[n ]] %% 1L != 0L ) {
169- stop(' The parameter "' , n ,
170- ' " must be an integer greater than or equal to 0.' , call. = FALSE )
171- }
169+ # ' Check if a value is a non-negative integer
170+ # '
171+ # ' Throws an error if the value is not a non-negative integer (>= 0).
172+ # '
173+ # ' @param x The value to check.
174+ # ' @param name The name of the parameter (for error messages).
175+ # '
176+ # ' @return None. Throws an error if the check fails.
177+ # ' @export
178+
179+ check_nonneg_integer <- function (x , name ) {
180+ if (is.null(x ) || x < 0L || x %% 1L != 0L ) {
181+ stop(
182+ sprintf(' The parameter "%s" must be an integer >= 0.' , name ),
183+ call. = FALSE
184+ )
172185 }
186+ }
187+
188+ # ' Validate parameter values
189+ # '
190+ # ' Ensures that specific parameters are positive numbers, non-negative integers,
191+ # ' and that probability vectors are in [0, 1] and sum to 1 (within tolerance).
192+ # '
193+ # ' @param param List containing parameters for the simulation.
194+ # '
195+ # ' @return None. Throws an error if any specified parameter value is invalid.
196+ # ' @export
197+
198+ check_param_values <- function (param ) {
199+ check_positive_integer(param [[" number_of_runs" ]], " number_of_runs" )
200+
201+ lapply(c(" asu_arrivals" , " rehab_arrivals" , " asu_los" , " rehab_los" ),
202+ function (nm ) check_all_positive(param [[nm ]], nm ))
203+
204+ lapply(c(" warm_up_period" , " data_collection_period" ),
205+ function (nm ) check_nonneg_integer(param [[nm ]], nm ))
173206
174- # Check that listed parameters are between 0 and 1 and sum to 1 (+- 0.01)
175207 for (routing in c(" asu_routing" , " rehab_routing" )) {
176- for (name in names(param [[routing ]])) {
177- vec <- unlist(param [[routing ]][[name ]], use.names = FALSE )
178- check_prob_vector(vec , paste0(routing , " $" , name ))
208+ if (! is.null(param [[routing ]])) {
209+ lapply(names(param [[routing ]]), function (name ) {
210+ vec <- unlist(param [[routing ]][[name ]], use.names = FALSE )
211+ check_prob_vector(vec , paste0(routing , " $" , name ))
212+ })
179213 }
180214 }
181215}
0 commit comments