/* Ċ Ġ* ĠCopyright Ġ 2 0 2 1 ĠRed ĠHat , ĠInc . Ġand / or Ġits Ġaffiliates . Ċ Ġ* Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ package Ġnet . card osi . gen et ical gorithm . util Ċ Ċ import Ġnet . card osi . gen et ical gorithm . model . Individual Ċ import Ġnet . card osi . gen et ical gorithm . model . Population Ċ import Ġjava . util .* ĊĊ Ċ // Cross over Ċ fun Ġfit test Off Spring ( first Fit test : ĠIndividual , Ġsecond Fit test : ĠIndividual , ĠnumberOf Gen es : ĠInt ) Ġ: ĠIndividual Ġ{ ĊĠĠĠ Ġval Ġrn Ġ= ĠRandom () ĊĠĠĠ Ġ// Select Ġa Ġrandom Ġcross over Ġpoint ĊĠĠĠ Ġval Ġcross Over Point Ġ= Ġrn . nextInt ( numberOf Gen es ) ĊĠĠĠ Ġ// Swap Ġvalues Ġamong Ġparents ĊĠĠĠ Ġval Ġfirst Off Spring Ġ= ĠIndividual () ĊĠĠĠ Ġfirst Off Spring . set Gen es ( first Fit test . get Gen es ()) ĊĠĠĠ Ġval Ġsecond Off Spring Ġ= ĠIndividual () ĊĠĠĠ Ġsecond Off Spring . set Gen es ( second Fit test . get Gen es ()) ĊĊĠĠĠ Ġfor Ġ( i Ġin Ġ 0 Ġuntil Ġcross Over Point ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġtemp Ġ= Ġfirst Off Spring . get Gen es ()[ i ] ĊĠĠĠĠĠĠĠ Ġfirst Off Spring . get Gen es ()[ i ] Ġ= Ġsecond Off Spring . get Gen es ()[ i ] ĊĠĠĠĠĠĠĠ Ġsecond Off Spring . get Gen es ()[ i ] Ġ= Ġtemp ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// Do Ġmutation Ġunder Ġa Ġrandom Ġprobability ĊĠĠĠ Ġif Ġ( rn . nextInt () Ġ% Ġ 7 Ġ< Ġ 5 ) Ġ{ ĊĠĠĠĠĠĠĠ Ġmutation ( first Off Spring , Ġsecond Off Spring , ĠnumberOf Gen es ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfirst Off Spring . calc Fitness () ĊĠĠĠ Ġsecond Off Spring . calc Fitness () ĊĠĠĠ Ġreturn Ġif Ġ( first Off Spring . fitness Ġ> Ġsecond Off Spring . fitness ) Ġ Ġfirst Off Spring Ġelse Ġsecond Off Spring Ċ } Ċ Ċ // Mutation Ċ fun Ġmutation ( first Fit test : ĠIndividual , Ġsecond Fit test : ĠIndividual , ĠnumberOf Gen es : ĠInt ) Ġ{ ĊĠĠĠ Ġval Ġrn Ġ= ĠRandom () ĊĊĠĠĠ Ġ// Select Ġa Ġrandom Ġmutation Ġpoint ĊĠĠĠ Ġvar Ġmutation Point Ġ= Ġrn . nextInt ( numberOf Gen es ) ĊĠĠĠ Ġ// Flip Ġvalues Ġat Ġthe Ġmutation Ġpoint ĊĠĠĠ Ġif Ġ( first Fit test . get Gen es ()[ mutation Point ] Ġ== Ġ 0 ) Ġ{ ĊĠĠĠĠĠĠĠ Ġfirst Fit test . get Gen es ()[ mutation Point ] Ġ= Ġ 1 ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ Ġfirst Fit test . get Gen es ()[ mutation Point ] Ġ= Ġ 0 ĊĠĠĠ Ġ} ĊĠĠĠ Ġmutation Point Ġ= Ġrn . nextInt ( numberOf Gen es ) ĊĠĠĠ Ġif Ġ( second Fit test . get Gen es ()[ mutation Point ] Ġ== Ġ 0 ) Ġ{ ĊĠĠĠĠĠĠĠ Ġsecond Fit test . get Gen es ()[ mutation Point ] Ġ= Ġ 1 ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ Ġsecond Fit test . get Gen es ()[ mutation Point ] Ġ= Ġ 0 ĊĠĠĠ Ġ} Ċ } Ċ Ċ fun Ġadd Fit test Off spring ( population Ġ: ĠPop ulation , ĠnumberOf Gen es : ĠInt ) Ġ{ ĊĠĠĠ Ġval Ġfirst Fit test : ĠIndividual Ġ= Ġpopulation . getFirst Fit test () ĊĠĠĠ Ġval Ġsecond Fit test : ĠIndividual Ġ= Ġpopulation . get Second Fit test () ĊĊĠĠĠ Ġval Ġfit test Off Spring Ġ= Ġfit test Off Spring ( first Fit test , Ġsecond Fit test , ĠnumberOf Gen es ) ĊĊĠĠĠ Ġpopulation . replace Fit test ( fit test Off Spring ) Ċ }