-
Notifications
You must be signed in to change notification settings - Fork 0
/
atfgoa2013.cpp
1595 lines (1559 loc) · 58.2 KB
/
atfgoa2013.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <admodel.h>
#include <time.h>
ofstream R_out;
ofstream CheckFile;
time_t start,finish;
long hour,minute,second;
double elapsed_time;
#include <admodel.h>
#include <contrib.h>
extern "C" {
void ad_boundf(int i);
}
#include <atfgoa2013.htp>
model_data::model_data(int argc,char * argv[]) : ad_comm(argc,argv)
{
pad_evalout = new ofstream("atfgoa2013.mcmc.out");;
styr.allocate("styr");
endyr.allocate("endyr");
styr_fut.allocate("styr_fut");
endyr_fut.allocate("endyr_fut");
M.allocate(1,2,"M");
phase_F40.allocate("phase_F40");
median_rec.allocate("median_rec");
median_rec_yrs.allocate("median_rec_yrs");
nages_read.allocate("nages_read");
nages.allocate("nages");
nselages.allocate("nselages");
nselages_srv1.allocate("nselages_srv1");
monot_sel.allocate("monot_sel");
monot_sel_srv1.allocate("monot_sel_srv1");
phase_logistic_sel.allocate("phase_logistic_sel");
phase_selcoffs.allocate("phase_selcoffs");
phase_logistic_sel_srv1.allocate("phase_logistic_sel_srv1");
phase_selcoffs_srv1.allocate("phase_selcoffs_srv1");
wt_like.allocate(1,8,"wt_like");
nlen_r.allocate("nlen_r");
nobs_fish.allocate("nobs_fish");
yrs_fish.allocate(1,nobs_fish,"yrs_fish");
nsamples_fish.allocate(1,2,1,nobs_fish,"nsamples_fish");
nobs_srv1.allocate("nobs_srv1");
yrs_srv1.allocate(1,nobs_srv1,"yrs_srv1");
nobs_srv1_length.allocate("nobs_srv1_length");
yrs_srv1_length.allocate(1,nobs_srv1_length,"yrs_srv1_length");
nsamples_srv1_length.allocate(1,2,1,nobs_srv1_length,"nsamples_srv1_length");
nobs_srv1_age.allocate("nobs_srv1_age");
yrs_srv1_age.allocate(1,nobs_srv1_age,"yrs_srv1_age");
like_wght.allocate(1,5,"like_wght");
nsamples_srv1_age.allocate(1,2,1,nobs_srv1_age,"nsamples_srv1_age");
obs_p_srv1_len_r.allocate(1,2,1,nobs_srv1_length,1,nlen_r,"obs_p_srv1_len_r");
obs_p_srv1_age_read.allocate(1,2,1,nobs_srv1_age,1,nages_read,"obs_p_srv1_age_read");
obs_p_fish_r.allocate(1,2,1,nobs_fish,1,nlen_r,"obs_p_fish_r");
catch_bio.allocate(styr,endyr,"catch_bio");
cout<<catch_bio<<endl;
obs_srv1.allocate(1,nobs_srv1,"obs_srv1");
obs_srv1_sd.allocate(1,nobs_srv1,"obs_srv1_sd");
wt.allocate(1,2,1,nages,"wt");
cout<<"wt"<<wt<<std::endl;
maturity.allocate(1,nages,"maturity");
cout<<"maturity"<<maturity<<std::endl;
lenage.allocate(1,2,1,nages,1,nlen_r-1,"lenage");
cout<<"lenage"<<lenage<<std::endl;
cv_srv1.allocate(1,nobs_srv1);
nlen=nlen_r-1;
styr_rec=styr-nages+1;
if(nselages>nages) nselages=nages;
if(nselages_srv1>nages) nselages_srv1=nages;
cv_srv1=elem_div(obs_srv1_sd,obs_srv1);
wt=wt*.001;
obs_p_srv1_age.allocate(1,2,1,nobs_srv1_age,1,nages);
obs_p_srv1_age_r.allocate(1,2,1,nobs_srv1_age,1,nages);
obs_p_srv1_length.allocate(1,2,1,nobs_srv1_length,1,nlen);
obs_p_fish.allocate(1,2,1,nobs_fish,1,nlen);
obs_sexr.allocate(1,nobs_fish);
obs_sexr_srv1.allocate(1,nobs_srv1_age);
obs_sexr_srv1_l.allocate(1,nobs_srv1_length);
}
void model_parameters::initializationfunction(void)
{
mean_log_rec.set_initial_value(18);
log_avg_fmort.set_initial_value(-5.);
q1.set_initial_value(1.);
fmort_dev.set_initial_value(0.00001);
fish_slope_f.set_initial_value(.4);
fish_sel50_f.set_initial_value(5.);
fish_slope_m.set_initial_value(.1);
fish_sel50_m.set_initial_value(8.);
srv1_slope_f.set_initial_value(.8);
srv1_sel50_f.set_initial_value(4.);
srv1_slope_m.set_initial_value(.4);
srv1_sel50_m.set_initial_value(8);
}
model_parameters::model_parameters(int sz,int argc,char * argv[]) :
model_data(argc,argv) , function_minimizer(sz)
{
initializationfunction();
q1.allocate(0.01,20.0,-8,"q1");
mean_log_rec.allocate(1,"mean_log_rec");
rec_dev.allocate(styr_rec,endyr,-15,15,2,"rec_dev");
log_avg_fmort.allocate(1,"log_avg_fmort");
fmort_dev.allocate(styr,endyr,-5.0,3.5,1,"fmort_dev");
log_selcoffs_fish.allocate(1,2,1,nselages,phase_selcoffs,"log_selcoffs_fish");
log_selcoffs_srv1.allocate(1,2,1,nselages_srv1,phase_selcoffs_srv1,"log_selcoffs_srv1");
fish_slope_f.allocate(.1,5.,phase_logistic_sel,"fish_slope_f");
fish_sel50_f.allocate(1.,8.,phase_logistic_sel,"fish_sel50_f");
fish_slope_m.allocate(.05,2.0,phase_logistic_sel,"fish_slope_m");
fish_sel50_m.allocate(1.,25.,phase_logistic_sel,"fish_sel50_m");
srv1_slope_f.allocate(.1,5.,phase_logistic_sel_srv1,"srv1_slope_f");
srv1_sel50_f.allocate(1.,10.,phase_logistic_sel_srv1,"srv1_sel50_f");
srv1_slope_m.allocate(.01,5.,phase_logistic_sel_srv1,"srv1_slope_m");
srv1_sel50_m.allocate(1.,10.,phase_logistic_sel_srv1,"srv1_sel50_m");
sexr_param_fish.allocate(1.0,1.0,-5,"sexr_param_fish");
sexr_param_srv.allocate(.25,1.0,phase_selcoffs_srv1,"sexr_param_srv");
F40.allocate(0.05,.5,phase_F40,"F40");
F35.allocate(0.05,.5,phase_F40,"F35");
log_sel_fish.allocate(1,2,1,nages,"log_sel_fish");
#ifndef NO_AD_INITIALIZE
log_sel_fish.initialize();
#endif
log_sel_srv1.allocate(1,2,1,nages,"log_sel_srv1");
#ifndef NO_AD_INITIALIZE
log_sel_srv1.initialize();
#endif
sel.allocate(1,2,1,nages,"sel");
#ifndef NO_AD_INITIALIZE
sel.initialize();
#endif
sel_srv1.allocate(1,2,1,nages,"sel_srv1");
#ifndef NO_AD_INITIALIZE
sel_srv1.initialize();
#endif
avgsel_fish.allocate(1,2,"avgsel_fish");
#ifndef NO_AD_INITIALIZE
avgsel_fish.initialize();
#endif
avgsel_srv1.allocate(1,2,"avgsel_srv1");
#ifndef NO_AD_INITIALIZE
avgsel_srv1.initialize();
#endif
popn.allocate(1,2,styr,endyr,"popn");
#ifndef NO_AD_INITIALIZE
popn.initialize();
#endif
totn_srv1.allocate(1,2,styr,endyr,"totn_srv1");
#ifndef NO_AD_INITIALIZE
totn_srv1.initialize();
#endif
explbiom.allocate(styr,endyr,"explbiom");
#ifndef NO_AD_INITIALIZE
explbiom.initialize();
#endif
pred_bio.allocate(styr,endyr,"pred_bio");
#ifndef NO_AD_INITIALIZE
pred_bio.initialize();
#endif
fspbio.allocate(styr,endyr,"fspbio");
#ifndef NO_AD_INITIALIZE
fspbio.initialize();
#endif
pred_srv1.allocate(styr,endyr,"pred_srv1");
#ifndef NO_AD_INITIALIZE
pred_srv1.initialize();
#endif
pred_p_fish.allocate(1,2,styr,endyr,1,nlen,"pred_p_fish");
#ifndef NO_AD_INITIALIZE
pred_p_fish.initialize();
#endif
pred_p_srv1_age.allocate(1,2,styr,endyr,1,nages,"pred_p_srv1_age");
#ifndef NO_AD_INITIALIZE
pred_p_srv1_age.initialize();
#endif
pred_p_srv1_len.allocate(1,2,styr,endyr,1,nlen,"pred_p_srv1_len");
#ifndef NO_AD_INITIALIZE
pred_p_srv1_len.initialize();
#endif
pred_catch.allocate(styr,endyr,"pred_catch");
#ifndef NO_AD_INITIALIZE
pred_catch.initialize();
#endif
natage.allocate(1,2,styr,endyr,1,nages,"natage");
#ifndef NO_AD_INITIALIZE
natage.initialize();
#endif
catage.allocate(1,2,styr,endyr,1,nages,"catage");
#ifndef NO_AD_INITIALIZE
catage.initialize();
#endif
natlength.allocate(1,2,styr,endyr,1,nlen,"natlength");
#ifndef NO_AD_INITIALIZE
natlength.initialize();
#endif
Z.allocate(1,2,styr,endyr,1,nages,"Z");
#ifndef NO_AD_INITIALIZE
Z.initialize();
#endif
F.allocate(1,2,styr,endyr,1,nages,"F");
#ifndef NO_AD_INITIALIZE
F.initialize();
#endif
S.allocate(1,2,styr,endyr,1,nages,"S");
#ifndef NO_AD_INITIALIZE
S.initialize();
#endif
fmort.allocate(styr,endyr,"fmort");
#ifndef NO_AD_INITIALIZE
fmort.initialize();
#endif
rbar.allocate("rbar");
#ifndef NO_AD_INITIALIZE
rbar.initialize();
#endif
surv.allocate(1,2,"surv");
#ifndef NO_AD_INITIALIZE
surv.initialize();
#endif
offset.allocate(1,3,"offset");
#ifndef NO_AD_INITIALIZE
offset.initialize();
#endif
rec_like.allocate("rec_like");
#ifndef NO_AD_INITIALIZE
rec_like.initialize();
#endif
rec_like2.allocate("rec_like2");
#ifndef NO_AD_INITIALIZE
rec_like2.initialize();
#endif
catch_like.allocate("catch_like");
#ifndef NO_AD_INITIALIZE
catch_like.initialize();
#endif
age_like.allocate(1,3,"age_like");
#ifndef NO_AD_INITIALIZE
age_like.initialize();
#endif
sel_like.allocate(1,4,"sel_like");
#ifndef NO_AD_INITIALIZE
sel_like.initialize();
#endif
fpen.allocate("fpen");
#ifndef NO_AD_INITIALIZE
fpen.initialize();
#endif
surv_like.allocate("surv_like");
#ifndef NO_AD_INITIALIZE
surv_like.initialize();
#endif
recruits.allocate(styr,endyr,"recruits");
biomassrep.allocate(styr,endyr,"biomassrep");
fspbiorep.allocate(styr,endyr,"fspbiorep");
endbiom.allocate("endbiom");
depletion.allocate("depletion");
f.allocate("f");
prior_function_value.allocate("prior_function_value");
likelihood_function_value.allocate("likelihood_function_value");
tmp.allocate("tmp");
#ifndef NO_AD_INITIALIZE
tmp.initialize();
#endif
pred_sexr.allocate(styr,endyr,"pred_sexr");
#ifndef NO_AD_INITIALIZE
pred_sexr.initialize();
#endif
preds_sexr.allocate(styr,endyr,"preds_sexr");
#ifndef NO_AD_INITIALIZE
preds_sexr.initialize();
#endif
sigmar.allocate("sigmar");
#ifndef NO_AD_INITIALIZE
sigmar.initialize();
#endif
ftmp.allocate("ftmp");
#ifndef NO_AD_INITIALIZE
ftmp.initialize();
#endif
SB0.allocate("SB0");
#ifndef NO_AD_INITIALIZE
SB0.initialize();
#endif
SBF40.allocate("SBF40");
#ifndef NO_AD_INITIALIZE
SBF40.initialize();
#endif
SBF35.allocate("SBF35");
#ifndef NO_AD_INITIALIZE
SBF35.initialize();
#endif
sprpen.allocate("sprpen");
#ifndef NO_AD_INITIALIZE
sprpen.initialize();
#endif
Nspr.allocate(1,3,1,nages,"Nspr");
#ifndef NO_AD_INITIALIZE
Nspr.initialize();
#endif
nage_future.allocate(1,2,styr_fut,endyr_fut,1,nages,"nage_future");
#ifndef NO_AD_INITIALIZE
nage_future.initialize();
#endif
F_future.allocate(1,2,styr_fut,endyr_fut,1,nages,"F_future");
#ifndef NO_AD_INITIALIZE
F_future.initialize();
#endif
Z_future.allocate(1,2,styr_fut,endyr_fut,1,nages,"Z_future");
#ifndef NO_AD_INITIALIZE
Z_future.initialize();
#endif
S_future.allocate(1,2,styr_fut,endyr_fut,1,nages,"S_future");
#ifndef NO_AD_INITIALIZE
S_future.initialize();
#endif
catage_future.allocate(1,2,styr_fut,endyr_fut,1,nages,"catage_future");
#ifndef NO_AD_INITIALIZE
catage_future.initialize();
#endif
avg_rec_dev_future.allocate("avg_rec_dev_future");
#ifndef NO_AD_INITIALIZE
avg_rec_dev_future.initialize();
#endif
avg_F_future.allocate(1,3,"avg_F_future");
#ifndef NO_AD_INITIALIZE
avg_F_future.initialize();
#endif
catch_future.allocate(1,5,styr_fut,endyr_fut,"catch_future");
#ifndef NO_AD_INITIALIZE
catch_future.initialize();
#endif
fspbiom_fut.allocate(1,5,styr_fut,endyr_fut,"fspbiom_fut");
future_biomass.allocate(1,5,styr_fut,endyr_fut,"future_biomass");
explbiom_fut.allocate(styr_fut,endyr_fut,"explbiom_fut");
#ifndef NO_AD_INITIALIZE
explbiom_fut.initialize();
#endif
maxsel_fish.allocate("maxsel_fish");
#ifndef NO_AD_INITIALIZE
maxsel_fish.initialize();
#endif
maxsel_srv1.allocate(1,2,"maxsel_srv1");
#ifndef NO_AD_INITIALIZE
maxsel_srv1.initialize();
#endif
B0.allocate("B0");
#ifndef NO_AD_INITIALIZE
B0.initialize();
#endif
B40.allocate("B40");
#ifndef NO_AD_INITIALIZE
B40.initialize();
#endif
B35.allocate("B35");
#ifndef NO_AD_INITIALIZE
B35.initialize();
#endif
AMeanRec.allocate("AMeanRec");
#ifndef NO_AD_INITIALIZE
AMeanRec.initialize();
#endif
like_natm.allocate("like_natm");
#ifndef NO_AD_INITIALIZE
like_natm.initialize();
#endif
like_q.allocate("like_q");
#ifndef NO_AD_INITIALIZE
like_q.initialize();
#endif
}
void model_parameters::preliminary_calculations(void)
{
#if defined(USE_ADPVM)
admaster_slave_variable_interface(*this);
#endif
//cout<<"to prelim calcs"<<endl;
//sex loop
for(k=1; k<=2; k++)
{
for (i=1; i <= nobs_srv1_age; i++)
{
for(j=1; j < nages; j++)
{
//ages go from 3 to 15
obs_p_srv1_age_r(k,i,j)=obs_p_srv1_age_read(k,i,j+2);
}
for(m=nages; m<=nages_read; m++) //error here you are adding 13+ rather than 15+
{
obs_p_srv1_age_r(k,i,nages)+=obs_p_srv1_age_read(k,i,m);
}
}
}
for(i=1; i<=nobs_fish;i++)
{
obs_sexr(i)=(sum(obs_p_fish_r(1,i)(2,nlen+1)))/(sum(obs_p_fish_r(1,i)(2,nlen+1))+sum(obs_p_fish_r(2,i)(2,nlen+1)));
}
cout<<"obs_sexr"<<obs_sexr<<std::endl;
for(i=1; i<=nobs_srv1_age;i++)
{
obs_sexr_srv1(i)=(sum(obs_p_srv1_age_r(1,i)))/(sum(obs_p_srv1_age_r(1,i))+sum(obs_p_srv1_age_r(2,i)));
}
for(i=1; i<=nobs_srv1_length;i++)
{
obs_sexr_srv1_l(i)=(sum(obs_p_srv1_len_r(1,i)(2,nlen+1)))/(sum(obs_p_srv1_len_r(1,i)(2,nlen+1))+sum(obs_p_srv1_len_r(2,i)(2,nlen+1)));
}
cout<<"obs_sexr_srv1"<<obs_sexr_srv1<<std::endl;
// cout<< " thru sex ratio "<<endl;
//Compute offset for multinomial
// offset is a constant nplog(p) is added to the likelihood
// magnitude depends on nsamples(sample size) and p's_
//k is sex loop
offset=0.;
for(k=1; k<=2; k++)
{
for (i=1; i <= nobs_fish; i++)
{
//make observations proportions by year
//fishery offset
for (j=1; j<=nlen; j++)
{
obs_p_fish(k,i,j)=(obs_p_fish_r(k,i,j+1))/(sum(obs_p_fish_r(1,i)(2,nlen_r))+sum(obs_p_fish_r(2,i)(2,nlen_r)));
if (obs_p_fish(k,i,j)>0.0)
{
offset(1)-=nsamples_fish(k,i)*obs_p_fish(k,i,j)*log(obs_p_fish(k,i,j));
}
}
}
}
//survey length offset
for(k=1; k<=2;k++)
{
for (i=1; i <= nobs_srv1_length; i++)
{
// cout<< " to obs_p_srv1_length "<<endl;
for (j=1; j<=nlen; j++)
{
obs_p_srv1_length(k,i,j)=obs_p_srv1_len_r(k,i,j+1)/sum(obs_p_srv1_len_r(1,i)(2,nlen_r)+obs_p_srv1_len_r(2,i)(2,nlen_r));
if (obs_p_srv1_length(k,i,j)>0.0)
{
offset(2)-=nsamples_srv1_length(k,i)*obs_p_srv1_length(k,i,j)*log(obs_p_srv1_length(k,i,j));
}
}
}
}
//survey age offset
for(k=1; k<=2;k++)
{
for (i=1; i <= nobs_srv1_age; i++)
{
obs_p_srv1_age(k,i)=obs_p_srv1_age_r(k,i)/(sum(obs_p_srv1_age_r(1,i))+sum(obs_p_srv1_age_r(2,i)));
//cout<<obs_p_srv1(i)<<endl;
for (j=1; j<=nages; j++)
{
if (obs_p_srv1_age(k,i,j)>0.0)
{
offset(3)-=nsamples_srv1_age(k,i)*obs_p_srv1_age(k,i,j)*log(obs_p_srv1_age(k,i,j));
}
}
}
}
//cout<<endl<<"to end of offset"<<endl;
}
void model_parameters::userfunction(void)
{
f =0.0;
ofstream& evalout= *pad_evalout;
get_selectivity();
//cout<<"sel"<<endl;
get_mortality();
//cout<<"mort"<<endl;
surv(1)=mfexp(-1.0* M(1));
surv(2)=mfexp(-1.0* M(2));
get_numbers_at_age();
//cout<<"numbers at age"<<endl;
get_catch_at_age();
//cout<<"catch at age"<<endl;
if (active(F40))
compute_spr_rates();
if (last_phase())
{
Future_projections();
}
if (sd_phase() || mceval_phase())
{
if (mceval_phase())
{
evalout << f << " " ;
// loop over years and print in one long row.
for (i=styr;i<=endyr;i++)
evalout<< fspbio(i) << " ";
for (i=styr;i<=endyr;i++)
evalout<< natage(1,i)*wt(1) + natage(2,i)*wt(2) <<" ";
for (i=styr;i<=endyr;i++)
evalout << 2*natage(1,i,1) <<" ";
// hit carriage return on file
evalout << endl;
}
}
evaluate_the_objective_function();
}
void model_parameters::get_growthmatrix(void)
{
ofstream& evalout= *pad_evalout;
// len_len(2,ilen,il2)=cumd_norm((length_bins(il2)+2.5-mean_length(2,ilen))/sd_mean_length(2,ilen));
// }
// if(il2>1)
// {
// rec_len(il2)=cumd_norm((length_bins(il2)-length_rec)/var_rec)-cumd_norm((length_bins(il2-1)-length_rec)/var_rec);
// len_len(1,ilen,il2)=cumd_norm((length_bins(il2)+2.5-mean_length(1,ilen))/sd_mean_length(1,ilen))-cumd_norm((length_bins(il2-1)+2.5-mean_length(1,ilen))/sd_mean_length(1,ilen));
// len_len(2,ilen,il2)=cumd_norm((length_bins(il2)+2.5-mean_length(2,ilen))/sd_mean_length(2,ilen))-cumd_norm((length_bins(il2-1)+2.5-mean_length(2,ilen))/sd_mean_length(2,ilen));
// cout<<(length_bins(il2)-mean_length(1,ilen))/sd_mean_length(1,ilen)<<endl;
// cout<<cumd_norm((length_bins(il2)-mean_length(1,ilen))/sd_mean_length(1,ilen))<<endl;
// cout<<len_len(1,ilen,il2-1)<<endl;
// }
// sum_len(1,ilen)+=len_len(1,ilen,il2);
// sum_len(2,ilen)+=len_len(2,ilen,il2);
// }
// len_len(1,ilen,nlenm)=len_len(1,ilen,nlenm)+1-sum_len(1,ilen);
// len_len(2,ilen,nlenm)=len_len(2,ilen,nlenm)+1-sum_len(2,ilen);
// }
}
void model_parameters::get_selectivity(void)
{
ofstream& evalout= *pad_evalout;
if(active(log_selcoffs_fish))
{
// phase_logistic_sel=-2; IS 2015 testing
for(k=1;k<=2;k++)
{
for (j=1;j<=nselages;j++)
{
log_sel_fish(k,j)=log_selcoffs_fish(k,j);
}
}
//sets selectivity of ages older than nselages to selectivity at nselages
//cout<<"to nselages loop"<<endl;
for(k=1;k<=2;k++)
{
for (j=nselages+1;j<=nages;j++)
{
log_sel_fish(k,j)=log_sel_fish(k,j-1);
}
}
for(k=1;k<=2;k++)
{
avgsel_fish(k)=log(mean(mfexp(log_selcoffs_fish(k))));
}
//vector=vector-scalar same as vector-=scalar
//scaling selectivities by subracting the mean so exp(mean(s))=1.
//selectivities can be greater than 1 but mean is 1.
//cout<<"calc log_sel_fish"<<endl;
for(k=1;k<=2;k++)
{
log_sel_fish(k)-=log(mean(mfexp(log_sel_fish(k))));
sel(k)=mfexp(log_sel_fish(k));
if(k==2)
{
sel(k)=sel(k)*sexr_param_fish;
}
//cout<<"sel survey"<<sel_srv1<<endl;
}
}
else
{
//logistic selectivity curve
for (j=1;j<=nages;j++)
{
if(j<=nselages)
{
sel(1,j)=1./(1.+mfexp(-1.*fish_slope_f*(double(j)-fish_sel50_f)));
sel(2,j)=1./(1.+mfexp(-1.*fish_slope_m*(double(j)-fish_sel50_m)));
}
else
{
sel(1,j)=sel(1,j-1);
sel(2,j)=sel(2,j-1);
}
}
}
if(active(log_selcoffs_srv1))
{
// phase_logistic_sel_srv1=-2;
for(k=1;k<=2;k++)
{
for (j=1;j<=nselages_srv1;j++)
{
log_sel_srv1(k,j)=log_selcoffs_srv1(k,j);
}
}
//sets selectivity of ages older than nselages_srv1 to selectivity at nselages_srv1
for(k=1;k<=2;k++)
{
for (j=nselages_srv1+1;j<=nages;j++)
{
log_sel_srv1(k,j)=log_sel_srv1(k,j-1);
}
}
for(k=1;k<=2;k++)
{
avgsel_srv1(k)=log(mean(mfexp(log_selcoffs_srv1(k))));
}
//vector=vector-scalar same as vector-=scalar
//scaling selectivities by subracting the mean so exp(mean(s))=1.
//selectivities can be greater than 1 but mean is 1.
for(k=1;k<=2;k++)
{
log_sel_srv1(k)-=log(mean(mfexp(log_sel_srv1(k))));
sel_srv1(k)=mfexp(log_sel_srv1(k));
if(k==2)
{
sel_srv1(k)=sel_srv1(k)*sexr_param_srv;
}
//cout<<"sel survey"<<sel_srv1<<endl;
}
}
else
{
//logistic selectivity curve
for (j=1;j<=nages;j++)
{
if(j<=nselages_srv1)
{
sel_srv1(1,j)=1./(1.+mfexp(-1.*srv1_slope_f*(double(j)-srv1_sel50_f)));
sel_srv1(2,j)=1./(1.+mfexp(-1.*srv1_slope_m*(double(j)-srv1_sel50_m)));
}
else
{
sel_srv1(1,j)=sel_srv1(1,j-1);
sel_srv1(2,j)=sel_srv1(2,j-1);
}
}
}
//cout<<"sel fishery"<<sel<<std::endl;
//cout<<"sel survey "<<sel_srv1<<std::endl;
}
void model_parameters::get_mortality(void)
{
ofstream& evalout= *pad_evalout;
maxsel_fish=max(sel(1));
if(maxsel_fish<max(sel(2)))
maxsel_fish=max(sel(2));
maxsel_srv1(1)=max(sel_srv1(1));
maxsel_srv1(2)=maxsel_srv1(1);
fmort=mfexp(log_avg_fmort+fmort_dev);
for(k=1;k<=2;k++)
{
for (i=styr;i<=endyr;i++)
{
F(k,i)=(sel(k)/maxsel_fish)*fmort(i);
Z(k,i)=F(k,i) + M(k);
}
}
S=mfexp(-1.0*Z);
// cout<<"to end of get_mortality"<<endl;
}
void model_parameters::get_numbers_at_age(void)
{
ofstream& evalout= *pad_evalout;
if(maxsel_srv1(1)<max(sel_srv1(2)))
{
maxsel_srv1(1)=max(sel_srv1(2));
maxsel_srv1(2)=maxsel_srv1(1);
}
// maxsel_srv1(1)=max(sel_srv1(1));
// cout<<"begin numbers at age"<<endl;
int itmp;
//calc initial population
for (j=1;j<nages;j++)
{
itmp=styr+1-j;
natage(1,styr,j)=mfexp(mean_log_rec-(M(1)*double(j-1))+rec_dev(itmp));
natage(2,styr,j)=mfexp(mean_log_rec-(M(2)*double(j-1))+rec_dev(itmp));
}
itmp=styr+1-nages;
//cout<<"initial 1"<<endl;
//cout<<"to last age "<<endl;
natage(1,styr,nages)=mfexp(mean_log_rec+rec_dev(itmp)-(M(1)*(nages-1)))/(1.- surv(1));
natage(2,styr,nages)=mfexp(mean_log_rec+rec_dev(itmp)-(M(2)*(nages-1)))/(1.- surv(2));
//cout<<"to next years"<<endl;
// Now do for next several years----------------------------------
for (i=styr+1;i<=endyr;i++) //added else clause in 2015 to match BSAI code
{
if(i<=(endyr-1))
{
natage(1,i,1)=mfexp(mean_log_rec+rec_dev(i));
natage(2,i,1)=natage(1,i,1);
}
else
{
natage(1,i,1)=median_rec;
natage(2,i,1)=natage(1,i,1);
}
}
//cout<<"initial 2"<<endl;
//numbers at age
for(k=1;k<=2;k++)
{
for (i=styr;i< endyr;i++)
{
//cout<<"to subvector op"<<endl;
//subvector - avoids writing a j loop =++ increments the right side
//(1,nages-1) to 1+1 to nages-1+1 then does the assignment x(i)(1,n)
//takes the ith row of x the columns 1 to n
// natage(k,i+1)(2,nages)=++elem_prod(natage(k,i)(1,nages-1),S(k,i)(1,nages-1));
for(j=1;j<nages;j++)
{
natage(k,i+1,j+1)=natage(k,i,j)*S(k,i,j);
}
//accumulates oldest ages
// cout<<"done with j loop"<<endl;
natage(k,i+1,nages)+=natage(k,i,nages)*S(k,i,nages);
// cout<<"done with natage nages"<<endl;
//popn is exploitable numbers
popn(k,i)= natage(k,i)*sel(k);
// cout<<"popn "<<endl;
// cout<<popn(k,i)<<endl;
}
// cout<<"to popn"<<endl;
popn(k,endyr)=natage(k,endyr)*sel(k);
}
for (i=styr;i<=endyr;i++)
{
for(k=1;k<=2;k++)
{
//population numbers at length
natlength(k,i)=natage(k,i)*lenage(k);
//numbers to rescale age comps and length comps
totn_srv1(k,i)=(natage(k,i)*sel_srv1(k));
}
}
//cout<<"2nd loop"<<endl;
//predicted survey values
fspbio=0.;
for (i=styr;i<=endyr;i++)
{
fspbio(i)+=natage(1,i)*elem_prod(wt(1),maturity);
explbiom(i)=0.;
pred_bio(i)=0.;
pred_srv1(i)=0.;
for(k=1;k<=2;k++)
{
pred_srv1(i)+=q1*(natage(k,i)*elem_prod(sel_srv1(k)/maxsel_srv1(k),wt(k)));
//next line used to fix q1 to 1.0 - problem is if you start from a bin file, even if the bounds
// are set different in the tpl file the program will take to value from the bin file and use that
// pred_srv1(i)=1.0*(natage(i)*elem_prod(sel_srv1,wt));
explbiom(i)+=natage(k,i)*elem_prod(sel(k),wt(k))/maxsel_fish;
pred_bio(i)+=natage(k,i)*wt(k);
// cout<<" to lenage calc"<<endl;
pred_p_srv1_len(k,i)=elem_prod(sel_srv1(k),natage(k,i))*lenage(k)/(totn_srv1(1,i)+totn_srv1(2,i));
// pred_p_srv1_len(k,i)=pred_p_srv1_len_1(k,i)/sum(pred_p_srv1_len_1(1,i)+pred_p_srv1_len_1(2,i));
pred_p_srv1_age(k,i)=elem_prod(sel_srv1(k),natage(k,i))/(totn_srv1(1,i)+totn_srv1(2,i));
// }
// pred_p_srv1_age(k,i)=pred_p_srv1_age_1(k,i)/sum(pred_p_srv1_age_1(1,i)+pred_p_srv1_age_1(2,i));
}
}
biomassrep=pred_bio;
fspbiorep=fspbio;
for(i=styr;i<=endyr;i++)
{
recruits(i)=mfexp(mean_log_rec+rec_dev(i));
}
depletion=pred_bio(endyr)/pred_bio(styr);
endbiom=pred_bio(endyr);
// cout<<"end get numbers at age"<<endl;
}
void model_parameters::get_catch_at_age(void)
{
ofstream& evalout= *pad_evalout;
for (i=styr; i<=endyr; i++)
{
pred_catch(i)=0.;
for(k=1;k<=2;k++)
{
//--Baranov's equation here-----------------------------------
for (j = 1 ; j<= nages; j++)
{
catage(k,i,j) = natage(k,i,j)*F(k,i,j)*(1.-S(k,i,j))/Z(k,i,j);
pred_catch(i)+=catage(k,i,j)*wt(k,j);
}
pred_p_fish(k,i)=elem_prod(sel(k),natage(k,i))*lenage(k)/(popn(1,i)+popn(2,i));
}
}
// cout<<"end catch at age"<<endl;
}
void model_parameters::Future_projections(void)
{
ofstream& evalout= *pad_evalout;
//cout<<"to future proj"<<endl;
for(k=1;k<=2;k++)
{
nage_future(k,styr_fut)(2,nages)=++elem_prod(natage(k,endyr)(1,nages-1),S(k,endyr)(1,nages-1));
nage_future(k,styr_fut,nages)+=natage(k,endyr,nages)*S(k,endyr,nages);
}
future_biomass=0.;
catch_future=0.;
fspbiom_fut=0.;
for (int l=1;l<=5;l++)
{
switch (l)
{
case 1:
ftmp=F40;
break;
case 2:
ftmp=F35;
break;
case 3:
ftmp=0.0;
break;
case 4:
ftmp=mean(mfexp(log_avg_fmort+fmort_dev(endyr-4,endyr)));
break;
case 5:
ftmp=mean(mfexp(log_avg_fmort+fmort_dev(endyr-4,endyr)));
break;
}
// Get future F's
for(k=1;k<=2;k++)
{
for (i=endyr+1;i<=endyr_fut;i++)
{
if(l>3){ftmp=mean(mfexp(log_avg_fmort+fmort_dev(endyr-4,endyr)));}
if(i>(endyr+1) && l==5) {ftmp=F35;}
if(i>(endyr+1) && l==4) {ftmp=F40;}
for (j=1;j<=nages;j++)
{
F_future(k,i,j) = (sel(k,j)/maxsel_fish)*ftmp;
Z_future(k,i,j) = F_future(k,i,j)+M(k);
S_future(k,i,j) = mfexp(-1.*Z_future(k,i,j));
}
}
// Future Recruitment (and spawners)
for (i=styr_fut;i<endyr_fut;i++)
{
nage_future(k,i,1) = AMeanRec;
// Now graduate for the next year....
nage_future(k,i+1)(2,nages) = ++elem_prod(nage_future(k,i)(1,nages-1),S_future(k,i)(1,nages-1));
nage_future(k,i+1,nages) += nage_future(k,i,nages)*S_future(k,i,nages);
}
nage_future(k,endyr_fut,1) = AMeanRec;
// Now get catch at future ages
fspbiom_fut(l)=0.;
for (i=styr_fut; i<=endyr_fut; i++)
{
for (j = 1 ; j<= nages; j++)
{
catage_future(k,i,j) = nage_future(k,i,j) * F_future(k,i,j) * ( 1.- S_future(k,i,j) ) / Z_future(k,i,j);
if(k==1)
{
fspbiom_fut(l,i) += nage_future(1,i,j)*wt(1,j)*maturity(j);
}
}
if (l!=3){
catch_future(l,i) += catage_future(k,i)*wt(k);
}
future_biomass(l,i) += nage_future(k,i)*wt(k);
} //end loop over future years
} //end loop over sex
fspbiom_fut(l)=0.;
for(i=styr_fut;i<=endyr_fut;i++)
{
for(j=1;j<=nages;j++)
{
fspbiom_fut(l,i)+= nage_future(1,i,j)*wt(1,j)*maturity(j);
}
}
} //End of loop over F's
}
void model_parameters::compute_spr_rates(void)
{
ofstream& evalout= *pad_evalout;
//Compute SPR Rates and add them to the likelihood for Females
SB0=0.;
SBF40=0.;
SBF35=0.;
//SBF30=0.;
AMeanRec=mean(mfexp(mean_log_rec+rec_dev(81,endyr)));
// Initialize the recruit (1) for each F (F40 etc)
for (i=1;i<=3;i++)
Nspr(i,1)=1.;
for (j=2;j<nages;j++)
{
Nspr(1,j)=Nspr(1,j-1)*mfexp(-1.*M(1));
Nspr(2,j)=Nspr(2,j-1)*mfexp(-1.*(M(1)+F40*sel(1,j-1)/maxsel_fish));
Nspr(3,j)=Nspr(3,j-1)*mfexp(-1.*(M(1)+F35*sel(1,j-1)/maxsel_fish));
}
//cout<<F40<<" "<<F30<<" "<<Nspr<<endl;
// cout<<"spr calc"<<endl;
// Now do plus group
Nspr(1,nages)=Nspr(1,nages-1)*mfexp(-1.*M(1))/(1.-mfexp(-1.*M(1)));
Nspr(2,nages)=Nspr(2,nages-1)*mfexp(-1.* (M(1)+F40*sel(1,nages-1)/maxsel_fish))/(1.-mfexp(-1.*(M(1)+F40*sel(1,nages)/maxsel_fish)));
Nspr(3,nages)=Nspr(3,nages-1)*mfexp(-1.* (M(1)+F35*sel(1,nages-1)/maxsel_fish))/(1.-mfexp(-1.*(M(1)+F35*sel(1,nages)/maxsel_fish)));
// Nspr(3,nages)=Nspr(3,nages-1)*mfexp(-1.* (M(1)+F30*sel(1,nages-1)/maxsel_fish))/ (1.-mfexp(-1.*(M(1)+F30*sel(1,nages)/maxsel_fish)));
//cout<<"plus group"<<endl;
for (j=1;j<=nages;j++)
{
// Kill them off till- use fraction of the year e.g. april=0.25 - not for atf they spawn in winter so put in 0.0
// Number ProportMat Wt Amount die off prior to spawning (within that year)
SB0 += Nspr(1,j)*maturity(j)*wt(1,j)*mfexp(-0.0*M(1));
SBF40 += Nspr(2,j)*maturity(j)*wt(1,j)*mfexp(-0.0*(M(1)+F40*sel(1,j)/maxsel_fish));
SBF35 += Nspr(3,j)*maturity(j)*wt(1,j)*mfexp(-0.0*(M(1)+F35*sel(1,j)/maxsel_fish));
// SBF30 += Nspr(3,j)*maturity(j)*wt(1,j)*mfexp(-0.0*(M(1)+F30*sel(1,j)/maxsel_fish));
}
// cout<<"kill thenm off"<<endl;
sprpen = 300.*square((SBF40/SB0)-0.4);
sprpen += 300.*square((SBF35/SB0)-0.35);
B0 = AMeanRec*SB0 ;
B40 = AMeanRec*SBF40 ;
B35 = AMeanRec*SBF35 ;
//cout<<"sbr/sno "<<endl;
//cout<<SBF40/SB0<<" "<<SBF30/SB0<<endl;
}
void model_parameters::evaluate_the_objective_function(void)
{
ofstream& evalout= *pad_evalout;
age_like=0.;
sel_like=0.;
fpen=.0;
rec_like=.0;
rec_like2=0.;
surv_like=.0;
catch_like=.0;
f=.0;
like_natm=0.; //not used
like_q=0.; //not used
if (active(rec_dev))
{
age_like=0.;
int ii;
//recruitment likelihood - norm2 is sum of square values
//cout<<"to rec_like"<<endl;
//cout<<" rec_devs = "<<rec_dev<<endl;
rec_like=norm2(rec_dev(styr_rec,endyr));
f+=rec_like;
f+=rec_like2;
//cout<<"to loop"<<endl;
for(k=1;k<=2;k++)
{
for (i=1; i <= nobs_fish; i++)
{
ii=yrs_fish(i);
// cout<<pred_p_fish(ii)<<endl<<endl;
//fishery length likelihood
for (j=1; j<=nlen; j++)
{
age_like(1)-=nsamples_fish(k,i)*(0.0001+obs_p_fish(k,i,j))*log(pred_p_fish(k,ii,j)+0.0001); //was 1e-5
//cout << age_like << " " << i << " "<<j<< endl;
}
}
}
//add the offset to the likelihood
age_like(1)-=offset(1);
//survey
for(k=1;k<=2;k++)
{
for (i=1; i <=nobs_srv1_length; i++)
{
ii=yrs_srv1_length(i);
//survey likelihood
for (j=1; j<=nlen; j++)
{
age_like(2)-=nsamples_srv1_length(k,i)*(1e-4+obs_p_srv1_length(k,i,j))*log(pred_p_srv1_len(k,ii,j)+1e-4); //was 1e-3
//cout << age_like << " " << i << " "<<j<< endl;
}
}
}
age_like(2)-=offset(2);
//bracket for active(fish_slope_f)
//survey ages
for(k=1;k<=2;k++)
{
for (i=1; i <=nobs_srv1_age; i++)
{
ii=yrs_srv1_age(i);
//survey likelihood
for (j=1; j<=nages; j++)
{
age_like(3)-=nsamples_srv1_age(k,i)*(1e-4+obs_p_srv1_age(k,i,j))*log(pred_p_srv1_age(k,ii,j)+1e-4); //was 1e-3
// cout << age_like << " " << i << " "<<j<< endl;
}
}
}