-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_popul.f90
2668 lines (2325 loc) · 125 KB
/
m_popul.f90
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
!> @file m_popul.f90
!! The Population objects for the AHA Model.
!! @author Sergey Budaev <[email protected]>
!! @author Jarl Giske <[email protected]>
!! @date 2016-2017
!-------------------------------------------------------------------------------
! $Id$
!-------------------------------------------------------------------------------
!-------------------------------------------------------------------------------
!> @brief Define the population of agents object, its properties and
!! functions.
!> @section the_population_module THE_POPULATION module
!! @details The population is in the simplest case an array of individual
!! agent objects. Individual properties of the population members can
!! be referred as, e.g. `proto_parents%individual(i)%fitness` .
module THE_POPULATION
use COMMONDATA ! Global definitions of the model objects
use THE_INDIVIDUAL ! The individual - properties of the individuals
implicit none
character (len=*), parameter, private :: MODNAME = "(THE_POPULATION)"
!> @brief Definition of individual member of a population.
!! @details Add an additional object, a member of the population.
!! It is the`INDIVIDUAL_AGENT` adding an (unique) integer
!! ID: `person_number`. Here we also have two functions for
!! setting and retrieving the IDs. We do not init members
!! of the population using a single init function, Instead,
!! init normally the INDIVIDUAL_AGENT object and for the
!! MEMBER_POPULATION just set_id. This is because we cannot
!! set id for isolated subject, only as a member of population.
type, public, extends(INDIVIDUAL_AGENT) :: MEMBER_POPULATION
integer :: person_number
contains
private
!> Set integer ID number to individual member of the population object.
!! See `the_population::set_individual_id()`.
procedure, private :: set_id => set_individual_id
!> Get integer ID number to individual member of the population object.
!! See `the_population::get_individual_id()`.
procedure, public :: get_id => get_individual_id
!> Places the individual agent, a member of the population, within a specific
!! environment at random with a **uniform** distribution.
!! See `the_population::individ_posit_in_environ_uniform()`.
procedure, public :: place_uniform => individ_posit_in_environ_uniform
!> Set the individual to be **dead**.
!! See `the_population::genome_individual_set_dead_non_pure()`.
procedure, public :: dies_debug => genome_individual_set_dead_non_pure
end type MEMBER_POPULATION
!> @brief Definition of the population object.
!! @details This is basically an array of individuals (of the type
!! `MEMBER_POPULATION`) plus population or generation descriptors.
type, public :: POPULATION
!> The size of the population.
integer :: population_size
!> @brief `POPULATION` is represented by an array of objects
!! of the type `MEMBER_POPULATION`
!! @details `POPULATION` is an array of objects of the type
!! `MEMBER_POPULATION`. It is represented as the \%individual
!! component of the type. The `i`-th individual of the population
!! `this` is accessed as `this%individual(i)`. the population also
!! has two descriptors: integer `pop_number` and string `pop_name`.
type(MEMBER_POPULATION), allocatable, dimension(:) :: individual
!> The numeric ID of the population (if we have several populations))
integer :: pop_number
!> The descriptive name of the population
character (len=LABEL_LENGTH) :: pop_name
contains
private
!> Initialise the population object.
!! See `the_population::init_population_random()`.
procedure, public :: init => init_population_random
!> Impose selective mortality at birth of the agents.
!! See `the_population::population_birth_mortality_init()`.
procedure, public :: mortality_birth => population_birth_mortality_init
!> Destroys this population and deallocates the array of individual
!! member objects.
!! See `the_population::population_destroy_deallocate_objects()`.
procedure, public :: destroy => population_destroy_deallocate_objects
!> Get the size of this population.
!! See `the_population::population_get_popsize()`.
procedure, public :: get_size => population_get_popsize
!> Get the population number ID.
!! See `the_population::population_get_pop_number()`.
procedure, public :: get_num_id => population_get_pop_number
!> Get the population character label ID.
!! See `the_population::population_get_pop_name()`.
procedure, public :: get_name => population_get_pop_name
!> Reset individual IDs of the population members.
!! See `the_population::reset_population_id_random()`.
procedure, public :: reset_id => reset_population_id_random
!> Determine the sex for each member of the population.
!! See `the_population::sex_initialise_from_genome()`.
procedure, public :: sex => sex_initialise_from_genome
!> Position each member of the population randomly within a
!! bounding environment with the **uniform** distribution.
!! See `the_population::position_individuals_uniform()`.
procedure, public :: scatter_uniform => position_individuals_uniform
!> Perform a single step of random walk by all agents, in 3D.
!! See `the_population::population_rwalk3d_all_agents_step()`.
procedure, public :: rwalk3d => population_rwalk3d_all_agents_step
!> Perform a single step of random walk by all agents, in 2.5D.
!! See `the_population::population_rwalk25d_all_agents_step()`.
procedure, public :: rwalk25d => population_rwalk25d_all_agents_step
!> This subroutine sorts the population `individual` object by
!! their \%fitness components.
!! See `the_population::sort_population_by_fitness()`.
procedure, public :: sort_by_fitness => sort_population_by_fitness
!> Save data for all agents within the population into a csv file.
!! See `the_population::population_save_data_all_agents_csv()`.
procedure, public :: save_csv => population_save_data_all_agents_csv
!> Save the genome data of all agents in this population to a CSV file.
!! See `the_population::population_save_data_all_genomes()`.
procedure, public :: save_genomes_csv => population_save_data_all_genomes
!> Save the perceptual and emotional memory stack data of all agents
!! in this population to a CSV file.
!! See `the_population::population_save_data_memory()`.
procedure, public :: save_memory_csv => population_save_data_memory
!> Save the latest movement history of all agents.
!! See `the_population::population_save_data_movements()`.
procedure, public :: save_movements_csv => population_save_data_movements
!> Save the behaviours history the_neurobio::behaviour::history_behave
!! for all agents.
!! See `the_population::population_save_data_behaviours()`.
procedure, public :: save_behaviour_csv => population_save_data_behaviours
!> Subject the population to an attack by a specific predator.
!! See `the_population::population_subject_predator_attack()`.
procedure, public :: attacked => population_subject_predator_attack
!> Subject the population to mortality caused by habitat-specific
!! mortality risk. Each agent is affected by the risk associated with
!! the habitat it is currently in.
!! See `the_population::population_subject_other_risks()`.
procedure, public :: mortality_habitat => population_subject_other_risks
!> Subject all members of this population to their individual mortality
!! risks.
!! See `the_population::population_subject_individual_risk_mortality()`.
procedure, public :: mortality_individ => &
population_subject_individual_risk_mortality
!> Calculate fitness for the pre-evolution phase of the genetic algorithm.
!! **Pre-evolution** is based on selection for a simple criterion without
!! explicit reproduction etc. The criterion for selection at this phase
!! is set by the integer the_individual::individual_agent::fitness
!! component. This procedure provides a whole-population wrapper for the
!! the_individual::fitness_calc() function.
!! See `the_population::population_preevol_fitness_calc()`.
procedure, public :: fitness_calc => population_preevol_fitness_calc
!> Determine the number of parents that have fitness higher than the
!! minimum acceptable value.
!! See `the_population::population_ga_reproduce_max()`.
procedure, public :: ga_reproduce_max => population_ga_reproduce_max
!> This function implements adaptive mutation rate that increases
!! as the population size reduces.
!! See `the_population::population_ga_mutation_rate_adaptive()`.
procedure, public :: ga_mutat_adaptive => &
population_ga_mutation_rate_adaptive
!> Perform a single step of the life cycle of the population.
!! See `the_population::population_lifecycle_step_preevol()`.
procedure, public :: lifecycle_step => population_lifecycle_step_preevol
!> Perform a single step of the life cycle of the population. This
!! version includes only optimal food selection and eating without
!! the full fledged behaviour selection cascade of procedures
!! ::do_behave().
!! See `the_population::population_lifecycle_step_eatonly_preevol()`.
procedure, public :: lifecycle_eatonly => &
population_lifecycle_step_eatonly_preevol
end type POPULATION
!> Global indicator variable that keeps the number of agents that have died
!! as a consequence of predatory attacks. All other dies are therefore caused
!! by starvation.
!! @note Note that this variable is initialised to zero at the start of each
!! generation in `GENERATIONS_PREEVOL` named loop in
!! the_evolution::generations_loop_ga().
integer, public :: Global_Ind_N_Eaten_by_Predators
contains ! ........ implementation of procedures for this level ................
!-----------------------------------------------------------------------------
!> Set integer ID number to individual member of the population object.
!! @note Note that this subroutine is private as setting individual IDs
!! makes sense only during the initialisation phase of the population.
subroutine set_individual_id (this, idnumber)
!> @param class, member of population.
class(MEMBER_POPULATION), intent(inout) :: this
!> @param id, integer id number assigned.
integer, intent(in) :: idnumber
this%person_number = idnumber !> `person_number` is assigned the idnumber
end subroutine set_individual_id
!-----------------------------------------------------------------------------
!> Get integer ID number to individual member of the population object.
!! @note Note that this is public, so we can retrieve individual IDs but not
!! set them (id's are always set at initialisation)
function get_individual_id (this) result (idnumber)
!> @param class, member of population.
class(MEMBER_POPULATION), intent(in) :: this
!> @param id, integer id number retreived.
integer :: idnumber
idnumber = this%person_number !> `person_number` is assigned the idnumber
end function get_individual_id
!-----------------------------------------------------------------------------
!> Places the individual agent, a member of the population, within a specific
!! environment at random with a **uniform** distribution.
!! The agents can be positioned with respect to their initial depth
!! - fixed depth, see commondata::init_agents_depth_is_fixed
!! - Gaussian depth, see commondata::init_agents_depth_is_gauss
!! - fully uniform distribution.
!! .
subroutine individ_posit_in_environ_uniform(this, environ)
class(MEMBER_POPULATION), intent(inout) :: this
!> @param environ sets the environment object where the agent is
!! placed randomly
class(ENVIRONMENT), intent(in) :: environ
if (INIT_AGENTS_DEPTH_IS_FIXED) then
call this%position( environ%uniform( within( INIT_AGENTS_DEPTH, &
environ%depth_min(), &
environ%depth_max() ) ) )
else if (INIT_AGENTS_DEPTH_IS_GAUSS) then
call this%position( &
environ%uniform( within( RNORM( INIT_AGENTS_DEPTH, &
cv2variance(INIT_AGENTS_DEPTH_CV, &
INIT_AGENTS_DEPTH)), &
environ%depth_min(), &
environ%depth_max() ) ) )
else
call this%position( environ%uniform() )
end if
end subroutine individ_posit_in_environ_uniform
!-----------------------------------------------------------------------------
!> Set the individual to be **dead**. Note that this function does not
!! deallocate the individual agent object, this may be a separate destructor
!! function.
!! @note This is a non-pure function logging some extended diagnostics
!! about the dying agent. See `dies` procedure from @ref the_genome
!! and all its overrides:
!! - the_genome::individual_genome::dies();
!! - the_neurobio::appraisal::dies();
!! - the_neurobio::gos_global::dies();
!! - the_individual::individual_agent::dies().
!! .
!! @warning This function should be normally used only while **debugging**.
!! In normal runs use the pure subroutine `dies` from
!! `ÌNDIVIDUAL_GENOME`.
subroutine genome_individual_set_dead_non_pure(this, non_debug_log)
class(MEMBER_POPULATION), intent(inout) :: this
logical, optional, intent(in) :: non_debug_log
!> Local flag to do show log.
logical :: do_show_log
!> Local parameter showing how many latest memory values to log out.
integer, parameter :: HIST_N=10
do_show_log = .FALSE. !> Don't show log by default.
call this%dies() !> This is a pure function from `THE_GENOME` level.
!> Turn on output logging only if in the DEBUG mode or if `non_debug_log`
!! is explicitly set to TRUE.
if (IS_DEBUG) then
do_show_log = .TRUE.
else
if (present(non_debug_log)) then
if (non_debug_log) do_show_log = .TRUE.
end if
end if
if (.not. do_show_log) return !> Just exit back if no logging.
call LOG_MSG( "Agent # " // TOSTR(this%get_id()) // " with name " // &
trim(this%individ_label()) // " dies." )
call LOG_MSG( "Agent properties: " )
call LOG_MSG( " body mass: " // TOSTR(this%get_mass()) // &
", body mass at birth: " // TOSTR(this%body_mass_birth) // &
", max body mass: " // TOSTR(this%body_mass_maximum) // &
", body length: " // TOSTR(this%get_length()) // &
", energy reserves: " // TOSTR(this%get_energy()) // &
", SMR: " // TOSTR(this%get_smr()) // &
", stomach content: " // TOSTR(this%get_stom_content()) )
call LOG_MSG( " Latest body mass (" // TOSTR(HIST_N) // ") history: " // &
TOSTR(this%body_mass_history( &
HISTORY_SIZE_AGENT_PROP-HIST_N+1:HISTORY_SIZE_AGENT_PROP)) )
call LOG_MSG( " Latest body length (" // TOSTR(HIST_N) // ") history: " &
// TOSTR(this%body_length_history( &
HISTORY_SIZE_AGENT_PROP-HIST_N+1:HISTORY_SIZE_AGENT_PROP)) )
call LOG_MSG( "Agent's GOS is " // this%gos_label() // &
", arousal: " // TOSTR(this%arousal()) // "." )
!> @note Note that `TOSTR` accepts arrays including (concatenated)
!! character arrays.
call LOG_MSG( " Latest GOS states: " // &
TOSTR(this%memory_motivations%gos_main( &
HISTORY_SIZE_MOTIVATION-HIST_N+1:HISTORY_SIZE_MOTIVATION)) )
end subroutine genome_individual_set_dead_non_pure
!-----------------------------------------------------------------------------
!> @brief Initialise the population object.
!! @details Initialise the population object, init it with random individuals
!! (function init on individuals), and assign sequential
!! person_number`s.
subroutine init_population_random(this, pop_size, pop_number_here, &
pop_name_here)
! Parameters for this subroutine:
!> @param class, This -- member of population class (this)).
class(POPULATION), intent(inout) :: this
!> @param pop_size, The size of the population.
integer, intent(in) :: pop_size
!> @param id, Optional numeric population ID.
integer, optional, intent(in) :: pop_number_here
!> @param descriptor, Optional population string descriptor.
character (len=*), optional, intent(in) :: pop_name_here
! Local variables:
integer :: i !> local variable `i` is counter
! PROCNAME is the procedure name for logging and debugging
character(len=*), parameter :: PROCNAME = "(init_population_random)"
!> Set population size from input parameter.
this%population_size = pop_size
!> Allocate the population
if (.not. allocated(this%individual)) &
allocate(this%individual(this%population_size))
!> Initialise all individuals of the population
do i=1, this%population_size
!> first, call `init to object individual(i)
call this%individual(i)%init()
!! second, ! call `set_id(i)` to identify agents within the population.
call this%individual(i)%set_id(i)
end do
!> Set optional descriptors for the whole population. Numeric ID and
!! a short text description.
if (present(pop_number_here)) then
this%pop_number = pop_number_here
else
!> If optional ID is absent, ID is set to a random integer value from 1
!! to the maximum integer allowed for the pop_number type (minus 1).
this%pop_number = RAND_I(1,huge(this%pop_number-1))
end if
if (present(pop_name_here)) then
this%pop_name = pop_name_here
else
!> If optional text description string of the population is absent,
!! it is set to the string representation of its numeric ID.
this%pop_name = TOSTR(this%pop_number)
end if
call LOG_MSG( LTAG_INFO // "Initialised population " // this%pop_name // &
" # " // TOSTR(this%pop_number) // " with " // &
TOSTR(this%population_size) // " agents." )
!> Log the initial location of the agents.
!! @warning The logic of the logger constructs here must coincide with
!! that in the population::individ_posit_in_environ_uniform().
if (INIT_AGENTS_DEPTH_IS_FIXED) then
call LOG_MSG( LTAG_INFO // "Initial location is FIXED at " // &
TOSTR(INIT_AGENTS_DEPTH) )
else if (INIT_AGENTS_DEPTH_IS_GAUSS) then
call LOG_MSG( LTAG_INFO // "Initial location is GAUSSIAN at " // &
TOSTR(INIT_AGENTS_DEPTH) // ", with CV " // &
TOSTR(INIT_AGENTS_DEPTH_CV) )
else
call LOG_MSG( LTAG_INFO // "Initial location is fully uniform." )
end if
end subroutine init_population_random
!-----------------------------------------------------------------------------
!> Destroys this population and deallocates the array of individual member
!! objects.
subroutine population_destroy_deallocate_objects(this)
class(POPULATION), intent(inout) :: this
this%population_size = 0
this%pop_number = UNKNOWN
this%pop_name = ""
if (allocated(this%individual)) deallocate(this%individual)
end subroutine population_destroy_deallocate_objects
!-----------------------------------------------------------------------------
!> Impose selective mortality at birth on the agents. Selective mortality
!! sets a fixed limit on uncontrolled evolution of the *energy reserves*
!! in newborn agents. If some newborn has too high energy at birth
!! (genetically fixed), such a deviating agent is killed at once.
!!
!! The values of the risk are chosen such that it is zero at the
!! fixed population mean (agent does not deviate) and reaches 1.0 if
!! the agent's energy reserves are 3.0 standard deviations higher than
!! the fixed mean value (agent strongly deviates).
subroutine population_birth_mortality_init(this, energy_mean, energy_sd)
class(POPULATION), intent(inout) :: this
!> @param[in] energy_mean optional mean energy at birth, if absent, is
!! calculated from the population data.
real(SRP), optional, intent(in) :: energy_mean
!> @param[in] energy_sd optional mean energy at birth, if absent, is
!! calculated from the population data.
real(SRP), optional, intent(in) :: energy_sd
! Local copies of optionals
real(SRP) :: energy_mean_loc, energy_sd_loc
real(SRP) :: mortality
integer :: ind
!> - `MORTALITY_BIRTH_INIT_ENERG_ABSCISSA` is the baseline abscissa for the
!! nonparametric interpolation function grid, in units of the standard
!! deviation (sigma) of the energy reserves in the newborn population,
!! i.e. the_body::condition::energy_birth.
! htintrpl.exe [1.4 4 5] [0 0.5 1]
! htintrpl.exe [0.7 1 2 3] [0 0.005 0.1 1]
real(SRP), parameter, dimension(*) :: MORTALITY_BIRTH_INIT_ENERG_ABSCISSA &
= [ 0.7_SRP, 1.0_SRP, 1.5_SRP, 2.0_SRP, 3.0_SRP ]
!> - `MORTALITY_BIRTH_INIT_ENERG_ORDINATE` is the ordinate for the
!! nonparametric interpolation function grid: sets the probability of
!! the agent's death given its energy reserves deviate from the fixed
!! mean by the number of standard deviations set by
!! `MORTALITY_BIRTH_INIT_ENERG_ABSCISSA`.
!! .
real(SRP), parameter, dimension(*) :: MORTALITY_BIRTH_INIT_ENERG_ORDINATE &
= [ 0.0_SRP, 0.002_SRP, 0.01_SRP, 0.1_SRP, 1.0_SRP ]
if (present(energy_mean)) then
energy_mean_loc = energy_mean
else
energy_mean_loc = average( this%individual%get_energ_birth() )
end if
if (present(energy_sd)) then
energy_sd_loc = energy_sd
else
energy_sd_loc = std_dev( this%individual%get_energ_birth() )
end if
INDS: do ind=1, this%population_size
!> Selective mortality sets a fixed limit on uncontrolled evolution of
!! the energy reserves in newborn agents. All newborn agents that have
!! the energy reserves exceeding some point may die with the probability
!! determined by the grid arrays:
!! - `MORTALITY_BIRTH_INIT_ENERG_ABSCISSA`;
!! - `MORTALITY_BIRTH_INIT_ENERG_ORDINATE`.
!! .
!!
!! The probability of death (mortality risk) is determined by the
!! nonparametric nonlinear function defined by `DDPINTERPOL`, where
!! the actual grid abscissa is the energy reserve of the agent in
!! and grid ordinate is the mortality risk.
!!
!! The values of the risk are chosen such that it is zero at the
!! fixed population mean (agent does not deviate) and reaches 1.0 if
!! the agent's energy reserves are 3.0 standard deviations higher than
!! the mean value.
mortality = within(DDPINTERPOL(energy_mean_loc + &
MORTALITY_BIRTH_INIT_ENERG_ABSCISSA * &
energy_sd_loc, &
MORTALITY_BIRTH_INIT_ENERG_ORDINATE, &
this%individual(ind)%get_energ_birth()), &
0.0_SRP, 1.0_SRP )
!> Interpolation plots can be saved in the @ref intro_debug_mode
!! "debug mode" using this plotting command:
!! `commondata::debug_interpolate_plot_save()`.
!! @warning Involves **huge** number of plots, should normally be
!! disabled.
call debug_interpolate_plot_save( &
grid_xx=energy_mean_loc + MORTALITY_BIRTH_INIT_ENERG_ABSCISSA * &
energy_sd_loc, &
grid_yy=MORTALITY_BIRTH_INIT_ENERG_ORDINATE, &
ipol_value=this%individual(ind)%get_energ_birth(), &
algstr="DDPINTERPOL", &
output_file="plot_debug_mortality_birth_" // &
"gen_" // TOSTR(Global_Generation_Number_Current) // &
"_step_"// TOSTR(Global_Time_Step_Model_Current) // &
MMDD // "_a_"// &
trim(this%individual(ind)%individ_label()) // &
"_" // RAND_STRING(LABEL_LENGTH, LABEL_CST,LABEL_CEN) &
// PS )
if ( RAND_R4() < mortality ) then
call this%individual(ind)%dies()
end if
end do INDS
end subroutine population_birth_mortality_init
!-----------------------------------------------------------------------------
!> Accessor get-function for the size of this population.
function population_get_popsize(this) result (pop_size_output)
class(POPULATION), intent(in) :: this
integer :: pop_size_output
pop_size_output = this%population_size
end function population_get_popsize
!-----------------------------------------------------------------------------
!> Accessor get-function for the population number ID.
function population_get_pop_number(this) result(pop_number_output)
class(POPULATION), intent(in) :: this
integer :: pop_number_output
pop_number_output = this%pop_number
end function population_get_pop_number
!-----------------------------------------------------------------------------
!> Accessor get-function for the population character label ID.
function population_get_pop_name(this) result (pop_name_string_out)
class(POPULATION), intent(in) :: this
character(len=LABEL_LENGTH) :: pop_name_string_out
pop_name_string_out = this%pop_name
end function population_get_pop_name
!-----------------------------------------------------------------------------
!> @brief Reset individual IDs of the population members.
!! @details Makes new random individual IDs for the population members.
subroutine reset_population_id_random(this, pop_number_here, pop_name_here)
! Parameters for this subroutine:
!> @param class, This -- member of population class (this)).
class(POPULATION), intent(inout) :: this
!> @param id, Optional numeric population ID.
integer, optional, intent(in) :: pop_number_here
!> @param descriptor, Optional population string descriptor.
character (len=*), optional, intent(in) :: pop_name_here
! Local variables:
integer :: i !> local variable `i` is counter
!> Reset all individual IDs of the population members
do i=1, this%population_size
call this%individual(i)%set_id(i) ! call set_id(i) to the same object
end do
!> Set optional descriptors for the whole population. Numeric ID and
!! a short text description.
if (present(pop_number_here)) then
this%pop_number = pop_number_here
else
!> If optional ID is absent, ID is set to a random integer value from 1
!! to the maximum integer allowed for the pop_number type (minus 1).
this%pop_number = RAND_I(1,huge(this%pop_number-1))
end if
if (present(pop_name_here)) then
this%pop_name = pop_name_here
else
!> If optional text description string of the population is absent,
!! it is set to the string representation of its numeric ID.
this%pop_name = TOSTR(this%pop_number)
end if
end subroutine reset_population_id_random
!-----------------------------------------------------------------------------
!> @brief Determine the sex for each member of the population.
subroutine sex_initialise_from_genome(this)
class(POPULATION), intent(inout) :: this
integer :: i
do i=1, this%population_size
call this%individual(i)%sex_init()
end do
end subroutine sex_initialise_from_genome
!-----------------------------------------------------------------------------
!> @brief Position each member of the population randomly within a
!! bounding environment.
!! @note Moved the positioning procedure into a separate procedure as
!! initialising population may involve different spatial positioning,
!! e.g. uniform within the bounding environment or gaussian localised.
subroutine position_individuals_uniform(this, environ)
class(POPULATION), intent(inout) :: this
!> @param environ the environment where we place the population
!! @warning Even though this parameter is optional, the bounding
!! environment should in most cases (almost?) be fixed, and
!! provided as a parameter. In most cases, unlimited environment
!! is useful for debugging only.
!! TODO: convert to class
class(ENVIRONMENT), optional, intent(in) :: environ
!> Local object representing the bounding environment for this population
type(ENVIRONMENT) :: environ_here
!> Local counter
integer :: i
!> Check if the bounding environment is provided, if not, place agents
!! without limits
!! @warning The bounding environment should in most cases be fixed, and
!! provided as a parameter.
if (present(environ)) then
call environ_here%build( environ%lim_min(), environ%lim_max() )
else
call environ_here%build_unlimited()
end if
!> Position agents randomly (uniform distribution) within the
!! bounding environment.
do i=1, this%population_size
call this%individual(i)%place_uniform(environ_here)
end do
end subroutine position_individuals_uniform
!-----------------------------------------------------------------------------
!> @brief This subroutine sorts the population `individual` object by
!! their \%fitness components.
!! @details The two subroutines `qsort` and `qs_partition_fitness` are a
!! variant of the recursive quick sort algorithm adapted for
!! `MEMBER_POPULATION` integer fitness component
elemental subroutine sort_population_by_fitness(this)
!> @param class, This -- member of population class (this)).
class(POPULATION), intent(inout) :: this
call qsort(this%individual) !> This is the array component we sort.
contains
!...........................................................................
!> `qsort` is a recursive frontend for `MEMBER_POPULATION` objects
recursive pure subroutine qsort(A)
!> @param `A` has the same type as the individual component objects
!! of the array-object that we are going to sort.
type(MEMBER_POPULATION), intent(in out), dimension(:) :: A
integer :: iq
if(size(A) > 1) then
call qs_partition_fitness(A, iq) ! partition
call qsort(A(:iq-1))
call qsort(A(iq:))
endif
end subroutine qsort
!...........................................................................
!> partition is a pivot backend for `fitness`
pure subroutine qs_partition_fitness(A, marker)
type(MEMBER_POPULATION), intent(in out), dimension(:) :: A
integer, intent(out) :: marker
integer :: i, j
type(MEMBER_POPULATION) :: temp
!> @note Pivot point `x`, has the same type **as
!! the sorted object component**.
integer :: x
!> Fitness is hardwired in this partition subroutine, but it can be used
!! as a model for similar othert sorting functions. Note that here integer
!! array is sorted.
x = A(1)%fitness
i= 0
j= size(A) + 1
do
j = j-1
do
if (A(j)%fitness <= x) exit
j = j-1
end do
i = i+1
do
if (A(i)%fitness >= x) exit
i = i+1
end do
if (i < j) then
! exchange A(i) and A(j)
temp = A(i)
A(i) = A(j)
A(j) = temp
elseif (i == j) then
marker = i+1
return
else
marker = i
return
endif
end do
end subroutine qs_partition_fitness
end subroutine sort_population_by_fitness
!-----------------------------------------------------------------------------
!> Perform one or several steps of random walk by all agents.
!! @note This procedure was used for debugging.
subroutine population_rwalk3d_all_agents_step( this, dist_array, cv_array, &
dist_all, cv_all, &
environment_limits, n_walks )
class(POPULATION), intent(inout) :: this
!> @param[in] step_size_array an array of step sizes for each individual.
real(SRP), optional, dimension(:), intent(in) :: dist_array
!> @param[in]cv_array Coefficients of variation for the walk.
real(SRP), optional, dimension(:), intent(in) :: cv_array
!> @param[in] dist_all the value of the walk step size that is identical in
!! all agents within the population.
real(SRP), optional, intent(in) :: dist_all
!> @param[in] cv_all the value of the walk coefficient of variation that is
!! identical in all agents within the population.
real(SRP), optional, intent(in) :: cv_all
!> @param environment_limits Limits of the environment area available for
!! the random walk. The moving object cannot get beyond this limit.
!! If this parameter is not provided, the environmental limits are
!! obtained automatically from the global array
!! the_environment::global_habitats_available.
class(ENVIRONMENT), intent(in), optional :: environment_limits
!> @param[in] n_walk optional number of walk steps that should be
!! performed, default just one.
integer, optional, intent(in) :: n_walks
! Local variables, copies of optionals.
real(SRP), dimension(this%population_size) :: dist_array_here, cv_array_here
integer :: n_walks_here
! Local params.
real(SRP), dimension(this%population_size) :: step_size_walk
integer :: j, i, ind, pop_n
integer, dimension(this%population_size) :: pop_permutation
! Default walk step CV.
real(SRP), parameter :: CV_DEFAULT = 0.5_SRP
!> ### Implementation details ###
!> - Calculate the distance array size.
pop_n = this%population_size
if (present(dist_array)) then
dist_array_here = dist_array
else
dist_array_here = this%individual%get_length()
end if
if (present(cv_array)) then
cv_array_here = cv_array
else
cv_array_here = CV_DEFAULT
end if
if (present(dist_all)) then
dist_array_here = dist_all
else
dist_array_here = this%individual%get_length()
end if
if (present(cv_all)) then
cv_array_here = cv_all
else
cv_array_here = CV_DEFAULT
end if
if (present(n_walks)) then
n_walks_here = n_walks
else
n_walks_here = 1
end if
!> - calculate the step size along the axes from the distance array.
step_size_walk = dist2step(dist_array_here)
!> - Calculate the random permutation of individual indices.
!! @warning Random order here is a prototype for testing for use in
!! behaviour selection by population members.
pop_permutation = PERMUTE_RANDOM(pop_n)
!> - Perform Gaussian random walks for each of the individuals in a random
!! order that is set by the `pop_permutation` array.
!! .
ENVIRON_RESTRICT: if (present(environment_limits)) then
do j=1, n_walks_here
do i=1, pop_n
ind = pop_permutation(i)
if (this%individual(ind)%is_alive()) &
call this%individual(ind)%rwalk( step_size_walk(ind), &
cv_array_here(ind), &
environment_limits )
end do
end do
else ENVIRON_RESTRICT
do j=1, n_walks_here
do i=1, pop_n
ind = pop_permutation(i)
if (this%individual(ind)%is_alive()) &
call this%individual(ind)%rwalk( &
step_size_walk(ind), &
cv_array_here(ind), &
Global_Habitats_Available( &
this%individual(ind)%find_environment( &
Global_Habitats_Available) ) )
end do
end do
end if ENVIRON_RESTRICT
end subroutine population_rwalk3d_all_agents_step
!-----------------------------------------------------------------------------
!> Perform one or several steps of random walk by all agents.
!! @note This procedure was used for debugging.
subroutine population_rwalk25d_all_agents_step ( this, &
dist_array_xy, cv_array_xy, &
dist_array_depth, cv_array_depth, &
dist_all_xy, cv_all_xy, &
dist_all_depth, cv_all_depth, &
environment_limits, n_walks )
class(POPULATION), intent(inout) :: this
!> @param[in] dist_array_xy an array of step sizes for each individual.
real(SRP), optional, dimension(:), intent(in) :: dist_array_xy
!> @param[in]cv_array_xy Coefficients of variation for the walk.
real(SRP), optional, dimension(:), intent(in) :: cv_array_xy
!> @param[in] dist_array_depth an array of step sizes for each individual.
real(SRP), optional, dimension(:), intent(in) :: dist_array_depth
!> @param[in]cv_array_depth Coefficients of variation for the walk.
real(SRP), optional, dimension(:), intent(in) :: cv_array_depth
!> @param[in] dist_all_xy the value of the walk step size for horizontal
!! plane that is identical in all agents within the population.
real(SRP), optional, intent(in) :: dist_all_xy
!> @param[in] cv_all_xy the value of the walk coefficient of variation in
!! the horizontal plane that is identical in all agents within
!! the population.
real(SRP), optional, intent(in) :: cv_all_xy
!> @param[in] dist_all_depth the value of the walk step size for the depth
!! plane that is identical in all agents within the population.
real(SRP), optional, intent(in) :: dist_all_depth
!> @param[in] cv_all_depth the value of the walk coefficient of variation
!! in the depth plane that is identical in all agents within
!! the population.
real(SRP), optional, intent(in) :: cv_all_depth
!> @param environment_limits Limits of the environment area available for
!! the random walk. The moving object cannot get beyond this limit.
!! If this parameter is not provided, the environmental limits are
!! obtained automatically from the global array
!! the_environment::global_habitats_available.
class(ENVIRONMENT), intent(in), optional :: environment_limits
!> @param[in] n_walk optional number of walk steps that should be
!! performed, default just one.
integer, optional, intent(in) :: n_walks
! Local variables, copies of optionals.
real(SRP), dimension(this%population_size) :: &
dist_array_xy_here, cv_array_xy_here
real(SRP), dimension(this%population_size) :: &
dist_array_depth_here, cv_array_depth_here
integer :: n_walks_here
! Local params.
real(SRP), dimension(this%population_size) :: step_size_walk_xy, &
step_size_walk_depth
integer :: j, i, ind, pop_n
integer, dimension(this%population_size) :: pop_permutation
! Default walk step CV.
real(SRP), parameter :: CV_DEFAULT = 0.5_SRP
!> ### Implementation details ###
!> - Calculate the distance array size.
pop_n = this%population_size
if (present(dist_array_xy)) then
dist_array_xy_here = dist_array_xy
else
dist_array_xy_here = this%individual%get_length()
end if
if (present(cv_array_xy)) then
cv_array_xy_here = cv_array_xy
else
cv_array_xy_here = CV_DEFAULT
end if
!> - If the depth walk step distance is not provided as a parameter,
!! 1/2 of the agent body size is used as the default value. Thus,
!! it is assumed that the extent of random movements of the agents
!! in the horizontal plane is greater than vertical movements.
if (present(dist_array_depth)) then
dist_array_depth_here = dist_array_depth
else
dist_array_depth_here = this%individual%get_length() / 2.0_SRP
end if
if (present(cv_array_depth)) then
cv_array_depth_here = cv_array_depth
else
cv_array_depth_here = CV_DEFAULT
end if
if (present(dist_all_xy)) then
dist_array_xy_here = dist_all_xy
else
dist_array_xy_here = this%individual%get_length()
end if
if (present(cv_all_xy)) then
cv_array_xy_here = cv_all_xy
else
cv_array_xy_here = CV_DEFAULT
end if
if (present(dist_all_depth)) then
dist_array_depth_here = dist_all_depth
else
dist_array_depth_here = this%individual%get_length() / 2.0_SRP
end if
if (present(cv_all_depth)) then
cv_array_depth_here = cv_all_depth
else
cv_array_depth_here = CV_DEFAULT
end if
if (present(n_walks)) then
n_walks_here = n_walks
else
n_walks_here = 1
end if
!> - Calculate the step size along the axes from the distance array.
step_size_walk_xy = dist2step(dist_array_xy_here)
step_size_walk_depth = dist2step(dist_array_depth_here)
!> - Calculate the random permutation of individual indices.
!! @warning Random order here is a prototype for testing for use in
!! behaviour selection by population members.
pop_permutation = PERMUTE_RANDOM(pop_n)
!> - Perform Gaussian random walks for each of the individuals in a random
!! order that is set by the `pop_permutation` array.
!! .
ENVIRON_RESTRICT: if (present(environment_limits)) then
do j=1, n_walks_here
do i=1, pop_n
ind = pop_permutation(i)
if (this%individual(ind)%is_alive()) &
call this%individual(ind)%rwalk25d &
( meanshift_xy = step_size_walk_xy(ind), &
cv_shift_xy = cv_array_xy_here(ind), &
meanshift_depth = step_size_walk_depth(ind), &
cv_shift_depth = cv_array_depth_here(ind), &
environment_limits = environment_limits )
end do
end do
else ENVIRON_RESTRICT
do j=1, n_walks_here
do i=1, pop_n
ind = pop_permutation(i)
if (this%individual(ind)%is_alive()) &
call this%individual(ind)%rwalk25d &
( meanshift_xy = step_size_walk_xy(ind), &
cv_shift_xy = cv_array_xy_here(ind), &
meanshift_depth = step_size_walk_depth(ind), &
cv_shift_depth = cv_array_depth_here(ind), &
environment_limits=Global_Habitats_Available( &
this%individual(ind)%find_environment( &
Global_Habitats_Available) ) )
end do
end do
end if ENVIRON_RESTRICT
end subroutine population_rwalk25d_all_agents_step
!-----------------------------------------------------------------------------
!> Subject the population to an attack by a specific predator. The predator
!! acts on agents in its proximity and takes account of the predation
!! confusion and dilution effects (see
!! the_environment::predator::risk_fish_group()).
subroutine population_subject_predator_attack(this, this_predator, &
time_step_model)
class(POPULATION), intent(inout) :: this
class(PREDATOR), intent(in) :: this_predator
integer, optional, intent(in) :: time_step_model
! Local copie sof optionals
integer :: time_step_model_here