📄 genal.shell
字号:
# This is a shell script to operate a global search on the basis# of genetic algorithms# Operates on an HP under POSIX (NOT csh)rm genetic.logtouch genetic.logecho >> genetic.logecho >> genetic.logecho " -----------------------------------------------" >> genetic.logecho " -----------------------------------------------" >> genetic.logecho " GENETIC ALGORITHMS" >> genetic.logecho " author: R. Doell" >> genetic.logecho " version 2.0 creation date: 08/29/95" >> genetic.logecho " -----------------------------------------------" >> genetic.logecho " -----------------------------------------------" >> genetic.logecho >> genetic.logecho >> genetic.log# cc new in v1.1: for different chromosomes with identical bitcode# cc the iv curves are not recalculated# cc v1.1 hardly faster than v1.0 ---> stay with v1.0# new in v1.5: number of best parents surviving can be set in file 'control'# new in v1.6: search stops as soon as parameters are determined within 0.05 A# new in v2.0: selection criterion moved from 'select' to 'crossover'# ======================================================================echo " -------------------------------------------------------" >> genetic.logecho " generate random starting population ....." >> genetic.logecho " -------------------------------------------------------" >> genetic.log# execute program 'sconf.exe' with input file 'control'.# the output file 'population' contains the starting population coded as# 1's and 0's. output file 'bithelp' contains the number of bits to describe# one chromosome.sconf.exe# cp prot PROTread word1 word2 < errorif [ $word1 = error ] then echo " genal.shell aborted due to error in 'sconf.exe'" exitfirm errorecho " ..... and calculate fitness for every chromosome" >> genetic.logecho " -------------------------------------------------------" >> genetic.logecho >> genetic.log# translate digital code of population back to 'real' parameters# execute program 'transl.exe' with input 'control' and 'population'# and output 'popw'transl.exe# cat prot >> PROT# cat popw >> PROT# get number of parameters from file 'control'# typeset -i nparam# grep nparam control | read nparam rest grep nparam control > temp read nparam rest < temp rm temp echo $nparam >> genetic.log# get number of chromosomes from file 'control'# typeset -i nindiv# grep nindiv control | read nindiv rest grep nindiv control > temp read nindiv rest < temp rm temp echo $nindiv >> genetic.log# =====================================================================# initialize loop over chromosomes# typeset -i indivcindivc=1while [ "$indivc" -le "$nindiv" ] do# file 'helpfile' contains the number of parameters and the number of the# presently treated chromosome echo $nparam > helpfile echo $indivc >> helpfile# execute program 'fitness.exe' with input files 'helpfile', 'bithelp',# 'popw' and 'population'. the output file 'fithelp' contains the fitness# of the presently treated chromosome (with number indivc).# calculate fitness (= r-factor) for the chromosomes# fitness1.exe extracts needed information from file 'popw' and writes# it to file 'ivhelp'# fitness1.exe# iv.exe calculates iv curves and writes them to file 'intens'# iv.exe < inir110 > /dev/null# rfacga.exe calculates respective r-factor and writes it to file 'rhelp'# rfacga.exe < intens > /dev/null# fitness2.exe converts results into proper formatecho $indivc > temppop=0echo $pop >> temp# convert.exe needs to be written to take# "popw" and writes it into a format for whatever means you have # for calculating and R-factor or figure of merit (in this case "sref")# the output ("phase.log" in this case) hold the R-factor which is written to the# file "rhelp". The "fitness2.exe" can be run without changes. ./convert.exe read match < match if [ $match -le 0 ] then# sref phase.run single.d single.at junk junk2 < phase.cnt > phase.log ./black_box.exe number_x # Non-complex R-factor# grep R-factor phase.log > temp2# read rest1 rest2 rest3 rest4 rfact < temp2# Complex R-factor# grep complex phase.log > temp2# read rest1 rest2 rest3 rest4 rest5 rfact < temp2 read rfact < box_out rm box_out echo $rfact > rhelp addnew.exe# rm temp2 fi fitness2.exe rm temp rm match cat fithelp >> fitfile# end loop over chromosomes# let indivc=indivc+1indivc=`expr $indivc + 1`done# =====================================================================# file 'fitfile' contains all chromosomes with fitness and binary code cat fitfile | sort -n > store mv store fitfile# echo "initial fitfile after sorting:" >> PROT# echo "fitness: code:" >> PROT# cat fitfile >> PROT# count total number of investigated chromosomes# typeset -i ncount# let ncount=nindivncount=$nindiv# =====================================================================# initialize loop over populations# get number of new children from file 'control'# typeset -i nchild# grep nchild control | read nchild rest grep nchild control > temp read nchild rest < temp rm temp# get maximum number of populations from file 'control'# typeset -i npop# grep npop control | read npop rest grep npop control > temp read npop rest < temp rm temp# get number of surviving parents from file 'control'# typeset -i nsurv# grep nsurv control | read nsurv rest grep nsurv control > temp read nsurv rest < temp rm temp echo $nsurv >> keep.control# get tension from file 'control' grep tension control > temp read tension rest < temp rm temp# make sure nindiv is set to correct value (Chris -- March 24) grep nindiv control > temp read nindiv rest < temp rm temp # initialize solutions and elite files (Chris -- April 10) cp fitfile solutions touch elite # cc preset some help variables# cc flag=nomatch# cc typeset -i cmatch# cc cmatch=0# cc typeset -i nchrompopc=1echo $popc# calculate average fitness of present population# input files: 'helpfile', 'fitfile'# output file containing value of average fitness: 'helpfile2'while [ "$popc" -lt "$npop" ]do # echo $nparam > helpfile# echo $nindiv >> helpfile avfit.exe # head -1 fitfile | read bf rest head -1 fitfile > temp read bf rest < temp rm temp echo " --> highest fitness: \c" >> genetic.log echo $bf >> genetic.log echo " achieved for: \c" >> genetic.log echo $rest >> genetic.log echo " parameter(s): \c" >> genetic.log# head -1 fitfile | 1transl.exe echo " --> average fitness of population $popc :\c" >> genetic.log cat helpfile2 >> genetic.log echo " number of chromosomes investigated up to now: $ncount" >> genetic.log echo >> genetic.logecho >> genetic.logecho " ---------------------------------------------------------" >> genetic.logecho " perform crossover and mutation in population $popc ....." >> genetic.logecho " ---------------------------------------------------------" >> genetic.log# crossover: input files: 'bithelp', 'control' and 'fitfile'# output file: 'lpop' (containing new large population) echo $nindiv >> bithelp grep idum control >> bithelp cross.exe tension# cross_share.exe tension# mutation: input files 'bithelp', 'control' and 'lpop'# output: 'mpop' (containing mutated population) mutate.exeecho " ..... and calculate fitness for every child of it" >> genetic.logecho " ---------------------------------------------------------" >> genetic.log# translate digital code of child population back to 'real' parameters# execute program 'ltransl.exe' with input 'control' and 'mpop'# cat prot >> PROT# cat popw >> PROT# increase counter for total number of investigated chromosomes# let ncount=ncount+nchildncount=`expr $ncount + $nchild`# ===================================================================== mv mpop population # translate mutated children to real parameters to go into the file popw# Chris -- March 24rm popwtransl.exe# initialize loop over chromosomes indivc=1 # get number of parameters from file 'control'# To make sure nparam is set correctly (Chris -- March 24) grep nparam control > temp read nparam rest < temp rm temp # let nsurv chromosomes with best fitness 'survive' cat elite > fitfilewhile [ "$indivc" -le "$nchild" ]do# file 'helpfile' contains the number of parameters and the number of the# presently treated chromosome echo $nparam > helpfile echo $indivc >> helpfile# execute program 'fitness.exe'# calculate fitness (= r-factor) for the chromosomes# cc skip computation of fitness if another chromosome has the same bitcode# cc # cc let nhelp=nchild+1-indivc# cc tail -$nhelp population | read help1 help2 rest# cc let i=1# cc while [ "$i" -lt "$indivc" ]# cc do# cc tail -$i fitfile | read help3 help4 rest# cc if test "$help2" = "$help4"# cc then# cc tail -$i fitfile | head -1 > fithelp# cc let i=i+nchild# cc flag=match# cc fi# cc let i=i+1# cc done# cc # cc # cc if test "$flag" = match# cc then# cc let cmatch=cmatch+1# cc flag=nomatch# cc else# fitness1.exe extracts needed information from file 'popw' and# writes it to file 'ivhelp'# fitness1.exe# iv.exe calculates iv curves and writes them to file 'intens'# iv.exe < inir110 > /dev/null# rfacga.exe calculates r-factor and writes it to file 'rhelp'# rfacga.exe < intens > /dev/null# fitness2.exe converts results into proper formatecho $indivc > tempecho $popc >> temp# convert.exe needs to be written to take# "popw" and writes it into a format for whatever means you have # for calculating and R-factor or figure of merit (in this case "sref")# the output ("phase.log" in this case) hold the R-factor which is written to the# file "rhelp". The "fitness2.exe" can be run without changes. ./convert.exe read match < match if [ $match -le 0 ] then# sref phase.run single.d single.at junk junk2 < phase.cnt > phase.log ./black_box.exe number_x # Non-complex R-factor# grep R-factor phase.log > temp2# read rest1 rest2 rest3 rest4 rfact < temp2# Complex R-factor# grep complex phase.log > temp2# read rest1 rest2 rest3 rest4 rest5 rfact < temp2 read rfact < box_out rm box_out echo $rfact > rhelp addnew.exe# rm temp2 fi fitness2.exe rm temp rm match# cc fi cat fithelp >> fitfile# end loop over chromosomes# let indivc=indivc+1indivc=`expr $indivc + 1` done# =====================================================================# CC# create file 'children': number of children, number of chromosomes# CC# in a parents' population and file 'fitfile'# CC# CC echo "$((nsurv+nchild)) number of children" > children# CC echo "$nindiv number of parent chromosomes" >> children# CC cat fitfile | sort -n >> children# CC rm fitfile# CC grep nselec control >> children# CC grep nsurv control >> children# CC# CC# echo >> PROT# CC# echo "children:" >> PROT# CC# cat children >> PROT# CC# echo >> PROT# CC# CC# select new parents from file 'children'# CC select.exe# CC cat fitfile | sort -n > store# CC mv store fitfile# create new 'fitfile' from which next parents are chosen cat fitfile | sort -n > store head -$nindiv store > fitfile # update solutions and elite files (Chris -- April 10) cp solutions solutions.tmp rm solutions rm elite keeper.exe rm solutions.tmp# echo " fitfile" >> PROT# echo >> PROT# cat fitfile >> PROT# echo >> PROT# echo >> PROT# end loop over populations# let popc=popc+1 popc=`expr $popc + 1` echo population = $popcdone# =====================================================================# final output# calculate average fitness of final populationecho $nparam > helpfileecho $nindiv >> helpfileavfit.exeecho >> genetic.loghead -1 fitfile | read bf restecho " --> highest fitness in last population: \c" >> genetic.logecho $bf >> genetic.logecho " achieved for: \c" >> genetic.logecho $rest >> genetic.logecho " parameter(s): \c" >> genetic.loghead -1 fitfile | 1transl.exeecho " --> average fitness of (final) population $fpop :\c" >> genetic.logcat helpfile2 >> genetic.logecho " total number of chromosomes investigated: $ncount" >> genetic.logecho >> genetic.log# delete files not needed any more rm helpfile rm fithelp rm bithelp rm fitfile rm store rm popw rm prot rm helpfile2 rm lpop rm population rm rhelp rm ab16in rm ab16out rm solutions.tmpexit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -