aboutsummaryrefslogtreecommitdiff
path: root/fitness.m
blob: 3dac363409b5d9184f5b9ca55d9b6815d0049cff (plain)
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
function [Energy, Waist, Penalty] = fitness( q_0, q_final, x_final, optics_positions, optics_focal_length, lambda )
%Outputs fitness of suggested solution
    x0 = 0;
    Energy = 0;
    
    x1=optics_positions(1);
    x2=optics_positions(2);
    x3=optics_positions(3);
    
    % penalty calculation
    % do not put lenses too close to each other and end positions
    lens_size=0.03;
    
    d(1)=abs(x1-x2);
    d(2)=abs(x2-x3);
    d(3)=abs(x1-x3);
    
    d_from_start=abs(x0-optics_positions);
    d_from_end=abs(x_final-optics_positions);
    
    
    d=cat(2, d, d_from_start, d_from_end);
    
    coef = 1;
   
    penalty_lenses_too_closely_spaced =coef*sum( exp(-(d/(lens_size)).^12) );
    
    Energy = Energy + penalty_lenses_too_closely_spaced;
    
    % make sure that lenses are between ends
    d_from_start=(x0-optics_positions);
    d_from_end=(optics_positions-x_final);
    
    d = cat(2, d_from_start, d_from_end);
    
    coef = 1;
    distance_scaling=100;
    penalty_lenses_outside_optical_path = coef * sum(1 + tanh(distance_scaling*d));
    
    Energy = Energy + penalty_lenses_outside_optical_path;
    
    % make collimated region between 2nd and 3rd lens   
    
    [w0, r0] = q2wr(q_0, lambda);
    [ w, w_pos ] = self_gbeam_propagation( w0, optics_positions, optics_focal_length, x0, lambda );
    
    coef = 1;
    d_object = abs(optics_positions(end) - w_pos(end - 1));
    d_lens = optics_positions(end) - optics_positions(end - 1);
    penalty_not_collimated_beam = coef * exp(-(d_object/d_lens)^2);
    
    Energy = Energy + penalty_not_collimated_beam;
    
%     % waist at end matches desired waist
    coef = 10;
    [waist_desired, r_desired] = q2wr(q_final, lambda);
    
    penalty_waist = coef *((( waist_desired-w(end) )/waist_desired)^2);
    
    Energy = Energy + penalty_waist;

    coef=10;
    dist_between_desired_and_final_waist_location =w_pos(end) - x_final;
    penalty_final_waist_position = coef*dist_between_desired_and_final_waist_location^2;

    Energy = Energy + penalty_final_waist_position;

   Penalty = [ penalty_lenses_too_closely_spaced; penalty_lenses_outside_optical_path; penalty_not_collimated_beam; penalty_waist; penalty_final_waist_position];
end