-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_consumption_factor.R
123 lines (87 loc) · 5 KB
/
batch_consumption_factor.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
predators<-sp.names[1:npr]
runs<-lapply(0:npr,sum) # just to make a lost with runs. One predator per combination
runs[[length(runs)+1]]<-16:20
runs[[length(runs)+1]]<-1:20
setwd(data.path)
oldDir<-data.path
lapply(runs,function(x)paste(as.character(x),collapse='_'))
names(runs)<-lapply(runs,function(x)paste(sp.names[x],collapse='_'))
cons<-matrix(scan(file.path(data.path,"cons_multiplier_options.in"),comment.char = "#"),ncol=3,byrow=T)
rownames(cons)<-predators
bio.interact<-TRUE
lapply(runs,function(x){
scenario.dir<-paste0('cons_',paste(as.character(x),collapse='_'))
copySMSfiles<-function(scenario.dir) {
if (file.exists(scenario.dir)) unlink(scenario.dir,recursive = T)
dir.create(scenario.dir,showWarnings = FALSE)
SMS.files.single<-c("area_names.in","natmor.in","canum.in","west.in","weca.in","propmat.in","fleet_catch.in",
"fleet_names.in","fleet_info.dat","just_one.in","sms.psv","species_names.in",
"SSB_R.in","Prediction_F.in","reference_points.in","predict_stock_N.in",
"proportion_M_and_F_before_spawning.in","proportion_landed.in","recruitment_years.in",
"zero_catch_season_ages.in","zero_catch_year_season.in","F_q_ini.in",
"Exploitation_pattern.in","covariance_N.in","HCR_options.dat","sms.dat",
"SMS.exe")
for (from.file in SMS.files.single) {
to.file<-file.path(scenario.dir,from.file)
file.copy(from.file, to.file, overwrite = TRUE)
}
SMS.files.multi<-c("alk_stom.in","consum.in","Length_weight_relations.in","lsea.in","N_haul_at_length.in",
"natmor1.in","other_food.in","season_overlap.in","stom_pred_length_at_sizecl.in","stom_struc_at_length.in",
"stomcon_at_length.in","stomlen_at_length.in","stomweight_at_length.in","stomtype_at_length.in",
"stomnumber_at_length.in","pred_prey_size_range_param.in","other_pred_N.in",
"incl_stom.in","temperature.in","n_proportion_m2.in","consum_ab.in","cons_multiplier_options.in")
for (from.file in SMS.files.multi) {
to.file<-file.path(scenario.dir,from.file)
file.copy(from.file, to.file, overwrite = TRUE)
}
SMS.parms<-c("run_ms2.dat","run_ms1.par")
for (from.file in SMS.parms) {
to.file<-file.path(scenario.dir,from.file)
file.copy(from.file, to.file, overwrite = TRUE)
}
}
copySMSfiles(scenario.dir)
if (sum(x)>0) {
cons[,3]<- -1
cons[x,3]<-2
write(t(cons),file=file.path(scenario.dir,"cons_multiplier_options.in"),ncolumns = 3)
}
cat("\nDoing run for scenario",scenario.dir,"\n")
do.a.full.SMS.run(outdir=file.path(data.path,scenario.dir), rundir=file.path(data.path,scenario.dir),
label="run_", # label for output
cleanup=F, # delete files in the deleteFiles variable?
do.single=F, # run SMS in single species mode
do.multi.1=F, # Make preliminary estimate of "predation parameters"
do.multi.2=T, # Run the full model, with simultaneously estimation of all parameters except the stomach variance parameter
do.multi.2.redo=F, # Run the full model, with simultaneously estimation of all parameters
do.multi.2.redo.Nbar=F, # Run the full model, with simultaneously estimation of all parameters, Use mean stock numbers (Nbar) for predation
do.hessian=F, # Make the Hessian matrix and estimate uncertainties
do.MCMC=F, # Prepare for MCMC analysis
mcmc=1000,mcsave=100, # Options for MCMS analysis
do.prediction=F, # Make a prediction
pause=F, # Make a pause between each stage
Screen.show=F, # show the output on screen, or save it in file
do.run=F, # Make the run immediately, or just make the batch file for the run
deleteFiles=NA ) # clean up in files before the run is made
})
#source(file.path(prog.path,"compare_runs.R"))
#rr<-runs[2:17]
rr<-runs
obj<-lapply(rr,function(x){
scenario.dir<-paste0('cons_',paste(as.character(x),collapse='_'))
a<-Read.objective.function(dir=scenario.dir,extend=FALSE,read.init.function=TRUE)
a<-subset(a,select=c(-n.catch, -n.CPUE, -n.SSB.R, -n.stom, -n.all.obs, -penalty, -stomachs.N))
})
obj
obj.all<-lapply(obj,function(x){
head(x,1)$all
})
obj.all
plot(unlist(obj.all),ylab='log likelihood')
pp<-lapply(rr,function(x){
# x<-rr[[22]]
scenario.dir<-paste0('cons_',paste(as.character(x),collapse='_'))
a<-readLines(file.path( scenario.dir,"sms.par"))
lapply(x,function(xx) as.numeric(a[grep(paste0("# cons_multiplier[",xx,']:'),a,fixed=TRUE)+1]))
})
pp