otchet (1108529), страница 3
Текст из файла (страница 3)
i n " )( lambda ( out )( begin( w r i t e l n ( gen - s t a r num- o f - v e r t e x ) out )( w r i t e l n ( random ( ∗ num- o f - v e r t e x 1 0 ) ) out )( w r i t e ( gen - t e s t - e d g e s num- o f - v e r t e x ) out ) ) )#: e x i s t s ' t r u n c a t e )( c a l l - with - output - f i l e ( s t r i n g - append f o l d e r " s t a r - t e s t - v" ( number ->s t r i n gnum- o f - v e r t e x ) " . out " )( lambda ( out )( w r i t e #f out ) )#: e x i s t s ' t r u n c a t e )( if (> count 1 )( gen - s t a r - t e s t s ( - count 1 ) ) ' ( ) ) )( define ( gen - f u l l - t e s t s count )( for ( [ i ( build - l i s t 5 inc ) ] )( let ( ( f u l l ( gen - f u l l count ) ) ( w e i g h t (+ ( ∗ count 5 ) ( random ( ∗ count1 0 ) ) ) ) ( e d g e s ( gen - t e s t - e d g e s count ) ) )( c a l l - with - output - f i l e ( s t r i n g - append f o l d e r " f u l l - t e s t - v"( number ->s t r i n g count ) " - " ( number ->s t r i n g i ) " .
i n " )( lambda ( out )( begin( w r i t e l n f u l l out )( w r i t e l n w e i g h t out )( w r i t e l n e d g e s out ) ) )#: e x i s t s ' t r u n c a t e )( c a l l - with - output - f i l e ( s t r i n g - append f o l d e r " f u l l - t e s t - v"( number ->s t r i n g count ) " - " ( number ->s t r i n g i ) " . out " )( lambda ( out )( let ( ( answer ( s o l v e r f u l l w e i g h t e d g e s ) ) )( if answer( begin( w r i t e l n ( c a r answer ) out )( w r i t e l n ( c a d r answer ) out )( w r i t e ( caddr answer ) out ) )( w r i t e #f out ) ) ) )#: e x i s t s ' t r u n c a t e )))( if (> count 1 )( gen - f u l l - t e s t s ( - count 1 ) ) ' ( ) ) )( define ( gen - random - t e s t s count )( for ( [ i ( build - l i s t 5 inc ) ] )15( let ( ( rnd ( gen - random count ) ) ( w e i g h t (+ ( ∗ count 5 ) ( random ( ∗ count1 0 ) ) ) ) ( e d g e s ( gen - t e s t - e d g e s count ) ) )( c a l l - with - output - f i l e ( s t r i n g - append f o l d e r "random - t e s t - v"( number ->s t r i n g count ) " - " ( number ->s t r i n g i ) " .
i n " )( lambda ( out )( begin( w r i t e l n rnd out )( w r i t e l n w e i g h t out )( w r i t e l n e d g e s out ) ) )#: e x i s t s ' t r u n c a t e )( c a l l - with - output - f i l e ( s t r i n g - append f o l d e r "random - t e s t - v"( number ->s t r i n g count ) " - " ( number ->s t r i n g i ) " . out " )( lambda ( out )( let ( ( answer ( s o l v e r rnd w e i g h t e d g e s ) ) )( if answer( begin( w r i t e l n ( c a r answer ) out )( w r i t e l n ( c a d r answer ) out )( w r i t e ( caddr answer ) out ) )( w r i t e #f out ) ) ) )#: e x i s t s ' t r u n c a t e )))( if (> count 1 )( gen - random - t e s t s ( - count 1 ) ) ' ( ) ) )( define ( gen - t e s t s )( c a l l - with - output - f i l e " t e s t s L o g .
t x t "( lambda ( out )( define a l l t i m e ( c u r r e n t - s e c o n d s ) )( define timeC ( c u r r e n t - s e c o n d s ) )( gen - c y c l e - t e s t s 2 5 ) ; 25( d i s p l a y "CYCLE-TESTS-GENERATED, ␣TIME␣ELAPSED␣ ( s e c ) : ␣ " out )( d i s p l a y l n ( - ( c u r r e n t - s e c o n d s ) timeC ) out )( define timeS ( c u r r e n t - s e c o n d s ) )( gen - s t a r - t e s t s 1 5 ) ; 25 + 15 = 40( d i s p l a y "STAR-TESTS-GENERATED, ␣TIME␣ELAPSED␣ ( s e c ) : ␣ " out )( d i s p l a y l n ( - ( c u r r e n t - s e c o n d s ) timeS ) out )( define timeF ( c u r r e n t - s e c o n d s ) )( gen - f u l l - t e s t s 1 0 ); 40 + 5 ∗ 10 = 90( d i s p l a y "FULL-TESTS-GENERATED, ␣TIME␣ELAPSED␣ ( s e c ) : ␣ " out )( d i s p l a y l n ( - ( c u r r e n t - s e c o n d s ) timeF ) out )( define timeR ( c u r r e n t - s e c o n d s ) )( gen - random - t e s t s 1 2 ) ; 90 + 5 ∗ 12 = 150( d i s p l a y "RANDOM-TESTS-GENERATED, ␣TIME␣ELAPSED␣ ( s e c ) : ␣ " out )( d i s p l a y l n ( - ( c u r r e n t - s e c o n d s ) timeR ) out )( d i s p l a y "ALL-TESTS-GENERATED, ␣TIME␣ELAPSED␣ ( s e c ) : ␣ " out )( d i s p l a y ( - ( c u r r e n t - s e c o n d s ) a l l t i m e ) out ) )#: e x i s t s ' t r u n c a t e ) )( gen - t e s t s )Листинг 5.2: Генетический алгоритм#l a n g scheme/base( define pop - s i z e 3 0 )( define mutate - prob 0 .
6 )( define par - p e r c e n t 0 . 6 )( define max - i t e r 1 0 0 0 0 )16( define e x i t - p e r c e n t 0 . 0 0 0 0 1 )( define ( g e n e t i c - a l g o r i t h m graphc o n t r o l - weightcontrol - edgespop - s i z emutate - probpar - p e r c e n tmax - i t e rexit - percent )( define d a u g h t e r - p e r c e n t ( - 1 par - p e r c e n t ) )( define ( push l s t elem )( if ( not ( member elem l s t ) ) ( c o n s elem l s t ) l s t ) )( define ( a l l - v e r t e x graph )( define ( h e l p e r graph r e s u l t )( if ( n u l l ? graph ) r e s u l t( h e l p e r ( c d r graph ) ( push ( push r e s u l t ( c a a r graph ) ) ( c a d a r graph ) ) ) ) )( h e l p e r graph ' ( ) ) )( define ( p i c k - random l s t )( l i s t - r e f l s t ( random ( l e n g t h l s t ) ) ) )( define ( s l i c e l s t s t a r t count )( define ( g e t - n - i t e m s l s t num)( if ( and (> num 0 ) ( not ( n u l l ? l s t ) ) )( c o n s ( c a r l s t ) ( g e t - n - i t e m s ( c d r l s t ) ( - num 1 ) ) )( if (> s t a r t 0 )( s l i c e ( c d r l s t ) ( - s t a r t 1 ) count )( g e t - n - i t e m s l s t count ) ) )'() ) )( define ( path - w e i g h t path )( define p e n a l t y ( ∗ ( l e n g t h path ) 1 0 ) )( define ( f i n d - w e i g h t edge )( define ( h e l p e r x )( if ( or ( and ( e q u a l ? ( c a r x ) ( c a r edge ) ) ( e q u a l ? ( c a d r x ) ( c d r edge ) ) )( and ( e q u a l ? ( c a r x ) ( c d r edge ) ) ( e q u a l ? ( c a d r x ) ( c a r edge ) ) ) )( if ( e q u a l ? ( c a r edge ) ( c d r edge ) ) 0 ( caddr x ) ) ' ( ) ) )( f i l t e r ( lambda ( x ) ( not ( n u l l ? x ) ) ) ( map h e l p e r graph ) ) )( define ( h e l p e r path r e s u l t )( if ( n u l l ? ( c d r path ) ) r e s u l t( let ( (w ( f i n d - w e i g h t ( c o n s( if ( n u l l ? w)( h e l p e r ( c d r path ) (+( h e l p e r ( c d r path ) (+( h e l p e r ( c o n s ( c a r path ) ( r e v e r s e( c a r path ) ( c a d r path ) ) ) ) )penalty r e s u l t ) )( c a r w) r e s u l t ) ) ) ) ) )path ) ) 0 ) )( define ( f i t n e s s - f u n c t i o n i n d i v i d )( define p e n a l t y ( ∗ ( l e n g t h i n d i v i d ) 1 0 ) )( define ( check - e d g e s path )( define ( f i n d - next y )( c a d r ( member y path ) ) )( define ( f i n d - c o n t r o l x )( ormap e q u a l ? ( map f i n d - next x ) ( r e v e r s e x ) ) )17( ∗ ( l e n g t h ( f i l t e r ( lambda ( z ) ( not z ) ) ( map f i n d - c o n t r o lcontrol - edges ) ) ) penalty ) )( let ( ( r e s (+ ( path - w e i g h t i n d i v i d ) ( check - e d g e s ( c o n s ( c a r i n d i v i d )( reverse individ ) ) ) ) ) )( if (> r e s p e n a l t y )(+ r e s c o n t r o l - w e i g h t 1 )res ) ) )( define ( f i t n e s s - pop p o p u l a t i o n )( define ( r e s u l t s x )( cons x ( f i t n e s s - f u n c t i o n x ) ) )( s o r t ( map r e s u l t s p o p u l a t i o n ) > #: key ( lambda ( x ) ( c d r x ) ) ) )( define ( s e l e c t i o n par - pop d a u g h t e r - pop )( let ( ( par - t h r e s h o l d ( - ( l e n g t h par - pop ) ( round ( ∗ ( l e n g t h par - pop )par - p e r c e n t ) ) ) )( d a u g h t e r - t h r e s h o l d ( - ( l e n g t h d a u g h t e r - pop ) ( round ( ∗ ( l e n g t hd a u g h t e r - pop ) d a u g h t e r - p e r c e n t ) ) ) ) )( append ( s l i c e ( map c a r ( f i t n e s s - pop par - pop ) ) par - t h r e s h o l d ( - ( l e n g t hpar - pop ) par - t h r e s h o l d ) )( s l i c e ( map c a r ( f i t n e s s - pop d a u g h t e r - pop ) ) d a u g h t e r - t h r e s h o l d ( ( l e n g t h d a u g h t e r - pop ) d a u g h t e r - t h r e s h o l d ) ) ) ) )( define ( gen - f i r s t - pop n graph )( define ( f a c t n )( if (= n 0 ) 1(∗ n ( f a c t ( - n 1) ) ) ) )( define ( h e l p e r v e r t i c e s n r e s u l t )( define ( gen - random - l i s t v e r t i c e s r e s u l t )( if ( n u l l ? v e r t i c e s ) r e s u l t( let ( ( rnd ( p i c k - random v e r t i c e s ) ) )( gen - random - l i s t ( remove rnd v e r t i c e s ) ( c o n s rnd r e s u l t ) ) ) ) )( if (= n 0 ) r e s u l t( let ( ( c u r r e n t ( gen - random - l i s t v e r t i c e s ' ( ) ) ) )( if ( member c u r r e n t r e s u l t )( helper vertices n result )( h e l p e r v e r t i c e s ( - n 1) ( cons current r e s u l t ) ) ) ) ) )( let ( ( v e r t i c e s ( a l l - v e r t e x graph ) ) )( if (> n ( f a c t ( l e n g t h v e r t i c e s ) ) )( helper v e r t i c e s ( length v e r t i c e s ) '() )( helper vertices n '() ) ) ) )( define ( c r o s s - pop p o p u l a t i o n )( define ( c r o s s par1 par2 )( define ( f i l l l s t 1 l s t 2 t o from tmp )( cond ( ( or ( n u l l ? l s t 2 ) (= (+ ( l e n g t h l s t 1 ) ( l e n g t h tmp ) ) ( l e n g t hpar1 ) ) ) ( append tmp l s t 1 ) )( ( member ( c a r l s t 2 ) l s t 1 ) ( f i l l l s t 1 ( c d r l s t 2 ) t o from tmp ) )((> t o 0 ) ( f i l l l s t 1 ( c d r l s t 2 ) ( - t o 1 ) from ( c o n s ( c a r l s t 2 )tmp ) ) )((> from 0 ) ( f i l l ( append l s t 1 ( l i s t ( c a r l s t 2 ) ) ) ( c d r l s t 2 ) t o( - from 1 ) tmp ) ) ) )( let* ( ( n ( q u o t i e n t ( l e n g t h par1 ) 2 ) ) ( g ( random ( - ( l e n g t h par1 ) n - 1 ) ) )( c h i l d ( s l i c e par2 g n ) ) )( f i l l c h i l d ( r e v e r s e par1 ) g ( - ( l e n g t h par1 ) g n ) ' ( ) ) ) )18( define ( h e l p e r pop r e s u l t )( if ( n u l l ? pop ) r e s u l t( h e l p e r ( c d r pop ) ( c o n s ( c r o s s ( c a r pop ) ( p i c k - random p o p u l a t i o n ) )result ))))( helper population '() ) )( define ( mutate - pop p o p u l a t i o n )( define ( mutate i n d i v i d )( define ( swap i n d 1 i n d 2 l s t )( append ( s l i c e l s t 0 i n d 1 ) ( l i s t ( l i s t - r e f l s t i n d 2 ) ) ( s l i c e l s t (+i n d 1 1 ) ( - i n d 2 i n d 1 1 ) ) ( l i s t ( l i s t - r e f l s t i n d 1 ) ) ( s l i c e l s t (+ind2 1) ( - ( length l s t ) ind2 - 1) ) ) )( define ( gen - o t h e r - i n d e x i n d e x )( let ( ( new ( random ( l e n g t h i n d i v i d ) ) ) )( if (= i n d e x new ) ( gen - o t h e r - i n d e x i n d e x ) new ) ) )( let* ( ( i n d e x 1 ( random ( l e n g t h i n d i v i d ) ) ) ( i n d e x 2 ( gen - o t h e r - i n d e xindex1 ) ) )( if (> i n d e x 2 i n d e x 1 )( swap i n d e x 1 i n d e x 2 i n d i v i d )( swap i n d e x 2 i n d e x 1 i n d i v i d ) ) ) )( define ( h e l p e r p o p u l a t i o n r e s u l t )( if ( n u l l ? p o p u l a t i o n ) r e s u l t( if (> ( random ) mutate - prob )( h e l p e r ( c d r p o p u l a t i o n ) ( c o n s ( mutate ( c a r p o p u l a t i o n ) ) r e s u l t ) )( h e l p e r ( cdr population ) ( cons ( car population ) r e s u l t ) ) ) ) )( let* ( ( s o r t e d - pop ( map c a r ( f i t n e s s - pop p o p u l a t i o n ) ) ) ( h a l f ( q u o t i e n t( l e n g t h s o r t e d - pop ) 2 ) ) )( append ( h e l p e r ( s l i c e s o r t e d - pop 0 h a l f ) ' ( ) ) ( s l i c e s o r t e d - pop h a l f ( ( l e n g t h s o r t e d - pop ) h a l f ) ) ) ) )( define ( p o p u l a t i o n - w e i g h t p o p u l a t i o n )( / ( f o l d l + 0 ( map f i t n e s s - f u n c t i o n p o p u l a t i o n ) ) ( l e n g t h p o p u l a t i o n ) 1 .
0 ) )( define ( e v o l u t i o n p o p u l a t i o n i t e r t o t a l - sum )( if ( or (= i t e r max - i t e r ) (= t o t a l - sum - 1 ) )( let ( ( answer ( c a r ( r e v e r s e ( f i t n e s s - pop p o p u l a t i o n ) ) ) ) )( if (> ( c d r answer ) c o n t r o l - w e i g h t ) ( l i s t i t e r #f )( if ( n u l l ? ( c d a r answer ) )( l i s t i t e r #t ( c d r answer ) ( c a r answer ) )( l i s t i t e r #t ( c d r answer ) ( c o n s ( c a r ( c a r answer ) ) ( r e v e r s e( c a r answer ) ) ) ) ) ) )( let* ( ( new - pop ( mutate - pop ( s e l e c t i o n p o p u l a t i o n ( c r o s s - poppopulation ) ) ) )( new - w e i g h t ( p o p u l a t i o n - w e i g h t new - pop ) ) )( if ( not (> ( abs ( - t o t a l - sum new - w e i g h t ) ) ( ∗ e x i t - p e r c e n tt o t a l - sum ) ) )( e v o l u t i o n new - pop (+ i t e r 1 ) - 1 )( e v o l u t i o n new - pop (+ i t e r 1 ) new - w e i g h t ) ) ) ) )( let ( ( f i r s t ( gen - f i r s t - pop pop - s i z e graph ) ) )( e v o l u t i o n f i r s t 1 ( population - weight f i r s t ) ) ) )( define (cmd - p a r s e r )( define ( p a r s e f i l e )( c a l l - with - i n p u t - f i l e( lambda ( i n )file19( l i s t ( read in ) ( read in ) ( read in ) ) ) ) )( define ( d i s p l a y - l i s t l s t )( if ( n u l l ? l s t ) ' ( )( begin ( d i s p l a y l n ( c a r l s t ) ) ( d i s p l a y - l i s t ( c d r l s t ) ) ) ) )( let ( ( cmd ( v e c t o r -> l i s t ( c u r r e n t - command - l i n e - arguments ) ) ) )( cond ( ( n u l l ? cmd) ( d i s p l a y "EMPTY" ) )( ( not ( n u l l ? ( c d r cmd) ) ) ( d i s p l a y "EXPECTED␣ 1 ␣ARGUMENT" ) )( else ( let ( ( a r g s ( p a r s e ( c a r cmd) ) ) )( let ( ( answer ( g e n e t i c - a l g o r i t h m ( c a r a r g s ) ( c a d r a r g s )( caddr a r g s ) pop - s i z e mutate - prob par - p e r c e n t max - i t e rexit - percent ) ) )( d i s p l a y - l i s t answer ) ) ) ) ) ) )(cmd - p a r s e r )Листинг 5.3: Генетический алгоритм#l a n g scheme/base( require racket/gui )( require plot ); genetic - algorithm( define ( g e n e t i c - a l g o r i t h m graphc o n t r o l - weightcontrol - edgespop - s i z emutate - probpar - p e r c e n tmax - i t e rexit - percent )( define d a u g h t e r - p e r c e n t ( - 1 par - p e r c e n t ) )( define ( push l s t elem )( if ( not ( member elem l s t ) ) ( c o n s elem l s t ) l s t ) )( define ( a l l - v e r t e x graph )( define ( h e l p e r graph r e s u l t )( if ( n u l l ? graph ) r e s u l t( h e l p e r ( c d r graph ) ( push ( push r e s u l t ( c a a r graph ) ) ( c a d a r graph ) ) ) ) )( h e l p e r graph ' ( ) ) )( define ( p i c k - random l s t )( l i s t - r e f l s t ( random ( l e n g t h l s t ) ) ) )( define ( s l i c e l s t s t a r t count )( define ( g e t - n - i t e m s l s t num)( if ( and (> num 0 ) ( not ( n u l l ? l s t ) ) )( c o n s ( c a r l s t ) ( g e t - n - i t e m s ( c d r l s t ) ( - num 1 ) ) )( if (> s t a r t 0 )( s l i c e ( c d r l s t ) ( - s t a r t 1 ) count )( g e t - n - i t e m s l s t count ) ) )( define ( path - w e i g h t path )( define p e n a l t y ( ∗ ( l e n g t h path ) 1 0 ) )( define ( f i n d - w e i g h t edge )( define ( h e l p e r x )20'() ) )( if ( or ( and ( e q u a l ? ( c a r x ) ( c a r edge ) ) ( e q u a l ? ( c a d r x ) ( c d r edge ) ) )( and ( e q u a l ? ( c a r x ) ( c d r edge ) ) ( e q u a l ? ( c a d r x ) ( c a r edge ) ) ) )( if ( e q u a l ? ( c a r edge ) ( c d r edge ) ) 0 ( caddr x ) ) ' ( ) ) )( f i l t e r ( lambda ( x ) ( not ( n u l l ? x ) ) ) ( map h e l p e r graph ) ) )( define ( h e l p e r path r e s u l t )( if ( n u l l ? ( c d r path ) ) r e s u l t( let ( (w ( f i n d - w e i g h t ( c o n s( if ( n u l l ? w)( h e l p e r ( c d r path ) (+( h e l p e r ( c d r path ) (+( h e l p e r ( c o n s ( c a r path ) ( r e v e r s e( c a r path ) ( c a d r path ) ) ) ) )penalty r e s u l t ) )( c a r w) r e s u l t ) ) ) ) ) )path ) ) 0 ) )( define ( f i t n e s s - f u n c t i o n i n d i v i d )( define p e n a l t y ( ∗ ( l e n g t h i n d i v i d ) 1 0 ) )( define ( check - e d g e s path )( define ( f i n d - next y )( c a d r ( member y path ) ) )( define ( f i n d - c o n t r o l x )( ormap e q u a l ? ( map f i n d - next x ) ( r e v e r s e x ) ) )( ∗ ( l e n g t h ( f i l t e r ( lambda ( z ) ( not z ) ) ( map f i n d - c o n t r o lcontrol - edges ) ) ) penalty ) )( let ( ( r e s (+ ( path - w e i g h t i n d i v i d ) ( check - e d g e s ( c o n s ( c a r i n d i v i d )( reverse individ ) ) ) ) ) )( if (> r e s p e n a l t y )(+ r e s c o n t r o l - w e i g h t 1 )res ) ) )( define ( f i t n e s s - pop p o p u l a t i o n )( define ( r e s u l t s x )( cons x ( f i t n e s s - f u n c t i o n x ) ) )( s o r t ( map r e s u l t s p o p u l a t i o n ) > #: key ( lambda ( x ) ( c d r x ) ) ) )( define ( s e l e c t i o n par - pop d a u g h t e r - pop )( let ( ( par - t h r e s h o l d ( - ( l e n g t h par - pop ) ( round ( ∗ ( l e n g t h par - pop )par - p e r c e n t ) ) ) )( d a u g h t e r - t h r e s h o l d ( - ( l e n g t h d a u g h t e r - pop ) ( round ( ∗ ( l e n g t hd a u g h t e r - pop ) d a u g h t e r - p e r c e n t ) ) ) ) )( append ( s l i c e ( map c a r ( f i t n e s s - pop par - pop ) ) par - t h r e s h o l d ( - ( l e n g t hpar - pop ) par - t h r e s h o l d ) )( s l i c e ( map c a r ( f i t n e s s - pop d a u g h t e r - pop ) ) d a u g h t e r - t h r e s h o l d ( ( l e n g t h d a u g h t e r - pop ) d a u g h t e r - t h r e s h o l d ) ) ) ) )( define ( gen - f i r s t - pop n graph )( define ( f a c t n )( if (= n 0 ) 1(∗ n ( f a c t ( - n 1) ) ) ) )( define ( h e l p e r v e r t i c e s n r e s u l t )( define ( gen - random - l i s t v e r t i c e s r e s u l t )( if ( n u l l ? v e r t i c e s ) r e s u l t( let ( ( rnd ( p i c k - random v e r t i c e s ) ) )( gen - random - l i s t ( remove rnd v e r t i c e s ) ( c o n s rnd r e s u l t ) ) ) ) )( if (= n 0 ) r e s u l t( let ( ( c u r r e n t ( gen - random - l i s t v e r t i c e s ' ( ) ) ) )( if ( member c u r r e n t r e s u l t )( helper vertices n result )21( h e l p e r v e r t i c e s ( - n 1) ( cons current r e s u l t ) ) ) ) ) )( let ( ( v e r t i c e s ( a l l - v e r t e x graph ) ) )( if (> n ( f a c t ( l e n g t h v e r t i c e s ) ) )( helper v e r t i c e s ( length v e r t i c e s ) '() )( helper vertices n '() ) ) ) )( define ( c r o s s - pop p o p u l a t i o n )( define ( c r o s s par1 par2 )( define ( f i l l l s t 1 l s t 2 t o from tmp )( cond ( ( or ( n u l l ? l s t 2 ) (= (+ ( l e n g t h l s t 1 ) ( l e n g t h tmp ) ) ( l e n g t hpar1 ) ) ) ( append tmp l s t 1 ) )( ( member ( c a r l s t 2 ) l s t 1 ) ( f i l l l s t 1 ( c d r l s t 2 ) t o from tmp ) )((> t o 0 ) ( f i l l l s t 1 ( c d r l s t 2 ) ( - t o 1 ) from ( c o n s ( c a r l s t 2 )tmp ) ) )((> from 0 ) ( f i l l ( append l s t 1 ( l i s t ( c a r l s t 2 ) ) ) ( c d r l s t 2 ) t o( - from 1 ) tmp ) ) ) )( let* ( ( n ( q u o t i e n t ( l e n g t h par1 ) 2 ) ) ( g ( random ( - ( l e n g t h par1 ) n - 1 ) ) )( c h i l d ( s l i c e par2 g n ) ) )( f i l l c h i l d ( r e v e r s e par1 ) g ( - ( l e n g t h par1 ) g n ) ' ( ) ) ) )( define ( h e l p e r pop r e s u l t )( if ( n u l l ? pop ) r e s u l t( h e l p e r ( c d r pop ) ( c o n s ( c r o s s ( c a r pop ) ( p i c k - random p o p u l a t i o n ) )result ))))( helper population '() ) )( define ( mutate - pop p o p u l a t i o n )( define ( mutate i n d i v i d )( define ( swap i n d 1 i n d 2 l s t )( append ( s l i c e l s t 0 i n d 1 ) ( l i s t ( l i s t - r e f l s t i n d 2 ) ) ( s l i c e l s t (+i n d 1 1 ) ( - i n d 2 i n d 1 1 ) ) ( l i s t ( l i s t - r e f l s t i n d 1 ) ) ( s l i c e l s t (+ind2 1) ( - ( length l s t ) ind2 - 1) ) ) )( define ( gen - o t h e r - i n d e x i n d e x )( let ( ( new ( random ( l e n g t h i n d i v i d ) ) ) )( if (= i n d e x new ) ( gen - o t h e r - i n d e x i n d e x ) new ) ) )( let* ( ( i n d e x 1 ( random ( l e n g t h i n d i v i d ) ) ) ( i n d e x 2 ( gen - o t h e r - i n d e xindex1 ) ) )( if (> i n d e x 2 i n d e x 1 )( swap i n d e x 1 i n d e x 2 i n d i v i d )( swap i n d e x 2 i n d e x 1 i n d i v i d ) ) ) )( define ( h e l p e r p o p u l a t i o n r e s u l t )( if ( n u l l ? p o p u l a t i o n ) r e s u l t( if (> ( random ) mutate - prob )( h e l p e r ( c d r p o p u l a t i o n ) ( c o n s ( mutate ( c a r p o p u l a t i o n ) ) r e s u l t ) )( h e l p e r ( cdr population ) ( cons ( car population ) r e s u l t ) ) ) ) )( let* ( ( s o r t e d - pop ( map c a r ( f i t n e s s - pop p o p u l a t i o n ) ) ) ( h a l f ( q u o t i e n t( l e n g t h s o r t e d - pop ) 2 ) ) )( append ( h e l p e r ( s l i c e s o r t e d - pop 0 h a l f ) ' ( ) ) ( s l i c e s o r t e d - pop h a l f ( ( l e n g t h s o r t e d - pop ) h a l f ) ) ) ) )( define ( p o p u l a t i o n - w e i g h t p o p u l a t i o n )( / ( f o l d l + 0 ( map f i t n e s s - f u n c t i o n p o p u l a t i o n ) ) ( l e n g t h p o p u l a t i o n ) 1 .