|
697 | 697 | end |
698 | 698 |
|
699 | 699 |
|
| 700 | +""" |
| 701 | + gene_transposition!(chromosome::Chromosome, len::Int=5) |
| 702 | +
|
| 703 | +Swaps two small segments of genes between two positions within the same chromosome, preserving their order but changing their context. This implements a swap to achieve transposition by exchanging the source and target regions. |
| 704 | +
|
| 705 | +# Arguments |
| 706 | +- `chromosome::Chromosome`: Target chromosome (mutable struct) |
| 707 | +- `len::Int=3`: Length of segment to transpose (default: 5) |
| 708 | +
|
| 709 | +""" |
| 710 | +@inline function gene_transposition!(chromosome::Chromosome, len::Int=5) |
| 711 | + toolbox = chromosome.toolbox |
| 712 | + head_len = toolbox.head_len |
| 713 | + gene_len = head_len * 2 + 1 |
| 714 | + gen_start_indices = toolbox.gen_start_indices |
| 715 | + |
| 716 | + # Get start positions |
| 717 | + source_start = rand(gen_start_indices) |
| 718 | + target_start = rand(gen_start_indices) |
| 719 | + |
| 720 | + # Ensure segment length is valid |
| 721 | + segment_len = min(len, gene_len - 1) |
| 722 | + |
| 723 | + # Randomly select source and target positions within gene bounds |
| 724 | + source_pos = rand(source_start+head_len:(source_start + gene_len-segment_len)) |
| 725 | + target_pos = rand(target_start+head_len:(target_start + gene_len-segment_len)) |
| 726 | + |
| 727 | + # Perform in-place swap to avoid allocations |
| 728 | + for i in 0:(segment_len - 1) |
| 729 | + chromosome.genes[source_pos + i], chromosome.genes[target_pos + i] = deepcopy(chromosome.genes[target_pos + i]), deepcopy(chromosome.genes[source_pos + i]) |
| 730 | + end |
| 731 | +end |
| 732 | + |
| 733 | + |
700 | 734 | """ |
701 | 735 | genetic_operations!(space_next::Vector{Chromosome}, i::Int, toolbox::Toolbox) |
702 | 736 |
|
@@ -738,7 +772,7 @@ Modify chromosome genes in place |
738 | 772 | @inline function genetic_operations!(space_next::Vector{Chromosome}, i::Int, toolbox::Toolbox; generation::Int64, max_generation::Int64, parents::Vector{Chromosome}) |
739 | 773 | #allocate them within the space - create them once instead of n time |
740 | 774 | space_next[i:i+1] = replicate(space_next[i], space_next[i+1], toolbox) |
741 | | - rand_space = rand(15) |
| 775 | + rand_space = rand(20) |
742 | 776 |
|
743 | 777 |
|
744 | 778 | if rand_space[1] < toolbox.gep_probs["one_point_cross_over_prob"] |
@@ -794,13 +828,21 @@ Modify chromosome genes in place |
794 | 828 | end |
795 | 829 |
|
796 | 830 | if rand_space[14] < toolbox.gep_probs["reverse_insertion_tail"] |
797 | | - reverse_insertion_tail!(space_next[i+1]) |
| 831 | + reverse_insertion_tail!(space_next[i]) |
798 | 832 | end |
799 | 833 |
|
800 | 834 | if rand_space[15] < toolbox.gep_probs["reverse_insertion_tail"] |
801 | 835 | reverse_insertion_tail!(space_next[i+1]) |
802 | 836 | end |
803 | 837 |
|
| 838 | + if rand_space[16] < toolbox.gep_probs["gene_transposition"] |
| 839 | + reverse_insertion_tail!(space_next[i]) |
| 840 | + end |
| 841 | + |
| 842 | + if rand_space[18] < toolbox.gep_probs["gene_transposition"] |
| 843 | + reverse_insertion_tail!(space_next[i+1]) |
| 844 | + end |
| 845 | + |
804 | 846 | end |
805 | 847 |
|
806 | 848 | @inline function prob_equation_djl(chromosome::Chromosome, coeff_count::Int, prob_data_set::AbstractArray) |
|
0 commit comments