-
Notifications
You must be signed in to change notification settings - Fork 0
/
budget_thresholds.R
1283 lines (977 loc) · 45.2 KB
/
budget_thresholds.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
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
## This script is to establish the upper and lower bounds for the manager and user budgets. Here I will use simulations to establish where the thresholds are below which the user/manager loses all power (i.e. user/manger budget is too low relative to the other actor's budget budget)
### Load libraries ####
library('tidyverse')
library('GMSE')
library('patchwork')
library('MESS')
### thresh1 - user budget static, manager budget varying ####
## Here I will fix the user budget at 1000, and then vary the manager budget from very low (50) to very high (4000) over a long time period to see if we can see the thresholds.
MB <- 50
thresh1_sim_old <- gmse_apply(
res_mod = resource,
obs_mod = observation,
man_mod = manager,
use_mod = user,
get_res = "FUll",
time_max = 100,
land_dim_1 = 150,
land_dim_2 = 150, # landscape is 22,500ha or 22.5km2
res_movement = 0, # trees don't move
remove_pr = 0, # Assume no death
lambda = 0, # assume no growth
agent_view = 150,
agent_move = 50,
res_birth_K = 1, # must be positive value, but I want it small i.e. no real recruitment
res_death_K = 5000000, # carrying capacity set to way above starting number of resources
res_move_type = 0, # 0=no move,
res_death_type = 0, # no natural death
observe_type = 2, # transect
times_observe = 1,
obs_move_type = 1, # uniform in any direction
res_min_age = 0, # age of resources before agents record/act on them
res_move_obs = FALSE, # trees don't move
plotting = FALSE,
res_consume = 0.08, # Trees have 8% impact on yield
# all genetic algorithm parameters left to default
move_agents = TRUE,
max_ages = 1000,
minimum_cost = 10,
user_budget = 1000,
manager_budget = 50,
usr_budget_rng = 100, # introduce variation around the mean user budget (removes step pattern)
manage_target = 1125000,
RESOURCE_ini = 1125000,
culling = TRUE,
tend_crops = TRUE,
tend_crop_yld = 0.01, # tending crops increases yield by 1% - less than that of culling trees
stakeholders = 20,
land_ownership = TRUE,
public_land = 0,
manage_freq = 1,
group_think = FALSE
)
# matrix for results
thresh1 <- matrix(data=NA, nrow=100, ncol=6)
# loop the simulation.
for(time_step in 1:100){
sim_new <- gmse_apply(get_res = "Full", old_list = thresh1_sim_old, manager_budget=MB)
thresh1[time_step, 1] <- time_step
thresh1[time_step, 2] <- sim_new$basic_output$resource_results[1]
thresh1[time_step, 3] <- sim_new$basic_output$observation_results[1]
thresh1[time_step, 4] <- sim_new$basic_output$manager_results[3]
thresh1[time_step, 5] <- sum(sim_new$basic_output$user_results[,3])
thresh1[time_step, 6] <- MB
thresh1_sim_old <- sim_new
MB <- MB + 50
}
colnames(thresh1) <- c("Time", "Trees", "Trees_est", "Cull_cost", "Cull_count","Manager_budget")
thresh1_summary <- data.frame(thresh1)
write.csv(thresh1_summary, file = "outputs/investment/null_scenarios/budget_thresholds/thresh1_summary.csv")
# Loads saved results from Thresh1
thresh1_summary <- read.csv("outputs/investment/null_scenarios/budget_thresholds/thresh1_summary.csv", header = T)
### thresh2 - manager budget static, user budget varying ####
### here I will fix the manager budget at 1000 and vary the user budget
UB <- 50
UBR <- UB/5
thresh2_sim_old <- gmse_apply(
res_mod = resource,
obs_mod = observation,
man_mod = manager,
use_mod = user,
get_res = "FUll",
time_max = 100,
land_dim_1 = 150,
land_dim_2 = 150, # landscape is 22,500ha or 22.5km2
res_movement = 0, # trees don't move
remove_pr = 0, # Assume no death
lambda = 0, # assume no growth
agent_view = 150,
agent_move = 50,
res_birth_K = 1, # must be positive value, but I want it small i.e. no real recruitment
res_death_K = 5000000, # carrying capacity set to way above starting number of resources
res_move_type = 0, # 0=no move,
res_death_type = 0, # no natural death
observe_type = 2, # transect
times_observe = 1,
obs_move_type = 1, # uniform in any direction
res_min_age = 0, # age of resources before agents record/act on them
res_move_obs = FALSE, # trees don't move
plotting = FALSE,
res_consume = 0.08, # Trees have 8% impact on yield
# all genetic algorithm parameters left to default
move_agents = TRUE,
max_ages = 1000,
minimum_cost = 10,
user_budget = UB,
manager_budget = 1000,
usr_budget_rng = UBR, # introduce variation around the mean user budget (removes step pattern)
manage_target = 1125000,
RESOURCE_ini = 1125000,
culling = TRUE,
tend_crops = TRUE,
tend_crop_yld = 0.01, # tending crops increases yield by 1% - less than that of culling trees
stakeholders = 20,
land_ownership = TRUE,
public_land = 0,
manage_freq = 1,
group_think = FALSE
)
# matrix for results
thresh2 <- matrix(data=NA, nrow=100, ncol=6)
# loop the simulation.
for(time_step in 1:100){
sim_new <- gmse_apply(get_res = "Full", old_list = thresh2_sim_old, user_budget=UB, usr_budget_rng = UBR)
thresh2[time_step, 1] <- time_step
thresh2[time_step, 2] <- sim_new$basic_output$resource_results[1]
thresh2[time_step, 3] <- sim_new$basic_output$observation_results[1]
thresh2[time_step, 4] <- sim_new$basic_output$manager_results[3]
thresh2[time_step, 5] <- sum(sim_new$basic_output$user_results[,3])
thresh2[time_step, 6] <- UB
thresh2_sim_old <- sim_new
UB <- UB + 50
UBR <- UB/5
}
colnames(thresh2) <- c("Time", "Trees", "Trees_est", "Cull_cost", "Cull_count","User_budget")
thresh2_summary <- data.frame(thresh2)
#write.csv(thresh2_summary, file = "outputs/investment/null_scenarios/budget_thresholds/thresh2_summary.csv")
# load saved results summary for thresh2
thresh2_summary <- read.csv("outputs/investment/null_scenarios/budget_thresholds/thresh2_summary.csv", header = T)
# I think the point at which the manager loses power over the user is when the cost of culling is maxes out, and is no longer varied. This means that the manager is throwing all resources into stopping culling, at every time step, and is no longer attempting any kind of adaptive management.
### thresh3 - as thresh1 but larger absolute budget value ####
## Here I will fix the user budget at 5000, and then vary the manager budget from very low (100) to very high (10,000) over a long time period to see if we can see the thresholds.
MB <- 100
thresh3_sim_old <- gmse_apply(
res_mod = resource,
obs_mod = observation,
man_mod = manager,
use_mod = user,
get_res = "FUll",
time_max = 100,
land_dim_1 = 150,
land_dim_2 = 150, # landscape is 22,500ha or 22.5km2
res_movement = 0, # trees don't move
remove_pr = 0, # Assume no death
lambda = 0, # assume no growth
agent_view = 150,
agent_move = 50,
res_birth_K = 1, # must be positive value, but I want it small i.e. no real recruitment
res_death_K = 5000000, # carrying capacity set to way above starting number of resources
res_move_type = 0, # 0=no move,
res_death_type = 0, # no natural death
observe_type = 2, # transect
times_observe = 1,
obs_move_type = 1, # uniform in any direction
res_min_age = 0, # age of resources before agents record/act on them
res_move_obs = FALSE, # trees don't move
plotting = FALSE,
res_consume = 0.08, # Trees have 8% impact on yield
# all genetic algorithm parameters left to default
move_agents = TRUE,
max_ages = 1000,
minimum_cost = 10,
user_budget = 5000,
manager_budget = MB,
usr_budget_rng = 100, # introduce variation around the mean user budget (removes step pattern)
manage_target = 1125000,
RESOURCE_ini = 1125000,
culling = TRUE,
tend_crops = TRUE,
tend_crop_yld = 0.01, # tending crops increases yield by 1% - less than that of culling trees
stakeholders = 20,
land_ownership = TRUE,
public_land = 0,
manage_freq = 1,
group_think = FALSE
)
# matrix for results
thresh3 <- matrix(data=NA, nrow=100, ncol=6)
# loop the simulation.
for(time_step in 1:100){
sim_new <- gmse_apply(get_res = "Full", old_list = thresh3_sim_old, manager_budget=MB)
thresh3[time_step, 1] <- time_step
thresh3[time_step, 2] <- sim_new$basic_output$resource_results[1]
thresh3[time_step, 3] <- sim_new$basic_output$observation_results[1]
thresh3[time_step, 4] <- sim_new$basic_output$manager_results[3]
thresh3[time_step, 5] <- sum(sim_new$basic_output$user_results[,3])
thresh3[time_step, 6] <- MB
thresh3_sim_old <- sim_new
MB <- MB + 100
}
colnames(thresh3) <- c("Time", "Trees", "Trees_est", "Cull_cost", "Cull_count","Manager_budget")
thresh3_summary <- data.frame(thresh3)
write.csv(thresh3_summary, file = "outputs/investment/null_scenarios/budget_thresholds/thresh3_summary.csv")
### thresh4 - as thresh2 but larger absolute budget value ####
# this is the same as thresh2 but with a manager budget of 5000 and a user budget ranging from 100 to 10,1000
UB <- 100
UBR <- UB/5
thresh4_sim_old <- gmse_apply(
res_mod = resource,
obs_mod = observation,
man_mod = manager,
use_mod = user,
get_res = "FUll",
time_max = 100,
land_dim_1 = 150,
land_dim_2 = 150, # landscape is 22,500ha or 22.5km2
res_movement = 0, # trees don't move
remove_pr = 0, # Assume no death
lambda = 0, # assume no growth
agent_view = 150,
agent_move = 50,
res_birth_K = 1, # must be positive value, but I want it small i.e. no real recruitment
res_death_K = 5000000, # carrying capacity set to way above starting number of resources
res_move_type = 0, # 0=no move,
res_death_type = 0, # no natural death
observe_type = 2, # transect
times_observe = 1,
obs_move_type = 1, # uniform in any direction
res_min_age = 0, # age of resources before agents record/act on them
res_move_obs = FALSE, # trees don't move
plotting = FALSE,
res_consume = 0.08, # Trees have 8% impact on yield
# all genetic algorithm parameters left to default
move_agents = TRUE,
max_ages = 1000,
minimum_cost = 10,
user_budget = UB,
manager_budget = 5000,
usr_budget_rng = UBR, # introduce variation around the mean user budget (removes step pattern)
manage_target = 1125000,
RESOURCE_ini = 1125000,
culling = TRUE,
tend_crops = TRUE,
tend_crop_yld = 0.01, # tending crops increases yield by 1% - less than that of culling trees
stakeholders = 20,
land_ownership = TRUE,
public_land = 0,
manage_freq = 1,
group_think = FALSE
)
# matrix for results
thresh4 <- matrix(data=NA, nrow=100, ncol=6)
# loop the simulation.
for(time_step in 1:100){
sim_new <- gmse_apply(get_res = "Full", old_list = thresh4_sim_old, user_budget=UB, usr_budget_rng = UBR)
thresh4[time_step, 1] <- time_step
thresh4[time_step, 2] <- sim_new$basic_output$resource_results[1]
thresh4[time_step, 3] <- sim_new$basic_output$observation_results[1]
thresh4[time_step, 4] <- sim_new$basic_output$manager_results[3]
thresh4[time_step, 5] <- sum(sim_new$basic_output$user_results[,3])
thresh4[time_step, 6] <- UB
thresh4_sim_old <- sim_new
UB <- UB + 100
UBR <- UB/5
}
colnames(thresh4) <- c("Time", "Trees", "Trees_est", "Cull_cost", "Cull_count","User_budget")
thresh4_summary <- data.frame(thresh4)
#write.csv(thresh2_summary, file = "outputs/investment/null_scenarios/budget_thresholds/thresh2_summary.csv")
### Plots - thresh1, 2, 3, 4 ####
thresh1_summary <- read.csv("outputs/investment/null_scenarios/budget_thresholds/thresh1_summary.csv", header = T)
thresh2_summary <- read.csv("outputs/investment/null_scenarios/budget_thresholds/thresh2_summary.csv", header = T)
thresh3_summary <- read.csv("outputs/investment/null_scenarios/budget_thresholds/thresh3_summary.csv", header = T)
thresh4_summary <- read.csv("outputs/investment/null_scenarios/budget_thresholds/thresh4_summary.csv", header = T)
# merge dataframes
thresh1_summary$Sim <- "Thresh1_manager"
thresh1_summary <- thresh1_summary %>% rename(Budget = Manager_budget)
thresh2_summary$Sim <- "Thresh2_user"
thresh2_summary <- thresh2_summary %>% rename(Budget = User_budget)
thresh_12_summary <- rbind(thresh1_summary, thresh2_summary)
thresh3_summary$Sim <- "Thresh3_manager"
thresh3_summary <- thresh3_summary %>% rename(Budget = Manager_budget)
thresh4_summary$Sim <- "Thresh4_user"
thresh4_summary <- thresh4_summary %>% rename(Budget = User_budget)
thresh_34_summary <- rbind(thresh3_summary, thresh4_summary)
p.cull_count_12 <- ggplot(thresh_12_summary, aes(x=Time, y=Cull_count, group=Sim, color=Sim))+
geom_line(size=3)+
theme_classic()+
theme(axis.text = element_text(size=15),
axis.title = element_text(size=15),
legend.text = element_text(size=12),
legend.title = element_text(size=12))
p.cull_count_34 <- ggplot(thresh_34_summary, aes(x=Time, y=Cull_count, group=Sim, color=Sim))+
geom_line(size=3)+
theme_classic()+
theme(axis.text = element_text(size=15),
axis.title = element_text(size=15),
legend.text = element_text(size=12),
legend.title = element_text(size=12))
p.cull_count_12 + p.cull_count_34
# dataframe for the static budget
static_budget <- data.frame(Time = rep(1:100, times=2),
Budget = rep(1000, times=200),
Sim = rep(c("Thresh1_manager","Thresh2_manager"), each=100))
p.budget <- ggplot(thresh_all_summary, aes(x=Time, y=Budget, group=Sim, color=Sim))+
geom_line(size=3)+
geom_line(data=static_budget, aes(x=Time, y=Budget), linetype="dashed", size=3)+
theme_classic()+
theme(axis.text = element_text(size=15),
axis.title = element_text(size=15),
legend.position = "none")
p.cost_12 <- ggplot(thresh_12_summary, aes(x=Cull_cost, y=Cull_count, group=Sim, color=Sim))+
geom_line(size=3)+
theme_classic()+
theme(axis.text = element_text(size=15),
axis.title = element_text(size=15),
legend.text = element_text(size=12),
legend.title = element_text(size=12))
p.cost_34 <- ggplot(thresh_34_summary, aes(x=Cull_cost, y=Cull_count, group=Sim, color=Sim))+
geom_line(size=3)+
theme_classic()+
theme(axis.text = element_text(size=15),
axis.title = element_text(size=15),
legend.text = element_text(size=12),
legend.title = element_text(size=12))
(p.cull_count_12 + p.cull_count_34) / (p.cost_12 + p.cost_34)
# Ok, so when the manager budget is varied (thresh1 and thresh3), we see that around time step 25 the user loses most of their ability to cull trees. This is less certain in thresh3, as the drop off in cull count is slower than in thresh1. There is some very minor culling right to the end of the simulation in thresh3, but it is very minor towards the end. For thresh1 the point at which the majority of culling stops (cull count into singel figures), is when the manager budget is 1300. So when the manager budget is about 130% of the user budget (or the user budget is ~ 75-77% of the manager budget). The cull count in thresh3 never drops into single figures, but the point at which it drops below 100 is time step 63 when the manager budget is 6300, or 126% of the user budget (or the user budget is 80% of the manager budget). The interesting thing to note is that the total number of trees lost is dramatically larger in thresh3. This will be because of the relative differences in manager and user budget - in thresh1 the manager budget starts off at 5% of the user budget, whereas in thresh3 the manager budget starts off at 2%, and so the manager has less power.
# When the user budget is varied there is little culling at all until around time step 14 (thresh2) and time step 19 (thresh4), when the user budget is 700 (so 70% of the manager budget), and 1900 (38% of the manager budget). I am not sure exactly where the manager loses all power, but I am assuming it is the point at which they stop varying the cost of culling (i.e. they have fixed the cost of culling at the maximum). For thresh2 this is time step 25, where the user budget is 1250. This is where the manager budget is 80% of the user budget. For thresh4 this actually never quite happens, but most of the cost variation stops around time step 50, when the user and manager budgets are equal.
# Brad mentioned that he viewed the point of the manager losing all power as the resource extinction, but seeing as in this landscape set up (millions of trees, few users), this is never going to happen. Myself and Nils think that in terms of mapping to reality, the point at which the manager effectively loses all power is when they are maxing out their budget on setting as high a cost as possible, but it is not really having any effect and they stop trying to adaptively manage.
### Changing frequency - IGNORE ####
## below is code that was experimenting with sine waves and changing the frequency as you go along the time period (with help from Brad). This was one approach to scenario 5, but we ended up going down a different route (See "Fourier Transform" section). At the bottom of this section was a crude attempt by me to randomly select the point at which the peaks appeared.
q <- seq(0,50,length.out=50)
r <- 500*sin(1*q+0)+1000
plot(q,r,type="l", ylim = c(0,1600))
# These are ways of just generating random numbers drawn from either Poisson or normal distributions, or random sampling
budget <- rpois(n = 1, lambda = r)
budget <- floor( rnorm(n = 1, mean = r, sd = 500) )
budget <- sample(x = 1000:20000, size = 1)
# this creates random noise around the sin wave
xx <- seq(from = 0, to = 2*3.14, by = 0.1)
yy <- sin(xx)
plot(xx, yy + runif(n = length(xx), min = -0.1, max = 0.1), type = "l")
plot(xx, yy + runif(n = length(xx), min = -0.4, max = 0.4), type = "l")
plot(xx, 10000 + floor(1000 * (yy + runif(n = length(xx), min = -0.4, max = 0.4))), type = "l")
# this changes the wavelength over time
# changing the number that xx is multiplied by in the definition of yy changes the number of peaks. Changing the power fraction of xx changes the amount the frequency changes by. Smaller fractions change the frequency by more
xx <- seq(from = 0, to = 8*pi, by = 0.1);
yy <- sin(6*xx^(3/10));
plot(xx, yy, type = "l");
# change the y values to positive, and the x values 1:50
xx <- seq(from = 0, to = 50, length.out=50)
yy <- 500*sin(6*xx^(3/10))+1000
plot(xx, yy, type = "l")
# this was the final sine wave, but I have made the funding peaks every 5 years, resulting in a total of 10 peaks
q <- seq(0,60,length.out=50)
r <- 500*sin(1*q+0)+1000
plot(q,r,type="l", ylim = c(0,1600))
# now I want to try and create a wave with varying wavelength that also has 10 peaks. Althoug this doesnt work - I cant have the same number of peaks and varying wavelengths. The thing that has to be the same is the AUC
xx <- seq(from = 0, to = 60, length.out=50)
yy <- 500*sin(10*xx^(3/10))+1000
plot(xx, yy, type = "l")
xx <- seq(from = 0, to = 60, length.out=50)
yy <- 500*sin(10*xx^(2/10))+1000
plot(xx, yy, type = "l")
yr <- sample(1:50, 10, replace = F)
yr <- sort(yr)
# dataframe of budget values
df <- data.frame(time = 1:50,
mean = rep(500, times=50),
y = NA)
# change peak values based on random sample
for(i in 1:length(df$time)){
if(df$time[i] %in% yr){
df$y[i] <- 1000
} else {
df$y[i] <- df$y[i]
}
}
# Create vector for the values either side of the peak values
peak_sides <- vector()
for(i in 1:length(yr)){
a <- yr[i]-1
b <- yr[i]+1
d <- c(a,b)
peak_sides <- c(peak_sides,d)
}
# change values for elements either side of the peaks
for(i in 1:length(df$time)){
if(df$time[i] %in% peak_sides){
df$y[i] <- 750
} else {
df$y[i] <- df$y[i]
}
}
df$y <- ifelse(is.na(df$y),500,df$y)
plot(df$time, df$y, type="l", ylim=c(0,1200))
#
### User budget ####
# define slope
xx <- 5204.1/1275
# empty vector
usr_budget <- NULL
# starting value
usr_budget[1] <- 400
# fill in budget vector by adding the slope onto each value
for(i in 2:50){
usr_budget[i] <- usr_budget[i-1] + xx
}
sum(usr_budget)
#
### sine waves - Scenarios 3 & 4 ####
a <- seq(0,25,length.out=100)
b <- sin(a)
plot(a,b,type="l")
c <- seq(0,50,length.out=100)
d <- sin(c)
plot(c,d,type="l")
# so changing the values of the sequence changes the frequency
e <- seq(50,100,length.out=50)
f <- sin(e)
plot(e,f,type = "l")
# changing the number of values just changes the smoothness of the line (number of points)
g <- seq(0,20,length.out=100)
h <- sin(g)
plot(g,h,type = "l")
# change the amplitude
i <- seq(0,20,length.out=100)
j <- 2*sin(i)
plot(i,j,type="l")
# try to make the y values positive
# need to use the equation y = a sin (bx +c) + d, where a=1, b=1, and c=0, d=2
k <- seq(0,20,length.out=100)
l <- 1*sin(1*k+0)+1
plot(k,l,type="l")
# Now increase amplitude
m <- seq(0,20,length.out=100)
n <- 1000*sin(1*m+0)+1000
plot(m,n,type="l")
# change x axis to 0:50 (time steps), and change y axis so the minimum is 500
o <- seq(0,50,length.out=100)
p <- 500*sin(1*o+0)+1000
plot(o,p,type="l", ylim = c(0,1600))
# calculate the area under the curve
auc(o,p)
# reduce to 50 data points
q <- seq(0,50,length.out=50)
r <- 500*sin(1.3*q+0)+501
plot(q,r,type="l", ylim = c(0,1300))
## final sine wave for scenario 3
s <- seq(0,50,1)
t <- 75*sin(1.3*s+0)+400
plot(s,t,type="l", ylim = c(300,500))
## final sine wave for scenario 4
s <- seq(0,50,1)
ss <- 30*sin(2.5*u+0)+500
plot(s,ss,type="l", ylim = c(400,600))
### Below are the sine waves for S3 for Run 4, where I am making all scenarios as extreme and different from ecah other as possible.I don't think I need to change S4, because once I have made S3 more extreme, then S4 as it is will be as different as you can get
# scenario 3
# increased amplitude
a <- seq(0,50,1)
b <- 350*sin(1.3*a+0)+400
plot(a,b,type="l", ylim = c(0,800))
# change frequency
e <- seq(0,50,1)
f <- 350*sin(0.5*e+0)+400
plot(e,f,type="l", ylim = c(0,900))
# standardise
f <- 25000*(f/sum(f))
## experiment with allocating manager budget values from the above sin wave
# create very small, simple landscape to speed up run time
MB <- 1000
sin1_sim_old <- gmse_apply(
res_mod = resource,
obs_mod = observation,
man_mod = manager,
use_mod = user,
get_res = "FUll",
time_max = 50,
land_dim_1 = 20,
land_dim_2 = 20,
res_movement = 0,
remove_pr = 0,
lambda = 0,
agent_view = 20,
agent_move = 20,
res_birth_K = 1,
res_death_K = 5000000,
res_move_type = 0,
res_death_type = 0,
observe_type = 2,
times_observe = 1,
obs_move_type = 1,
res_min_age = 0,
res_move_obs = FALSE,
plotting = FALSE,
res_consume = 0.08,
# all genetic algorithm parameters left to default
move_agents = TRUE,
max_ages = 1000,
minimum_cost = 10,
user_budget = 1000,
manager_budget = MB,
usr_budget_rng = 100,
manage_target = 4000,
RESOURCE_ini = 4000,
culling = TRUE,
tend_crops = TRUE,
tend_crop_yld = 0.01,
stakeholders = 20,
land_ownership = TRUE,
public_land = 0,
manage_freq = 1,
group_think = FALSE
)
# matrix for results
sin1 <- matrix(data=NA, nrow=50, ncol=6)
# loop the simulation.
for(time_step in 1:50){
sim_new <- gmse_apply(get_res = "Full", old_list = sin1_sim_old, manager_budget=MB)
sin1[time_step, 1] <- time_step
sin1[time_step, 2] <- sim_new$basic_output$resource_results[1]
sin1[time_step, 3] <- sim_new$basic_output$observation_results[1]
sin1[time_step, 4] <- sim_new$basic_output$manager_results[3]
sin1[time_step, 5] <- sum(sim_new$basic_output$user_results[,3])
sin1[time_step, 6] <- MB
sin1_sim_old <- sim_new
MB <- r[time_step]
}
colnames(sin1) <- c("Time", "Trees", "Trees_est", "Cull_cost", "Cull_count",
"Manager_budget")
sin1 <- data.frame(sin1)
plot(sin1$Time, sin1$Manager_budget, type = "l")
## Fourier transform - scenario 5 ####
# Experimenting from the tutorial ####
# tutorial found here: http://www.di.fc.ul.pt/~jpn/r/fourier/fourier.html
# Function for plotting trajectories given a fourier series
plot.fourier <- function(fourier.series, f.0, ts) {
w <- 2*pi*f.0
trajectory <- sapply(ts, function(t) fourier.series(t,w))
plot(ts, trajectory, type="l", xlab="time", ylab="f(t)"); abline(h=0,lty=3)
}
## function to create Inverse Fourier Transform
# returns the x.n time series for a given time sequence (ts) and
# a vector with the amount of frequencies k in the signal (X.k)
get.trajectory <- function(X.k,ts,acq.freq) {
N <- length(ts)
i <- complex(real = 0, imaginary = 1)
x.n <- rep(0,N) # create vector to keep the trajectory
ks <- 0:(length(X.k)-1)
for(n in 0:(N-1)) { # compute each time point x_n based on freqs X.k
x.n[n+1] <- sum(X.k * exp(i*2*pi*ks*n/N)) / N
}
x.n * acq.freq
}
# function to plot a frequenc spectrum of a given Xk
plot.frequency.spectrum <- function(X.k, xlimits=c(0,length(X.k))) {
plot.data <- cbind(0:(length(X.k)-1), Mod(X.k))
# TODO: why this scaling is necessary?
plot.data[2:length(X.k),2] <- 2*plot.data[2:length(X.k),2]
plot(plot.data, t="h", lwd=2, main="",
xlab="Frequency (Hz)", ylab="Strength",
xlim=xlimits, ylim=c(0,max(Mod(plot.data[,2]))))
}
# Function to plot the i-th harmonic on the current plot
# Plot the i-th harmonic
# Xk: the frequencies computed by the FFt
# i: which harmonic
# ts: the sampling time points
# acq.freq: the acquisition rate
plot.harmonic <- function(Xk, i, ts, acq.freq, color="red") {
Xk.h <- rep(0,length(Xk))
Xk.h[i+1] <- Xk[i+1] # i-th harmonic
harmonic.trajectory <- get.trajectory(Xk.h, ts, acq.freq=acq.freq)
points(ts, harmonic.trajectory, type="l", col=color)
}
# example from the tutorial:
acq.freq <- 100 # data acquisition (sample) frequency (Hz)
time <- 6 # measuring time interval (seconds)
ts <- seq(0,time-1/acq.freq,1/acq.freq) # vector of sampling time-points (s)
f.0 <- 1/time # f.0 is the fundamental frequency of the complex wave
dc.component <- 1
component.freqs <- c(3,7,10) # frequency of signal components (Hz)
component.delay <- c(0,0,0) # delay of signal components (radians)
component.strength <- c(1.5,0.5,0.75) # strength of signal components
f <- function(t,w) {
dc.component +
sum( component.strength * sin(component.freqs*w*t + component.delay))
}
plot.fourier(f,f.0,ts=ts)
## increase the time interval to 50
acq.freq <- 100 # data acquisition (sample) frequency (Hz)
time <- 50 # measuring time interval (seconds)
ts <- seq(0,time-1/acq.freq,1/acq.freq) # vector of sampling time-points (s)
f.0 <- 1/time # f.0 is the fundamental frequency of the complex wave
dc.component <- 1
component.freqs <- c(3,7,10) # frequency of signal components (Hz)
component.delay <- c(0,0,0) # delay of signal components (radians)
component.strength <- c(1.5,0.5,0.75) # strength of signal components
f <- function(t,w) {
dc.component +
sum( component.strength * sin(component.freqs*w*t + component.delay))
}
plot.fourier(f,f.0,ts=ts)
## change the fundamental frequency (f.0)
acq.freq <- 100 # data acquisition (sample) frequency (Hz)
time <- 50 # measuring time interval (seconds)
ts <- seq(0,time-1/acq.freq,1/acq.freq) # vector of sampling time-points (s)
f.0 <- 3/time # f.0 is the fundamental frequency of the complex wave
dc.component <- 1
component.freqs <- c(3,7,10) # frequency of signal components (Hz)
component.delay <- c(0,0,0) # delay of signal components (radians)
component.strength <- c(1.5,0.5,0.75) # strength of signal components
f <- function(t,w) {
dc.component +
sum( component.strength * sin(component.freqs*w*t + component.delay))
}
plot.fourier(f,f.0,ts=ts)
## increasing f.0 increases the number of peaks
## change the dc.component
acq.freq <- 100 # data acquisition (sample) frequency (Hz)
time <- 50 # measuring time interval (seconds)
ts <- seq(0,time-1/acq.freq,1/acq.freq) # vector of sampling time-points (s)
f.0 <- 1/time # f.0 is the fundamental frequency of the complex wave
dc.component <- 500 # additive constant signal
component.freqs <- c(3,7,10) # frequency of signal components (Hz)
component.delay <- c(0,0,0) # delay of signal components (radians)
component.strength <- c(1.5,0.5,0.75) # strength of signal components
f <- function(t,w) {
dc.component +
sum( component.strength * sin(component.freqs*w*t + component.delay))
}
plot.fourier(f,f.0,ts=ts)
## the DC component is a constant that affects the y axis
## Now I will mess with the components
acq.freq <- 100 # data acquisition (sample) frequency (Hz)
time <- 50 # measuring time interval (seconds)
ts <- seq(0,time-1/acq.freq,1/acq.freq) # vector of sampling time-points (s)
f.0 <- 1/time # f.0 is the fundamental frequency of the complex wave
dc.component <- 500 # additive constant signal
component.freqs <- c(2,5,3) # frequency of signal components (Hz)
component.delay <- c(30,75,0) # delay of signal components (radians)
component.strength <- c(1.5,0.5,0.75) # strength of signal components
f <- function(t,w) {
dc.component +
sum( component.strength * sin(component.freqs*w*t + component.delay))
}
plot.fourier(f,f.0,ts=ts)
## Tried lots of different combinations, and you can produce all sorts of patterns
## now fiddle with the delays
acq.freq <- 100 # data acquisition (sample) frequency (Hz)
time <- 50 # measuring time interval (seconds)
ts <- seq(0,time-1/acq.freq,1/acq.freq) # vector of sampling time-points (s)
f.0 <- 1/time # f.0 is the fundamental frequency of the complex wave
dc.component <- 500 # additive constant signal
component.freqs <- c(5,7,3) # frequency of signal components (Hz)
component.delay <- c(35,0,20) # delay of signal components (radians)
component.strength <- c(2.5,2.5,5.75) # strength of signal components
f <- function(t,w) {
dc.component +
sum( component.strength * sin(component.freqs*w*t + component.delay))
}
plot.fourier(f,f.0,ts=ts)
# Messing with the delays produces some nice random peaks.
# so I think I need to work out a function that essentially samples random numbers (within bounds) to populate the component frequencies and delays. Then in the function have a test whereby the AUC is calculated and the only ones that are saved are the ones that have approximately the correct AUC.
# Code from Brad - clarifying the tutorial functions ####
# Here is a rewrite of the initial code, which might give a bit more information
# and allow you to see the component frequencies underlying the whole curve
acq.freq <- 100 # data acquisition frequency (Hz)
time <- 50 # measuring time interval (seconds)
ts <- seq(0,time,1/acq.freq) # vector of sampling time-points (s)
f.0 <- 1/time # fundamental frequency (Hz)
dc.component <- 500
component.freqs <- c(5,7,3) # frequency of signal components (Hz)
component.delay <- c(35,0,20) # delay of signal components (radians)
component.strength <- c(2.5,2.5,5.75) # strength of signal components
f <- function(t, w, cs, cf, cd) {
ft <- dc.component + sum( cs * sin(cf*w*t + cd));
return(ft);
}
# Before, this was using a trick in R that allowed the author to define the
# function from within 'sapply'. I'm not a fan of this, nor do I think that
# The use of sapply is terribly instructive. I've recoded to show how the
# function 'f' itself is passed to the function below and used inside 'lapply',
# Which essentially applies the function to all elements in ts and makes a list
plot.fourier <- function(f_function, f.0, ts, cs, cf, cd) {
w <- 2*pi*f.0
traj_list <- lapply(ts, f_function, w = w, cs = cs, cf = cf, cd = cd);
trajectory <- unlist(x = traj_list);
minval <- min(trajectory);
maxval <- max(trajectory);
trajectory_c <- NULL; # For the components
for(i in 1:length(cf)){
traj_list <- lapply(ts, f, w = w, cs = cs[i], cf = cf[i],
cd = cd[i]);
trajectory_c[[i]] <- unlist(x = traj_list);
# Don't worry about these maxval and minval lines line -- just to help plot
if(minval > min(trajectory_c[[i]])){
minval <- min(trajectory_c[[i]])
}
if(maxval < max(trajectory_c[[i]])){
maxval <- max(trajectory_c[[i]])
}
}
plot(x = ts, y = trajectory, type="l", xlab = "time", ylab = "f(t)", lwd = 2,
ylim = c(minval, maxval));
for(i in 1:length(cf)){
points(x = ts, y = trajectory_c[[i]], type = "l", lwd = 0.35, col = i + 1);
}
points(x = ts, y = trajectory, type="l", lwd = 2); # put to foreground
abline(h = 500,lty = 3);
}
plot.fourier(f = f, f.0 = f.0, ts = ts, cs = component.strength,
cf = component.freqs, cd = component.delay);
# Component frequencies are now given by the different coloured curves
# Now let's say that we want to play around and make the function gradually
# increasing over time. I can add another frequency that is much higher
new_freqs <- c(5, 7, 3, 0.5) # frequency of signal components (Hz)
new_delay <- c(35, 0, 20, 0) # delay of signal components (radians)
new_strength <- c(2.5, 2.5, 5.75, 7) # strength of signal components
plot.fourier(f = f, f.0 = f.0, ts = ts, cs = new_strength, cf = new_freqs,
cd = new_delay);
# Maybe we don't want it to come down so hard though? Reducing the other amps
new_freqs <- c(5, 7, 3, 0.5) # frequency of signal components (Hz)
new_delay <- c(35, 0, 20, 0) # delay of signal components (radians)
new_strength <- c(1, 1, 2, 7) # strength of signal components
plot.fourier(f = f, f.0 = f.0, ts = ts, cs = new_strength, cf = new_freqs,
cd = new_delay);
# Custom function to produce multiple random waves ####
# f function
f <- function(t, w, cs, cf, cd) {
ft <- dc.component + sum( cs * sin(cf*w*t + cd));
return(ft);
}
# plot.fourier function (Brad's re-write, plus it returns trajectory)
plot.fourier <- function(f_function, f.0, ts, cs, cf, cd) {
w <- 2*pi*f.0
traj_list <- lapply(ts, f_function, w = w, cs = cs, cf = cf, cd = cd);
trajectory <- unlist(x = traj_list);
minval <- min(trajectory);
maxval <- max(trajectory);
trajectory_c <- NULL; # For the components
for(i in 1:length(cf)){
traj_list <- lapply(ts, f, w = w, cs = cs[i], cf = cf[i],
cd = cd[i]);
trajectory_c[[i]] <- unlist(x = traj_list);
# Don't worry about these maxval and minval lines line -- just to help plot
if(minval > min(trajectory_c[[i]])){
minval <- min(trajectory_c[[i]])
}
if(maxval < max(trajectory_c[[i]])){
maxval <- max(trajectory_c[[i]])
}
}
plot(x = ts, y = trajectory, type="l", xlab = "time", ylab = "f(t)", lwd = 2,
ylim = c(minval, maxval));
for(i in 1:length(cf)){
points(x = ts, y = trajectory_c[[i]], type = "l", lwd = 0.35, col = i + 1);
}
points(x = ts, y = trajectory, type="l", lwd = 2); # put to foreground
abline(h = 500,lty = 3);
return(trajectory)
}
# function to produce random waves made from 3 component waves
random_wave <- function(f.0, dc.component, freq, delay, strength){
acq.freq <- 100 # data acquisition (sample) frequency (Hz)
time <- 50 # measuring time interval (time steps)
ts <- seq(1,time,1) # vector of sampling time-points (one sample per time step - manager budget)
f.0 <- f.0 # f.0 is the fundamental frequency of the complex wave
dc.component <- dc.component # additive constant signal
component.freqs <- freq # frequency of signal components (Hz)